added files to integrate with ec2 user data
authorPaul Tonelli <paul.tonelli@logilab.fr>
Mon, 11 Mar 2013 11:45:41 +0100
changeset 52 543be5448292
parent 51 555bcf9abc33
child 53 037d03aca265
added files to integrate with ec2 user data
others/ec2-run-user-data
others/ec2-run-user-data.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/others/ec2-run-user-data	Mon Mar 11 11:45:41 2013 +0100
@@ -0,0 +1,50 @@
+#!/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/othes/ec2-run-user-data.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/others/ec2-run-user-data.py	Mon Mar 11 11:45:41 2013 +0100
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# author: Paul Tonelli <ptonelli@logilab.fr>
+
+import urllib, os
+from salt import grains
+from sys import argv
+from subprocess import Popen
+__outputter__ = {
+        'sls': 'highstate',
+        'top': 'highstate',
+        'single': 'highstate',
+        'highstate': 'highstate',
+        }
+
+
+url = 'http://169.254.169.254/2008-02-01/user-data'
+
+def parse_line(line):
+    if 'http' or 'ssh' in line:
+        #grains.present(name='custom_hg_address', value=line)
+        Popen(['salt-call', 'state.single', 'grains.present',
+        'name=custom_hg_address', 'value=' + line.replace(' ', '')])
+    if 'rev' in line:
+        Popen(['salt-call', 'state.single', 'grains.present',
+            'name=custom_hg_revision', 'value=' + line.split(':')[-1].replace(' ','')])
+
+
+def exec_highstate():
+    '''
+    ugly, but did not find how to do it more cleanly
+
+    from salt.modules import state
+    state.highstate() #does not work and requires __salt__ dict
+    '''
+    Popen(['salt-call','state.highstate'])
+    Popen(['salt-call','state.highstate'])
+    Popen(['salt-call','state.sls','ami_creator_pre'])
+    Popen(['salt-call','state.sls','ami_creator_pre'])
+
+if __name__ == "__main__":
+    os.chdir('/tmp')
+    if len(argv)>1: #we have a filename, no need to download it
+        myfile = open(argv[1],'r')
+    else:
+        myfile = open(urllib.urlretrieve(url)[0],'r')
+    for line in myfile:
+        parse_line(line)
+    #exec_highstate()
+