[user_data] upgrade user_data_parser
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 26 Nov 2013 13:56:40 +0100
changeset 182 46334bff1690
parent 181 52418fb97f5d
child 183 de34235629b5
[user_data] upgrade user_data_parser
others/ec2_ami_parser.py
others/rc.local
others/user_data_parser.py
--- a/others/ec2_ami_parser.py	Tue Nov 26 17:48:59 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +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
-import ConfigParser
-
-url = 'http://169.254.169.254/2008-02-01/user-data'
-
-def add_grain(pair):
-    '''
-    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 isinstance(pair[1],list) and len(pair[1]) > 0:
-        pair=(pair[0],pair[1][0])
-    if pair[1] != '':
-        Popen(['salt-call', 'grains.setval',
-            pair[0], pair[1]]).wait()
-
-class FakeSecHead(object):
-   def __init__(self, fp):
-     self.fp = fp
-     self.sechead = '[asection]\n'
-   def readline(self):
-     if self.sechead:
-       try: return self.sechead
-       finally: self.sechead = None
-     else: return self.fp.readline()
-
-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')
-    found_flag = False
-    while found_flag == False:
-        line = myfile.readline()
-        if line.startswith("#salt-grains"):
-            found_flag = True
-        if line == '':
-            break
-    if found_flag:
-        config = ConfigParser.ConfigParser()
-        try:
-            config.readfp(FakeSecHead(myfile))
-        except ConfigParser.ParsingError:
-            pass
-        for item in config.items('asection'):
-            print item
-            add_grain(item)
--- a/others/rc.local	Tue Nov 26 17:48:59 2013 +0100
+++ b/others/rc.local	Tue Nov 26 13:56:40 2013 +0100
@@ -13,7 +13,7 @@
 
 ### Specific to salt-call machine creation
 ## parse the user-data for ami related information
-/usr/bin/env python /srv/salt/others/ec2_ami_parser.py
+/usr/bin/env python /srv/salt/others/user_data_parser.py
 
 ## get updated code if necessary 
 for i in `seq 3`
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/others/user_data_parser.py	Tue Nov 26 13:56:40 2013 +0100
@@ -0,0 +1,58 @@
+#!/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
+import ConfigParser
+
+url = 'http://169.254.169.254/2008-02-01/user-data'
+
+def add_grain(pair):
+    '''
+    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 isinstance(pair[1],list) and len(pair[1]) > 0:
+        pair=(pair[0],pair[1][0])
+    if pair[1] != '':
+        Popen(['salt-call', 'grains.setval',
+            pair[0], pair[1]]).wait()
+
+class FakeSecHead(object):
+   def __init__(self, fp):
+     self.fp = fp
+     self.sechead = '[asection]\n'
+   def readline(self):
+     if self.sechead:
+       try: return self.sechead
+       finally: self.sechead = None
+     else: return self.fp.readline()
+
+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')
+    found_flag = False
+    while found_flag == False:
+        line = myfile.readline()
+        if line.startswith("#salt-grains"):
+            found_flag = True
+        if line == '':
+            break
+    if found_flag:
+        config = ConfigParser.ConfigParser()
+        try:
+            config.readfp(FakeSecHead(myfile))
+        except ConfigParser.ParsingError:
+            pass
+        for item in config.items('asection'):
+            print item
+            add_grain(item)