01 January 2012

This is part 2 of the tutorial. Part 1 is available here.

Contextualization

One really nice feature of OpenNebula is the contextualization mechanism. By defining a CONTEXT parameter in the instance template file, OpenNebula will take care of generating an ISO image and pass it to the instance created. You then include the script provided in $ONE_SRC_CODE_PATH/share/scripts/vmcontext.sh into the /etc/init.d directory in the VM root file system, chmod +x to set execution bit on and create a link in the /etc/rc2.d directory (or whatever path your OS need). Your instance will be configure at boot time!. See http://opennebula.org/documentation:rel3.0:cong>the documentation</a> for more information on this feature.

I wanted to use this feature on Amazon AMI as well and fortunately it is possible! Instead of passing a ISO image to the instance, you send "user-data" to it by using the '-d' or '-f' arguments when launching your instance with 'ec2-run-instances'.You can then later get the data in your instance by querying http://169.254.169.254/latest/user-data with curl...

#!/bin/bash
# Author: Patrice LACHANCE, ItIsOpen.net
##############################################################################
USER_DATA=`/usr/bin/curl -s http://169.254.169.254/latest/user-data`
... do whatever you need to parse the data ...

So I changed the EC2 driver to fetch specific keywords in the template's CONTEXT parameter and use them to pass user-data to 'ec2-run-instances'.


Parameter Attribute Usage
CONTEXT USERDATA User data to send to the instance from the command line. Note: use only one attribute for user data, USERDATA or USERDATAFILE, not both...
Example(s): USERDATA="some data format"
(ec2-run-instances [GENERAL OPTIONS] $AMI -d "$USERDATA" ...)
CONTEXT USERDATAFILE User data to the instance from the content stored in the indicated file. Note: use only one attribute for user data, USERDATA or USERDATAFILE, not both...
Example(s): USERDATAFILE=/path/to/the/file
(ec2-run-instances [GENERAL OPTIONS] $AMI -f $USERDATAFILE ...)


Using EC2 Regions...

The next challenge was to add regions and datacenter management. With the standard driver you have to make multiple definitions of the vmm_ec2 driver in the $ONE_lOCATION/etc/oned.conf and a –region attribute to the arguments attribute of the driver’s definition.
Since onehost allow to define custom attributes to a host, I decided that the driver would rely on a custom EC2REGION attribute pointing to the corresponding AWS REGION. When invoked, onehost update <hostid> reads the EDITOR environment variable and pipes to it the editable attributes of the designated host. To avoid having to run it interactively, I created the following script that generates an uninteractive text editing job using ed.

#!/bin/sh
# Author: Patrice LACHANCE, ItIsOpen.net
######################################################################
usage() {
  echo "Usage: `basename $0` <hostid> <key> <value>"
  exit 1
}

echo "param = $#"
[ $# -ne 3 ] && usage
ONEHOST=$1
ONEKEY=$2
ONEVAL=$3

SCRIPTFILE=/tmp/tmpscrit.$$

echo "editing host $ONEHOST, setting $ONEKEY to $ONEVAL"
cat <<EOT >$SCRIPTFILE
#!/bin/sh
/bin/ed <<EOS \$1 >/dev/null 2>&1
$
a
$ONEKEY="$ONEVAL"
.
wq
EOS
EOT

chmod +x $SCRIPTFILE
sync
EDITOR=$SCRIPTFILE
export EDITOR
onehost update $ONEHOST
sleep 1
rm $SCRIPTFILE

The following example will set the attribute EC2REGION to eu-west-1 for the opennebula host number 1. Please make sure the region you specify corresponds to the real region of the EC2 fake host you created.

$ scriptname 1 EC2REGION eu-west-1

The patch is available in part 3.



blog comments powered by Disqus