diff options
author | Scott Moser <smoser@nelson> | 2010-01-11 23:29:18 -0500 |
---|---|---|
committer | Scott Moser <smoser@nelson> | 2010-01-11 23:29:18 -0500 |
commit | 969cbd1b5daa98a6e4d0d87750bde348fcf267e6 (patch) | |
tree | 62e3678a5c6dfea631726acb112fc9c924a4095b /ec2init | |
parent | d29998f111bf02e66d600b0aefbb12fc2517d5d6 (diff) | |
download | vyos-cloud-init-969cbd1b5daa98a6e4d0d87750bde348fcf267e6.tar.gz vyos-cloud-init-969cbd1b5daa98a6e4d0d87750bde348fcf267e6.zip |
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
Diffstat (limited to 'ec2init')
-rw-r--r-- | ec2init/CloudConfig.py | 155 | ||||
-rw-r--r-- | ec2init/ec2config.py | 92 |
2 files changed, 155 insertions, 92 deletions
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 <chuck.short@canonical.com> +# +# 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 <http://www.gnu.org/licenses/>. +# +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 <chuck.short@canonical.com> -# -# 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 <http://www.gnu.org/licenses/>. -# -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) |