diff options
43 files changed, 619 insertions, 487 deletions
diff --git a/cloud-init-cfg.py b/cloud-init-cfg.py index de64ef9c..37fe6398 100755 --- a/cloud-init-cfg.py +++ b/cloud-init-cfg.py @@ -49,13 +49,13 @@ def main(): else: freq = None run_args = [] - name=sys.argv[1] + name = sys.argv[1] if len(sys.argv) > 2: freq = sys.argv[2] if freq == "None": freq = None if len(sys.argv) > 3: - run_args=sys.argv[3:] + run_args = sys.argv[3:] cfg_path = cloudinit.get_ipath_cur("cloud_config") cfg_env_name = cloudinit.cfg_env_name @@ -69,10 +69,10 @@ def main(): # there was no datasource found, theres nothing to do sys.exit(0) - cc = CC.CloudConfig(cfg_path,cloud) + cc = CC.CloudConfig(cfg_path, cloud) try: - (outfmt, errfmt) = CC.get_output_cfg(cc.cfg,modename) + (outfmt, errfmt) = CC.get_output_cfg(cc.cfg, modename) CC.redirect_output(outfmt, errfmt) except Exception as e: err("Failed to get and set output config: %s\n" % e) @@ -84,25 +84,25 @@ def main(): module_list = [ ] if name == "all": modlist_cfg_name = "cloud_%s_modules" % modename - module_list = CC.read_cc_modules(cc.cfg,modlist_cfg_name) + module_list = CC.read_cc_modules(cc.cfg, modlist_cfg_name) if not len(module_list): - err("no modules to run in cloud_config [%s]" % modename,log) + err("no modules to run in cloud_config [%s]" % modename, log) sys.exit(0) else: module_list.append( [ name, freq ] + run_args ) - failures = CC.run_cc_modules(cc,module_list,log) + failures = CC.run_cc_modules(cc, module_list, log) if len(failures): - err("errors running cloud_config [%s]: %s" % (modename,failures), log) + err("errors running cloud_config [%s]: %s" % (modename, failures), log) sys.exit(len(failures)) -def err(msg,log=None): +def err(msg, log=None): if log: log.error(msg) sys.stderr.write(msg + "\n") -def fail(msg,log=None): - err(msg,log) +def fail(msg, log=None): + err(msg, log) sys.exit(1) if __name__ == '__main__': diff --git a/cloud-init-query.py b/cloud-init-query.py index cfe9b429..a066cb73 100755 --- a/cloud-init-query.py +++ b/cloud-init-query.py @@ -37,7 +37,7 @@ def main(): 'instance_id' : cc.cloud.get_instance_id(), } - name = sys.argv[1].replace('-','_') + name = sys.argv[1].replace('-', '_') if name not in data: sys.stderr.write("unknown name '%s'. Known values are:\n %s\n" % diff --git a/cloud-init.py b/cloud-init.py index 8279a0b0..9806c22c 100755 --- a/cloud-init.py +++ b/cloud-init.py @@ -54,10 +54,10 @@ def main(): sys.stderr.write("bad command %s. use one of %s\n" % (cmd, cmds)) sys.exit(1) - now = time.strftime("%a, %d %b %Y %H:%M:%S %z",time.gmtime()) + now = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime()) try: - uptimef=open("/proc/uptime") - uptime=uptimef.read().split(" ")[0] + uptimef = open("/proc/uptime") + uptime = uptimef.read().split(" ")[0] uptimef.close() except IOError as e: warn("unable to open /proc/uptime\n") @@ -74,7 +74,7 @@ def main(): raise try: - (outfmt, errfmt) = CC.get_output_cfg(cfg,"init") + (outfmt, errfmt) = CC.get_output_cfg(cfg, "init") CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) @@ -98,7 +98,7 @@ def main(): # most sense to exit early and silently for f in stop_files: try: - fp = open(f,"r") + fp = open(f, "r") fp.close() except: continue @@ -109,7 +109,7 @@ def main(): # cache is not instance specific, so it has to be purged # but we want 'start' to benefit from a cache if # a previous start-local populated one - manclean = util.get_cfg_option_bool(cfg, 'manual_cache_clean',False) + manclean = util.get_cfg_option_bool(cfg, 'manual_cache_clean', False) if manclean: log.debug("not purging cache, manual_cache_clean = True") cloudinit.purge_cache(not manclean) @@ -117,7 +117,8 @@ def main(): try: os.unlink(nonet_path) except OSError as e: - if e.errno != errno.ENOENT: raise + if e.errno != errno.ENOENT: + raise msg = "cloud-init %s running: %s. up %s seconds" % (cmd, now, uptime) sys.stderr.write(msg + "\n") @@ -146,7 +147,7 @@ def main(): # parse the user data (ec2-run-userdata.py) try: ran = cloud.sem_and_run("consume_userdata", cloudinit.per_instance, - cloud.consume_userdata,[cloudinit.per_instance],False) + cloud.consume_userdata, [cloudinit.per_instance], False) if not ran: cloud.consume_userdata(cloudinit.per_always) except: @@ -160,9 +161,9 @@ def main(): try: outfmt_orig = outfmt errfmt_orig = errfmt - (outfmt, errfmt) = CC.get_output_cfg(cc.cfg,"init") + (outfmt, errfmt) = CC.get_output_cfg(cc.cfg, "init") if outfmt_orig != outfmt or errfmt_orig != errfmt: - warn("stdout, stderr changing to (%s,%s)" % (outfmt,errfmt)) + warn("stdout, stderr changing to (%s,%s)" % (outfmt, errfmt)) CC.redirect_output(outfmt, errfmt) except Exception as e: warn("Failed to get and set output config: %s\n" % e) @@ -173,15 +174,15 @@ def main(): ['initctl', 'emit', 'cloud-config', '%s=%s' % (cloudinit.cfg_env_name, cc_path) ]) if cc_ready: - if isinstance(cc_ready,str): + if isinstance(cc_ready, str): cc_ready = [ 'sh', '-c', cc_ready] subprocess.Popen(cc_ready).communicate() - module_list = CC.read_cc_modules(cc.cfg,"cloud_init_modules") + module_list = CC.read_cc_modules(cc.cfg, "cloud_init_modules") failures = [] if len(module_list): - failures = CC.run_cc_modules(cc,module_list,log) + failures = CC.run_cc_modules(cc, module_list, log) else: msg = "no cloud_init_modules to run" sys.stderr.write(msg + "\n") diff --git a/cloudinit/CloudConfig/__init__.py b/cloudinit/CloudConfig/__init__.py index f5c4143c..76cafebd 100644 --- a/cloudinit/CloudConfig/__init__.py +++ b/cloudinit/CloudConfig/__init__.py @@ -25,7 +25,7 @@ import os import subprocess import time -per_instance= cloudinit.per_instance +per_instance = cloudinit.per_instance per_always = cloudinit.per_always per_once = cloudinit.per_once @@ -33,7 +33,7 @@ class CloudConfig(): cfgfile = None cfg = None - def __init__(self,cfgfile, cloud=None, ds_deps=None): + def __init__(self, cfgfile, cloud=None, ds_deps=None): if cloud == None: self.cloud = cloudinit.CloudInit(ds_deps) self.cloud.get_data_source() @@ -41,15 +41,17 @@ class CloudConfig(): self.cloud = cloud self.cfg = self.get_config_obj(cfgfile) - def get_config_obj(self,cfgfile): + def get_config_obj(self, cfgfile): try: cfg = util.read_conf(cfgfile) except: # TODO: this 'log' could/should be passed in - cloudinit.log.critical("Failed loading of cloud config '%s'. Continuing with empty config\n" % cfgfile) + cloudinit.log.critical("Failed loading of cloud config '%s'. " + "Continuing with empty config\n" % cfgfile) cloudinit.log.debug(traceback.format_exc() + "\n") cfg = None - if cfg is None: cfg = { } + if cfg is None: + cfg = { } try: ds_cfg = self.cloud.datasource.get_config_obj() @@ -57,12 +59,12 @@ class CloudConfig(): ds_cfg = { } cfg = util.mergedict(cfg, ds_cfg) - return(util.mergedict(cfg,self.cloud.cfg)) + return(util.mergedict(cfg, self.cloud.cfg)) def handle(self, name, args, freq=None): try: - mod = __import__("cc_" + name.replace("-","_"),globals()) - def_freq = getattr(mod, "frequency",per_instance) + mod = __import__("cc_" + name.replace("-", "_"), globals()) + def_freq = getattr(mod, "frequency", per_instance) handler = getattr(mod, "handle") if not freq: @@ -75,23 +77,24 @@ class CloudConfig(): # reads a cloudconfig module list, returns # a 2 dimensional array suitable to pass to run_cc_modules -def read_cc_modules(cfg,name): - if name not in cfg: return([]) +def read_cc_modules(cfg, name): + if name not in cfg: + return([]) module_list = [] # create 'module_list', an array of arrays # where array[0] = config # array[1] = freq # array[2:] = arguemnts for item in cfg[name]: - if isinstance(item,str): + if isinstance(item, str): module_list.append((item,)) - elif isinstance(item,list): + elif isinstance(item, list): module_list.append(item) else: raise TypeError("failed to read '%s' item in config") return(module_list) -def run_cc_modules(cc,module_list,log): +def run_cc_modules(cc, module_list, log): failures = [] for cfg_mod in module_list: name = cfg_mod[0] @@ -109,7 +112,7 @@ def run_cc_modules(cc,module_list,log): except: log.warn(traceback.format_exc()) log.error("config handling of %s, %s, %s failed\n" % - (name,freq,run_args)) + (name, freq, run_args)) failures.append(name) return(failures) @@ -126,24 +129,27 @@ def run_cc_modules(cc,module_list,log): # None if if none is given def get_output_cfg(cfg, mode="init"): ret = [ None, None ] - if not 'output' in cfg: return ret + if not 'output' in cfg: + return ret outcfg = cfg['output'] if mode in outcfg: modecfg = outcfg[mode] else: - if 'all' not in outcfg: return ret + if 'all' not in outcfg: + return ret # if there is a 'all' item in the output list # then it applies to all users of this (init, config, final) modecfg = outcfg['all'] # if value is a string, it specifies stdout and stderr - if isinstance(modecfg,str): + if isinstance(modecfg, str): ret = [ modecfg, modecfg ] # if its a list, then we expect (stdout, stderr) - if isinstance(modecfg,list): - if len(modecfg) > 0: ret[0] = modecfg[0] + if isinstance(modecfg, list): + if len(modecfg) > 0: + ret[0] = modecfg[0] if len(modecfg) > 1: ret[1] = modecfg[1] @@ -157,16 +163,18 @@ def get_output_cfg(cfg, mode="init"): # if err's entry == "&1", then make it same as stdout # as in shell syntax of "echo foo >/dev/null 2>&1" - if ret[1] == "&1": ret[1] = ret[0] + if ret[1] == "&1": + ret[1] = ret[0] swlist = [ ">>", ">", "|" ] for i in range(len(ret)): - if not ret[i]: continue + if not ret[i]: + continue val = ret[i].lstrip() found = False for s in swlist: if val.startswith(s): - val = "%s %s" % (s,val[len(s):].strip()) + val = "%s %s" % (s, val[len(s):].strip()) found = True break if not found: @@ -186,12 +194,13 @@ def get_output_cfg(cfg, mode="init"): # # with a '|', arguments are passed to shell, so one level of # shell escape is required. -def redirect_output(outfmt,errfmt, o_out=sys.stdout, o_err=sys.stderr): +def redirect_output(outfmt, errfmt, o_out=sys.stdout, o_err=sys.stderr): if outfmt: - (mode, arg) = outfmt.split(" ",1) + (mode, arg) = outfmt.split(" ", 1) if mode == ">" or mode == ">>": owith = "ab" - if mode == ">": owith = "wb" + if mode == ">": + owith = "wb" new_fp = open(arg, owith) elif mode == "|": proc = subprocess.Popen(arg, shell=True, stdin=subprocess.PIPE) @@ -206,10 +215,11 @@ def redirect_output(outfmt,errfmt, o_out=sys.stdout, o_err=sys.stderr): return if errfmt: - (mode, arg) = errfmt.split(" ",1) + (mode, arg) = errfmt.split(" ", 1) if mode == ">" or mode == ">>": owith = "ab" - if mode == ">": owith = "wb" + if mode == ">": + owith = "wb" new_fp = open(arg, owith) elif mode == "|": proc = subprocess.Popen(arg, shell=True, stdin=subprocess.PIPE) @@ -222,31 +232,32 @@ def redirect_output(outfmt,errfmt, o_out=sys.stdout, o_err=sys.stderr): return def run_per_instance(name, func, args, clear_on_fail=False): - semfile = "%s/%s" % (cloudinit.get_ipath_cur("data"),name) - if os.path.exists(semfile): return + semfile = "%s/%s" % (cloudinit.get_ipath_cur("data"), name) + if os.path.exists(semfile): + return - util.write_file(semfile,str(time.time())) + util.write_file(semfile, str(time.time())) try: func(*args) except: - if clear_on_fail: os.unlink(semfile) + if clear_on_fail: + os.unlink(semfile) raise # apt_get top level command (install, update...), and args to pass it -def apt_get(tlc,args=None): +def apt_get(tlc, args=None): if args is None: args = [] - e=os.environ.copy() - e['DEBIAN_FRONTEND']='noninteractive' - cmd=[ 'apt-get', - '--option', 'Dpkg::Options::=--force-confold', '--assume-yes', - tlc ] + e = os.environ.copy() + e['DEBIAN_FRONTEND'] = 'noninteractive' + cmd = ['apt-get', '--option', 'Dpkg::Options::=--force-confold', + '--assume-yes', tlc] cmd.extend(args) - subprocess.check_call(cmd,env=e) + subprocess.check_call(cmd, env=e) def update_package_sources(): run_per_instance("update-sources", apt_get, ("update",)) def install_packages(pkglist): update_package_sources() - apt_get("install",pkglist) + apt_get("install", pkglist) diff --git a/cloudinit/CloudConfig/cc_apt_update_upgrade.py b/cloudinit/CloudConfig/cc_apt_update_upgrade.py index 0cbe02d4..dea89d25 100644 --- a/cloudinit/CloudConfig/cc_apt_update_upgrade.py +++ b/cloudinit/CloudConfig/cc_apt_update_upgrade.py @@ -22,7 +22,7 @@ import os import glob import cloudinit.CloudConfig as cc -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): update = util.get_cfg_option_bool(cfg, 'apt_update', False) upgrade = util.get_cfg_option_bool(cfg, 'apt_upgrade', False) @@ -35,7 +35,7 @@ def handle(_name,cfg,cloud,log,_args): if not util.get_cfg_option_bool(cfg, \ 'apt_preserve_sources_list', False): generate_sources_list(release, mirror) - old_mir = util.get_cfg_option_str(cfg,'apt_old_mirror', \ + old_mir = util.get_cfg_option_str(cfg, 'apt_old_mirror', \ "archive.ubuntu.com/ubuntu") rename_apt_lists(old_mir, mirror) @@ -46,7 +46,7 @@ def handle(_name,cfg,cloud,log,_args): if proxy: try: contents = "Acquire::HTTP::Proxy \"%s\";\n" - with open(proxy_filename,"w") as fp: + with open(proxy_filename, "w") as fp: fp.write(contents % proxy) except Exception as e: log.warn("Failed to write proxy to %s" % proxy_filename) @@ -69,7 +69,7 @@ def handle(_name,cfg,cloud,log,_args): log.error("Failed to run debconf-set-selections") log.debug(traceback.format_exc()) - pkglist = util.get_cfg_option_list_or_str(cfg,'packages',[]) + pkglist = util.get_cfg_option_list_or_str(cfg, 'packages', []) errors = [ ] if update or len(pkglist) or upgrade: @@ -102,26 +102,29 @@ def handle(_name,cfg,cloud,log,_args): return(True) def mirror2lists_fileprefix(mirror): - string=mirror + string = mirror # take of http:// or ftp:// - if string.endswith("/"): string=string[0:-1] - pos=string.find("://") + if string.endswith("/"): + string = string[0:-1] + pos = string.find("://") if pos >= 0: - string=string[pos+3:] - string=string.replace("/","_") + string = string[pos+3:] + string = string.replace("/", "_") return string -def rename_apt_lists(omirror,new_mirror,lists_d="/var/lib/apt/lists"): +def rename_apt_lists(omirror, new_mirror, lists_d="/var/lib/apt/lists"): - oprefix="%s/%s" % (lists_d,mirror2lists_fileprefix(omirror)) - nprefix="%s/%s" % (lists_d,mirror2lists_fileprefix(new_mirror)) - if(oprefix==nprefix): return - olen=len(oprefix) + oprefix = "%s/%s" % (lists_d, mirror2lists_fileprefix(omirror)) + nprefix = "%s/%s" % (lists_d, mirror2lists_fileprefix(new_mirror)) + if(oprefix == nprefix): + return + olen = len(oprefix) for filename in glob.glob("%s_*" % oprefix): - os.rename(filename,"%s%s" % (nprefix, filename[olen:])) + os.rename(filename, "%s%s" % (nprefix, filename[olen:])) def get_release(): - stdout, _stderr = subprocess.Popen(['lsb_release', '-cs'], stdout=subprocess.PIPE).communicate() + stdout, _stderr = subprocess.Popen(['lsb_release', '-cs'], + stdout=subprocess.PIPE).communicate() return(stdout.strip()) def generate_sources_list(codename, mirror): @@ -141,9 +144,10 @@ def add_sources(srclist, searchList=None): elst.append([ "", "missing source" ]) continue - source=ent['source'] + source = ent['source'] if source.startswith("ppa:"): - try: util.subp(["add-apt-repository",source]) + try: + util.subp(["add-apt-repository", source]) except: elst.append([source, "add-apt-repository failed"]) continue @@ -151,7 +155,7 @@ def add_sources(srclist, searchList=None): source = util.render_string(source, searchList) if not ent.has_key('filename'): - ent['filename']='cloud_config_sources.list' + ent['filename'] = 'cloud_config_sources.list' if not ent['filename'].startswith("/"): ent['filename'] = "%s/%s" % \ @@ -159,19 +163,22 @@ def add_sources(srclist, searchList=None): if ( ent.has_key('keyid') and not ent.has_key('key') ): ks = "keyserver.ubuntu.com" - if ent.has_key('keyserver'): ks = ent['keyserver'] + if ent.has_key('keyserver'): + ks = ent['keyserver'] try: ent['key'] = util.getkeybyid(ent['keyid'], ks) except: - elst.append([source,"failed to get key from %s" % ks]) + elst.append([source, "failed to get key from %s" % ks]) continue if ent.has_key('key'): - try: util.subp(('apt-key', 'add', '-'), ent['key']) + try: + util.subp(('apt-key', 'add', '-'), ent['key']) except: elst.append([source, "failed add key"]) - try: util.write_file(ent['filename'], source + "\n", omode="ab") + try: + util.write_file(ent['filename'], source + "\n", omode="ab") except: elst.append([source, "failed write to file %s" % ent['filename']]) @@ -189,7 +196,7 @@ def find_apt_mirror(cloud, cfg): } mirror = None - cfg_mirror = cfg.get("apt_mirror",None) + cfg_mirror = cfg.get("apt_mirror", None) if cfg_mirror: mirror = cfg["apt_mirror"] elif cfg.has_key("apt_mirror_search"): diff --git a/cloudinit/CloudConfig/cc_bootcmd.py b/cloudinit/CloudConfig/cc_bootcmd.py index fc925447..98a7a747 100644 --- a/cloudinit/CloudConfig/cc_bootcmd.py +++ b/cloudinit/CloudConfig/cc_bootcmd.py @@ -21,7 +21,7 @@ import tempfile from cloudinit.CloudConfig import per_always frequency = per_always -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): if not cfg.has_key("bootcmd"): return @@ -35,8 +35,8 @@ def handle(_name,cfg,cloud,log,_args): raise try: - env=os.environ.copy() - env['INSTANCE_ID']=cloud.get_instance_id() + env = os.environ.copy() + env['INSTANCE_ID'] = cloud.get_instance_id() subprocess.check_call(['/bin/sh'], env=env, stdin=tmpf) tmpf.close() except: diff --git a/cloudinit/CloudConfig/cc_byobu.py b/cloudinit/CloudConfig/cc_byobu.py index dd510dda..04825521 100644 --- a/cloudinit/CloudConfig/cc_byobu.py +++ b/cloudinit/CloudConfig/cc_byobu.py @@ -19,13 +19,14 @@ import cloudinit.util as util import subprocess import traceback -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: value = args[0] else: - value = util.get_cfg_option_str(cfg,"byobu_by_default","") + value = util.get_cfg_option_str(cfg, "byobu_by_default", "") - if not value: return + if not value: + return if value == "user" or value == "system": value = "enable-%s" % value @@ -50,7 +51,7 @@ def handle(_name,cfg,_cloud,log,args): shcmd = "" if mod_user: - user = util.get_cfg_option_str(cfg,"user","ubuntu") + user = util.get_cfg_option_str(cfg, "user", "ubuntu") shcmd += " sudo -Hu \"%s\" byobu-launcher-%s" % (user, bl_inst) shcmd += " || X=$(($X+1)); " if mod_sys: diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index 977fe80f..4f740aff 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -23,9 +23,10 @@ import cloudinit.util as util ruby_version_default = "1.8" -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): # If there isn't a chef key in the configuration don't do anything - if not cfg.has_key('chef'): return + if not cfg.has_key('chef'): + return chef_cfg = cfg['chef'] # ensure the chef directories we use exist @@ -35,7 +36,8 @@ def handle(_name,cfg,cloud,log,_args): # set the validation key based on the presence of either 'validation_key' # or 'validation_cert'. In the case where both exist, 'validation_key' # takes precedence - if chef_cfg.has_key('validation_key') or chef_cfg.has_key('validation_cert'): + if (chef_cfg.has_key('validation_key') or + chef_cfg.has_key('validation_cert')): validation_key = util.get_cfg_option_str(chef_cfg, 'validation_key', chef_cfg['validation_cert']) with open('/etc/chef/validation.pem', 'w') as validation_key_fh: @@ -43,12 +45,12 @@ def handle(_name,cfg,cloud,log,_args): # create the chef config from template util.render_to_file('chef_client.rb', '/etc/chef/client.rb', - {'server_url': chef_cfg['server_url'], - 'node_name': util.get_cfg_option_str(chef_cfg, 'node_name', - cloud.datasource.get_instance_id()), - 'environment': util.get_cfg_option_str(chef_cfg, 'environment', - '_default'), - 'validation_name': chef_cfg['validation_name']}) + {'server_url': chef_cfg['server_url'], + 'node_name': util.get_cfg_option_str(chef_cfg, 'node_name', + cloud.datasource.get_instance_id()), + 'environment': util.get_cfg_option_str(chef_cfg, 'environment', + '_default'), + 'validation_name': chef_cfg['validation_name']}) # set the firstboot json with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh: @@ -57,12 +59,14 @@ def handle(_name,cfg,cloud,log,_args): initial_json['run_list'] = chef_cfg['run_list'] if chef_cfg.has_key('initial_attributes'): initial_attributes = chef_cfg['initial_attributes'] - for k in initial_attributes.keys(): initial_json[k] = initial_attributes[k] + for k in initial_attributes.keys(): + initial_json[k] = initial_attributes[k] firstboot_json_fh.write(json.dumps(initial_json)) # If chef is not installed, we install chef based on 'install_type' if not os.path.isfile('/usr/bin/chef-client'): - install_type = util.get_cfg_option_str(chef_cfg, 'install_type', 'packages') + install_type = util.get_cfg_option_str(chef_cfg, 'install_type', + 'packages') if install_type == "gems": # this will install and run the chef-client from gems chef_version = util.get_cfg_option_str(chef_cfg, 'version', None) @@ -71,7 +75,8 @@ def handle(_name,cfg,cloud,log,_args): install_chef_from_gems(ruby_version, chef_version) # and finally, run chef-client log.debug('running chef-client') - subprocess.check_call(['/usr/bin/chef-client', '-d', '-i', '1800', '-s', '20']) + subprocess.check_call(['/usr/bin/chef-client', '-d', '-i', '1800', + '-s', '20']) else: # this will install and run the chef-client from packages cc.install_packages(('chef',)) @@ -90,13 +95,13 @@ def install_chef_from_gems(ruby_version, chef_version = None): if not os.path.exists('/usr/bin/ruby'): os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby') if chef_version: - subprocess.check_call(['/usr/bin/gem','install','chef', + subprocess.check_call(['/usr/bin/gem', 'install', 'chef', '-v %s' % chef_version, '--no-ri', - '--no-rdoc','--bindir','/usr/bin','-q']) + '--no-rdoc', '--bindir', '/usr/bin', '-q']) else: - subprocess.check_call(['/usr/bin/gem','install','chef', - '--no-ri','--no-rdoc','--bindir', - '/usr/bin','-q']) + subprocess.check_call(['/usr/bin/gem', 'install', 'chef', + '--no-ri', '--no-rdoc', '--bindir', + '/usr/bin', '-q']) def ensure_dir(d): if not os.path.exists(d): diff --git a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py index f06d4dfc..383e3b0c 100644 --- a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py +++ b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py @@ -21,7 +21,7 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(_name,cfg,_cloud,_log,_args): +def handle(_name, cfg, _cloud, _log, _args): if util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False): - fwall="route add -host 169.254.169.254 reject" + fwall = "route add -host 169.254.169.254 reject" subprocess.call(fwall.split(' ')) diff --git a/cloudinit/CloudConfig/cc_final_message.py b/cloudinit/CloudConfig/cc_final_message.py index c8631d01..7930bab5 100644 --- a/cloudinit/CloudConfig/cc_final_message.py +++ b/cloudinit/CloudConfig/cc_final_message.py @@ -24,15 +24,15 @@ frequency = per_always final_message = "cloud-init boot finished at $TIMESTAMP. Up $UPTIME seconds" -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: msg_in = args[0] else: - msg_in = util.get_cfg_option_str(cfg,"final_message",final_message) + msg_in = util.get_cfg_option_str(cfg, "final_message", final_message) try: - uptimef=open("/proc/uptime") - uptime=uptimef.read().split(" ")[0] + uptimef = open("/proc/uptime") + uptime = uptimef.read().split(" ")[0] uptimef.close() except IOError as e: log.warn("unable to open /proc/uptime\n") @@ -40,7 +40,7 @@ def handle(_name,cfg,_cloud,log,args): try: - ts = time.strftime("%a, %d %b %Y %H:%M:%S %z",time.gmtime()) + ts = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.gmtime()) except: ts = "na" diff --git a/cloudinit/CloudConfig/cc_foo.py b/cloudinit/CloudConfig/cc_foo.py index 48d20e5b..82a44baf 100644 --- a/cloudinit/CloudConfig/cc_foo.py +++ b/cloudinit/CloudConfig/cc_foo.py @@ -22,5 +22,5 @@ from cloudinit.CloudConfig import per_instance frequency = per_instance -def handle(_name,_cfg,_cloud,_log,_args): +def handle(_name, _cfg, _cloud, _log, _args): print "hi" diff --git a/cloudinit/CloudConfig/cc_grub_dpkg.py b/cloudinit/CloudConfig/cc_grub_dpkg.py index 97d79bdb..1437d481 100644 --- a/cloudinit/CloudConfig/cc_grub_dpkg.py +++ b/cloudinit/CloudConfig/cc_grub_dpkg.py @@ -20,23 +20,26 @@ import cloudinit.util as util import traceback import os -def handle(_name,cfg,_cloud,log,_args): +def handle(_name, cfg, _cloud, log, _args): - idevs=None - idevs_empty=None + idevs = None + idevs_empty = None if "grub-dpkg" in cfg: - idevs=util.get_cfg_option_str(cfg["grub-dpkg"], - "grub-pc/install_devices",None) - idevs_empty=util.get_cfg_option_str(cfg["grub-dpkg"], - "grub-pc/install_devices_empty",None) + idevs = util.get_cfg_option_str(cfg["grub-dpkg"], + "grub-pc/install_devices", None) + idevs_empty = util.get_cfg_option_str(cfg["grub-dpkg"], + "grub-pc/install_devices_empty", None) if (( os.path.exists("/dev/sda1") and not os.path.exists("/dev/sda") ) or ( os.path.exists("/dev/xvda1") and not os.path.exists("/dev/xvda") )): - if idevs == None: idevs="" - if idevs_empty == None: idevs_empty="true" + if idevs == None: + idevs = "" + if idevs_empty == None: + idevs_empty = "true" else: - if idevs_empty == None: idevs_empty="false" + if idevs_empty == None: + idevs_empty = "false" if idevs == None: idevs = "/dev/sda" for dev in ( "/dev/sda", "/dev/vda", "/dev/sda1", "/dev/vda1"): @@ -50,7 +53,7 @@ def handle(_name,cfg,_cloud,log,_args): dconf_sel = "grub-pc grub-pc/install_devices string %s\n" % idevs + \ "grub-pc grub-pc/install_devices_empty boolean %s\n" % idevs_empty log.debug("setting grub debconf-set-selections with '%s','%s'" % - (idevs,idevs_empty)) + (idevs, idevs_empty)) try: util.subp(('debconf-set-selections'), dconf_sel) diff --git a/cloudinit/CloudConfig/cc_keys_to_console.py b/cloudinit/CloudConfig/cc_keys_to_console.py index 08e8f085..d462a0a8 100644 --- a/cloudinit/CloudConfig/cc_keys_to_console.py +++ b/cloudinit/CloudConfig/cc_keys_to_console.py @@ -21,12 +21,14 @@ import subprocess frequency = per_instance -def handle(_name,cfg,_cloud,log,_args): +def handle(_name, cfg, _cloud, log, _args): cmd = [ '/usr/lib/cloud-init/write-ssh-key-fingerprints' ] - fp_blacklist = util.get_cfg_option_list_or_str(cfg, "ssh_fp_console_blacklist", []) - key_blacklist = util.get_cfg_option_list_or_str(cfg, "ssh_key_console_blacklist", ["ssh-dss"]) + fp_blacklist = util.get_cfg_option_list_or_str(cfg, + "ssh_fp_console_blacklist", []) + key_blacklist = util.get_cfg_option_list_or_str(cfg, + "ssh_key_console_blacklist", ["ssh-dss"]) try: - confp = open('/dev/console',"wb") + confp = open('/dev/console', "wb") cmd.append(','.join(fp_blacklist)) cmd.append(','.join(key_blacklist)) subprocess.call(cmd, stdout=confp) diff --git a/cloudinit/CloudConfig/cc_landscape.py b/cloudinit/CloudConfig/cc_landscape.py index 22a90665..d2d2bd19 100644 --- a/cloudinit/CloudConfig/cc_landscape.py +++ b/cloudinit/CloudConfig/cc_landscape.py @@ -32,7 +32,7 @@ lsc_builtincfg = { } } -def handle(_name,cfg,_cloud,log,_args): +def handle(_name, cfg, _cloud, log, _args): """ Basically turn a top level 'landscape' entry with a 'client' dict and render it to ConfigObj format under '[client]' section in diff --git a/cloudinit/CloudConfig/cc_locale.py b/cloudinit/CloudConfig/cc_locale.py index 8e91d3bf..991f5861 100644 --- a/cloudinit/CloudConfig/cc_locale.py +++ b/cloudinit/CloudConfig/cc_locale.py @@ -28,16 +28,17 @@ def apply_locale(locale, cfgfile): util.render_to_file('default-locale', cfgfile, { 'locale' : locale }) -def handle(_name,cfg,cloud,log,args): +def handle(_name, cfg, cloud, log, args): if len(args) != 0: locale = args[0] else: - locale = util.get_cfg_option_str(cfg,"locale",cloud.get_locale()) + locale = util.get_cfg_option_str(cfg, "locale", cloud.get_locale()) locale_cfgfile = util.get_cfg_option_str(cfg, "locale_configfile", "/etc/default/locale") - if not locale: return + if not locale: + return log.debug("setting locale to %s" % locale) diff --git a/cloudinit/CloudConfig/cc_mcollective.py b/cloudinit/CloudConfig/cc_mcollective.py index 38fe4a3c..8ad8caab 100644 --- a/cloudinit/CloudConfig/cc_mcollective.py +++ b/cloudinit/CloudConfig/cc_mcollective.py @@ -34,13 +34,17 @@ class FakeSecHead(object): self.sechead = '[nullsection]\n' def readline(self): if self.sechead: - try: return self.sechead - finally: self.sechead = None - else: return self.fp.readline() + try: + return self.sechead + finally: + self.sechead = None + else: + return self.fp.readline() -def handle(_name,cfg,_cloud,_log,_args): +def handle(_name, cfg, _cloud, _log, _args): # If there isn't a mcollective key in the configuration don't do anything - if not cfg.has_key('mcollective'): return + if not cfg.has_key('mcollective'): + return mcollective_cfg = cfg['mcollective'] # Start by installing the mcollective package ... cc.install_packages(("mcollective",)) @@ -49,27 +53,30 @@ def handle(_name,cfg,_cloud,_log,_args): if mcollective_cfg.has_key('conf'): # Create object for reading server.cfg values mcollective_config = ConfigParser.ConfigParser() - # Read server.cfg values from original file in order to be able to mix the rest up - mcollective_config.readfp(FakeSecHead(open('/etc/mcollective/server.cfg'))) + # Read server.cfg values from original file in order to be able to mix + # the rest up + mcollective_config.readfp(FakeSecHead(open('/etc/mcollective/' + 'server.cfg'))) for cfg_name, cfg in mcollective_cfg['conf'].iteritems(): if cfg_name == 'public-cert': util.write_file(pubcert_file, cfg, mode=0644) mcollective_config.set(cfg_name, 'plugin.ssl_server_public', pubcert_file) - mcollective_config.set(cfg_name,'securityprovider','ssl') + mcollective_config.set(cfg_name, 'securityprovider', 'ssl') elif cfg_name == 'private-cert': util.write_file(pricert_file, cfg, mode=0600) mcollective_config.set(cfg_name, 'plugin.ssl_server_private', pricert_file) - mcollective_config.set(cfg_name,'securityprovider','ssl') + mcollective_config.set(cfg_name, 'securityprovider', 'ssl') else: # Iterate throug the config items, we'll use ConfigParser.set # to overwrite or create new items as needed for o, v in cfg.iteritems(): - mcollective_config.set(cfg_name,o,v) + mcollective_config.set(cfg_name, o, v) # We got all our config as wanted we'll rename # the previous server.cfg and create our new one - os.rename('/etc/mcollective/server.cfg','/etc/mcollective/server.cfg.old') + os.rename('/etc/mcollective/server.cfg', + '/etc/mcollective/server.cfg.old') outputfile = StringIO.StringIO() mcollective_config.write(outputfile) # Now we got the whole file, write to disk except first line @@ -79,7 +86,8 @@ def handle(_name,cfg,_cloud,_log,_args): # search and replace of '=' with ':' could be problematic though. # this most likely needs fixing. util.write_file('/etc/mcollective/server.cfg', - outputfile.getvalue().replace('[nullsection]\n','').replace(' =',':'), + outputfile.getvalue().replace('[nullsection]\n', '').replace(' =', + ':'), mode=0644) # Start mcollective diff --git a/cloudinit/CloudConfig/cc_mounts.py b/cloudinit/CloudConfig/cc_mounts.py index a3036d5a..f7d8c702 100644 --- a/cloudinit/CloudConfig/cc_mounts.py +++ b/cloudinit/CloudConfig/cc_mounts.py @@ -31,7 +31,7 @@ def is_mdname(name): return True return False -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): # fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno defvals = [ None, None, "auto", "defaults,nobootwait", "0", "2" ] defvals = cfg.get("mount_default_fields", defvals) @@ -50,7 +50,8 @@ def handle(_name,cfg,cloud,log,_args): for i in range(len(cfgmnt)): # skip something that wasn't a list - if not isinstance(cfgmnt[i],list): continue + if not isinstance(cfgmnt[i], list): + continue # workaround, allow user to specify 'ephemeral' # rather than more ec2 correct 'ephemeral0' @@ -75,7 +76,7 @@ def handle(_name,cfg,cloud,log,_args): # but do not convert None to 'None' (LP: #898365) for j in range(len(cfgmnt[i])): if isinstance(cfgmnt[i][j], int): - cfgmnt[i][j]=str(cfgmnt[i][j]) + cfgmnt[i][j] = str(cfgmnt[i][j]) for i in range(len(cfgmnt)): # fill in values with defaults from defvals above @@ -98,7 +99,8 @@ def handle(_name,cfg,cloud,log,_args): # entry has the same device name for defmnt in defmnts: devname = cloud.device_name_to_device(defmnt[0]) - if devname is None: continue + if devname is None: + continue if devname.startswith("/"): defmnt[0] = devname else: @@ -110,7 +112,8 @@ def handle(_name,cfg,cloud,log,_args): cfgmnt_has = True break - if cfgmnt_has: continue + if cfgmnt_has: + continue cfgmnt.append(defmnt) @@ -118,26 +121,30 @@ def handle(_name,cfg,cloud,log,_args): # if the second field is None (not the string, the value) we skip it actlist = [x for x in cfgmnt if x[1] is not None] - if len(actlist) == 0: return + if len(actlist) == 0: + return - comment="comment=cloudconfig" + comment = "comment=cloudconfig" cc_lines = [ ] needswap = False dirs = [ ] for line in actlist: # write 'comment' in the fs_mntops, entry, claiming this - line[3]="%s,comment=cloudconfig" % line[3] - if line[2] == "swap": needswap = True - if line[1].startswith("/"): dirs.append(line[1]) + line[3] = "%s,comment=cloudconfig" % line[3] + if line[2] == "swap": + needswap = True + if line[1].startswith("/"): + dirs.append(line[1]) cc_lines.append('\t'.join(line)) fstab_lines = [ ] - fstab=open("/etc/fstab","r+") + fstab = open("/etc/fstab", "r+") ws = re.compile("[%s]+" % string.whitespace) for line in fstab.read().splitlines(): try: toks = ws.split(line) - if toks[3].find(comment) != -1: continue + if toks[3].find(comment) != -1: + continue except: pass fstab_lines.append(line) @@ -150,13 +157,20 @@ def handle(_name,cfg,cloud,log,_args): fstab.close() if needswap: - try: util.subp(("swapon", "-a")) - except: log.warn("Failed to enable swap") + try: + util.subp(("swapon", "-a")) + except: + log.warn("Failed to enable swap") for d in dirs: - if os.path.exists(d): continue - try: os.makedirs(d) - except: log.warn("Failed to make '%s' config-mount\n",d) + if os.path.exists(d): + continue + try: + os.makedirs(d) + except: + log.warn("Failed to make '%s' config-mount\n", d) - try: util.subp(("mount","-a")) - except: log.warn("'mount -a' failed") + try: + util.subp(("mount", "-a")) + except: + log.warn("'mount -a' failed") diff --git a/cloudinit/CloudConfig/cc_phone_home.py b/cloudinit/CloudConfig/cc_phone_home.py index 7897d31b..05caf8eb 100644 --- a/cloudinit/CloudConfig/cc_phone_home.py +++ b/cloudinit/CloudConfig/cc_phone_home.py @@ -20,7 +20,8 @@ import cloudinit.util as util from time import sleep frequency = per_instance -post_list_all = [ 'pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', 'hostname' ] +post_list_all = ['pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', + 'hostname'] # phone_home: # url: http://my.foo.bar/$INSTANCE/ @@ -31,11 +32,12 @@ post_list_all = [ 'pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', # url: http://my.foo.bar/$INSTANCE_ID/ # post: [ pub_key_dsa, pub_key_rsa, pub_key_ecdsa, instance_id # -def handle(_name,cfg,cloud,log,args): +def handle(_name, cfg, cloud, log, args): if len(args) != 0: ph_cfg = util.readconf(args[0]) else: - if not 'phone_home' in cfg: return + if not 'phone_home' in cfg: + return ph_cfg = cfg['phone_home'] if 'url' not in ph_cfg: @@ -44,7 +46,7 @@ def handle(_name,cfg,cloud,log,args): url = ph_cfg['url'] post_list = ph_cfg.get('post', 'all') - tries = ph_cfg.get('tries',10) + tries = ph_cfg.get('tries', 10) try: tries = int(tries) except: @@ -83,7 +85,7 @@ def handle(_name,cfg,cloud,log,args): url = util.render_string(url, { 'INSTANCE_ID' : all_keys['instance_id'] }) last_e = None - for i in range(0,tries): + for i in range(0, tries): try: util.readurl(url, submit_keys) log.debug("succeeded submit to %s on try %i" % (url, i+1)) diff --git a/cloudinit/CloudConfig/cc_puppet.py b/cloudinit/CloudConfig/cc_puppet.py index 3748559a..5fb0c1ee 100644 --- a/cloudinit/CloudConfig/cc_puppet.py +++ b/cloudinit/CloudConfig/cc_puppet.py @@ -25,9 +25,10 @@ import ConfigParser import cloudinit.CloudConfig as cc import cloudinit.util as util -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): # If there isn't a puppet key in the configuration don't do anything - if not cfg.has_key('puppet'): return + if not cfg.has_key('puppet'): + return puppet_cfg = cfg['puppet'] # Start by installing the puppet package ... cc.install_packages(("puppet",)) @@ -38,8 +39,10 @@ def handle(_name,cfg,cloud,log,_args): puppet_conf_fh = open('/etc/puppet/puppet.conf', 'r') # Create object for reading puppet.conf values puppet_config = ConfigParser.ConfigParser() - # Read puppet.conf values from original file in order to be able to mix the rest up - puppet_config.readfp(StringIO.StringIO(''.join(i.lstrip() for i in puppet_conf_fh.readlines()))) + # Read puppet.conf values from original file in order to be able to + # mix the rest up + puppet_config.readfp(StringIO.StringIO(''.join(i.lstrip() for i in + puppet_conf_fh.readlines()))) # Close original file, no longer needed puppet_conf_fh.close() for cfg_name, cfg in puppet_cfg['conf'].iteritems(): @@ -63,7 +66,8 @@ def handle(_name,cfg,cloud,log,_args): util.restorecon_if_possible('/var/lib/puppet', recursive=True) else: #puppet_conf_fh.write("\n[%s]\n" % (cfg_name)) - # If puppet.conf already has this section we don't want to write it again + # If puppet.conf already has this section we don't want to + # write it again if puppet_config.has_section(cfg_name) == False: puppet_config.add_section(cfg_name) # Iterate throug the config items, we'll use ConfigParser.set @@ -77,11 +81,11 @@ def handle(_name,cfg,cloud,log,_args): cloud.datasource.get_instance_id()) # certname needs to be downcase v = v.lower() - puppet_config.set(cfg_name,o,v) + puppet_config.set(cfg_name, o, v) #puppet_conf_fh.write("%s=%s\n" % (o, v)) # We got all our config as wanted we'll rename # the previous puppet.conf and create our new one - os.rename('/etc/puppet/puppet.conf','/etc/puppet/puppet.conf.old') + os.rename('/etc/puppet/puppet.conf', '/etc/puppet/puppet.conf.old') with open('/etc/puppet/puppet.conf', 'wb') as configfile: puppet_config.write(configfile) util.restorecon_if_possible('/etc/puppet/puppet.conf') diff --git a/cloudinit/CloudConfig/cc_resizefs.py b/cloudinit/CloudConfig/cc_resizefs.py index adec70be..d960afd5 100644 --- a/cloudinit/CloudConfig/cc_resizefs.py +++ b/cloudinit/CloudConfig/cc_resizefs.py @@ -25,15 +25,16 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: resize_root = False if str(args[0]).lower() in [ 'true', '1', 'on', 'yes']: resize_root = True else: - resize_root = util.get_cfg_option_bool(cfg,"resize_rootfs",True) + resize_root = util.get_cfg_option_bool(cfg, "resize_rootfs", True) - if not resize_root: return + if not resize_root: + return # this really only uses the filename from mktemp, then we mknod into it (fd, devpth) = tempfile.mkstemp() @@ -41,8 +42,8 @@ def handle(_name,cfg,_cloud,log,args): os.close(fd) try: - st_dev=os.stat("/").st_dev - dev=os.makedev(os.major(st_dev),os.minor(st_dev)) + st_dev = os.stat("/").st_dev + dev = os.makedev(os.major(st_dev), os.minor(st_dev)) os.mknod(devpth, 0400 | stat.S_IFBLK, dev) except: if util.islxc(): @@ -53,7 +54,7 @@ def handle(_name,cfg,_cloud,log,args): cmd = [ 'blkid', '-c', '/dev/null', '-sTYPE', '-ovalue', devpth ] try: - (fstype,_err) = util.subp(cmd) + (fstype, _err) = util.subp(cmd) except subprocess.CalledProcessError as e: log.warn("Failed to get filesystem type of maj=%s, min=%s via: %s" % (os.major(st_dev), os.minor(st_dev), cmd)) @@ -62,9 +63,9 @@ def handle(_name,cfg,_cloud,log,args): raise log.debug("resizing root filesystem (type=%s, maj=%i, min=%i)" % - (fstype.rstrip("\n"), os.major(st_dev), os.minor(st_dev))) + (str(fstype).rstrip("\n"), os.major(st_dev), os.minor(st_dev))) - if fstype.startswith("ext"): + if str(fstype).startswith("ext"): resize_cmd = [ 'resize2fs', devpth ] elif fstype == "xfs": resize_cmd = [ 'xfs_growfs', devpth ] diff --git a/cloudinit/CloudConfig/cc_rightscale_userdata.py b/cloudinit/CloudConfig/cc_rightscale_userdata.py index 2b43023c..61aa89d1 100644 --- a/cloudinit/CloudConfig/cc_rightscale_userdata.py +++ b/cloudinit/CloudConfig/cc_rightscale_userdata.py @@ -42,7 +42,7 @@ frequency = per_instance my_name = "cc_rightscale_userdata" my_hookname = 'CLOUD_INIT_REMOTE_HOOK' -def handle(_name,_cfg,cloud,log,_args): +def handle(_name, _cfg, cloud, log, _args): try: ud = cloud.get_userdata_raw() except: @@ -51,7 +51,8 @@ def handle(_name,_cfg,cloud,log,_args): try: mdict = parse_qs(ud) - if not my_hookname in mdict: return + if not my_hookname in mdict: + return except: log.warn("failed to urlparse.parse_qa(userdata_raw())") raise @@ -60,13 +61,14 @@ def handle(_name,_cfg,cloud,log,_args): i = 0 first_e = None for url in mdict[my_hookname]: - fname = "%s/rightscale-%02i" % (scripts_d,i) + fname = "%s/rightscale-%02i" % (scripts_d, i) i = i +1 try: content = util.readurl(url) util.write_file(fname, content, mode=0700) except Exception as e: - if not first_e: first_e = None + if not first_e: + first_e = None log.warn("%s failed to read %s: %s" % (my_name, url, e)) if first_e: diff --git a/cloudinit/CloudConfig/cc_rsyslog.py b/cloudinit/CloudConfig/cc_rsyslog.py index ab85a6d8..e5f38c36 100644 --- a/cloudinit/CloudConfig/cc_rsyslog.py +++ b/cloudinit/CloudConfig/cc_rsyslog.py @@ -24,7 +24,7 @@ import traceback DEF_FILENAME = "20-cloud-config.conf" DEF_DIR = "/etc/rsyslog.d" -def handle(_name,cfg,_cloud,log,_args): +def handle(_name, cfg, _cloud, log, _args): # rsyslog: # - "*.* @@192.158.1.1" # - content: "*.* @@192.0.2.1:10514" @@ -33,7 +33,8 @@ def handle(_name,cfg,_cloud,log,_args): # *.* @@syslogd.example.com # process 'rsyslog' - if not 'rsyslog' in cfg: return + if not 'rsyslog' in cfg: + return def_dir = cfg.get('rsyslog_dir', DEF_DIR) def_fname = cfg.get('rsyslog_filename', DEF_FILENAME) @@ -41,7 +42,7 @@ def handle(_name,cfg,_cloud,log,_args): files = [ ] elst = [ ] for ent in cfg['rsyslog']: - if isinstance(ent,dict): + if isinstance(ent, dict): if not "content" in ent: elst.append((ent, "no 'content' entry")) continue @@ -52,7 +53,7 @@ def handle(_name,cfg,_cloud,log,_args): filename = def_fname if not filename.startswith("/"): - filename = "%s/%s" % (def_dir,filename) + filename = "%s/%s" % (def_dir, filename) omode = "ab" # truncate filename first time you see it diff --git a/cloudinit/CloudConfig/cc_runcmd.py b/cloudinit/CloudConfig/cc_runcmd.py index d255223b..3c9baa6f 100644 --- a/cloudinit/CloudConfig/cc_runcmd.py +++ b/cloudinit/CloudConfig/cc_runcmd.py @@ -18,12 +18,12 @@ import cloudinit.util as util -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): if not cfg.has_key("runcmd"): return - outfile="%s/runcmd" % cloud.get_ipath('scripts') + outfile = "%s/runcmd" % cloud.get_ipath('scripts') try: content = util.shellify(cfg["runcmd"]) - util.write_file(outfile,content,0700) + util.write_file(outfile, content, 0700) except: log.warn("failed to open %s for runcmd" % outfile) diff --git a/cloudinit/CloudConfig/cc_scripts_per_boot.py b/cloudinit/CloudConfig/cc_scripts_per_boot.py index fb643c6d..ee79f0a3 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_boot.py +++ b/cloudinit/CloudConfig/cc_scripts_per_boot.py @@ -23,7 +23,7 @@ from cloudinit import get_cpath frequency = per_always runparts_path = "%s/%s" % (get_cpath(), "scripts/per-boot") -def handle(_name,_cfg,_cloud,log,_args): +def handle(_name, _cfg, _cloud, log, _args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_per_instance.py b/cloudinit/CloudConfig/cc_scripts_per_instance.py index b0f0601a..499829ec 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_instance.py +++ b/cloudinit/CloudConfig/cc_scripts_per_instance.py @@ -23,7 +23,7 @@ from cloudinit import get_cpath frequency = per_instance runparts_path = "%s/%s" % (get_cpath(), "scripts/per-instance") -def handle(_name,_cfg,_cloud,log,_args): +def handle(_name, _cfg, _cloud, log, _args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_per_once.py b/cloudinit/CloudConfig/cc_scripts_per_once.py index 2ab81840..6c43c6f0 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_once.py +++ b/cloudinit/CloudConfig/cc_scripts_per_once.py @@ -23,7 +23,7 @@ from cloudinit import get_cpath frequency = per_once runparts_path = "%s/%s" % (get_cpath(), "scripts/per-once") -def handle(_name,_cfg,_cloud,log,_args): +def handle(_name, _cfg, _cloud, log, _args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_user.py b/cloudinit/CloudConfig/cc_scripts_user.py index 9c7f2322..3db3c7a6 100644 --- a/cloudinit/CloudConfig/cc_scripts_user.py +++ b/cloudinit/CloudConfig/cc_scripts_user.py @@ -23,7 +23,7 @@ from cloudinit import get_ipath_cur frequency = per_instance runparts_path = "%s/%s" % (get_ipath_cur(), "scripts") -def handle(_name,_cfg,_cloud,log,_args): +def handle(_name, _cfg, _cloud, log, _args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_set_hostname.py b/cloudinit/CloudConfig/cc_set_hostname.py index 4f19b0c8..18189ed0 100644 --- a/cloudinit/CloudConfig/cc_set_hostname.py +++ b/cloudinit/CloudConfig/cc_set_hostname.py @@ -18,8 +18,8 @@ import cloudinit.util as util -def handle(_name,cfg,cloud,log,_args): - if util.get_cfg_option_bool(cfg,"preserve_hostname",False): +def handle(_name, cfg, cloud, log, _args): + if util.get_cfg_option_bool(cfg, "preserve_hostname", False): log.debug("preserve_hostname is set. not setting hostname") return(True) @@ -34,5 +34,5 @@ def handle(_name,cfg,cloud,log,_args): def set_hostname(hostname, log): util.subp(['hostname', hostname]) - util.write_file("/etc/hostname","%s\n" % hostname, 0644) + util.write_file("/etc/hostname", "%s\n" % hostname, 0644) log.debug("populated /etc/hostname with %s on first boot", hostname) diff --git a/cloudinit/CloudConfig/cc_set_passwords.py b/cloudinit/CloudConfig/cc_set_passwords.py index 07e3ca1b..15533460 100644 --- a/cloudinit/CloudConfig/cc_set_passwords.py +++ b/cloudinit/CloudConfig/cc_set_passwords.py @@ -21,14 +21,14 @@ import sys import random import string -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: # if run from command line, and give args, wipe the chpasswd['list'] password = args[0] if 'chpasswd' in cfg and 'list' in cfg['chpasswd']: del cfg['chpasswd']['list'] else: - password = util.get_cfg_option_str(cfg,"password",None) + password = util.get_cfg_option_str(cfg, "password", None) expire = True pw_auth = "no" @@ -37,11 +37,11 @@ def handle(_name,cfg,_cloud,log,args): if 'chpasswd' in cfg: chfg = cfg['chpasswd'] - plist = util.get_cfg_option_str(chfg,'list',plist) - expire = util.get_cfg_option_bool(chfg,'expire', expire) + plist = util.get_cfg_option_str(chfg, 'list', plist) + expire = util.get_cfg_option_bool(chfg, 'expire', expire) if not plist and password: - user = util.get_cfg_option_str(cfg,"user","ubuntu") + user = util.get_cfg_option_str(cfg, "user", "ubuntu") plist = "%s:%s" % (user, password) errors = [] @@ -50,11 +50,11 @@ def handle(_name,cfg,_cloud,log,args): randlist = [] users = [] for line in plist.splitlines(): - u,p = line.split(':',1) + u, p = line.split(':', 1) if p == "R" or p == "RANDOM": p = rand_user_password() - randlist.append("%s:%s" % (u,p)) - plist_in.append("%s:%s" % (u,p)) + randlist.append("%s:%s" % (u, p)) + plist_in.append("%s:%s" % (u, p)) users.append(u) ch_in = '\n'.join(plist_in) @@ -70,7 +70,7 @@ def handle(_name,cfg,_cloud,log,args): '\n'.join(randlist) )) if expire: - enum=len(errors) + enum = len(errors) for u in users: try: util.subp(['passwd', '--expire', u]) @@ -83,13 +83,13 @@ def handle(_name,cfg,_cloud,log,args): if 'ssh_pwauth' in cfg: val = str(cfg['ssh_pwauth']).lower() if val in ( "true", "1", "yes"): - pw_auth="yes" - change_pwauth=True + pw_auth = "yes" + change_pwauth = True elif val in ( "false", "0", "no"): - pw_auth="no" - change_pwauth=True + pw_auth = "no" + change_pwauth = True else: - change_pwauth=False + change_pwauth = False if change_pwauth: pa_s = "\(#*\)\(PasswordAuthentication[[:space:]]\+\)\(yes\|no\)" @@ -118,7 +118,7 @@ def rand_str(strlen=32, select_from=string.letters+string.digits): return("".join([random.choice(select_from) for _x in range(0, strlen)])) def rand_user_password(pwlen=9): - selfrom=(string.letters.translate(None,'loLOI') + - string.digits.translate(None,'01')) - return(rand_str(pwlen,select_from=selfrom)) + selfrom = (string.letters.translate(None, 'loLOI') + + string.digits.translate(None, '01')) + return(rand_str(pwlen, select_from=selfrom)) diff --git a/cloudinit/CloudConfig/cc_ssh.py b/cloudinit/CloudConfig/cc_ssh.py index 0aad2187..b6ac1edb 100644 --- a/cloudinit/CloudConfig/cc_ssh.py +++ b/cloudinit/CloudConfig/cc_ssh.py @@ -21,20 +21,24 @@ import os import glob import subprocess -DISABLE_ROOT_OPTS="no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" rather than the user \\\"root\\\".\';echo;sleep 10\"" +DISABLE_ROOT_OPTS = "no-port-forwarding,no-agent-forwarding," \ +"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" " \ +"rather than the user \\\"root\\\".\';echo;sleep 10\"" global_log = None -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): global global_log global_log = log # remove the static keys from the pristine image if cfg.get("ssh_deletekeys", True): for f in glob.glob("/etc/ssh/ssh_host_*key*"): - try: os.unlink(f) - except: pass + try: + os.unlink(f) + except: + pass if cfg.has_key("ssh_keys"): # if there are keys in cloud-config, use them @@ -47,17 +51,18 @@ def handle(_name,cfg,cloud,log,_args): "ecdsa_public" : ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644), } - for key,val in cfg["ssh_keys"].items(): + for key, val in cfg["ssh_keys"].items(): if key2file.has_key(key): - util.write_file(key2file[key][0],val,key2file[key][1]) + util.write_file(key2file[key][0], val, key2file[key][1]) priv2pub = { 'rsa_private':'rsa_public', 'dsa_private':'dsa_public', 'ecdsa_private': 'ecdsa_public', } cmd = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"' - for priv,pub in priv2pub.iteritems(): - if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']: continue - pair=(key2file[priv][0], key2file[pub][0]) + for priv, pub in priv2pub.iteritems(): + if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']: + continue + pair = (key2file[priv][0], key2file[pub][0]) subprocess.call(('sh', '-xc', cmd % pair)) log.debug("generated %s from %s" % pair) else: @@ -72,7 +77,7 @@ def handle(_name,cfg,cloud,log,_args): util.restorecon_if_possible('/etc/ssh', recursive=True) try: - user = util.get_cfg_option_str(cfg,'user') + user = util.get_cfg_option_str(cfg, 'user') disable_root = util.get_cfg_option_bool(cfg, "disable_root", True) disable_root_opts = util.get_cfg_option_str(cfg, "disable_root_opts", DISABLE_ROOT_OPTS) @@ -82,12 +87,13 @@ def handle(_name,cfg,cloud,log,_args): cfgkeys = cfg["ssh_authorized_keys"] keys.extend(cfgkeys) - apply_credentials(keys,user,disable_root, disable_root_opts) + apply_credentials(keys, user, disable_root, disable_root_opts) except: util.logexc(log) log.warn("applying credentials failed!\n") -def apply_credentials(keys, user, disable_root, disable_root_opts=DISABLE_ROOT_OPTS, log=global_log): +def apply_credentials(keys, user, disable_root, + disable_root_opts=DISABLE_ROOT_OPTS, log=global_log): keys = set(keys) if user: sshutil.setup_user_keys(keys, user, '', log) diff --git a/cloudinit/CloudConfig/cc_ssh_import_id.py b/cloudinit/CloudConfig/cc_ssh_import_id.py index 7e7a54a1..efcd4296 100644 --- a/cloudinit/CloudConfig/cc_ssh_import_id.py +++ b/cloudinit/CloudConfig/cc_ssh_import_id.py @@ -19,17 +19,18 @@ import cloudinit.util as util import subprocess import traceback -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: user = args[0] ids = [ ] if len(args) > 1: ids = args[1:] else: - user = util.get_cfg_option_str(cfg,"user","ubuntu") - ids = util.get_cfg_option_list_or_str(cfg,"ssh_import_id",[]) + user = util.get_cfg_option_str(cfg, "user", "ubuntu") + ids = util.get_cfg_option_list_or_str(cfg, "ssh_import_id", []) - if len(ids) == 0: return + if len(ids) == 0: + return cmd = [ "sudo", "-Hu", user, "ssh-import-id" ] + ids diff --git a/cloudinit/CloudConfig/cc_timezone.py b/cloudinit/CloudConfig/cc_timezone.py index 26b2796d..87855503 100644 --- a/cloudinit/CloudConfig/cc_timezone.py +++ b/cloudinit/CloudConfig/cc_timezone.py @@ -24,13 +24,14 @@ import shutil frequency = per_instance tz_base = "/usr/share/zoneinfo" -def handle(_name,cfg,_cloud,log,args): +def handle(_name, cfg, _cloud, log, args): if len(args) != 0: timezone = args[0] else: - timezone = util.get_cfg_option_str(cfg,"timezone",False) + timezone = util.get_cfg_option_str(cfg, "timezone", False) - if not timezone: return + if not timezone: + return tz_file = "%s/%s" % (tz_base , timezone) @@ -39,7 +40,7 @@ def handle(_name,cfg,_cloud,log,args): raise Exception("Invalid timezone %s" % tz_file) try: - fp=open("/etc/timezone","wb") + fp = open("/etc/timezone", "wb") fp.write("%s\n" % timezone) fp.close() except: diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py index 1c245e67..66f0537c 100644 --- a/cloudinit/CloudConfig/cc_update_etc_hosts.py +++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py @@ -21,10 +21,10 @@ import StringIO frequency = per_always -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) - manage_hosts = util.get_cfg_option_bool(cfg,"manage_etc_hosts", False) + manage_hosts = util.get_cfg_option_bool(cfg, "manage_etc_hosts", False) if manage_hosts in ("True", "true", True, "template"): # render from template file try: @@ -76,7 +76,7 @@ def update_etc_hosts(hostname, fqdn, _log): new_etchosts.write("%s%s" % (header, hosts_line)) need_write = True if need_write == True: - new_etcfile = open ('/etc/hosts','wb') + new_etcfile = open('/etc/hosts', 'wb') new_etcfile.write(new_etchosts.getvalue()) new_etcfile.close() new_etchosts.close() diff --git a/cloudinit/CloudConfig/cc_update_hostname.py b/cloudinit/CloudConfig/cc_update_hostname.py index 4bc1cb2b..893c99e0 100644 --- a/cloudinit/CloudConfig/cc_update_hostname.py +++ b/cloudinit/CloudConfig/cc_update_hostname.py @@ -22,14 +22,14 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(_name,cfg,cloud,log,_args): - if util.get_cfg_option_bool(cfg,"preserve_hostname",False): +def handle(_name, cfg, cloud, log, _args): + if util.get_cfg_option_bool(cfg, "preserve_hostname", False): log.debug("preserve_hostname is set. not updating hostname") return ( hostname, _fqdn ) = util.get_hostname_fqdn(cfg, cloud) try: - prev ="%s/%s" % (cloud.get_cpath('data'),"previous-hostname") + prev ="%s/%s" % (cloud.get_cpath('data'), "previous-hostname") update_hostname(hostname, prev, log) except Exception: log.warn("failed to set hostname\n") @@ -40,7 +40,7 @@ def handle(_name,cfg,cloud,log,_args): # if file doesn't exist, or no contents, return default def read_hostname(filename, default=None): try: - fp = open(filename,"r") + fp = open(filename, "r") lines = fp.readlines() fp.close() for line in lines: @@ -51,7 +51,8 @@ def read_hostname(filename, default=None): if line: return line except IOError as e: - if e.errno != errno.ENOENT: raise + if e.errno != errno.ENOENT: + raise return default def update_hostname(hostname, prev_file, log): @@ -80,14 +81,14 @@ def update_hostname(hostname, prev_file, log): try: for fname in update_files: - util.write_file(fname,"%s\n" % hostname, 0644) - log.debug("wrote %s to %s" % (hostname,fname)) + util.write_file(fname, "%s\n" % hostname, 0644) + log.debug("wrote %s to %s" % (hostname, fname)) except: log.warn("failed to write hostname to %s" % fname) if hostname_in_etc and hostname_prev and hostname_in_etc != hostname_prev: log.debug("%s differs from %s. assuming user maintained" % - (prev_file,etc_file)) + (prev_file, etc_file)) if etc_file in update_files: log.debug("setting hostname to %s" % hostname) diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py index ac79f757..7e539b67 100644 --- a/cloudinit/DataSource.py +++ b/cloudinit/DataSource.py @@ -35,7 +35,7 @@ class DataSource: # datasource config, the cloud-config['datasource']['__name__'] ds_cfg = { } # datasource config - def __init__(self,sys_cfg=None): + def __init__(self, sys_cfg=None): if not self.cfgname: name = str(self.__class__).split(".")[-1] if name.startswith("DataSource"): @@ -45,7 +45,7 @@ class DataSource: self.sys_cfg = sys_cfg self.ds_cfg = util.get_cfg_by_path(self.sys_cfg, - ("datasource",self.cfgname),self.ds_cfg) + ("datasource", self.cfgname), self.ds_cfg) def get_userdata(self): if self.userdata == None: @@ -64,7 +64,8 @@ class DataSource: def get_public_ssh_keys(self): keys = [] - if not self.metadata.has_key('public-keys'): return([]) + if not self.metadata.has_key('public-keys'): + return([]) if isinstance(self.metadata['public-keys'], str): return([self.metadata['public-keys'],]) @@ -73,7 +74,7 @@ class DataSource: # lp:506332 uec metadata service responds with # data that makes boto populate a string for 'klist' rather # than a list. - if isinstance(klist,str): + if isinstance(klist, str): klist = [ klist ] for pkey in klist: # there is an empty string at the end of the keylist, trim it @@ -131,7 +132,7 @@ class DataSource: # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx lhost = self.metadata['local-hostname'] if is_ipv4(lhost): - toks = "ip-%s" % lhost.replace(".","-") + toks = "ip-%s" % lhost.replace(".", "-") else: toks = lhost.split(".") @@ -142,7 +143,7 @@ class DataSource: hostname = toks[0] if fqdn: - return "%s.%s" % (hostname,domain) + return "%s.%s" % (hostname, domain) else: return hostname @@ -163,7 +164,8 @@ def list_sources(cfg_list, depends, pkglist=None): retlist = [] for ds_coll in cfg_list: for pkg in pkglist: - if pkg: pkg="%s." % pkg + if pkg: + pkg = "%s." % pkg try: mod = __import__("%sDataSource%s" % (pkg, ds_coll)) if pkg: diff --git a/cloudinit/DataSourceEc2.py b/cloudinit/DataSourceEc2.py index 9191e647..14484bdd 100644 --- a/cloudinit/DataSourceEc2.py +++ b/cloudinit/DataSourceEc2.py @@ -35,8 +35,8 @@ class DataSourceEc2(DataSource.DataSource): return("DataSourceEc2") def get_data(self): - seedret={ } - if util.read_optional_seed(seedret,base=self.seeddir+ "/"): + seedret = { } + if util.read_optional_seed(seedret, base=self.seeddir+"/"): self.userdata_raw = seedret['user-data'] self.metadata = seedret['meta-data'] log.debug("using seeded ec2 data in %s" % self.seeddir) @@ -46,9 +46,12 @@ class DataSourceEc2(DataSource.DataSource): if not self.wait_for_metadata_service(): return False start = time.time() - self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver, None, self.metadata_address) - self.metadata = boto_utils.get_instance_metadata(self.api_ver, self.metadata_address) - log.debug("crawl of metadata service took %ds" % (time.time()-start)) + self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver, + None, self.metadata_address) + self.metadata = boto_utils.get_instance_metadata(self.api_ver, + self.metadata_address) + log.debug("crawl of metadata service took %ds" % (time.time() - + start)) return True except Exception as e: print e @@ -74,7 +77,7 @@ class DataSourceEc2(DataSource.DataSource): return fallback try: - host="%s.ec2.archive.ubuntu.com" % availability_zone[:-1] + host = "%s.ec2.archive.ubuntu.com" % availability_zone[:-1] socket.getaddrinfo(host, None, 0, socket.SOCK_STREAM) return 'http://%s/ubuntu/' % host except: @@ -88,7 +91,7 @@ class DataSourceEc2(DataSource.DataSource): max_wait = 120 try: - max_wait = int(mcfg.get("max_wait",max_wait)) + max_wait = int(mcfg.get("max_wait", max_wait)) except Exception: util.logexc(log) log.warn("Failed to get max wait. using %s" % max_wait) @@ -98,7 +101,7 @@ class DataSourceEc2(DataSource.DataSource): timeout = 50 try: - timeout = int(mcfg.get("timeout",timeout)) + timeout = int(mcfg.get("timeout", timeout)) except Exception: util.logexc(log) log.warn("Failed to get timeout, using %s" % timeout) @@ -168,17 +171,18 @@ class DataSourceEc2(DataSource.DataSource): short = os.path.basename(found) if not found.startswith("/"): - found="/dev/%s" % found + found = "/dev/%s" % found if os.path.exists(found): return(found) for nfrom, tlist in mappings.items(): - if not short.startswith(nfrom): continue + if not short.startswith(nfrom): + continue for nto in tlist: cand = "/dev/%s%s" % (nto, short[len(nfrom):]) if os.path.exists(cand): - log.debug("remapped device name %s => %s" % (found,cand)) + log.debug("remapped device name %s => %s" % (found, cand)) return(cand) # on t1.micro, ephemeral0 will appear in block-device-mapping from @@ -192,14 +196,16 @@ class DataSourceEc2(DataSource.DataSource): def is_vpc(self): # per comment in LP: #615545 - ph="public-hostname"; p4="public-ipv4" + ph = "public-hostname" + p4 = "public-ipv4" if ((ph not in self.metadata or self.metadata[ph] == "") and (p4 not in self.metadata or self.metadata[p4] == "")): return True return False -def wait_for_metadata_service(urls, max_wait=None, timeout=None, status_cb=None): +def wait_for_metadata_service(urls, max_wait=None, timeout=None, + status_cb=None): """ urls: a list of urls to try max_wait: roughly the maximum time to wait before giving up @@ -236,7 +242,7 @@ def wait_for_metadata_service(urls, max_wait=None, timeout=None, status_cb=None) loop_n = 0 while True: - sleeptime=int(loop_n/5)+1 + sleeptime = int(loop_n/5)+1 for url in urls: now = time.time() if loop_n != 0: diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index 2f4bd604..d63cdc95 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -31,7 +31,7 @@ class DataSourceNoCloud(DataSource.DataSource): seeddir = seeddir + '/nocloud' def __str__(self): - mstr="DataSourceNoCloud" + mstr = "DataSourceNoCloud" mstr = mstr + " [seed=%s]" % self.seed return(mstr) @@ -53,9 +53,9 @@ class DataSourceNoCloud(DataSource.DataSource): return False # check to see if the seeddir has data. - seedret={ } - if util.read_optional_seed(seedret,base=self.seeddir + "/"): - md = util.mergedict(md,seedret['meta-data']) + seedret = { } + if util.read_optional_seed(seedret, base=self.seeddir + "/"): + md = util.mergedict(md, seedret['meta-data']) ud = seedret['user-data'] found.append(self.seeddir) log.debug("using seeded cache data in %s" % self.seeddir) @@ -72,7 +72,7 @@ class DataSourceNoCloud(DataSource.DataSource): seedfound = False for proto in self.supported_seed_starts: if seedfrom.startswith(proto): - seedfound=proto + seedfound = proto break if not seedfound: log.debug("seed from %s not supported by %s" % @@ -81,14 +81,14 @@ class DataSourceNoCloud(DataSource.DataSource): # this could throw errors, but the user told us to do it # so if errors are raised, let them raise - (md_seed,ud) = util.read_seeded(seedfrom, timeout=None) + (md_seed, ud) = util.read_seeded(seedfrom, timeout=None) log.debug("using seeded cache data from %s" % seedfrom) # values in the command line override those from the seed - md = util.mergedict(md,md_seed) + md = util.mergedict(md, md_seed) found.append(seedfrom) - md = util.mergedict(md,defaults) + md = util.mergedict(md, defaults) self.seed = ",".join(found) self.metadata = md self.userdata_raw = ud @@ -98,7 +98,7 @@ class DataSourceNoCloud(DataSource.DataSource): # that this module should be used # example cmdline: # root=LABEL=uec-rootfs ro ds=nocloud -def parse_cmdline_data(ds_id,fill,cmdline=None): +def parse_cmdline_data(ds_id, fill, cmdline=None): if cmdline is None: cmdline = util.get_cmdline() cmdline = " %s " % cmdline @@ -106,30 +106,32 @@ def parse_cmdline_data(ds_id,fill,cmdline=None): if not ( " %s " % ds_id in cmdline or " %s;" % ds_id in cmdline ): return False - argline="" + argline = "" # cmdline can contain: # ds=nocloud[;key=val;key=val] for tok in cmdline.split(): - if tok.startswith(ds_id): argline=tok.split("=",1) + if tok.startswith(ds_id): + argline = tok.split("=", 1) # argline array is now 'nocloud' followed optionally by # a ';' and then key=value pairs also terminated with ';' - tmp=argline[1].split(";") + tmp = argline[1].split(";") if len(tmp) > 1: - kvpairs=tmp[1:] + kvpairs = tmp[1:] else: - kvpairs=() + kvpairs = () # short2long mapping to save cmdline typing s2l = { "h" : "local-hostname", "i" : "instance-id", "s" : "seedfrom" } for item in kvpairs: try: - (k,v) = item.split("=",1) + (k, v) = item.split("=", 1) except: - k=item - v=None - if k in s2l: k=s2l[k] - fill[k]=v + k = item + v = None + if k in s2l: + k = s2l[k] + fill[k] = v return(True) diff --git a/cloudinit/DataSourceOVF.py b/cloudinit/DataSourceOVF.py index 1c510688..4e960ffa 100644 --- a/cloudinit/DataSourceOVF.py +++ b/cloudinit/DataSourceOVF.py @@ -39,7 +39,7 @@ class DataSourceOVF(DataSource.DataSource): supported_seed_starts = ( "/" , "file://" ) def __str__(self): - mstr="DataSourceOVF" + mstr = "DataSourceOVF" mstr = mstr + " [seed=%s]" % self.seed return(mstr) @@ -55,7 +55,7 @@ class DataSourceOVF(DataSource.DataSource): (seedfile, contents) = get_ovf_env(seeddir) if seedfile: # found a seed dir - seed = "%s/%s" % (seeddir,seedfile) + seed = "%s/%s" % (seeddir, seedfile) (md, ud, cfg) = read_ovf_environment(contents) self.environment = contents @@ -65,7 +65,8 @@ class DataSourceOVF(DataSource.DataSource): 'vmware-guestd' : transport_vmware_guestd, } for name, transfunc in np.iteritems(): (contents, _dev, _fname) = transfunc() - if contents: break + if contents: + break if contents: (md, ud, cfg) = read_ovf_environment(contents) @@ -88,14 +89,14 @@ class DataSourceOVF(DataSource.DataSource): (seedfrom, self.__class__)) return False - (md_seed,ud) = util.read_seeded(seedfrom, timeout=None) + (md_seed, ud) = util.read_seeded(seedfrom, timeout=None) log.debug("using seeded cache data from %s" % seedfrom) - md = util.mergedict(md,md_seed) + md = util.mergedict(md, md_seed) found.append(seedfrom) - md = util.mergedict(md,defaults) + md = util.mergedict(md, defaults) self.seed = ",".join(found) self.metadata = md self.userdata_raw = ud @@ -103,7 +104,8 @@ class DataSourceOVF(DataSource.DataSource): return True def get_public_ssh_keys(self): - if not 'public-keys' in self.metadata: return([]) + if not 'public-keys' in self.metadata: + return([]) return([self.metadata['public-keys'],]) # the data sources' config_obj is a cloud-config formated @@ -126,7 +128,8 @@ def read_ovf_environment(contents): cfg_props = [ 'password', ] md_props = [ 'seedfrom', 'local-hostname', 'public-keys', 'instance-id' ] for prop, val in props.iteritems(): - if prop == 'hostname': prop = "local-hostname" + if prop == 'hostname': + prop = "local-hostname" if prop in md_props: md[prop] = val elif prop in cfg_props: @@ -144,12 +147,12 @@ def read_ovf_environment(contents): def get_ovf_env(dirname): env_names = ("ovf-env.xml", "ovf_env.xml", "OVF_ENV.XML", "OVF-ENV.XML" ) for fname in env_names: - if os.path.isfile("%s/%s" % (dirname,fname)): - fp = open("%s/%s" % (dirname,fname)) + if os.path.isfile("%s/%s" % (dirname, fname)): + fp = open("%s/%s" % (dirname, fname)) contents = fp.read() fp.close() - return(fname,contents) - return(None,False) + return(fname, contents) + return(None, False) # transport functions take no input and return # a 3 tuple of content, path, filename @@ -161,7 +164,7 @@ def transport_iso9660(require_iso=False): envname = "CLOUD_INIT_CDROM_DEV_REGEX" default_regex = "^(sr[0-9]+|hd[a-z]|xvd.*)" - devname_regex = os.environ.get(envname,default_regex) + devname_regex = os.environ.get(envname, default_regex) cdmatch = re.compile(devname_regex) # go through mounts to see if it was already mounted @@ -171,17 +174,18 @@ def transport_iso9660(require_iso=False): mounted = { } for mpline in mounts: - (dev,mp,fstype,_opts,_freq,_passno) = mpline.split() - mounted[dev]=(dev,fstype,mp,False) - mp = mp.replace("\\040"," ") - if fstype != "iso9660" and require_iso: continue + (dev, mp, fstype, _opts, _freq, _passno) = mpline.split() + mounted[dev] = (dev, fstype, mp, False) + mp = mp.replace("\\040", " ") + if fstype != "iso9660" and require_iso: + continue if cdmatch.match(dev[5:]) == None: # take off '/dev/' continue - (fname,contents) = get_ovf_env(mp) + (fname, contents) = get_ovf_env(mp) if contents is not False: - return(contents,dev,fname) + return(contents, dev, fname) tmpd = None dvnull = None @@ -201,7 +205,8 @@ def transport_iso9660(require_iso=False): fp.read(512) fp.close() except: - if fp: fp.close() + if fp: + fp.close() continue if tmpd is None: @@ -213,19 +218,20 @@ def transport_iso9660(require_iso=False): pass cmd = [ "mount", "-o", "ro", fullp, tmpd ] - if require_iso: cmd.extend(('-t','iso9660')) + if require_iso: + cmd.extend(('-t', 'iso9660')) rc = subprocess.call(cmd, stderr=dvnull, stdout=dvnull, stdin=dvnull) if rc: continue - (fname,contents) = get_ovf_env(tmpd) + (fname, contents) = get_ovf_env(tmpd) subprocess.call(["umount", tmpd]) if contents is not False: os.rmdir(tmpd) - return(contents,fullp,fname) + return(contents, fullp, fname) if tmpd: os.rmdir(tmpd) @@ -236,23 +242,27 @@ def transport_iso9660(require_iso=False): return(False, None, None) def transport_vmware_guestd(): + # pylint: disable=C0301 # http://blogs.vmware.com/vapp/2009/07/selfconfiguration-and-the-ovf-environment.html # try: # cmd = ['vmware-guestd', '--cmd', 'info-get guestinfo.ovfEnv'] - # (out,err) = subp(cmd) + # (out, err) = subp(cmd) # return(out, 'guestinfo.ovfEnv', 'vmware-guestd') # except: # # would need to error check here and see why this failed # # to know if log/error should be raised # return(False, None, None) + # pylint: enable=C0301 return(False, None, None) -def findChild(node,filter_func): +def findChild(node, filter_func): ret = [] - if not node.hasChildNodes(): return ret + if not node.hasChildNodes(): + return ret for child in node.childNodes: - if filter_func(child): ret.append(child) + if filter_func(child): + ret.append(child) return(ret) def getProperties(environString): @@ -277,8 +287,8 @@ def getProperties(environString): propElems = findChild(propSections[0], lambda n: n.localName == "Property") for elem in propElems: - key = elem.attributes.getNamedItemNS(envNsURI,"key").value - val = elem.attributes.getNamedItemNS(envNsURI,"value").value + key = elem.attributes.getNamedItemNS(envNsURI, "key").value + val = elem.attributes.getNamedItemNS(envNsURI, "value").value props[key] = val return(props) diff --git a/cloudinit/SshUtil.py b/cloudinit/SshUtil.py index 125ca618..7ca60388 100644 --- a/cloudinit/SshUtil.py +++ b/cloudinit/SshUtil.py @@ -18,13 +18,13 @@ class AuthKeyEntry(): line_in = "" def __init__(self, line, def_opt=None): - line=line.rstrip("\n\r") + line = line.rstrip("\n\r") self.line_in = line if line.startswith("#") or line.strip() == "": self.is_comment = True else: ent = line.strip() - toks = ent.split(None,3) + toks = ent.split(None, 3) if len(toks) == 1: self.base64 = toks[0] elif len(toks) == 2: @@ -53,7 +53,7 @@ class AuthKeyEntry(): try: self.options = ent[0:i] (self.keytype, self.base64, self.comment) = \ - ent[i+1:].split(None,3) + ent[i+1:].split(None, 3) except ValueError: # we did not understand this line self.is_comment = True @@ -64,8 +64,9 @@ class AuthKeyEntry(): return def debug(self): - print("line_in=%s\ncomment: %s\noptions=%s\nkeytype=%s\nbase64=%s\ncomment=%s\n" % - (self.line_in, self.is_comment, self.options, self.keytype, self.base64, self.comment)), + print("line_in=%s\ncomment: %s\noptions=%s\nkeytype=%s\nbase64=%s\n" + "comment=%s\n" % (self.line_in, self.is_comment, self.options, + self.keytype, self.base64, self.comment)), def __repr__(self): if self.is_comment: return(self.line_in) @@ -95,7 +96,7 @@ def update_authorized_keys(fname, keys): for key in keys: to_add.append(key) - for i in range(0,len(lines)): + for i in range(0, len(lines)): ent = AuthKeyEntry(lines[i]) for k in keys: if k.base64 == ent.base64 and not k.is_comment: @@ -129,7 +130,7 @@ def setup_user_keys(keys, user, key_prefix, log=None): try: ssh_cfg = parse_ssh_config() - akeys = ssh_cfg.get("AuthorizedKeysFile","%h/.ssh/authorized_keys") + akeys = ssh_cfg.get("AuthorizedKeysFile", "%h/.ssh/authorized_keys") akeys = akeys.replace("%h", pwent.pw_dir) akeys = akeys.replace("%u", user) authorized_keys = akeys @@ -153,6 +154,7 @@ def setup_user_keys(keys, user, key_prefix, log=None): if __name__ == "__main__": import sys + # pylint: disable=C0301 # usage: orig_file, new_keys, [key_prefix] # prints out merged, where 'new_keys' will trump old ## example @@ -168,6 +170,7 @@ if __name__ == "__main__": # # Then run as: # program authorized_keys new_keys 'no-port-forwarding,command=\"echo hi world;\"' + # pylint: enable=C0301 def_prefix = None orig_key_file = sys.argv[1] new_key_file = sys.argv[2] @@ -184,13 +187,13 @@ if __name__ == "__main__": def parse_ssh_config(fname="/etc/ssh/sshd_config"): ret = { } - fp=open(fname) + fp = open(fname) for l in fp.readlines(): l = l.strip() if not l or l.startswith("#"): continue - key,val = l.split(None,1) - ret[key]=val + key, val = l.split(None, 1) + ret[key] = val fp.close() return(ret) diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index 14aea58b..19c331be 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -26,7 +26,7 @@ import cloudinit.util as util import hashlib import urllib -starts_with_mappings={ +starts_with_mappings = { '#include' : 'text/x-include-url', '#include-once' : 'text/x-include-once-url', '#!' : 'text/x-shellscript', @@ -42,7 +42,7 @@ def decomp_str(string): import StringIO import gzip try: - uncomp = gzip.GzipFile(None,"rb",1,StringIO.StringIO(string)).read() + uncomp = gzip.GzipFile(None, "rb", 1, StringIO.StringIO(string)).read() return(uncomp) except: return(string) @@ -53,7 +53,8 @@ def do_include(content, appendmsg): # also support '#include <url here>' includeonce = False for line in content.splitlines(): - if line == "#include": continue + if line == "#include": + continue if line == "#include-once": includeonce = True continue @@ -62,7 +63,8 @@ def do_include(content, appendmsg): includeonce = True elif line.startswith("#include"): line = line[len("#include"):].lstrip() - if line.startswith("#"): continue + if line.startswith("#"): + continue # urls cannot not have leading or trailing white space msum = hashlib.md5() @@ -92,7 +94,7 @@ def explode_cc_archive(archive, appendmsg): # scalar(payload) def_type = "text/cloud-config" - if isinstance(ent,str): + if isinstance(ent, str): ent = { 'content': ent } content = ent.get('content', '') @@ -116,7 +118,7 @@ def explode_cc_archive(archive, appendmsg): continue msg.add_header(header, ent['header']) - _attach_part(appendmsg,msg) + _attach_part(appendmsg, msg) def multi_part_count(outermsg, newcount=None): @@ -193,13 +195,13 @@ def message_from_string(data, headers=None): headers = {} if "mime-version:" in data[0:4096].lower(): msg = email.message_from_string(data) - for (key,val) in headers.items(): + for (key, val) in headers.items(): if key in msg: - msg.replace_header(key,val) + msg.replace_header(key, val) else: msg[key] = val else: - mtype = headers.get("Content-Type","text/plain") + mtype = headers.get("Content-Type", "text/plain") maintype, subtype = mtype.split("/", 1) msg = MIMEBase(maintype, subtype, *headers) msg.set_payload(data) @@ -212,7 +214,8 @@ def preprocess_userdata(data): process_includes(message_from_string(decomp_str(data)), newmsg) return(newmsg.as_string()) -# callback is a function that will be called with (data, content_type, filename, payload) +# callback is a function that will be called with (data, content_type, +# filename, payload) def walk_userdata(istr, callback, data = None): partnum = 0 for part in message_from_string(istr).walk(): diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py index d74a0f1f..3bc6516a 100644 --- a/cloudinit/__init__.py +++ b/cloudinit/__init__.py @@ -46,9 +46,9 @@ pathmap = { None : "", } -per_instance="once-per-instance" -per_always="always" -per_once="once" +per_instance = "once-per-instance" +per_always = "always" +per_once = "once" parsed_cfgs = { } @@ -70,24 +70,25 @@ import glob import traceback class NullHandler(logging.Handler): - def emit(self,record): pass + def emit(self, record): + pass log = logging.getLogger(logger_name) log.addHandler(NullHandler()) def logging_set_from_cfg_file(cfg_file=system_config): - logging_set_from_cfg(util.get_base_cfg(cfg_file,cfg_builtin,parsed_cfgs)) + logging_set_from_cfg(util.get_base_cfg(cfg_file, cfg_builtin, parsed_cfgs)) def logging_set_from_cfg(cfg): log_cfgs = [] - logcfg=util.get_cfg_option_str(cfg, "log_cfg", False) + logcfg = util.get_cfg_option_str(cfg, "log_cfg", False) if logcfg: # if there is a 'logcfg' entry in the config, respect # it, it is the old keyname log_cfgs = [ logcfg ] elif "log_cfgs" in cfg: for cfg in cfg['log_cfgs']: - if isinstance(cfg,list): + if isinstance(cfg, list): log_cfgs.append('\n'.join(cfg)) else: log_cfgs.append() @@ -130,15 +131,15 @@ class CloudInit: if ds_deps != None: self.ds_deps = ds_deps - self.sysconfig=sysconfig - self.cfg=self.read_cfg() + self.sysconfig = sysconfig + self.cfg = self.read_cfg() def read_cfg(self): if self.cfg: return(self.cfg) try: - conf = util.get_base_cfg(self.sysconfig,cfg_builtin, parsed_cfgs) + conf = util.get_base_cfg(self.sysconfig, cfg_builtin, parsed_cfgs) except Exception: conf = get_builtin_cfg() @@ -147,8 +148,9 @@ class CloudInit: try: from configobj import ConfigObj oldcfg = ConfigObj(self.old_conffile) - if oldcfg is None: oldcfg = { } - conf = util.mergedict(conf,oldcfg) + if oldcfg is None: + oldcfg = { } + conf = util.mergedict(conf, oldcfg) except: pass @@ -160,7 +162,7 @@ class CloudInit: # by using the instance link, if purge_cache was called # the file wont exist cache = get_ipath_cur('obj_pkl') - f=open(cache, "rb") + f = open(cache, "rb") data = cPickle.load(f) f.close() self.datasource = data @@ -177,21 +179,22 @@ class CloudInit: return False try: - f=open(cache, "wb") - cPickle.dump(self.datasource,f) + f = open(cache, "wb") + cPickle.dump(self.datasource, f) f.close() - os.chmod(cache,0400) + os.chmod(cache, 0400) except: raise def get_data_source(self): - if self.datasource is not None: return True + if self.datasource is not None: + return True if self.restore_from_cache(): log.debug("restored from cache type %s" % self.datasource) return True - cfglist=self.cfg['datasource_list'] + cfglist = self.cfg['datasource_list'] dslist = list_sources(cfglist, self.ds_deps) dsnames = [f.__name__ for f in dslist] @@ -206,7 +209,7 @@ class CloudInit: log.debug("found data source %s" % ds) return True except Exception as e: - log.warn("get_data of %s raised %s" % (ds,e)) + log.warn("get_data of %s raised %s" % (ds, e)) util.logexc(log) msg = "Did not find data source. searched classes: %s" % dsnames log.debug(msg) @@ -216,7 +219,8 @@ class CloudInit: try: os.unlink(cur_instance_link) except OSError as e: - if e.errno != errno.ENOENT: raise + if e.errno != errno.ENOENT: + raise iid = self.get_instance_id() os.symlink("./instances/%s" % iid, cur_instance_link) @@ -252,21 +256,22 @@ class CloudInit: util.write_file(self.get_ipath('userdata'), self.datasource.get_userdata(), 0600) - def sem_getpath(self,name,freq): + def sem_getpath(self, name, freq): if freq == 'once-per-instance': - return("%s/%s" % (self.get_ipath("sem"),name)) + return("%s/%s" % (self.get_ipath("sem"), name)) return("%s/%s.%s" % (get_cpath("sem"), name, freq)) - def sem_has_run(self,name,freq): - if freq == per_always: return False - semfile = self.sem_getpath(name,freq) + def sem_has_run(self, name, freq): + if freq == per_always: + return False + semfile = self.sem_getpath(name, freq) if os.path.exists(semfile): return True return False - def sem_acquire(self,name,freq): + def sem_acquire(self, name, freq): from time import time - semfile = self.sem_getpath(name,freq) + semfile = self.sem_getpath(name, freq) try: os.makedirs(os.path.dirname(semfile)) @@ -279,15 +284,15 @@ class CloudInit: # race condition try: - f = open(semfile,"w") + f = open(semfile, "w") f.write("%s\n" % str(time())) f.close() except: return(False) return(True) - def sem_clear(self,name,freq): - semfile = self.sem_getpath(name,freq) + def sem_clear(self, name, freq): + semfile = self.sem_getpath(name, freq) try: os.unlink(semfile) except OSError as e: @@ -300,20 +305,20 @@ class CloudInit: # if that does not exist, then call 'func' with given 'args' # if 'clear_on_fail' is True and func throws an exception # then remove the lock (so it would run again) - def sem_and_run(self,semname,freq,func,args=None,clear_on_fail=False): + def sem_and_run(self, semname, freq, func, args=None, clear_on_fail=False): if args is None: args = [] - if self.sem_has_run(semname,freq): + if self.sem_has_run(semname, freq): log.debug("%s already ran %s", semname, freq) return False try: - if not self.sem_acquire(semname,freq): + if not self.sem_acquire(semname, freq): raise Exception("Failed to acquire lock on %s" % semname) func(*args) except: if clear_on_fail: - self.sem_clear(semname,freq) + self.sem_clear(semname, freq) raise return True @@ -322,7 +327,7 @@ class CloudInit: # (/var/lib/cloud/instances/<instance>/name)<name>) def get_ipath(self, name=None): return("%s/instances/%s%s" - % (varlibdir,self.get_instance_id(), pathmap[name])) + % (varlibdir, self.get_instance_id(), pathmap[name])) def consume_userdata(self, frequency=per_instance): self.get_userdata() @@ -333,18 +338,20 @@ class CloudInit: # add the path to the plugins dir to the top of our list for import # instance dir should be read before cloud-dir - sys.path.insert(0,cdir) - sys.path.insert(0,idir) + sys.path.insert(0, cdir) + sys.path.insert(0, idir) part_handlers = { } # add handlers in cdir for fname in glob.glob("%s/*.py" % cdir): - if not os.path.isfile(fname): continue + if not os.path.isfile(fname): + continue modname = os.path.basename(fname)[0:-3] try: mod = __import__(modname) handler_register(mod, part_handlers, data, frequency) - log.debug("added handler for [%s] from %s" % (mod.list_types(), fname)) + log.debug("added handler for [%s] from %s" % (mod.list_types(), + fname)) except: log.warn("failed to initialize handler in %s" % fname) util.logexc(log) @@ -369,32 +376,34 @@ class CloudInit: continue handler_call_end(mod, data, frequency) - def handle_user_script(self,_data,ctype,filename,payload, _frequency): - if ctype == "__end__": return + def handle_user_script(self, _data, ctype, filename, payload, _frequency): + if ctype == "__end__": + return if ctype == "__begin__": # maybe delete existing things here return - filename=filename.replace(os.sep,'_') + filename = filename.replace(os.sep, '_') scriptsdir = get_ipath_cur('scripts') util.write_file("%s/%s" % - (scriptsdir,filename), util.dos2unix(payload), 0700) + (scriptsdir, filename), util.dos2unix(payload), 0700) - def handle_upstart_job(self,_data,ctype,filename,payload, frequency): + def handle_upstart_job(self, _data, ctype, filename, payload, frequency): # upstart jobs are only written on the first boot if frequency != per_instance: return - if ctype == "__end__" or ctype == "__begin__": return + if ctype == "__end__" or ctype == "__begin__": + return if not filename.endswith(".conf"): - filename=filename+".conf" + filename = filename+".conf" - util.write_file("%s/%s" % ("/etc/init",filename), + util.write_file("%s/%s" % ("/etc/init", filename), util.dos2unix(payload), 0644) - def handle_cloud_config(self,_data,ctype,filename,payload, _frequency): + def handle_cloud_config(self, _data, ctype, filename, payload, _frequency): if ctype == "__begin__": - self.cloud_config_str="" + self.cloud_config_str = "" return if ctype == "__end__": cloud_config = self.get_ipath("cloud_config") @@ -410,32 +419,35 @@ class CloudInit: return - self.cloud_config_str+="\n#%s\n%s" % (filename,payload) + self.cloud_config_str += "\n#%s\n%s" % (filename, payload) - def handle_cloud_boothook(self,_data,ctype,filename,payload, _frequency): - if ctype == "__end__": return - if ctype == "__begin__": return + def handle_cloud_boothook(self, _data, ctype, filename, payload, + _frequency): + if ctype == "__end__": + return + if ctype == "__begin__": + return - filename=filename.replace(os.sep,'_') + filename = filename.replace(os.sep, '_') payload = util.dos2unix(payload) - prefix="#cloud-boothook" + prefix = "#cloud-boothook" start = 0 if payload.startswith(prefix): start = len(prefix) + 1 boothooks_dir = self.get_ipath("boothooks") - filepath = "%s/%s" % (boothooks_dir,filename) + filepath = "%s/%s" % (boothooks_dir, filename) util.write_file(filepath, payload[start:], 0700) try: - env=os.environ.copy() - env['INSTANCE_ID']= self.datasource.get_instance_id() + env = os.environ.copy() + env['INSTANCE_ID'] = self.datasource.get_instance_id() subprocess.check_call([filepath], env=env) except subprocess.CalledProcessError as e: log.error("boothooks script %s returned %i" % - (filepath,e.returncode)) + (filepath, e.returncode)) except Exception as e: log.error("boothooks unknown exception %s when running %s" % - (e,filepath)) + (e, filepath)) def get_public_ssh_keys(self): return(self.datasource.get_public_ssh_keys()) @@ -449,14 +461,14 @@ class CloudInit: def get_hostname(self, fqdn=False): return(self.datasource.get_hostname(fqdn=fqdn)) - def device_name_to_device(self,name): + def device_name_to_device(self, name): return(self.datasource.device_name_to_device(name)) # I really don't know if this should be here or not, but # I needed it in cc_update_hostname, where that code had a valid 'cloud' # reference, but did not have a cloudinit handle # (ie, no cloudinit.get_cpath()) - def get_cpath(self,name=None): + def get_cpath(self, name=None): return(get_cpath(name)) @@ -468,26 +480,30 @@ def initfs(): dlist.append("%s/%s" % (varlibdir, subd)) util.ensure_dirs(dlist) - cfg = util.get_base_cfg(system_config,cfg_builtin,parsed_cfgs) + cfg = util.get_base_cfg(system_config, cfg_builtin, parsed_cfgs) log_file = util.get_cfg_option_str(cfg, 'def_log_file', None) perms = util.get_cfg_option_str(cfg, 'syslog_fix_perms', None) if log_file: - fp = open(log_file,"ab") + fp = open(log_file, "ab") fp.close() if log_file and perms: - (u,g) = perms.split(':',1) - if u == "-1" or u == "None": u = None - if g == "-1" or g == "None": g = None + (u, g) = perms.split(':', 1) + if u == "-1" or u == "None": + u = None + if g == "-1" or g == "None": + g = None util.chownbyname(log_file, u, g) def purge_cache(rmcur=True): rmlist = [ boot_finished ] - if rmcur: rmlist.append(cur_instance_link) + if rmcur: + rmlist.append(cur_instance_link) for f in rmlist: try: os.unlink(f) except OSError as e: - if e.errno == errno.ENOENT: continue + if e.errno == errno.ENOENT: + continue return(False) except: return(False) @@ -503,8 +519,9 @@ def get_cpath(name=None): return("%s%s" % (varlibdir, pathmap[name])) def get_base_cfg(cfg_path=None): - if cfg_path is None: cfg_path = system_config - return(util.get_base_cfg(cfg_path,cfg_builtin,parsed_cfgs)) + if cfg_path is None: + cfg_path = system_config + return(util.get_base_cfg(cfg_path, cfg_builtin, parsed_cfgs)) def get_builtin_cfg(): return(yaml.load(cfg_builtin)) @@ -513,7 +530,7 @@ class DataSourceNotFoundException(Exception): pass def list_sources(cfg_list, depends): - return(DataSource.list_sources(cfg_list,depends, ["cloudinit", "" ])) + return(DataSource.list_sources(cfg_list, depends, ["cloudinit", "" ])) def handler_register(mod, part_handlers, data, frequency=per_instance): if not hasattr(mod, "handler_version"): diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py index 9ca72b77..9cfd5ba3 100644 --- a/cloudinit/netinfo.py +++ b/cloudinit/netinfo.py @@ -28,7 +28,7 @@ def netdev_info(): pass for field in ("addr", "bcast", "mask"): target = "%s%s" % (field, fieldpost) - if devs[curdev].get(target,""): + if devs[curdev].get(target, ""): continue if toks[i] == "%s:" % field: try: @@ -54,7 +54,7 @@ def route_info(): def getgateway(): for r in route_info(): if r[3].find("G") >= 0: - return("%s[%s]" % (r[1],r[7])) + return("%s[%s]" % (r[1], r[7])) return(None) def debug_info(pre="ci-info: "): @@ -66,7 +66,7 @@ def debug_info(pre="ci-info: "): netdev = {} for (dev, d) in netdev.iteritems(): lines.append("%s%-6s: %i %-15s %-15s %s" % - (pre, dev, d["up"], d["addr"],d["mask"], d["hwaddr"])) + (pre, dev, d["up"], d["addr"], d["mask"], d["hwaddr"])) try: routes = route_info() except Exception: diff --git a/cloudinit/util.py b/cloudinit/util.py index 5bf8e8b2..7df773ce 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -39,7 +39,7 @@ except ImportError: def read_conf(fname): try: - stream = open(fname,"r") + stream = open(fname, "r") conf = yaml.load(stream) stream.close() return conf @@ -48,7 +48,7 @@ def read_conf(fname): return { } raise -def get_base_cfg(cfgfile,cfg_builtin="", parsed_cfgs=None): +def get_base_cfg(cfgfile, cfg_builtin="", parsed_cfgs=None): kerncfg = { } syscfg = { } if parsed_cfgs and cfgfile in parsed_cfgs: @@ -65,7 +65,7 @@ def get_base_cfg(cfgfile,cfg_builtin="", parsed_cfgs=None): if cfg_builtin: builtin = yaml.load(cfg_builtin) - fin = mergedict(combined,builtin) + fin = mergedict(combined, builtin) else: fin = combined @@ -74,53 +74,60 @@ def get_base_cfg(cfgfile,cfg_builtin="", parsed_cfgs=None): return(fin) def get_cfg_option_bool(yobj, key, default=False): - if not yobj.has_key(key): return default + if not yobj.has_key(key): + return default val = yobj[key] - if val is True: return True + if val is True: + return True if str(val).lower() in [ 'true', '1', 'on', 'yes']: return True return False def get_cfg_option_str(yobj, key, default=None): - if not yobj.has_key(key): return default + if not yobj.has_key(key): + return default return yobj[key] def get_cfg_option_list_or_str(yobj, key, default=None): - if not yobj.has_key(key): return default - if yobj[key] is None: return [] - if isinstance(yobj[key],list): return yobj[key] + if not yobj.has_key(key): + return default + if yobj[key] is None: + return [] + if isinstance(yobj[key], list): + return yobj[key] return([yobj[key]]) # get a cfg entry by its path array # for f['a']['b']: get_cfg_by_path(mycfg,('a','b')) -def get_cfg_by_path(yobj,keyp,default=None): +def get_cfg_by_path(yobj, keyp, default=None): cur = yobj for tok in keyp: - if tok not in cur: return(default) + if tok not in cur: + return(default) cur = cur[tok] return(cur) # merge values from cand into source # if src has a key, cand will not override -def mergedict(src,cand): - if isinstance(src,dict) and isinstance(cand,dict): - for k,v in cand.iteritems(): +def mergedict(src, cand): + if isinstance(src, dict) and isinstance(cand, dict): + for k, v in cand.iteritems(): if k not in src: src[k] = v else: - src[k] = mergedict(src[k],v) + src[k] = mergedict(src[k], v) return src -def write_file(filename,content,mode=0644,omode="wb"): +def write_file(filename, content, mode=0644, omode="wb"): try: os.makedirs(os.path.dirname(filename)) except OSError as e: if e.errno != errno.EEXIST: raise e - f=open(filename,omode) + f = open(filename, omode) if mode != None: - os.chmod(filename,mode) + os.chmod(filename, mode) f.write(content) f.close() restorecon_if_possible(filename) @@ -130,8 +137,8 @@ def restorecon_if_possible(path, recursive=False): selinux.restorecon(path, recursive=recursive) # get keyid from keyserver -def getkeybyid(keyid,keyserver): - shcmd=""" +def getkeybyid(keyid, keyserver): + shcmd = """ k=${1} ks=${2}; exec 2>/dev/null [ -n "$k" ] || exit 1; @@ -143,32 +150,35 @@ def getkeybyid(keyid,keyserver): fi [ -n "${armour}" ] && echo "${armour}" """ - args=['sh', '-c', shcmd, "export-gpg-keyid", keyid, keyserver] + args = ['sh', '-c', shcmd, "export-gpg-keyid", keyid, keyserver] return(subp(args)[0]) def runparts(dirp, skip_no_exist=True): - if skip_no_exist and not os.path.isdir(dirp): return + if skip_no_exist and not os.path.isdir(dirp): + return # per bug 857926, Fedora's run-parts will exit failure on empty dir - if os.path.isdir(dirp) and os.listdir(dirp) == []: return + if os.path.isdir(dirp) and os.listdir(dirp) == []: + return cmd = [ 'run-parts', '--regex', '.*', dirp ] sp = subprocess.Popen(cmd) sp.communicate() if sp.returncode is not 0: - raise subprocess.CalledProcessError(sp.returncode,cmd) + raise subprocess.CalledProcessError(sp.returncode, cmd) return def subp(args, input_=None): sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) - out,err = sp.communicate(input_) + out, err = sp.communicate(input_) if sp.returncode is not 0: - raise subprocess.CalledProcessError(sp.returncode,args, (out,err)) - return(out,err) + raise subprocess.CalledProcessError(sp.returncode, args, (out, err)) + return(out, err) def render_to_file(template, outfile, searchList): - t = Template(file='/etc/cloud/templates/%s.tmpl' % template, searchList=[searchList]) + t = Template(file='/etc/cloud/templates/%s.tmpl' % template, + searchList=[searchList]) f = open(outfile, 'w') f.write(t.respond()) f.close() @@ -181,11 +191,11 @@ def render_string(template, searchList): # returns boolean indicating success or failure (presense of files) # if files are present, populates 'fill' dictionary with 'user-data' and # 'meta-data' entries -def read_optional_seed(fill,base="",ext="", timeout=5): +def read_optional_seed(fill, base="", ext="", timeout=5): try: - (md,ud) = read_seeded(base,ext,timeout) - fill['user-data']= ud - fill['meta-data']= md + (md, ud) = read_seeded(base, ext, timeout) + fill['user-data'] = ud + fill['meta-data'] = md return True except OSError, e: if e.errno == errno.ENOENT: @@ -196,7 +206,7 @@ def read_optional_seed(fill,base="",ext="", timeout=5): # raise OSError with enoent if not found def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): if base.startswith("/"): - base="file://%s" % base + base = "file://%s" % base # default retries for file is 0. for network is 10 if base.startswith("file://"): @@ -210,18 +220,18 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): md_url = "%s%s%s" % (base, "meta-data", ext) raise_err = None - for attempt in range(0,retries+1): + for attempt in range(0, retries+1): try: md_str = readurl(md_url, timeout=timeout) ud = readurl(ud_url, timeout=timeout) md = yaml.load(md_str) - return(md,ud) + return(md, ud) except urllib2.HTTPError as e: raise_err = e except urllib2.URLError as e: raise_err = e - if isinstance(e.reason,OSError) and e.reason.errno == errno.ENOENT: + if isinstance(e.reason, OSError) and e.reason.errno == errno.ENOENT: raise_err = e.reason if attempt == retries: @@ -232,8 +242,8 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): raise(raise_err) -def logexc(log,lvl=logging.DEBUG): - log.log(lvl,traceback.format_exc()) +def logexc(log, lvl=logging.DEBUG): + log.log(lvl, traceback.format_exc()) class RecursiveInclude(Exception): pass @@ -253,7 +263,7 @@ def read_file_with_includes(fname, rel = ".", stack=None, patt = None): (fname, len(stack)))) if patt == None: - patt = re.compile("^#(opt_include|include)[ \t].*$",re.MULTILINE) + patt = re.compile("^#(opt_include|include)[ \t].*$", re.MULTILINE) try: fp = open(fname) @@ -268,11 +278,12 @@ def read_file_with_includes(fname, rel = ".", stack=None, patt = None): cur = 0 while True: match = patt.search(contents[cur:]) - if not match: break + if not match: + break loc = match.start() + cur endl = match.end() + cur - (key, cur_fname) = contents[loc:endl].split(None,2) + (key, cur_fname) = contents[loc:endl].split(None, 2) cur_fname = cur_fname.strip() try: @@ -289,17 +300,17 @@ def read_file_with_includes(fname, rel = ".", stack=None, patt = None): def read_conf_d(confd): # get reverse sorted list (later trumps newer) - confs = sorted(os.listdir(confd),reverse=True) + confs = sorted(os.listdir(confd), reverse=True) # remove anything not ending in '.cfg' confs = [f for f in confs if f.endswith(".cfg")] # remove anything not a file - confs = [f for f in confs if os.path.isfile("%s/%s" % (confd,f))] + confs = [f for f in confs if os.path.isfile("%s/%s" % (confd, f))] cfg = { } for conf in confs: - cfg = mergedict(cfg,read_conf("%s/%s" % (confd,conf))) + cfg = mergedict(cfg, read_conf("%s/%s" % (confd, conf))) return(cfg) @@ -309,16 +320,18 @@ def read_conf_with_confd(cfgfile): if "conf_d" in cfg: if cfg['conf_d'] is not None: confd = cfg['conf_d'] - if not isinstance(confd,str): - raise Exception("cfgfile %s contains 'conf_d' with non-string" % cfgfile) + if not isinstance(confd, str): + raise Exception("cfgfile %s contains 'conf_d' " + "with non-string" % cfgfile) elif os.path.isdir("%s.d" % cfgfile): confd = "%s.d" % cfgfile - if not confd: return(cfg) + if not confd: + return(cfg) confd_cfg = read_conf_d(confd) - return(mergedict(confd_cfg,cfg)) + return(mergedict(confd_cfg, cfg)) def get_cmdline(): @@ -345,8 +358,8 @@ def read_cc_from_cmdline(cmdline=None): if cmdline is None: cmdline = get_cmdline() - tag_begin="cc:" - tag_end="end_cc" + tag_begin = "cc:" + tag_end = "end_cc" begin_l = len(tag_begin) end_l = len(tag_end) clen = len(cmdline) @@ -356,7 +369,7 @@ def read_cc_from_cmdline(cmdline=None): end = cmdline.find(tag_end, begin + begin_l) if end < 0: end = clen - tokens.append(cmdline[begin+begin_l:end].lstrip().replace("\\n","\n")) + tokens.append(cmdline[begin+begin_l:end].lstrip().replace("\\n", "\n")) begin = cmdline.find(tag_begin, end + end_l) @@ -371,16 +384,19 @@ def ensure_dirs(dirlist, mode=0755): else: os.makedirs(d, mode) except OSError as e: - if e.errno != errno.EEXIST: raise - if mode != None: fixmodes.append(d) + if e.errno != errno.EEXIST: + raise + if mode != None: + fixmodes.append(d) for d in fixmodes: os.chmod(d, mode) -def chownbyname(fname,user=None,group=None): +def chownbyname(fname, user=None, group=None): uid = -1 gid = -1 - if user == None and group == None: return + if user == None and group == None: + return if user: import pwd uid = pwd.getpwnam(user).pw_uid @@ -388,7 +404,7 @@ def chownbyname(fname,user=None,group=None): import grp gid = grp.getgrnam(group).gr_gid - os.chown(fname,uid,gid) + os.chown(fname, uid, gid) def readurl(url, data=None, timeout=None): openargs = { } @@ -409,25 +425,26 @@ def readurl(url, data=None, timeout=None): # if it is an array, shell protect it (with single ticks) # if it is a string, do nothing def shellify(cmdlist): - content="#!/bin/sh\n" - escaped="%s%s%s%s" % ( "'", '\\', "'", "'" ) + content = "#!/bin/sh\n" + escaped = "%s%s%s%s" % ( "'", '\\', "'", "'" ) for args in cmdlist: # if the item is a list, wrap all items in single tick # if its not, then just write it directly - if isinstance(args,list): + if isinstance(args, list): fixed = [ ] for f in args: - fixed.append("'%s'" % str(f).replace("'",escaped)) - content="%s%s\n" % ( content, ' '.join(fixed) ) + fixed.append("'%s'" % str(f).replace("'", escaped)) + content = "%s%s\n" % ( content, ' '.join(fixed) ) else: - content="%s%s\n" % ( content, str(args) ) + content = "%s%s\n" % ( content, str(args) ) return content def dos2unix(string): # find first end of line pos = string.find('\n') - if pos <= 0 or string[pos-1] != '\r': return(string) - return(string.replace('\r\n','\n')) + if pos <= 0 or string[pos-1] != '\r': + return(string) + return(string.replace('\r\n', '\n')) def islxc(): # is this host running lxc? @@ -440,8 +457,8 @@ def islxc(): raise try: - # try to run a program named 'lxc-is-container'. if it returns true, then - # we're inside a container. otherwise, no + # try to run a program named 'lxc-is-container'. if it returns true, + # then we're inside a container. otherwise, no sp = subprocess.Popen(['lxc-is-container'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) sp.communicate(None) @@ -458,7 +475,7 @@ def get_hostname_fqdn(cfg, cloud): if "fqdn" in cfg: # user specified a fqdn. Default hostname then is based off that fqdn = cfg['fqdn'] - hostname = get_cfg_option_str(cfg,"hostname",fqdn.split('.')[0]) + hostname = get_cfg_option_str(cfg, "hostname", fqdn.split('.')[0]) else: if "hostname" in cfg and cfg['hostname'].find('.') > 0: # user specified hostname, and it had '.' in it |