0

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

2 Answers 2

1

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.)

1
  • 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, 2013 at 16:55
0

1. The messages weren't displayed because $JOBID and $ERROR are unset, but based on the task description, neither variable is needed until those are set.

2. The loop only runs once, it can't deploy CloudStack 4 times.

3. The last if block should be folded in to the previous one.

4. $HOSTNAME is set to the string "HOSTNAME", (not a variable). Setting it to "$HOSTNAME" would work, but is redundant.

Suggested revision, (but no changes to the CloudStack portion, which needs help, later on that...):

#!/bin/bash
vm_deployed=0
vm_failed=0

for i in {1..4}; 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 (( vm_failed > 0 )); then
    echo "deployed $vm_deployed, failed $vm_failed VMs"
    exit 1
else
    echo "deployed all $vm_deployed VMs"
fi
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .