@@ -14,26 +14,36 @@ function onError(){
1414
1515ensure_azure_cli () {
1616 if [[ -z " $( command -v az) " ]]; then
17- echo " installing Azure CLI"
17+ echo " installing Azure CLI v2.76.0 "
1818 apt-get update && apt-get install -y ca-certificates curl apt-transport-https lsb-release gnupg
1919 curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
2020 AZ_REPO=$( lsb_release -cs)
2121 echo " deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${AZ_REPO} main" | tee /etc/apt/sources.list.d/azure-cli.list
22- apt-get update && apt-get install -y azure-cli
23-
24- if [[ -n " ${AZURE_FEDERATED_TOKEN_FILE:- } " ]]; then
25- echo " Logging in with federated token"
26- # AZURE_CLIENT_ID has been overloaded with Azure Workload ID in the preset-azure-cred-wi.
27- # This is done to avoid exporting Azure Workload ID as AZURE_CLIENT_ID in the test scenarios.
28- az login --service-principal -u " ${AZURE_CLIENT_ID} " -t " ${AZURE_TENANT_ID} " --federated-token " $( cat " ${AZURE_FEDERATED_TOKEN_FILE} " ) " > /dev/null
29-
30- # Use --auth-mode "login" in az storage commands to use RBAC permissions of login identity. This is a well known ENV variable the Azure cli
31- export AZURE_STORAGE_AUTH_MODE=" login"
22+ apt-get update && apt-get install -y azure-cli=2.76.0-1~${AZ_REPO}
23+ else
24+ # Check if we have the correct version
25+ CURRENT_VERSION=$( az version --query ' ."azure-cli"' -o tsv 2> /dev/null || echo " unknown" )
26+ REQUIRED_VERSION=" 2.76.0"
27+ if [[ " $CURRENT_VERSION " != " $REQUIRED_VERSION " ]]; then
28+ echo " Warning: Azure CLI version is $CURRENT_VERSION , but $REQUIRED_VERSION is required"
29+ echo " Consider running: apt-get install -y azure-cli=${REQUIRED_VERSION} -1~$( lsb_release -cs) "
3230 else
33- echo " AZURE_FEDERATED_TOKEN_FILE environment variable must be set to path location of token file"
34- exit 1
31+ echo " Azure CLI version $CURRENT_VERSION is correct"
3532 fi
3633 fi
34+
35+ if [[ -n " ${AZURE_FEDERATED_TOKEN_FILE:- } " ]]; then
36+ echo " Logging in with federated token"
37+ # AZURE_CLIENT_ID has been overloaded with Azure Workload ID in the preset-azure-cred-wi.
38+ # This is done to avoid exporting Azure Workload ID as AZURE_CLIENT_ID in the test scenarios.
39+ az login --service-principal -u " ${AZURE_CLIENT_ID} " -t " ${AZURE_TENANT_ID} " --federated-token " $( cat " ${AZURE_FEDERATED_TOKEN_FILE} " ) " > /dev/null
40+
41+ # Use --auth-mode "login" in az storage commands to use RBAC permissions of login identity. This is a well known ENV variable the Azure cli
42+ export AZURE_STORAGE_AUTH_MODE=" login"
43+ else
44+ echo " AZURE_FEDERATED_TOKEN_FILE environment variable must be set to path location of token file"
45+ exit 1
46+ fi
3747}
3848
3949
@@ -148,11 +158,48 @@ run_remote_cmd() {
148158
149159enable_ssh_windows () {
150160 echo " Enabling SSH for Windows VM"
151- az vm run-command invoke --command-id RunPowerShellScript -n ${VM_NAME} -g ${AZURE_RESOURCE_GROUP} --scripts " @$( pwd) /scripts/enable_ssh_windows.ps1" --parameters " SSHPublicKey=${AZURE_SSH_PUBLIC_KEY} "
161+ local SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
162+ local ENABLE_SSH_WINDOWS_SCRIPT=" ${SCRIPT_DIR} /enable_ssh_windows.ps1"
163+ echo " Using run-command script: ${ENABLE_SSH_WINDOWS_SCRIPT} "
164+ if [ ! -f " ${ENABLE_SSH_WINDOWS_SCRIPT} " ]; then
165+ echo " Enable-SSH script not found at ${ENABLE_SSH_WINDOWS_SCRIPT} "
166+ return 1
167+ fi
168+ local run_command_output
169+ if ! run_command_output=$( az vm run-command invoke --command-id RunPowerShellScript \
170+ -n ${VM_NAME} -g ${AZURE_RESOURCE_GROUP} \
171+ --scripts @${ENABLE_SSH_WINDOWS_SCRIPT} \
172+ --parameters " SSHPublicKey=${AZURE_SSH_PUBLIC_KEY} " \
173+ --only-show-errors -o json 2>&1 ) ; then
174+ echo " Failed to enable SSH on Windows VM"
175+ echo " Azure CLI output:"
176+ echo " ${run_command_output} "
177+ return 1
178+ fi
179+ echo " Raw Azure run-command output:"
180+ printf ' %s\n' " ${run_command_output} "
181+ echo " Azure run-command output:"
182+ printf ' %s\n' " ${run_command_output} " | jq -r ' .value[].message'
152183}
153184
154185test_ssh_connection () {
155- echo " Testing ssh connection to Windows VM"
186+ echo " Checking sshd service state on Windows VM"
187+ local service_check_output
188+ if ! service_check_output=$( az vm run-command invoke --command-id RunPowerShellScript \
189+ -n ${VM_NAME} -g ${AZURE_RESOURCE_GROUP} \
190+ --scripts ' param([string]$serviceName) $svc = Get-Service -Name $serviceName -ErrorAction Stop; Write-Output ("sshd service status: {0}" -f $svc.Status); if ($svc.Status -ne "Running") { throw "Service $serviceName is not running" }' \
191+ --parameters " serviceName=sshd" \
192+ --only-show-errors -o json 2>&1 ) ; then
193+ echo " Azure run-command indicates sshd service is not running"
194+ echo " Azure CLI output:"
195+ echo " ${service_check_output} "
196+ exit 1
197+ fi
198+ echo " Raw Azure run-command output:"
199+ printf ' %s\n' " ${service_check_output} "
200+ echo " Azure run-command output:"
201+ printf ' %s\n' " ${service_check_output} " | jq -r ' .value[].message'
202+ echo " Testing ssh connection to Windows VM"
156203 SSH_KEY_FILE=.sshkey
157204 if ! ssh -i ${SSH_KEY_FILE} ${SSH_OPTS} azureuser@${VM_PUB_IP} " hostname" ;
158205 then
0 commit comments