From 969cbd1b5daa98a6e4d0d87750bde348fcf267e6 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 11 Jan 2010 23:29:18 -0500 Subject: refactor the config class and jobs to run through cloud-init-cfg At this point, the following should be functional: cloud-init-cfg apt-update-upgrade --- cloud-init-cfg.py | 40 +++++++++ ec2init/CloudConfig.py | 155 ++++++++++++++++++++++++++++++++++ ec2init/ec2config.py | 92 -------------------- setup.py | 3 +- upstart/cloud-apt-update-upgrade.conf | 12 +++ upstart/cloud-config-ssh.conf | 13 +++ upstart/cloud-ebs-mounts.conf | 12 +++ upstart/cloud-raid.conf | 12 +++ upstart/cloud-run-user-script.conf | 2 +- upstart/cloud-runurl.conf | 13 +++ upstart/ec2-apt-update.conf | 13 --- upstart/ec2-ebs-mounts.conf | 12 --- upstart/ec2-preconf-ssh-keys.conf | 13 --- upstart/ec2-raid.conf | 12 --- upstart/ec2-runurl.conf | 13 --- upstart/ec2-update-software.conf | 12 --- 16 files changed, 260 insertions(+), 169 deletions(-) create mode 100755 cloud-init-cfg.py create mode 100644 ec2init/CloudConfig.py delete mode 100644 ec2init/ec2config.py create mode 100644 upstart/cloud-apt-update-upgrade.conf create mode 100644 upstart/cloud-config-ssh.conf create mode 100644 upstart/cloud-ebs-mounts.conf create mode 100644 upstart/cloud-raid.conf create mode 100644 upstart/cloud-runurl.conf delete mode 100644 upstart/ec2-apt-update.conf delete mode 100644 upstart/ec2-ebs-mounts.conf delete mode 100644 upstart/ec2-preconf-ssh-keys.conf delete mode 100644 upstart/ec2-raid.conf delete mode 100644 upstart/ec2-runurl.conf delete mode 100644 upstart/ec2-update-software.conf diff --git a/cloud-init-cfg.py b/cloud-init-cfg.py new file mode 100755 index 00000000..88216e10 --- /dev/null +++ b/cloud-init-cfg.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +import sys +import ec2init + +def Usage(out = sys.stdout): + out.write("Usage: %s name\n" % sys.argv[0]) + +def main(): + # expect to be called with + # name freq [ args ] + if len(sys.argv) < 2: + Usage(sys.stderr) + sys.exit(1) + + name=sys.argv[1] + run_args=sys.argv[2:] + + import ec2init.CloudConfig + import os + + cfg_path = ec2init.cloud_config + cfg_env_name = ec2init.cfg_env_name + if os.environ.has_key(cfg_env_name): + cfg_path = os.environ[cfg_env_name] + + cc = ec2init.CloudConfig.CloudConfig(cfg_path) + + try: + cc.handle(name,run_args) + except: + import traceback + traceback.print_exc(file=sys.stderr) + sys.stderr.write("config handling of %s failed\n" % name) + sys.exit(1) + + sys.exit(0) + +if __name__ == '__main__': + main() diff --git a/ec2init/CloudConfig.py b/ec2init/CloudConfig.py new file mode 100644 index 00000000..3ba84154 --- /dev/null +++ b/ec2init/CloudConfig.py @@ -0,0 +1,155 @@ +# +# Common code for the EC2 configuration files in Ubuntu +# Copyright (C) 2008-2010 Canonical Ltd. +# +# Author: Chuck Short +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import yaml +import re +import ec2init +import subprocess +import os + +per_instance="once-per-instance" + +class CloudConfig(): + cfgfile = None + handlers = { } + cfg = None + + def __init__(self,cfgfile): + print "reading %s" % cfgfile + self.cfg=read_conf(cfgfile) + import pprint; pprint.pprint(self.cfg) + self.cloud = ec2init.EC2Init() + self.cloud.get_data_source() + self.add_handler('apt-update-upgrade', self.h_apt_update_upgrade) + self.add_handler('config-ssh') + + def add_handler(self, name, handler=None, freq=None): + if handler is None: + try: + handler=getattr(self,'h_%s' % name.replace('-','_')) + except: + raise Exception("Unknown hander for name %s" %name) + if freq is None: + freq = per_instance + + self.handlers[name]= { 'handler': handler, 'freq': freq } + + def get_handler_info(self, name): + return(self.handlers[name]['handler'], self.handlers[name]['freq']) + + def check_for_updates(self): + value = self.cfg['apt_update'] + return value + + def check_for_upgrade(self): + value = self.cfg['apt_upgrade'] + return value + + def parse_ssh_keys(self): + disableRoot = self.cfg['disable_root'] + if disableRoot == 'true': + value = 'disabled_root' + return value + else: + ec2Key = self.cfg['ec2_fetch_key'] + if ec2Key != 'none': + value = 'default_key' + return value + else: + return ec2Key + + def add_ppa(self): + #value = self.cfg['apt_sources'] + for ent in self.cfg['apt_sources']: + ppa = ent['source'] + where = ppa.find('ppa:') + if where != -1: + return ppa + + def add_custom_repo(self): + sources = [] + value = self.cfg['apt_sources'] + for ent in self.cfg['apt_sources']: + if ent.has_key('keyserver'): + keyserver = ent['keyserver'] + if ent.has_key('keyid'): + keyid = ent['keyid'] + if ent.has_key('filename'): + filename = ent['filename'] + source = ent['source'] + if source.startswith("deb"): + sources.append(source) + + return (keyserver,sources,keyid,filename) + + def handle(self, name, args): + handler = None + freq = None + try: + (handler, freq) = self.get_handler_info(name) + except: + raise Exception("Unknown config key %s\n", name) + + self.cloud.sem_and_run(name, freq, handler, [ name, args ]) + + def h_apt_update_upgrade(self,name,args): + update = get_cfg_option_bool(self.cfg, 'apt_update', False) + upgrade = get_cfg_option_bool(self.cfg, 'apt_upgrade', False) + + print "update = %s , upgrade = %s\n" % (update,upgrade) + if update or upgrade: + #retcode = subprocess.call(list) + subprocess.Popen(['apt-get', 'update']).communicate() + + if upgrade: + e=os.environ.copy() + e['DEBIAN_FRONTEND']='noninteractive' + subprocess.Popen(['apt-get', 'upgrade', '--assume-yes'], env=e).communicate() + + return(True) + + def h_config_ssh(self,name,args): + print "Warning, not doing anything for config %s" % name + + def h_config_ec2_ebs_mounts(self,name,args): + print "Warning, not doing anything for config %s" % name + + def h_config_setup_raid(self,name,args): + print "Warning, not doing anything for config %s" % name + + def h_config_runurl(self,name,args): + print "Warning, not doing anything for config %s" % name + + +def get_cfg_option_bool(yobj, key, default=False): + print "searching for %s" % key + if not yobj.has_key(key): return default + val = yobj[key] + if yobj[key] in [ True, '1', 'on', 'yes', 'true']: + return True + return False + +def get_cfg_option_str(yobj, key, default=None): + if not yobj.has_key(key): return default + return yobj[key] + +def read_conf(fname): + stream = file(fname) + conf = yaml.load(stream) + stream.close() + return conf diff --git a/ec2init/ec2config.py b/ec2init/ec2config.py deleted file mode 100644 index 23ab5ac3..00000000 --- a/ec2init/ec2config.py +++ /dev/null @@ -1,92 +0,0 @@ -# -# Common code for the EC2 configuration files in Ubuntu -# Copyright (C) 2008-2010 Canonical Ltd. -# -# Author: Chuck Short -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -import yaml -import re - -class EC2Config(): - def read_conf(self, ec2Config): - #stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - return ec2Config - - def check_for_updates(self): - #stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - - value = ec2Config['apt_update'] - return value - - def check_for_upgrade(self): - #stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - - value = ec2Config['apt_upgrade'] - return value - - def parse_ssh_keys(self): - #stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - - disableRoot = ec2Config['disable_root'] - if disableRoot == 'true': - value = 'disabled_root' - return value - else: - ec2Key = ec2Config['ec2_fetch_key'] - if ec2Key != 'none': - value = 'default_key' - return value - else: - return ec2Key - - def add_ppa(self): - stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - - value = ec2Config['apt_sources'] - for ent in ec2Config['apt_sources']: - ppa = ent['source'] - where = ppa.find('ppa:') - if where != -1: - return ppa - - def add_custom_repo(self): - stream = file('/tmp/ec2.yaml') - ec2Config = yaml.load(stream) - stream.close() - - sources = [] - value = ec2Config['apt_sources'] - for ent in ec2Config['apt_sources']: - if ent.has_key('keyserver'): - keyserver = ent['keyserver'] - if ent.has_key('keyid'): - keyid = ent['keyid'] - if ent.has_key('filename'): - filename = ent['filename'] - source = ent['source'] - if source.startswith("deb"): - sources.append(source) - - return (keyserver,sources,keyid,filename) diff --git a/setup.py b/setup.py index 4b4d0ae7..cc4b8329 100755 --- a/setup.py +++ b/setup.py @@ -31,7 +31,8 @@ setup(name='EC2-init', packages=['ec2init'], scripts=['ec2-init.py', 'ec2-is-compat-env', - 'cloud-init-run-module.py' + 'cloud-init-run-module.py', + 'cloud-init-cfg.py' ], data_files=[('/etc/ec2-init', ['ec2-config.cfg']), ('/etc/ec2-init/templates', glob('templates/*')), diff --git a/upstart/cloud-apt-update-upgrade.conf b/upstart/cloud-apt-update-upgrade.conf new file mode 100644 index 00000000..a5060f69 --- /dev/null +++ b/upstart/cloud-apt-update-upgrade.conf @@ -0,0 +1,12 @@ +# ec2-software-update - Update software at boot +# +# Update software at boot + +description "Update software at boot" + +start on (cloud-config + and local-filesystem + and net-device-up IFACE=eth0) +console output + +exec cloud-init-cfg apt-update-upgrade diff --git a/upstart/cloud-config-ssh.conf b/upstart/cloud-config-ssh.conf new file mode 100644 index 00000000..65d29941 --- /dev/null +++ b/upstart/cloud-config-ssh.conf @@ -0,0 +1,13 @@ +# ec2-preconf-ssh-keys +# +# Download preconfigured ssh-keys for EC2. + +description "Download preconfigured ssh keys" + +start on (cloud-config + and local-filesystem + and net-device-ifup IFACE=eth0 and starting ssh) +console output +task + +exec cloud-init-cfg config-ssh diff --git a/upstart/cloud-ebs-mounts.conf b/upstart/cloud-ebs-mounts.conf new file mode 100644 index 00000000..03ddfa40 --- /dev/null +++ b/upstart/cloud-ebs-mounts.conf @@ -0,0 +1,12 @@ +# ec2-ebs-mounts +# +# Mount EC2 EBS mount points + +description "Populate EBS mountpoints" + +start on cloud-config + +console output +task + +exec cloud-init-cfg ec2-ebs-mounts diff --git a/upstart/cloud-raid.conf b/upstart/cloud-raid.conf new file mode 100644 index 00000000..d4644c89 --- /dev/null +++ b/upstart/cloud-raid.conf @@ -0,0 +1,12 @@ +# ec2-raid - Setup ephemeral storage RAID and mount points +# +# Setup ephemeral storage RAID and mount points + +description "Setup RAID storage and moint points" + +start on (cloud-config + and local-filesystem) +console output +task + +exec cloud-init-cfg setup-raid diff --git a/upstart/cloud-run-user-script.conf b/upstart/cloud-run-user-script.conf index cf76fe32..272bc409 100644 --- a/upstart/cloud-run-user-script.conf +++ b/upstart/cloud-run-user-script.conf @@ -3,7 +3,7 @@ description "execute cloud user scripts" -start on (stopped rc RUNLEVEL=[2345] and cloud-config) +start on (stopped ec2init and stopped rc RUNLEVEL=[2345]) console output task diff --git a/upstart/cloud-runurl.conf b/upstart/cloud-runurl.conf new file mode 100644 index 00000000..f2140c32 --- /dev/null +++ b/upstart/cloud-runurl.conf @@ -0,0 +1,13 @@ +# ec2-runurl - Run runurl at boot +# +# Runurl at boot + +description "Run runurl" + +start on (cloud-config + and local-filesystem + and net-device-ifup IFACE=eth0) +console output +task + +exec cloud-init-cfg runurl diff --git a/upstart/ec2-apt-update.conf b/upstart/ec2-apt-update.conf deleted file mode 100644 index b280801b..00000000 --- a/upstart/ec2-apt-update.conf +++ /dev/null @@ -1,13 +0,0 @@ -# ec2-apt-update - Update archive at boot time -# -# Update archive at boot - -description "Update archive at boot" - -start on (cloud-config - and local-filesystems - and net-device-up IFACE=eth0) -console output -task -# exec /usr/sbin/cloud-init-run --once ec2-apt-update ec2-apt-update -exec /usr/sbin/ec2-apt-update diff --git a/upstart/ec2-ebs-mounts.conf b/upstart/ec2-ebs-mounts.conf deleted file mode 100644 index a1457f6a..00000000 --- a/upstart/ec2-ebs-mounts.conf +++ /dev/null @@ -1,12 +0,0 @@ -# ec2-ebs-mounts -# -# Mount EC2 EBS mount points - -description "Populate EBS mountpoints" - -start on cloud-config - -console output -task - -exec /usr/sbin/ec2-ebs-mount diff --git a/upstart/ec2-preconf-ssh-keys.conf b/upstart/ec2-preconf-ssh-keys.conf deleted file mode 100644 index 383b5bbc..00000000 --- a/upstart/ec2-preconf-ssh-keys.conf +++ /dev/null @@ -1,13 +0,0 @@ -# ec2-preconf-ssh-keys -# -# Download preconfigured ssh-keys for EC2. - -description "Download preconfigured ssh keys" - -start on (cloud-config - and local-filesystem - and net-device-ifup IFACE=eth0) -console out -task - -exec /usr/sbin/ec2-preconf-ssh-keys diff --git a/upstart/ec2-raid.conf b/upstart/ec2-raid.conf deleted file mode 100644 index e4364373..00000000 --- a/upstart/ec2-raid.conf +++ /dev/null @@ -1,12 +0,0 @@ -# ec2-raid - Setup ephemeral storage RAID and mount points -# -# Setup ephemeral storage RAID and mount points - -description "Setup RAID storage and moint points" - -start on (cloud-config - and local-filesystem) -console output -task - -exec /usr/sbin/ec2-setup-raid diff --git a/upstart/ec2-runurl.conf b/upstart/ec2-runurl.conf deleted file mode 100644 index 28249f8d..00000000 --- a/upstart/ec2-runurl.conf +++ /dev/null @@ -1,13 +0,0 @@ -# ec2-runurl - Run runurl at boot -# -# Runurl at boot - -description "Run runurl" - -start on (cloud-config - and local-filesystem - and net-device-ifup IFACE=eth0) -console output -task - -exec /usr/sbin/ec2-runurl diff --git a/upstart/ec2-update-software.conf b/upstart/ec2-update-software.conf deleted file mode 100644 index ee3131d1..00000000 --- a/upstart/ec2-update-software.conf +++ /dev/null @@ -1,12 +0,0 @@ -# ec2-software-update - Update software at boot -# -# Update software at boot - -description "Update software at boot" - -start on (cloud-config - and local-filesystem - and net-device-iup IFACE=eth0) -console output - -exec /usr/sbin/ec2-update-software -- cgit v1.2.3