Skip to content

Commit

Permalink
Feature/script improvements (#418)
Browse files Browse the repository at this point in the history
* wip

* added scripts checking requisites

---------

Co-authored-by: MTS\Tomas_K <tomas.kovac@mts.sk>
  • Loading branch information
TomKovac and MTS\Tomas_K committed Jul 8, 2024
1 parent 951dcc5 commit 61257f5
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 41 deletions.
103 changes: 93 additions & 10 deletions scripts/check_requisites.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ try {
}


$apaxUrl = "https://console.simatic-ax.siemens.io/"
$accessToApax = $false;
if($isApaxInstalled){
try {
# Just check the access by trying to get the feed
$response = Invoke-RestMethod -Uri $apaxUrl -Method Get
Write-Host "Feed: $apaxUrl accessible by means of network." -ForegroundColor Green
$accessToApax = $true;
}
catch {
Write-Host "Failed to access feed: $apaxUrl. Error: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Try to access it manually, check your connection, firewall setttings, etc. " -ForegroundColor Red
}
}


# Check for apax
$isApaxAccessible = $false
try {
$errorOutput =""
$output =""
$command = "apax info --ax-scopes"
$resp = $(Invoke-Expression "$command" -OutVariable output -ErrorVariable errorOutput )
Write-Host "output $output" -ForegroundColor Red
Write-Host "errorOutput $errorOutput" -ForegroundColor Red
Write-Host "resp $resp" -ForegroundColor Red
if($errorOutput[1].ToString().Contains("No access to the Simatic-AX registry"))
{
Write-Host "Unable to access apax packages. Check your connections, firewall, credentials etc." -ForegroundColor Red
Write-Host "$errorOutput" -ForegroundColor Red
}
else
{
Write-Host "Apax packages are accessible." -ForegroundColor Green
$isApaxAccessible = $true;
}
} catch {
Write-Host "Error: Unable to access apax packages. Check your connections, firewall, credentials etc. : $($_.Exception.Message)" -ForegroundColor Red
}

# Define the command to get the version
$command = "axcode --version"

Expand All @@ -86,27 +126,70 @@ try {
Write-Host "The AXCode version does not match the expected version: $expectedVersion" -ForegroundColor Red
}
} catch {
Write-Host "Error: Unable to determine the AXCode version. Ensure AXCode is correctly installed and accessible from the command line."
Write-Host "Error: Unable to determine the AXCode version. Ensure AXCode is correctly installed and accessible from the command line." -ForegroundColor Red
}

$feedUrl = "https://nuget.pkg.github.com/ix-ax/index.json"

$headers = @{
"Authorization" = "Bearer $userToken"
"User-Agent" = "PowerShell"
"Accept" = "application/vnd.github.package-preview+json"
}

$hasFeedAccess = $false;
$feedUrl = "https://nuget.pkg.github.com/ix-ax/index.json"

# Check if the feed is added
$isFeedAlreadyAdded = $false;
try {
# Just check the access by trying to get the feed
$response = Invoke-RestMethod -Uri $feedUrl -Headers $headers -Method Get
Write-Host "Successfully accessed feed: $feedUrl" -ForegroundColor Green
$hasFeedAccess = $true;
$feeds=$(dotnet nuget list source)

$isFeedAlreadyAdded = $feeds | Select-String -Pattern $feedUrl

if ($isFeedAlreadyAdded) {
Write-Host "The NuGet feed with URL $feedUrl is already added."
} else {
Write-Host "The NuGet feed with URL $feedUrl is not added." -ForegroundColor Red
Write-Host "You will need to add $feedUrl to your nuget sources manually (more information in src/README.md)." -ForegroundColor Red
}
}
catch {
Write-Host "Failed to access feed: $feedUrl. Error: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "You will need to add $feed to your nuget sources manually (more information in src/README.md)." -ForegroundColor Red
Write-Host "Check if the NuGet feed with URL $feedUrl is properly added to your nuget sources." -ForegroundColor Red
}

# Check if the feed is accessible by means of network
$hasFeedAccess = $false;
if($isFeedAlreadyAdded){
try {
# Just check the access by trying to get the feed
$response = Invoke-RestMethod -Uri $feedUrl -Headers $headers -Method Get
Write-Host "Feed: $feedUrl accessible by means of network." -ForegroundColor Green
$hasFeedAccess = $true;
}
catch {
Write-Host "Failed to access feed: $feedUrl. Error: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Try to access it manually, check your connection, firewall setttings, etc. " -ForegroundColor Red
}
}

$hasFeedAutorization = $false;
if($hasFeedAccess){
try {

$response = dotnet tool update axsharp.ixc --prerelease
$status = $?
if($status -match "^(?i)true$")
{
write-host "Authentification passed successfully while accessing feed $feedurl." -foregroundcolor green
$hasfeedautorization = $true;
}
else
{
Write-Host "Authentification error when trying to access the feed $feedUrl. " -ForegroundColor Red
}
}
catch {
Write-Host "Authentification error when trying to access the feed $feedUrl : $($_.Exception.Message)" -ForegroundColor Red
}
}

# Define a function to prompt and download
Expand Down Expand Up @@ -151,7 +234,7 @@ To download Apax:
Write-Host "Apax is not installed or not found in PATH. You need to have a valid SIMATIC-AX license." $apaxGuide -ForegroundColor Yellow
}

if(-not $hasFeedAccess)
if(-not ($isFeedAlreadyAdded -and $hasFeedAccess -and $hasFeedAutorization))
{
$nugetGuide = @"
To manually add the GitHub NuGet feed to your sources:
Expand Down
41 changes: 22 additions & 19 deletions src/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,45 @@ fi

export GREEN='\033[0;32m'
export RED='\033[0;31m'
export NC='\033[0m' # No Color


certfile="./certs/$PLC_NAME/$PLC_NAME.cer"
if ! [[ -e "$certfile" ]]; then
printf "${RED}Certification file $certfile does not exist.\r\n"
printf "${RED}Certification file $certfile does not exist.\r\n${NC}"
#alf #clear plc except ip and name and provide all actions for install all, build and initial download hw so as sw
alf=$( dirname ${BASH_SOURCE[0]})"\\all_first.sh"
$alf $PLC_NAME $PLC_IP_ADDRESS $PLATFORM $USERNAME $PASSWORD
else
check_requisites_apax_script=$( dirname ${BASH_SOURCE[0]})"\\check_requisites_apax.sh"
if ! $check_requisites_apax_script ; then
exit 1
fi

check_requisites_nuget_script=$( dirname ${BASH_SOURCE[0]})"\\check_requisites_nuget.sh"
if ! $check_requisites_nuget_script ; then
exit 1
fi

plcsim_script=$( dirname ${BASH_SOURCE[0]})"\\plcsim.sh"
$plcsim_script

#apax run ci # clean and install dependencies
apax clean
apax install

is_cert_hash_sha1_equal_script=$( dirname ${BASH_SOURCE[0]})"\\is_cert_hash_sha1_equal.sh"
if ! $is_cert_hash_sha1_equal_script "$PLC_NAME" "$PLC_IP_ADDRESS"; then
printf "${RED}Certification file $certfile exists, but its sha1 hash is different to the PLC's one.\r\n"
printf "${RED}It has to be regenerated again.\r\n"
printf "${RED}It has to be regenerated again.\r\n${NC}"
#alf #clear plc except ip and name and provide all actions for install all, build and initial download hw so as sw
alf=$( dirname ${BASH_SOURCE[0]})"\\all_first.sh"
$alf $PLC_NAME $PLC_IP_ADDRESS $PLATFORM $USERNAME $PASSWORD
else
use_plcsim=AXUSEPLCSIM
use_plcsim_value=$(printenv "$use_plcsim")

if [ -z "$use_plcsim_value" ]; then
echo "Environment variable '$use_plcsim' is not set."
else
echo "The value of '$use_plcsim' is: $use_plcsim_value"

if [ "$(echo 'true' | tr '[:upper:]' '[:lower:]')" == "$(echo "$use_plcsim_value" | tr '[:upper:]' '[:lower:]')" ]; then
plcsimscript=$( dirname ${BASH_SOURCE[0]})"\\StartPlcSimAdvCli.exe"
$plcsimscript
fi
fi

printf "${GREEN}Certification file $certfile exists and its sha1 hash is equal to the PLC's one.\r\n"
printf "${GREEN}No prompt will popup during execution, so you could leave your PC and enjoy your coffee now.\r\n"
printf "${GREEN}No prompt will popup during execution, so you could leave your PC and enjoy your coffee now.\r\n${NC}"

#apax run ci # clean and install dependencies
apax clean
apax install

#hw_update # copy and install gsd, copy templates, compile, copy the HwIds, download HW using certificate
hw_update=$( dirname ${BASH_SOURCE[0]})"\\hw_update.sh"
Expand Down
23 changes: 11 additions & 12 deletions src/scripts/all_first.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@ fi

export GREEN='\033[0;32m'
export RED='\033[0;31m'
export NC='\033[0m' # No Color

printf "${RED}This command will prompt during execution, so do not leave your PC. You can enjoy your coffee afterward.\r\n"
printf "${RED}This command will prompt during execution, so do not leave your PC. You can enjoy your coffee afterward.\r\n${NC}"

use_plcsim=AXUSEPLCSIM
use_plcsim_value=$(printenv "$use_plcsim")

if [ -z "$use_plcsim_value" ]; then
echo "Environment variable '$use_plcsim' is not set."
else
echo "The value of '$use_plcsim' is: $use_plcsim_value"
check_requisites_apax_script=$( dirname ${BASH_SOURCE[0]})"\\check_requisites_apax.sh"
if ! $check_requisites_apax_script ; then
exit 1
fi

if [ "$(echo 'true' | tr '[:upper:]' '[:lower:]')" == "$(echo "$use_plcsim_value" | tr '[:upper:]' '[:lower:]')" ]; then
plcsimscript=$( dirname ${BASH_SOURCE[0]})"\\StartPlcSimAdvCli.exe"
$plcsimscript
fi
check_requisites_nuget_script=$( dirname ${BASH_SOURCE[0]})"\\check_requisites_nuget.sh"
if ! $check_requisites_nuget_script ; then
exit 1
fi

plcsim_script=$( dirname ${BASH_SOURCE[0]})"\\plcsim.sh"
$plcsim_script

#apax run ci # clean and install dependencies
apax clean
Expand Down
96 changes: 96 additions & 0 deletions src/scripts/check_requisites_apax.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash
apaxUrl="https://console.simatic-ax.siemens.io/"
expectedApaxVersion="3.2.1"

export GREEN='\033[0;32m'
export RED='\033[0;31m'
export NC='\033[0m\r\n' # No Color+CRLF

# Function to check if the apax is installed
is_apax_installed(){
# Try to get the apax version
apaxVersion=$(apax --version 2>/dev/null)

if [[ $? -eq 0 ]]; then
printf "${GREEN}Apax installed.${NC}"
return 0
else
printf "${RED}Apax is not installed or not found in PATH. You need to have a valid SIMATIC-AX license.${NC}"
return 1
fi
}

# Function to check if the if the apax version matches
is_apax_version_equal(){
local expected=$1
# Get the apax version
apaxVersion=$(apax --version 2>/dev/null)

if [[ "$apaxVersion" == "$expected" ]]; then
printf "${GREEN}Apax $expected detected.${NC}"
return 0
else
printf "${RED}Apax version mismatch. Expected $expected but found $apaxVersion.${NC}"
printf "${RED}Run apax self-update $expected.${NC}"
return 1
fi
}

# Function to check if the if the apax site is accessible
is_apax_site_accessible(){
# Just check the access by trying to get the feed
response=$(curl -L -s -o /dev/null -w "%{http_code}" "$apaxUrl")

if [[ "$response" -eq 200 ]]; then
printf "${GREEN}Feed: $apaxUrl accessible by means of network.${NC}"
return 0
else
printf "${RED}Failed to access feed: $apaxUrl. Error: HTTP status $response.${NC}"
printf "${RED}Try to access it manually, check your connection, firewall settings, credentials, etc.${NC}"
return 1
fi
}

# Function to check for valid access to the apax registries
has_access_to_apax_registries(){
command="apax info --ax-scopes"
output=$(eval "$command 2>&1")
if echo "$output" | grep -q "No access to the Simatic-AX registry"; then
printf "${RED}Unable to access apax registries. Check your connections, firewall, credentials etc.${NC}"
printf "${RED}$output.${NC}"
return 1
else
printf "${GREEN}Apax registries are accessible.${NC}"
return 0
fi
}

# Check if the correct number of arguments are provided
if [ "$#" -ne 0 ]; then
printf "${RED}Invalid number of parameters.${NC}"
printf "${RED}Usage: $0 ${NC}"
exit 1
fi

if ! is_apax_installed ; then
printf "${RED}Apax is not installed or not found in PATH. You need to have a valid SIMATIC-AX license.${NC}"
exit 1
elif ! is_apax_version_equal "$expectedApaxVersion" ; then
printf "${RED}Apax installed, but version found does not match the verion required $expectedApaxVersion.${NC}"
exit 1
else
printf "${GREEN}Apax installed, verion matches required $expectedApaxVersion.${NC}"
if ! is_apax_site_accessible ; then
printf "${RED}Failed to access feed: $apaxUrl. Error: HTTP status $response.${NC}"
printf "${RED}Try to access it manually, check your connection, firewall settings, credentials, etc.${NC}"
exit 1
elif ! has_access_to_apax_registries ; then
printf "${RED}Feed: $apaxUrl accessible by means of network,${NC}"
printf "${RED}but there is no access to the apax registries.${NC}"
printf "${RED}Check your connection, firewall settings, credentials, etc.${NC}"
exit 1
else
printf "${GREEN}Apax registries accessible, credentials verified.${NC}"
exit 0
fi
fi
Loading

0 comments on commit 61257f5

Please sign in to comment.