summaryrefslogtreecommitdiff
path: root/ec2-run-user-data.py
diff options
context:
space:
mode:
authorSoren Hansen <soren@canonical.com>2009-08-25 13:51:13 +0200
committerSoren Hansen <soren@canonical.com>2009-08-25 13:51:13 +0200
commitc4f3133dc99b68a41353f763a65dee1b323f8868 (patch)
treee2d87225c418cf82f479e6b04e2916a5c8ceca79 /ec2-run-user-data.py
parent41d05bf5c7980588cd289d95f9beb4a0ac1462d4 (diff)
downloadvyos-cloud-init-c4f3133dc99b68a41353f763a65dee1b323f8868.tar.gz
vyos-cloud-init-c4f3133dc99b68a41353f763a65dee1b323f8868.zip
Added basic appliance config handling (specifically package installation and removal)
Also added unit tests for the above.
Diffstat (limited to 'ec2-run-user-data.py')
-rwxr-xr-xec2-run-user-data.py72
1 files changed, 61 insertions, 11 deletions
diff --git a/ec2-run-user-data.py b/ec2-run-user-data.py
index 51e0d68d..784a1b3f 100755
--- a/ec2-run-user-data.py
+++ b/ec2-run-user-data.py
@@ -22,20 +22,10 @@ import email
import os
import subprocess
import tempfile
+from xml.dom.minidom import parse, parseString
import ec2init
-content_type_handlers = { 'text/x-shellscript' : handle_shell_script,
- 'text/x-ebs-mount-description' : handle_ebs_mount_description }
-
-def main():
- ec2 = ec2init.EC2Init()
-
- user_data = ec2.get_user_data()
-
- msg = email.message_from_string(user_data)
- handle_part(msg)
-
def handle_part(part):
if part.is_multipart():
for p in part.get_payload():
@@ -51,6 +41,14 @@ def handle_unknown_payload(payload):
# Try to detect magic
if payload.startswith('#!'):
content_type_handlers['text/x-shellscript'](payload)
+ return
+ if payload.startswith('<appliance>'):
+ content_type_handlers['text/x-appliance-config'](payload)
+
+
+def handle_appliance_config(payload):
+ app = ApplianceConfig(payload)
+ app.handle()
def handle_ebs_mount_description(payload):
(volume_description, path) = payload.split(':')
@@ -79,5 +77,57 @@ def handle_shell_script(payload):
os.unlink(path)
+content_type_handlers = { 'text/x-shellscript' : handle_shell_script,
+ 'text/x-ebs-mount-description' : handle_ebs_mount_description,
+ 'text/x-appliance-config': handle_appliance_config }
+
+class ApplianceConfig(object):
+ def __init__(self, data):
+ self.data = data
+
+ def handle(self):
+ self.dom = parseString(self.data)
+
+ if self.dom.childNodes[0].tagName == 'appliance':
+ root = self.dom.childNodes[0]
+ else:
+ return
+
+ for node in root.childNodes:
+ if node.tagName == 'package':
+ pkg = None
+ for subnode in node.childNodes:
+ if subnode.nodeType == root.TEXT_NODE:
+ pkg = subnode.nodeValue
+ if not pkg:
+ # Something's fishy. We should have been passed the name of
+ # a package.
+ return
+ if node.getAttribute('action') == 'remove':
+ remove_package(pkg)
+ else:
+ install_package(pkg)
+
+def main():
+ ec2 = ec2init.EC2Init()
+
+ user_data = ec2.get_user_data()
+ msg = parse_user_data(user_data)
+ handle_part(msg)
+
+def parse_user_data(user_data):
+ return email.message_from_string(user_data)
+
+def install_remove_package(pkg, action):
+ apt_get = subprocess.Popen(['apt-get', action, pkg], stdout=subprocess.PIPE)
+ logger_process = subprocess.Popen(['logger', '-t', 'user-data'], stdin=apt_get.stdout)
+ logger_process.communicate()
+
+def install_package(pkg):
+ return install_remove_package(pkg, 'install')
+
+def remove_package(pkg):
+ return install_remove_package(pkg, 'remove')
+
if __name__ == '__main__':
main()