Thursday, 10 December 2009

Scripts for the EC2 !

Hi all,

I’m currently deep into EC2, mixing cloud computing and BI. Very interesting, awesome. As you know, Amazon released two interesting features a few days ago :

  • you can store an instance image in EBS, not in S3. You will only pay for EBS.
  • you run and stop an instance, instead of just launching it and terminate it. The stop feature halts the instance and stops the money counter. Once you decide to run it, you will find your machine back with all the customization you did before. Note : you have to suscribe to Elastic IP in order to have your external ip adress unchanged.

As I said, I’m now deep into EC2, making prototypes around open source BI. That’s why I started to build my own tool box. Today I want to share two scripts with you. These scripts were written to start and stop instances from EBS backed AMIs :

  • stop instance script : use it to stop a specific instance (previously started). I added some extra infos about the instance to be stopped. Look at the command output below.
  • start instance script : use it to start specific instance (previously stopped). I added some extra infos about the instance to be stopped. Look at the command output below.

Here is the stop script :

#!/bin/bash
#Script by Vincent Teyssier-12/2009

speeder_spinner()
{
PROC=$1;COUNT=0
while [ -d /proc/$PROC ];do
echo -ne '/\b' ; sleep 0.05
echo -ne '-\b' ; sleep 0.05
echo -ne '\\\b' ; sleep 0.05
echo -ne '|\b' ; sleep 0.05
done
}

get_ami_infos()
{
X=0

    for ELEMENT in `ec2-describe-instances $INSTANCE`
        do
            MY_ARRAY[$X]=$ELEMENT
            ((X = X + 1))
        done

echo " "
echo "-- STOPPING INSTANCE --"
echo -e "Instance :""\t\033[1m$INSTANCE\033[0m"
echo -e "From AMI :""\t${MY_ARRAY[6]}"
echo -e "Desc     :""\t${MY_ARRAY[3]}"
echo -e "Config   :""\t${MY_ARRAY[10]}"
echo -e "OS       :""\t${MY_ARRAY[13]}"
echo "------------------------"
echo " "
}

get_stopping_result()
{
# Stop the ESB backed instance
Y=0
    for ELEMENT2 in `ec2-stop-instances $INSTANCE`
        do
            MY_ARRAY2[$Y]=$ELEMENT2
            ((Y = Y + 1))
        done

echo " "
echo "-- RESULT FROM STOPPING COMMAND --"
echo -e "Instance      :""\t${MY_ARRAY2[1]}"
echo -e "Initial state :""\t\033[1m${MY_ARRAY2[2]}\033[0m"
echo -e "New state     :""\t\033[1m${MY_ARRAY2[3]}\033[0m"
echo "----------------------------------"
echo " "
}

get_instance_status()
{
status=${MY_ARRAY2[3]}

while [ "$status" == "${MY_ARRAY2[3]}" ]
do
#echo $status
status=`ec2-describe-instances $INSTANCE | cut -f6 | sed -n 2p`
done

echo " "
echo "-- INSTANCE STATUS --"
echo -e "Instance :""\t$INSTANCE"
echo -e "From AMI :""\t\033[1m$status\033[0m"
echo "------------------------"
echo " "
}

#MAIN
INSTANCE=$1 #get params, more to come
echo "Please wait, gathering infos "
get_ami_infos &
speeder_spinner $!

echo "Please wait, stopping the instance "
get_stopping_result &
speeder_spinner $!

echo "Please wait, doing the job "
get_instance_status &
speeder_spinner $!

exit


And here is the start script :

#!/bin/bash
#Script by Vincent Teyssier-12/2009

speeder_spinner()
{
PROC=$1;COUNT=0
while [ -d /proc/$PROC ];do
echo -ne '/\b' ; sleep 0.05
echo -ne '-\b' ; sleep 0.05
echo -ne '\\\b' ; sleep 0.05
echo -ne '|\b' ; sleep 0.05
done
}

get_ami_infos()
{
X=0
    for ELEMENT in `ec2-describe-instances $INSTANCE`
        do
            MY_ARRAY[$X]=$ELEMENT
            ((X = X + 1))
        done
echo " "
echo "-- STARTING INSTANCE --"
echo -e "Instance :""\t\033[1m$INSTANCE\033[0m"
echo -e "From AMI :""\t${MY_ARRAY[6]}"
echo -e "Desc     :""\t${MY_ARRAY[3]}"
echo -e "Config   :""\t${MY_ARRAY[10]}"
echo -e "OS       :""\t${MY_ARRAY[13]}"
echo "------------------------"
echo " "
}

get_starting_result()
{
# Start the ESB backed instance
Y=0
    for ELEMENT2 in `ec2-start-instances $INSTANCE`
        do
            MY_ARRAY2[$Y]=$ELEMENT2
            ((Y = Y + 1))
        done
echo " "
echo "-- RESULT FROM STOPPING COMMAND --"
echo -e "Instance      :""\t${MY_ARRAY2[1]}"
echo -e "Initial state :""\t${MY_ARRAY2[2]}"
echo -e "New state     :""\t\033[1m${MY_ARRAY2[3]}\033[0m"
echo "----------------------------------"
echo " "
}

get_instance_status()
{
status=${MY_ARRAY2[3]}
#last_status=${MY_ARRAY2[3]}

while [ "$status" == "${MY_ARRAY2[3]}" ]
do
#echo $status
status=`ec2-describe-instances $INSTANCE | cut -f6 | sed -n 2p`
done

echo " "
echo "-- INSTANCE STATUS --"
echo -e "Instance :""\t$INSTANCE"
echo -e "From AMI :""\t\033[1m$status\033[0m"
echo "------------------------"
echo " "
}

#MAIN
INSTANCE=$1 #get params, more to come
echo "Please wait, gathering infos"
get_ami_infos &
speeder_spinner $!

echo "Please wait, starting the instance"
get_starting_result &
speeder_spinner $!

echo "Please wait, updating status"
get_instance_status &
speeder_spinner $!
exit


Enjoy and give me feed back about your EC2 adventures.