others/ec2-run-user-data
author Paul Tonelli <paul.tonelli@logilab.fr>
Tue, 12 Mar 2013 11:51:26 +0100
changeset 66 23a31e4cbb65
parent 60 ea9ba2e759fc
child 181 52418fb97f5d
permissions -rwxr-xr-x
the scripts now cleans the ssh keys if present as well as the apt cache

#!/bin/bash
### BEGIN INIT INFO
# Provides:       ec2-run-user-data
# Required-Start: ec2-get-credentials
# Required-Stop:  
# Should-Start:   
# Should-Stop:    
# Default-Start:  2 3 4 5
# Default-Stop:   
# Description:    Run instance user-data if it looks like a script.
### END INIT INFO
#
# Only retrieves and runs the user-data script once per instance.  If
# you want the user-data script to run again (e.g., on the next boot)
# then readd this script with insserv:
#   insserv -d ec2-run-user-data
#
prog=$(basename $0)
logger="logger -t $prog"
instance_data_url="http://169.254.169.254/2008-02-01"
python_handler=/etc/salt/srv/others/ec2_ami_parser.py


# Retrieve the instance user-data and run it if it looks like a script
user_data_file=$(tempfile --prefix ec2 --suffix .user-data --mode 700)
$logger "Retrieving user-data"
wget -qO $user_data_file $instance_data_url/user-data 2>&1 | $logger

if [ $(file -b --mime-type $user_data_file) = 'application/x-gzip' ]; then
	$logger "Uncompressing gzip'd user-data"
	mv $user_data_file $user_data_file.gz
	gunzip $user_data_file.gz
fi

if [ ! -s $user_data_file ]; then
	$logger "No user-data available"
elif head -1 $user_data_file | egrep -v '^#!'; then
        if [ `cat $user_data_file|grep 'http'|wc -l` -gt 0 ]; then
                /usr/bin/env python $python_handler $user_data_file
        fi
	$logger "Skipping user-data as it does not begin with #!"
else
	$logger "Running user-data"
	$user_data_file 2>&1 | logger -t "user-data"
	$logger "user-data exit code: $?"
fi
rm -f $user_data_file

# Disable this script, it may only run once
insserv -r $0