Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm working on my first bash script that will deploy VMs into a cloudstack enviornment. The script below will deploy 4 VMs. What I'd like to include is an echo message at the very end of the script stating how many VMs were deployed or weren't. Is there a way to have this come up at the very end? Every time the command is run, cloudstack give an error or 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, ie 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
share|improve this question
add comment

1 Answer

Assuming the Cloudstack script exits with an apropriate status (0 for success, ≥1 for failure), this will work:

vm_deployed=0
vm_failed=0

for i in {1..4}; do
    if cloudstack vm deploy ...; then
        (( ++vm_deployed ))
    else
        (( ++vm_failed ))
    fi
done

# I am not sure if I got your description right.
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

(Note: (( )) is a bashism, so it will work only as long as you have bash in the #! line.)

share|improve this answer
    
Hi Grawity, I updated my script above to include your tweaks and added a little bit more information. The VM is still deployed but I'm not getting the ECHO at the end –  RomeNYRR Nov 3 '13 at 16:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.