I'm trying to create a script to delete containers in Azure Container Instances. The command is the following:
az container delete --resource-group myResourceX --name myContainerX
In a regular console that command asks to confirm the operation with y or n. But in my script after that line, it hangs and won't do anything (I have to close the editor). My intention was to keep executing the following lines to emulate the y, but it never reaches them:
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate("ACI management");
Start-Sleep -s 1;
$wshell.SendKeys('y');
Another way of preventing the system from hanging would be to send the --yes flag into the CLI. This may not achieve the same goal if the system is architected around wscript.shell.
For example:
az container delete --resource-group myResourceX --name myContainerX --yes
Related
I have a compute instance that starts at 12:00 with the scheduler of Azure ML and does a job scheduled in the crontab of the CI at 12:10. The thing is that this job doesn't always takes the same time to finish. So i want the CI to shut down itself when done.
The script that the crontab executes is the following:
---------------------------------------------------------
#!/bin/bash
...
# CRREATE FOLDER FOR LOGS
foldername=$PROJECT_PATH/$(date '+%d_%m_%Y_%H_%M_%S')
mkdir $foldername
filename=az_login.txt
path=$foldername/$filename
touch $path
az login -u *<USERNAME>* -p *<PASSWORD>* > $path
filename=acr_login.txt
path=$foldername/$filename
touch $path
# Authenticate to ACR
az acr login --name $ACR_NAME > $path
filename=pull_container.txt
path=$foldername/$filename
touch $path
# Pull the container image from ACR
docker pull $ACR_NAME.azurecr.io/$IMAGE_NAME:$IMAGE_TAG > $path
filename=run.txt
path=$foldername/$filename
touch $path
# Run the container image
docker run -v $CREDENTIALS_PATH:/app/config_privilegies $ACR_NAME.azurecr.io/$IMAGE_NAME:$IMAGE_TAG > $path
filename=rm_container.txt
path=$foldername/$filename
touch $path
# Delete the exited containers
docker rm $(docker ps -a -q --filter "status=exited") > $path
az ml compute stop --name *<CI_NAME>* --resource-group *<RESOURCE_NAME>* --workspace-name *<WORKSPACE_NAME>* --subscription *<SUBSCRIPTION_NAME>*
Everything works great until the stop command. In this particular code, it does nothing.
I've tried to put the last command in a seperate bash script and changing the last line for "./close_ci.sh". However, this doesn't work either, it restarts the CI instead of stopping it.
I have a bash script vault.sh
az login
Source_Kv_Name="myKeyVault2020"
SECRETS+=($(az keyvault secret list --vault-name $Source_Kv_Name --query "[].id" -o tsv))
If I run it as bash vault.sh it fails to connect to vault (authenticate)
If I run the same commands from terminal, not the script, it works fine.
Why is happening, and how do I authenticate bash script to run the same?
What is the error? Can you share the output?
I can say that for a bash script usually you need to "hard code users password" on the script, or use SPN authentication.
If your script is running from Azure Automation, you can use the Identity Managment on the Azure Automation and give access to the automation account to the component and use that access.
Example:
$azContext = (Connect-AzAccount -Identity).context
I tried to reproduce the same in my environment and got the result successfully.
In my bash I login with az login like below:
And copy the Https://microsoft.com/devicelogin in browser and enter the code -> continue and close the tab like below:
Now, when I create a file vi vault.sh with same script like below.
az login
Source_Kv_Name="khankeyvault "
az keyvault secret list --vault-name $Source_Kv_Name --query "[].id" -o tsv
When I run bash vault.sh I got authenticate login as same and got the result successfully like below:
I am running a script that requires the resource group and name of current AKS client config. Previously configured with az aks get-credentials ...
Current script:
(I type AKS=something and RG=SOMETHING before running)
az aks update -g $RG -n $AKSNAME ...
Wanted script:
(I type nothing before running)
AKSNAME=$(what goes here?)
RG=$(what goes here?)
az aks update -g $RG -n $AKSNAME ...
How can I load RG and AKSNAME values automatically through a shell script?
EDIT: I current assign the values to those variables by hand. I want the script to find the values automatically, corresponding to the cluster in the current context e.g. which kubectl is using.
If you just get the credential via the command az aks get-credentials .... without parameter --admin, then you can get the cluster name like this:
AKSNAME=$(kubectl config current-context)
And if you use the parameter --admin, then you need to change the command like this:
AKSNAME=$(kubectl config view --minify -o jsonpath='{.contexts[0].context.cluster}')
Then you can get the group name like this:
RG=$(az aks list --query "[?name == '$AKSNAME'].resourceGroup" -o tsv)
I am trying to launch an ACI container from Azure CLI. The deployment fails when I send multiple commands from command-line and it succeeds when I just pass one command, like 'ls'.
Am I passing multiple arguments to the command line in the wrong way?
az container create --resource-group rg-***-Prod-Whse-Containers --name test--image alpine:3.5 --command-line "apt-get update && apt-get install -y && wget wget https://www.google.com" --restart-policy never --vnet vnet-**-eastus2 --subnet **-ACI
Unfortunately, it seems that you cannot run multi-command in one time. See the Restrictions of the exec command for ACI:
Azure Container Instances currently supports launching a single process with az container exec, and you cannot pass command arguments. For example, you cannot chain commands like in sh -c "echo FOO && echo BAR", or execute echo FOO.
You just can execute the command such as whoami, ls and etc in the CLI command.
I suggest that you can run the command to create an interactive session with the container instance to execute command continuously after you create the ACI, refer to this similar issue.
I have a script that deallocates all VMs in the subscription based on the tags assigned - off hours and start them back up the next day using Jenkins. I want to be able to query these VMs based on the state (Running/Stopped(deallocated) and output it to a file.
Startup command - az vm start --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table)
Query command -
az resource list --tag Restart=yes --query "[].{Name:name,Group:resourceGroup,Location:location}" -o table
This command returns output (Name, RG and location). I want it to also show the powerstate and possibly OS type once the restart script is complete. If it is also possible to export the output to a spreadsheet.
You could use az vm show -d --ids to get powershell state.
Sorry, I don't have a Mac VM. On Linux VM, I use the following command to get it.
az vm show -d --ids $(az resource list --tag Restart=shui --query "[?type=='Microsoft.Compute/virtualMachines'].id"|jq -r ".[]") --query 'powerState'
On Mac, maybe you could use the following command.
az vm show -d --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table) --query 'powerState'
You could get help by using az vm show -h
--show-details -d : Show public ip address, FQDN, and power states. command will run slow.