diff options
author | Scott Moser <smoser@ubuntu.com> | 2010-01-20 16:26:21 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2010-01-20 16:26:21 -0500 |
commit | 272bdb4ad359f2f7b8a809ec16b1e5df90695a66 (patch) | |
tree | 6df58ebbbce9b6642a9a9168491bfea36bc08c1f | |
parent | 89bff8f826040f7c9d1cc85880eaf016e72d2191 (diff) | |
download | vyos-cloud-init-272bdb4ad359f2f7b8a809ec16b1e5df90695a66.tar.gz vyos-cloud-init-272bdb4ad359f2f7b8a809ec16b1e5df90695a66.zip |
lots of shuffling around. more fully support old ec2-init.cfg and, add
new preferred config file /etc/cloud/cloud.cfg.
cloud.cfg is read, its values are overridden by the cloud-config data
from user data
-rwxr-xr-x | ec2-init.py | 8 | ||||
-rw-r--r-- | ec2init/CloudConfig.py | 46 | ||||
-rw-r--r-- | ec2init/__init__.py | 62 |
3 files changed, 66 insertions, 50 deletions
diff --git a/ec2-init.py b/ec2-init.py index b483fd5d..4b4bd494 100755 --- a/ec2-init.py +++ b/ec2-init.py @@ -12,7 +12,6 @@ def warn(str): def main(): cloud = ec2init.EC2Init() - data = None try: cloud.get_data_source() except Exception as e: @@ -42,13 +41,6 @@ def main(): except: warn("failed to set defaults\n") - # set the ssh keys up - try: - cloud.sem_and_run("apply_credentials", "once-per-instance", - cloud.apply_credentials,[],False) - except: - warn("applying credentials failed!\n") - # enable swap try: cloud.sem_and_run("enable_swap", "once-per-instance", diff --git a/ec2init/CloudConfig.py b/ec2init/CloudConfig.py index a6393df9..2b628edb 100644 --- a/ec2init/CloudConfig.py +++ b/ec2init/CloudConfig.py @@ -19,6 +19,7 @@ import yaml import re import ec2init +import ec2init.util as util import subprocess import os @@ -28,29 +29,19 @@ class CloudConfig(): cfgfile = None handlers = { } cfg = None - old_conffile = '/etc/ec2-init/ec2-config.cfg' def __init__(self,cfgfile): - self.cfg = self.get_config_obj(cfgfile) self.cloud = ec2init.EC2Init() + self.cfg = self.get_config_obj(cfgfile) self.cloud.get_data_source() self.add_handler('apt-update-upgrade', self.h_apt_update_upgrade) self.add_handler('config-ssh') def get_config_obj(self,cfgfile): - str="" - # support reading the old ConfigObj format file and turning it - # into a yaml string - try: - f = file(self.old_conffile) - str+=file.read().replace('=',': ') - f.close() - except: - pass - - f = file(cfgfile) - cfg=yaml.load(str + f.read()) + f=file(cfgfile) + cfg=yaml.load(f.read()) f.close() + util.mergedict(cfg,self.cloud.read_cfg) return(cfg) def convert_old_config(self): @@ -134,10 +125,9 @@ class CloudConfig(): 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) + update = util.get_cfg_option_bool(self.cfg, 'apt_update', False) + upgrade = util.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() @@ -150,7 +140,6 @@ class CloudConfig(): return(True) def h_config_ssh(self,name,args): - print "Warning, not doing anything for config %s" % name if False: # if there are keys in cloud-config, use them # TODO: need to get keys from cloud-config if present @@ -164,8 +153,8 @@ class CloudConfig(): subprocess.call(('sh', '-c', clean_and_gen)) try: - user = get_cfg_option_str(self.cfg,'user') - disable_root = get_cfg_option_bool(self.cfg, "disable_root", True) + user = util.get_cfg_option_str(self.cfg,'user') + disable_root = util.get_cfg_option_bool(self.cfg, "disable_root", True) keys = self.cloud.get_public_ssh_keys() apply_credentials(keys,user,disable_root) except: @@ -185,23 +174,6 @@ class CloudConfig(): print "Warning, not doing anything for config %s" % name -def get_cfg_option_bool(yobj, key, default=False): - 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 - def apply_credentials(keys, user, disable_root): if user: setup_user_keys(keys, user, '') diff --git a/ec2init/__init__.py b/ec2init/__init__.py index a3bab271..f2e9f014 100644 --- a/ec2init/__init__.py +++ b/ec2init/__init__.py @@ -26,6 +26,7 @@ import os.path import errno import pwd import subprocess +import yaml datadir = '/var/lib/cloud/data' semdir = '/var/lib/cloud/sem' @@ -35,14 +36,22 @@ userdata = datadir + '/user-data.txt.i' user_scripts_dir = datadir + "/scripts" cloud_config = datadir + '/cloud-config.txt' data_source_cache = cachedir + '/obj.pkl' +system_config = '/etc/cloud/cloud.cfg' cfg_env_name = "CLOUD_CFG" import DataSourceEc2 import UserDataHandler +import util class EC2Init: - datasource_list = [ DataSourceEc2.DataSourceEc2 ] + datasource_map = { + "ec2" : DataSourceEc2.DataSourceEc2, + } + auto_order = [ 'ec2' ] + + cfg = None part_handlers = { } + old_conffile = '/etc/ec2-init/ec2-config.cfg' def __init__(self): self.part_handlers = { @@ -51,7 +60,37 @@ class EC2Init: 'text/upstart-job' : self.handle_upstart_job, 'text/part-handler' : self.handle_handler } - + self.cfg=self.read_cfg() + + import pprint + pprint.pprint(self.cfg) + + def read_cfg(self): + if self.cfg: + return(self.cfg) + + conf = { } + try: + stream = file(system_config) + conf = yaml.load(stream) + stream.close() + except: + pass + + # support reading the old ConfigObj format file and merging + # it into the yaml dictionary + try: + from configobj import ConfigObj + oldcfg = ConfigObj(self.old_conffile) + conf = util.mergedict(conf,oldcfg) + except: + pass + + if not conf.has_key("cloud_type"): + conf["cloud_type"]=None + + return(conf) + def restore_from_cache(self): try: f=open(data_source_cache, "rb") @@ -69,15 +108,28 @@ class EC2Init: except: return False + def get_cloud_type(self): + pass + def get_data_source(self): if self.restore_from_cache(): return True - for source in self.datasource_list: + dslist=[ ] + cfglist=self.cfg['cloud_type'] + if cfglist == "auto": + dslist = self.auto_order + elif cfglist: + for ds in cfglist.split(','): + dslist.append(strip(ds).tolower()) + + for ds in dslist: + if ds not in self.datasource_map: continue try: - s = source() + s = self.datasource_map[ds]() if s.get_data(): self.datasource = s + self.datasource_name = ds return True except Exception as e: pass @@ -226,7 +278,7 @@ class EC2Init: try: swaps=self.datasource.getswap_devs() except: - print "using fstab" + print "using blkid" process = subprocess.Popen( ['blkid', '-t', 'TYPE=swap', '-o', 'device'], stdout=subprocess.PIPE) |