diff options
43 files changed, 278 insertions, 278 deletions
diff --git a/cloud-init-cfg.py b/cloud-init-cfg.py index 0717ca95..37fe6398 100755 --- a/cloud-init-cfg.py +++ b/cloud-init-cfg.py @@ -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 f6313ba2..9806c22c 100755 --- a/cloud-init.py +++ b/cloud-init.py @@ -54,7 +54,7 @@ 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] @@ -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) @@ -147,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: @@ -161,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) @@ -174,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 fb390090..caf7e333 100644 --- a/cloudinit/CloudConfig/__init__.py +++ b/cloudinit/CloudConfig/__init__.py @@ -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,7 +41,7 @@ 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: @@ -58,12 +58,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: @@ -76,7 +76,7 @@ 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): +def read_cc_modules(cfg, name): if name not in cfg: return([]) module_list = [] @@ -85,15 +85,15 @@ def read_cc_modules(cfg,name): # 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] @@ -111,7 +111,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) @@ -142,11 +142,11 @@ def get_output_cfg(cfg, mode="init"): 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 isinstance(modecfg, list): if len(modecfg) > 0: ret[0] = modecfg[0] if len(modecfg) > 1: @@ -173,7 +173,7 @@ def get_output_cfg(cfg, mode="init"): 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: @@ -193,9 +193,9 @@ 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 == ">": @@ -214,7 +214,7 @@ 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 == ">": @@ -231,11 +231,11 @@ 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) + 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: @@ -244,7 +244,7 @@ def run_per_instance(name, func, args, clear_on_fail=False): 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() @@ -252,11 +252,11 @@ def apt_get(tlc,args=None): 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 e4c08708..1da4e613 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: @@ -109,18 +109,18 @@ def mirror2lists_fileprefix(mirror): pos = string.find("://") if pos >= 0: string = string[pos+3:] - string = string.replace("/","_") + 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)) + 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() @@ -146,7 +146,7 @@ def add_sources(srclist, searchList=None): source = ent['source'] if source.startswith("ppa:"): try: - util.subp(["add-apt-repository",source]) + util.subp(["add-apt-repository", source]) except: elst.append([source, "add-apt-repository failed"]) continue @@ -167,7 +167,7 @@ def add_sources(srclist, searchList=None): 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'): @@ -195,7 +195,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 47778897..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 diff --git a/cloudinit/CloudConfig/cc_byobu.py b/cloudinit/CloudConfig/cc_byobu.py index 76c6f286..04825521 100644 --- a/cloudinit/CloudConfig/cc_byobu.py +++ b/cloudinit/CloudConfig/cc_byobu.py @@ -19,11 +19,11 @@ 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 @@ -51,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 c40f1293..55ecdab4 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -23,7 +23,7 @@ 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 @@ -92,13 +92,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 ff95f4bf..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" subprocess.call(fwall.split(' ')) diff --git a/cloudinit/CloudConfig/cc_final_message.py b/cloudinit/CloudConfig/cc_final_message.py index ef0d9ad0..7930bab5 100644 --- a/cloudinit/CloudConfig/cc_final_message.py +++ b/cloudinit/CloudConfig/cc_final_message.py @@ -24,11 +24,11 @@ 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") @@ -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 4e1ba4d4..1437d481 100644 --- a/cloudinit/CloudConfig/cc_grub_dpkg.py +++ b/cloudinit/CloudConfig/cc_grub_dpkg.py @@ -20,16 +20,16 @@ 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 if "grub-dpkg" in cfg: idevs = util.get_cfg_option_str(cfg["grub-dpkg"], - "grub-pc/install_devices",None) + "grub-pc/install_devices", None) idevs_empty = util.get_cfg_option_str(cfg["grub-dpkg"], - "grub-pc/install_devices_empty",None) + "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") )): @@ -53,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..bbc45c4a 100644 --- a/cloudinit/CloudConfig/cc_keys_to_console.py +++ b/cloudinit/CloudConfig/cc_keys_to_console.py @@ -21,12 +21,12 @@ 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"]) 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 31e3f56e..991f5861 100644 --- a/cloudinit/CloudConfig/cc_locale.py +++ b/cloudinit/CloudConfig/cc_locale.py @@ -28,11 +28,11 @@ 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") diff --git a/cloudinit/CloudConfig/cc_mcollective.py b/cloudinit/CloudConfig/cc_mcollective.py index 0d1f01b3..ec5da4f8 100644 --- a/cloudinit/CloudConfig/cc_mcollective.py +++ b/cloudinit/CloudConfig/cc_mcollective.py @@ -41,7 +41,7 @@ class FakeSecHead(object): 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 @@ -60,20 +60,20 @@ def handle(_name,cfg,_cloud,_log,_args): 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 @@ -83,7 +83,7 @@ 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 248abb24..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,7 @@ 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): + if not isinstance(cfgmnt[i], list): continue # workaround, allow user to specify 'ephemeral' @@ -138,7 +138,7 @@ def handle(_name,cfg,cloud,log,_args): 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: @@ -168,9 +168,9 @@ def handle(_name,cfg,cloud,log,_args): try: os.makedirs(d) except: - log.warn("Failed to make '%s' config-mount\n",d) + log.warn("Failed to make '%s' config-mount\n", d) try: - util.subp(("mount","-a")) + 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 008139b7..61b769a7 100644 --- a/cloudinit/CloudConfig/cc_phone_home.py +++ b/cloudinit/CloudConfig/cc_phone_home.py @@ -31,7 +31,7 @@ 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: @@ -45,7 +45,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: @@ -84,7 +84,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 1a8c7f3a..2686c849 100644 --- a/cloudinit/CloudConfig/cc_puppet.py +++ b/cloudinit/CloudConfig/cc_puppet.py @@ -25,7 +25,7 @@ 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 @@ -78,11 +78,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 1fdf8647..1fd9f094 100644 --- a/cloudinit/CloudConfig/cc_resizefs.py +++ b/cloudinit/CloudConfig/cc_resizefs.py @@ -25,13 +25,13 @@ 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 @@ -43,7 +43,7 @@ def handle(_name,cfg,_cloud,log,args): try: st_dev = os.stat("/").st_dev - dev = os.makedev(os.major(st_dev),os.minor(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(): @@ -54,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)) diff --git a/cloudinit/CloudConfig/cc_rightscale_userdata.py b/cloudinit/CloudConfig/cc_rightscale_userdata.py index fdbee954..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: @@ -61,7 +61,7 @@ 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) diff --git a/cloudinit/CloudConfig/cc_rsyslog.py b/cloudinit/CloudConfig/cc_rsyslog.py index 90d09545..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" @@ -42,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 @@ -53,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 0adf31ad..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') 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 4059e9c1..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) @@ -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 6031a436..9751bba5 100644 --- a/cloudinit/CloudConfig/cc_ssh.py +++ b/cloudinit/CloudConfig/cc_ssh.py @@ -26,7 +26,7 @@ DISABLE_ROOT_OPTS = "no-port-forwarding,no-agent-forwarding,no-X11-forwarding,co global_log = None -def handle(_name,cfg,cloud,log,_args): +def handle(_name, cfg, cloud, log, _args): global global_log global_log = log @@ -49,15 +49,15 @@ 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(): + 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]) @@ -75,7 +75,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) @@ -85,7 +85,7 @@ 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") diff --git a/cloudinit/CloudConfig/cc_ssh_import_id.py b/cloudinit/CloudConfig/cc_ssh_import_id.py index e4588eaf..efcd4296 100644 --- a/cloudinit/CloudConfig/cc_ssh_import_id.py +++ b/cloudinit/CloudConfig/cc_ssh_import_id.py @@ -19,15 +19,15 @@ 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 diff --git a/cloudinit/CloudConfig/cc_timezone.py b/cloudinit/CloudConfig/cc_timezone.py index e7c855d6..87855503 100644 --- a/cloudinit/CloudConfig/cc_timezone.py +++ b/cloudinit/CloudConfig/cc_timezone.py @@ -24,11 +24,11 @@ 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 @@ -40,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 9d09bbb8..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: @@ -81,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 bb4b6c19..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: @@ -74,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 @@ -132,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(".") @@ -143,7 +143,7 @@ class DataSource: hostname = toks[0] if fqdn: - return "%s.%s" % (hostname,domain) + return "%s.%s" % (hostname, domain) else: return hostname diff --git a/cloudinit/DataSourceEc2.py b/cloudinit/DataSourceEc2.py index f4de0288..35aa75d5 100644 --- a/cloudinit/DataSourceEc2.py +++ b/cloudinit/DataSourceEc2.py @@ -36,7 +36,7 @@ class DataSourceEc2(DataSource.DataSource): def get_data(self): seedret = { } - if util.read_optional_seed(seedret,base=self.seeddir+ "/"): + 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) @@ -88,7 +88,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 +98,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) @@ -179,7 +179,7 @@ class DataSourceEc2(DataSource.DataSource): 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 diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index d2e9b5a1..d63cdc95 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -54,8 +54,8 @@ class DataSourceNoCloud(DataSource.DataSource): # 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']) + 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) @@ -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 @@ -111,7 +111,7 @@ def parse_cmdline_data(ds_id,fill,cmdline=None): # ds=nocloud[;key=val;key=val] for tok in cmdline.split(): if tok.startswith(ds_id): - argline = tok.split("=",1) + argline = tok.split("=", 1) # argline array is now 'nocloud' followed optionally by # a ';' and then key=value pairs also terminated with ';' @@ -125,7 +125,7 @@ def parse_cmdline_data(ds_id,fill,cmdline=None): 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 diff --git a/cloudinit/DataSourceOVF.py b/cloudinit/DataSourceOVF.py index 4da21df1..995dbe67 100644 --- a/cloudinit/DataSourceOVF.py +++ b/cloudinit/DataSourceOVF.py @@ -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 @@ -89,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 @@ -147,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 @@ -164,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 @@ -174,18 +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"," ") + (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 @@ -219,19 +219,19 @@ def transport_iso9660(require_iso=False): cmd = [ "mount", "-o", "ro", fullp, tmpd ] if require_iso: - cmd.extend(('-t','iso9660')) + 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) @@ -245,7 +245,7 @@ def transport_vmware_guestd(): # 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 @@ -254,7 +254,7 @@ def transport_vmware_guestd(): return(False, None, None) -def findChild(node,filter_func): +def findChild(node, filter_func): ret = [] if not node.hasChildNodes(): return ret @@ -285,8 +285,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 198f7206..32370ff1 100644 --- a/cloudinit/SshUtil.py +++ b/cloudinit/SshUtil.py @@ -24,7 +24,7 @@ class AuthKeyEntry(): 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 @@ -95,7 +95,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 +129,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 @@ -189,7 +189,7 @@ def parse_ssh_config(fname="/etc/ssh/sshd_config"): l = l.strip() if not l or l.startswith("#"): continue - key,val = l.split(None,1) + key, val = l.split(None, 1) ret[key] = val fp.close() return(ret) diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index b21eacbe..6372dc09 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -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) @@ -94,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', '') @@ -118,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): @@ -195,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) diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py index 3a3c9c85..449e4648 100644 --- a/cloudinit/__init__.py +++ b/cloudinit/__init__.py @@ -70,14 +70,14 @@ import glob import traceback class NullHandler(logging.Handler): - def emit(self,record): + 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 = [] @@ -88,7 +88,7 @@ def logging_set_from_cfg(cfg): 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() @@ -139,7 +139,7 @@ class CloudInit: 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() @@ -150,7 +150,7 @@ class CloudInit: oldcfg = ConfigObj(self.old_conffile) if oldcfg is None: oldcfg = { } - conf = util.mergedict(conf,oldcfg) + conf = util.mergedict(conf, oldcfg) except: pass @@ -180,9 +180,9 @@ class CloudInit: try: f = open(cache, "wb") - cPickle.dump(self.datasource,f) + cPickle.dump(self.datasource, f) f.close() - os.chmod(cache,0400) + os.chmod(cache, 0400) except: raise @@ -209,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) @@ -256,22 +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): + def sem_has_run(self, name, freq): if freq == per_always: return False - semfile = self.sem_getpath(name,freq) + 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)) @@ -284,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: @@ -305,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 @@ -327,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() @@ -338,8 +338,8 @@ 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 @@ -375,19 +375,19 @@ class CloudInit: continue handler_call_end(mod, data, frequency) - def handle_user_script(self,_data,ctype,filename,payload, _frequency): + 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 @@ -397,10 +397,10 @@ class CloudInit: if not filename.endswith(".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 = "" return @@ -418,15 +418,15 @@ 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): + 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" start = 0 @@ -434,7 +434,7 @@ class CloudInit: 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() @@ -442,10 +442,10 @@ class CloudInit: 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()) @@ -459,14 +459,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)) @@ -478,14 +478,14 @@ 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) + (u, g) = perms.split(':', 1) if u == "-1" or u == "None": u = None if g == "-1" or g == "None": @@ -519,7 +519,7 @@ def get_cpath(name=None): 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)) + return(util.get_base_cfg(cfg_path, cfg_builtin, parsed_cfgs)) def get_builtin_cfg(): return(yaml.load(cfg_builtin)) @@ -528,7 +528,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 f57392fd..e59ad3a0 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 @@ -93,13 +93,13 @@ def get_cfg_option_list_or_str(yobj, key, default=None): return default if yobj[key] is None: return [] - if isinstance(yobj[key],list): + 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: @@ -109,25 +109,25 @@ def get_cfg_by_path(yobj,keyp,default=None): # 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) @@ -137,7 +137,7 @@ def restorecon_if_possible(path, recursive=False): selinux.restorecon(path, recursive=recursive) # get keyid from keyserver -def getkeybyid(keyid,keyserver): +def getkeybyid(keyid, keyserver): shcmd = """ k=${1} ks=${2}; exec 2>/dev/null @@ -165,16 +165,16 @@ def runparts(dirp, skip_no_exist=True): 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]) @@ -190,9 +190,9 @@ 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) + (md, ud) = read_seeded(base, ext, timeout) fill['user-data'] = ud fill['meta-data'] = md return True @@ -219,18 +219,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: @@ -241,8 +241,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 @@ -262,7 +262,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) @@ -282,7 +282,7 @@ def read_file_with_includes(fname, rel = ".", stack=None, patt = None): 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: @@ -299,17 +299,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) @@ -319,7 +319,7 @@ 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): + 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 @@ -329,7 +329,7 @@ def read_conf_with_confd(cfgfile): confd_cfg = read_conf_d(confd) - return(mergedict(confd_cfg,cfg)) + return(mergedict(confd_cfg, cfg)) def get_cmdline(): @@ -367,7 +367,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) @@ -390,7 +390,7 @@ def ensure_dirs(dirlist, mode=0755): 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: @@ -402,7 +402,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 = { } @@ -428,10 +428,10 @@ def shellify(cmdlist): 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)) + fixed.append("'%s'" % str(f).replace("'", escaped)) content = "%s%s\n" % ( content, ' '.join(fixed) ) else: content = "%s%s\n" % ( content, str(args) ) @@ -442,7 +442,7 @@ def dos2unix(string): pos = string.find('\n') if pos <= 0 or string[pos-1] != '\r': return(string) - return(string.replace('\r\n','\n')) + return(string.replace('\r\n', '\n')) def islxc(): # is this host running lxc? @@ -473,7 +473,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 |