renamed script used to parse user data in python and cleaned it
authorPaul Tonelli <paul.tonelli@logilab.fr>
Mon, 11 Mar 2013 16:31:52 +0100
changeset 60 ea9ba2e759fc
parent 59 c062ff185a14
child 61 3cd4cfb57874
renamed script used to parse user data in python and cleaned it
others/ec2-run-user-data
others/ec2-run-user-data.py
others/ec2_ami_parser.py
--- a/others/ec2-run-user-data	Mon Mar 11 16:31:00 2013 +0100
+++ b/others/ec2-run-user-data	Mon Mar 11 16:31:52 2013 +0100
@@ -18,7 +18,7 @@
 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-run-user-data.py
+python_handler=/etc/salt/srv/others/ec2_ami_parser.py
 
 
 # Retrieve the instance user-data and run it if it looks like a script
--- a/others/ec2-run-user-data.py	Mon Mar 11 16:31:00 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-# author: Paul Tonelli <ptonelli@logilab.fr>
-
-import urllib, os
-from time import sleep
-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):
-    '''
-    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
-    '''
-    if 'address:' in line:
-        #grains.present(name='custom_hg_address', value=line)
-        Popen(['salt-call', 'state.single', 'grains.present',
-        'name=custom_hg_address', 'value=' + line.split(':')[-1].replace(' ','')])
-        sleep(1)
-    elif 'http://' in line:
-        #grains.present(name='custom_hg_address', value=line)
-        Popen(['salt-call', 'state.single', 'grains.present',
-        'name=custom_hg_address', 'value=' + line.replace(' ', '')])
-        sleep(1)
-    elif ('rev:' or 'revision:') in line:
-        Popen(['salt-call', 'state.single', 'grains.present',
-            'name=custom_hg_revision', 'value=' + line.split(':')[-1].replace(' ','')])
-        sleep(1)
-
-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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/others/ec2_ami_parser.py	Mon Mar 11 16:31:52 2013 +0100
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# author: Paul Tonelli <ptonelli@logilab.fr>
+
+import urllib, os
+from time import sleep
+from salt import grains
+from sys import argv
+from subprocess import Popen
+
+url = 'http://169.254.169.254/2008-02-01/user-data'
+
+def parse_line(line):
+    '''
+    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
+    '''
+    line = line.rsplit('#')[0].lstrip().rstrip()
+    if line.startswith('address:'):
+        #grains.present(name='custom_hg_address', value=line)
+        Popen(['salt-call', 'state.single', 'grains.present',
+            'name=ami_hg_address', 'value=' +
+            line.split(':',1)[-1].lstrip().rstrip()])
+    elif line.startswith('rev:') or line.startswith('revision:') :
+        Popen(['salt-call', 'state.single', 'grains.present',
+            'name=ami_hg_revision', 'value=' + line.split(':',1)[-1].lstrip().rstrip()])
+    elif line.startswith('origin:'):
+        Popen(['salt-call', 'state.single', 'grains.present',
+            'name=ami_origin', 'value=' + line.split(':',1)[-1].lstrip().rstrip()])
+    sleep(1)
+
+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)