summaryrefslogtreecommitdiff
path: root/cloudinit/parts/boot_hook.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/parts/boot_hook.py')
-rw-r--r--cloudinit/parts/boot_hook.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/cloudinit/parts/boot_hook.py b/cloudinit/parts/boot_hook.py
new file mode 100644
index 00000000..881ffc58
--- /dev/null
+++ b/cloudinit/parts/boot_hook.py
@@ -0,0 +1,42 @@
+import os
+
+from cloudinit import util
+from cloudinit.settings import (PER_ALWAYS, PER_INSTANCE)
+from cloudinit import log as logging
+from cloudinit import parts
+
+LOG = logging.getLogger(__name__)
+
+
+
+class BootHookPartHandler(parts.PartHandler):
+ def __init__(self, boothook_dir, instance_id):
+ parts.PartHandler.__init__(self, PER_ALWAYS)
+ self.boothook_dir = boothook_dir
+ self.instance_id = instance_id
+
+ def list_types(self):
+ return ['text/cloud-boothook']
+
+ def _handle_part(self, _data, ctype, filename, payload, _frequency):
+ if ctype in [CONTENT_START, CONTENT_END]:
+ return
+
+ filename = util.clean_filename(filename)
+ payload = util.dos2unix(payload)
+ prefix = "#cloud-boothook"
+ start = 0
+ if payload.startswith(prefix):
+ start = len(prefix) + 1
+
+ filepath = os.path.join(self.boothook_dir, filename)
+ util.write_file(filepath, payload[start:], 0700)
+ try:
+ env = os.environ.copy()
+ env['INSTANCE_ID'] = str(self.instance_id)
+ util.subp([filepath], env=env)
+ except util.ProcessExecutionError as e:
+ LOG.error("Boothooks script %s returned %s", filepath, e.exit_code)
+ except Exception as e:
+ LOG.error("Boothooks unknown exception %s when running %s", e, filepath)
+