I'm working on my first bash script that will deploy 4 VMs into a cloudstack environment. I'd like to include an echo message at the very end of the script stating how many VMs were deployed. Is there a way to have this come up at the very end? Every time the command is run, cloudstack gives an error or a success message. Is there a way to incorporate the "error" or "jobid" message into an IF statement for the very last echo of my script?
If the VM is deployed, cloudstack returns the following:
deployvirtualmachineresponse:
id: 4fe88c45-fc7c-4ac2-9554-c815e1ada3f6
jobid: 01f52a39-8e57-4968-af34-c4ee53f29e25
If the VM fails due to an error, i.e. same VM name cloudstack returns the following:
deployvirtualmachineresponse:
uuidList: []
errorcode: 431
errortext: ! 'The vm with hostName HOSTNAME01 already exists in the network
domain: cs1null; network=Ntwk[251|Guest|5]'
If the VM fails due to a API timeout, cloud stack returns the following:
Timeout connecting to https://cloud.datapipe.com/api/compute/v1
My Script
#!/bin/bash
HOSTNAME=HOSTNAME
vm_deployed=0
vm_failed=0
for i in {1..1}; do
if cloudstack virtualmachine deploy \
--serviceofferingid 48 \
--templateid XXX \
--zoneid 7 \
--keypair XXX \
--securitygroupids XXX \
--name $(printf "%s%02d" $HOSTNAME $i);
then
(( ++vm_deployed ))
else
(( ++vm_failed ))
fi
done
if [ "$JOBID" ] || [ "$ERROR" ]; then
if (( vm_failed > 0 )); then
echo "deployed $vm_deployed, failed $vm_failed VMs"
else
echo "deployed all $vm_deployed VMs"
fi
fi
if (( vm_failed > 0 )); then
exit 1
fi