summaryrefslogtreecommitdiff
path: root/cloudinit/__init__.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2010-06-18 00:23:25 -0400
committerScott Moser <smoser@ubuntu.com>2010-06-18 00:23:25 -0400
commit17fabe47dcbbe6b2180e73cac6211f7394d61a8a (patch)
tree9601bf8d16d19584f1a03749cb1e69965ab23148 /cloudinit/__init__.py
parent6cdd4bf0a05d90e1e5b9c7b9b3c9f5c251c0a652 (diff)
downloadvyos-cloud-init-17fabe47dcbbe6b2180e73cac6211f7394d61a8a.tar.gz
vyos-cloud-init-17fabe47dcbbe6b2180e73cac6211f7394d61a8a.zip
add 'cloud-boothook' type
if user data is of type text/cloud-boothook, or begins with #cloud-boothook, then assume it to be code to be executed. Boothooks are a very simple format. Basically, its a one line header ('#cloud-config\n') and then executable payload. The executable payload is written to a file, then that file is executed at the time it is read. The file is left in /var/lib/cloud/data/boothooks There is no "first-time-only" protection. If running only once is desired, the boothook must handle that itself.
Diffstat (limited to 'cloudinit/__init__.py')
-rw-r--r--cloudinit/__init__.py37
1 files changed, 36 insertions, 1 deletions
diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py
index 2b76a73f..b0ee5fe8 100644
--- a/cloudinit/__init__.py
+++ b/cloudinit/__init__.py
@@ -25,6 +25,7 @@ cachedir = datadir + '/cache'
userdata_raw = datadir + '/user-data.txt'
userdata = datadir + '/user-data.txt.i'
user_scripts_dir = datadir + "/scripts"
+boothooks_dir = datadir + "/boothooks"
cloud_config = datadir + '/cloud-config.txt'
#cloud_config = '/tmp/cloud-config.txt'
data_source_cache = cachedir + '/obj.pkl'
@@ -174,7 +175,8 @@ class CloudInit:
'text/x-shellscript' : self.handle_user_script,
'text/cloud-config' : self.handle_cloud_config,
'text/upstart-job' : self.handle_upstart_job,
- 'text/part-handler' : self.handle_handler
+ 'text/part-handler' : self.handle_handler,
+ 'text/cloud-boothook' : self.handle_cloud_boothook
}
self.sysconfig=sysconfig
self.cfg=self.read_cfg()
@@ -412,6 +414,39 @@ class CloudInit:
self.cloud_config_str+="\n#%s\n%s" % (filename,payload)
+ def handle_cloud_boothook(self,data,ctype,filename,payload):
+ if ctype == "__end__": return
+ if ctype == "__begin__": return
+
+ filename=filename.replace(os.sep,'_')
+ prefix="#cloud-boothooks"
+ dos=False
+ start = 0
+ if payload.startswith(prefix):
+ start = len(prefix)+1
+ if payload[start] == '\r':
+ start=start+1
+ dos = True
+ else:
+ if payload.find('\r\n',0,100) >= 0:
+ dos = True
+
+ if dos:
+ payload=payload[start:].replace('\r\n','\n')
+ elif start != 0:
+ payload=payload[start:]
+
+ filepath = "%s/%s" % (boothooks_dir,filename)
+ util.write_file(filepath, payload, 0700)
+ try:
+ ret = subprocess.check_call([filepath])
+ except subprocess.CalledProcessError as e:
+ log.error("boothooks script %s returned %i" %
+ (filepath,e.returncode))
+ except Exception as e:
+ log.error("boothooks unknown exception %s when running %s" %
+ (e,filepath))
+
def get_public_ssh_keys(self):
return(self.datasource.get_public_ssh_keys())