diff options
Diffstat (limited to 'cloudinit/CloudConfig')
32 files changed, 494 insertions, 375 deletions
diff --git a/cloudinit/CloudConfig/__init__.py b/cloudinit/CloudConfig/__init__.py index f5c4143c..c9acfbf9 100644 --- a/cloudinit/CloudConfig/__init__.py +++ b/cloudinit/CloudConfig/__init__.py @@ -25,15 +25,16 @@ 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 + 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,62 +42,67 @@ 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() except: - ds_cfg = { } + 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: freq = def_freq self.cloud.sem_and_run("config-" + name, freq, handler, - [ name, self.cfg, self.cloud, cloudinit.log, args ]) + [name, self.cfg, self.cloud, cloudinit.log, args]) except: raise + # 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] freq = None - run_args = [ ] + run_args = [] if len(cfg_mod) > 1: freq = cfg_mod[1] if len(cfg_mod) > 2: @@ -104,17 +110,18 @@ def run_cc_modules(cc,module_list,log): try: log.debug("handling %s with freq=%s and args=%s" % - (name, freq, run_args )) + (name, freq, run_args)) cc.handle(name, run_args, freq=freq) 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) -# always returns well formated values + +# always returns well formated values # cfg is expected to have an entry 'output' in it, which is a dictionary # that includes entries for 'init', 'config', 'final' or 'all' # init: /var/log/cloud.out @@ -125,25 +132,28 @@ def run_cc_modules(cc,module_list,log): # this returns the specific 'mode' entry, cleanly formatted, with value # None if if none is given def get_output_cfg(cfg, mode="init"): - ret = [ None, None ] - if not 'output' in cfg: return ret + ret = [None, None] + 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): - ret = [ modecfg, modecfg ] + 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,26 +167,28 @@ 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 = [ ">>", ">", "|" ] + 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: # default behavior is append - val = "%s %s" % ( ">>", val.strip()) + val = "%s %s" % (">>", val.strip()) ret[i] = val return(ret) - + # redirect_output(outfmt, errfmt, orig_out, orig_err) # replace orig_out and orig_err with filehandles specified in outfmt or errfmt # fmt can be: @@ -185,13 +197,14 @@ def get_output_cfg(cfg, mode="init"): # | program [ arg1 [ arg2 [ ... ] ] ] # # 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): +# shell escape is required. +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 +219,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) @@ -221,32 +235,37 @@ def redirect_output(outfmt,errfmt, o_out=sys.stdout, o_err=sys.stderr): os.dup2(new_fp.fileno(), o_err.fileno()) 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..8aaaa334 100644 --- a/cloudinit/CloudConfig/cc_apt_update_upgrade.py +++ b/cloudinit/CloudConfig/cc_apt_update_upgrade.py @@ -22,7 +22,8 @@ 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,18 +36,17 @@ 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) - # set up proxy proxy = cfg.get("apt_proxy", None) proxy_filename = "/etc/apt/apt.conf.d/95cloud-init-proxy" 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) @@ -54,9 +54,9 @@ def handle(_name,cfg,cloud,log,_args): os.unlink(proxy_filename) # process 'apt_sources' - if cfg.has_key('apt_sources'): + if 'apt_sources' in cfg: errors = add_sources(cfg['apt_sources'], - { 'MIRROR' : mirror, 'RELEASE' : release } ) + {'MIRROR': mirror, 'RELEASE': release}) for e in errors: log.warn("Source Error: %s\n" % ':'.join(e)) @@ -69,9 +69,9 @@ 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 = [ ] + errors = [] if update or len(pkglist) or upgrade: try: cc.update_package_sources() @@ -101,77 +101,90 @@ 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"): - - oprefix="%s/%s" % (lists_d,mirror2lists_fileprefix(omirror)) - nprefix="%s/%s" % (lists_d,mirror2lists_fileprefix(new_mirror)) - if(oprefix==nprefix): return - olen=len(oprefix) + +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) 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() - return(stdout.strip()) + stdout, _stderr = subprocess.Popen(['lsb_release', '-cs'], + stdout=subprocess.PIPE).communicate() + return(str(stdout).strip()) + def generate_sources_list(codename, mirror): util.render_to_file('sources.list', '/etc/apt/sources.list', \ - { 'mirror' : mirror, 'codename' : codename }) + {'mirror': mirror, 'codename': codename}) + -# srclist is a list of dictionaries, -# each entry must have: 'source' -# may have: key, ( keyid and keyserver) def add_sources(srclist, searchList=None): + """ + add entries in /etc/apt/sources.list.d for each abbreviated + sources.list entry in 'srclist'. When rendering template, also + include the values in dictionary searchList + """ if searchList is None: searchList = {} elst = [] for ent in srclist: - if not ent.has_key('source'): - elst.append([ "", "missing source" ]) + if 'source' not in ent: + 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 source = util.render_string(source, searchList) - if not ent.has_key('filename'): - ent['filename']='cloud_config_sources.list' + if 'filename' not in ent: + ent['filename'] = 'cloud_config_sources.list' if not ent['filename'].startswith("/"): ent['filename'] = "%s/%s" % \ ("/etc/apt/sources.list.d/", ent['filename']) - if ( ent.has_key('keyid') and not ent.has_key('key') ): + if ('keyid' in ent and 'key' not in ent): ks = "keyserver.ubuntu.com" - if ent.has_key('keyserver'): ks = ent['keyserver'] + if 'keyserver' in ent: + 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']) + if 'key' in ent: + 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,10 +202,10 @@ 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"): + elif "apt_mirror_search" in cfg: mirror = util.search_for_mirror(cfg['apt_mirror_search']) else: if cloud: @@ -204,7 +217,7 @@ def find_apt_mirror(cloud, cfg): if not mirror and cloud: # if we have a fqdn, then search its domain portion first - ( _hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) + (_hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud) mydom = ".".join(fqdn.split(".")[1:]) if mydom: doms.append(".%s" % mydom) @@ -213,7 +226,7 @@ def find_apt_mirror(cloud, cfg): doms.extend((".localdomain", "",)) mirror_list = [] - mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro ) + mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro) for post in doms: mirror_list.append(mirrorfmt % post) diff --git a/cloudinit/CloudConfig/cc_bootcmd.py b/cloudinit/CloudConfig/cc_bootcmd.py index fc925447..66c452e9 100644 --- a/cloudinit/CloudConfig/cc_bootcmd.py +++ b/cloudinit/CloudConfig/cc_bootcmd.py @@ -18,11 +18,13 @@ import cloudinit.util as util import subprocess import tempfile +import os from cloudinit.CloudConfig import per_always frequency = per_always -def handle(_name,cfg,cloud,log,_args): - if not cfg.has_key("bootcmd"): + +def handle(_name, cfg, cloud, log, _args): + if "bootcmd" not in cfg: return try: @@ -33,10 +35,10 @@ def handle(_name,cfg,cloud,log,_args): except: log.warn("failed to shellify bootcmd") 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..7e455a7a 100644 --- a/cloudinit/CloudConfig/cc_byobu.py +++ b/cloudinit/CloudConfig/cc_byobu.py @@ -19,19 +19,21 @@ 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 - valid = ( "enable-user", "enable-system", "enable", - "disable-user", "disable-system", "disable" ) + valid = ("enable-user", "enable-system", "enable", + "disable-user", "disable-system", "disable") if not value in valid: log.warn("Unknown value %s for byobu_by_default" % value) @@ -50,7 +52,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: @@ -58,7 +60,7 @@ def handle(_name,cfg,_cloud,log,args): shcmd += " && dpkg-reconfigure byobu --frontend=noninteractive" shcmd += " || X=$(($X+1)); " - cmd = [ "/bin/sh", "-c", "%s %s %s" % ("X=0;", shcmd, "exit $X" ) ] + cmd = ["/bin/sh", "-c", "%s %s %s" % ("X=0;", shcmd, "exit $X")] log.debug("setting byobu to %s" % value) @@ -66,7 +68,7 @@ def handle(_name,cfg,_cloud,log,args): subprocess.check_call(cmd) except subprocess.CalledProcessError as e: log.debug(traceback.format_exc(e)) - raise Exception("Cmd returned %s: %s" % ( e.returncode, cmd)) + raise Exception("Cmd returned %s: %s" % (e.returncode, cmd)) except OSError as e: log.debug(traceback.format_exc(e)) - raise Exception("Cmd failed to execute: %s" % ( cmd )) + raise Exception("Cmd failed to execute: %s" % (cmd)) diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index e6cdc3f5..c18821f9 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -56,7 +56,7 @@ def remove_default_ca_certs(): write_file(CA_CERT_CONFIG, "", mode=0644) -def handle(name, cfg, cloud, log, args): +def handle(_name, cfg, _cloud, log, _args): """ Call to handle ca-cert sections in cloud-config file. @@ -74,12 +74,14 @@ def handle(name, cfg, cloud, log, args): # If there is a remove-defaults option set to true, remove the system # default trusted CA certs first. if ca_cert_cfg.get("remove-defaults", False): + log.debug("removing default certificates") remove_default_ca_certs() # If we are given any new trusted CA certs to add, add them. if "trusted" in ca_cert_cfg: trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, "trusted") if trusted_certs: + log.debug("adding %d certificates" % len(trusted_certs)) add_ca_certs(trusted_certs) # Update the system with the new cert configuration. diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index 977fe80f..c9b464b5 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -23,9 +23,11 @@ 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 'chef' not in cfg: + return chef_cfg = cfg['chef'] # ensure the chef directories we use exist @@ -35,7 +37,7 @@ 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 ('validation_key' in chef_cfg or 'validation_cert' in chef_cfg): 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,26 +45,28 @@ 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: initial_json = {} - if chef_cfg.has_key('run_list'): + if 'run_list' in chef_cfg: initial_json['run_list'] = chef_cfg['run_list'] - if chef_cfg.has_key('initial_attributes'): + if 'initial_attributes' in chef_cfg: 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,37 +75,42 @@ 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',)) + def get_ruby_packages(version): # return a list of packages needed to install ruby at version - pkgs = [ 'ruby%s' % version, 'ruby%s-dev' % version ] + pkgs = ['ruby%s' % version, 'ruby%s-dev' % version] if version == "1.8": pkgs.extend(('libopenssl-ruby1.8', 'rubygems1.8')) return(pkgs) -def install_chef_from_gems(ruby_version, chef_version = None): + +def install_chef_from_gems(ruby_version, chef_version=None): cc.install_packages(get_ruby_packages(ruby_version)) if not os.path.exists('/usr/bin/gem'): os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem') 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): os.makedirs(d) + def mkdirs(dirs): for d in dirs: ensure_dir(d) diff --git a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py index f06d4dfc..7deec324 100644 --- a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py +++ b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py @@ -21,7 +21,8 @@ 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..63618fd2 100644 --- a/cloudinit/CloudConfig/cc_final_message.py +++ b/cloudinit/CloudConfig/cc_final_message.py @@ -24,28 +24,28 @@ 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") uptime = "na" - 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" try: - subs = { 'UPTIME' : uptime, 'TIMESTAMP' : ts } + subs = {'UPTIME': uptime, 'TIMESTAMP': ts} sys.stdout.write("%s\n" % util.render_string(msg_in, subs)) except Exception as e: log.warn("failed to render string to stdout: %s" % e) diff --git a/cloudinit/CloudConfig/cc_foo.py b/cloudinit/CloudConfig/cc_foo.py index 48d20e5b..98e2d648 100644 --- a/cloudinit/CloudConfig/cc_foo.py +++ b/cloudinit/CloudConfig/cc_foo.py @@ -22,5 +22,6 @@ 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..69cc96b9 100644 --- a/cloudinit/CloudConfig/cc_grub_dpkg.py +++ b/cloudinit/CloudConfig/cc_grub_dpkg.py @@ -20,37 +20,40 @@ import cloudinit.util as util import traceback import os -def handle(_name,cfg,_cloud,log,_args): - - idevs=None - idevs_empty=None + +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) - 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" + 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" 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"): + for dev in ("/dev/sda", "/dev/vda", "/dev/sda1", "/dev/vda1"): if os.path.exists(dev): idevs = dev break - + # now idevs and idevs_empty are set to determined values # or, those set by user 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..941c49de 100644 --- a/cloudinit/CloudConfig/cc_keys_to_console.py +++ b/cloudinit/CloudConfig/cc_keys_to_console.py @@ -21,12 +21,15 @@ import subprocess frequency = per_instance -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"]) + +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..f228d2cf 100644 --- a/cloudinit/CloudConfig/cc_landscape.py +++ b/cloudinit/CloudConfig/cc_landscape.py @@ -23,7 +23,7 @@ frequency = per_instance lsc_client_cfg_file = "/etc/landscape/client.conf" # defaults taken from stock client.conf in landscape-client 11.07.1.1-0ubuntu2 -lsc_builtincfg = { +lsc_builtincfg = { 'client': { 'log_level': "info", 'url': "https://landscape.canonical.com/message-system", @@ -32,14 +32,15 @@ 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 /etc/landscape/client.conf """ - ls_cloudcfg = cfg.get("landscape", { }) + ls_cloudcfg = cfg.get("landscape", {}) if not isinstance(ls_cloudcfg, dict): raise(Exception("'landscape' existed in config, but not a dict")) @@ -51,6 +52,7 @@ def handle(_name,cfg,_cloud,log,_args): log.debug("updated %s" % lsc_client_cfg_file) + def mergeTogether(objs): """ merge together ConfigObj objects or things that ConfigObj() will take in diff --git a/cloudinit/CloudConfig/cc_locale.py b/cloudinit/CloudConfig/cc_locale.py index 8e91d3bf..9129ca30 100644 --- a/cloudinit/CloudConfig/cc_locale.py +++ b/cloudinit/CloudConfig/cc_locale.py @@ -20,24 +20,27 @@ import os.path import subprocess import traceback + def apply_locale(locale, cfgfile): if os.path.exists('/usr/sbin/locale-gen'): subprocess.Popen(['locale-gen', locale]).communicate() if os.path.exists('/usr/sbin/update-locale'): subprocess.Popen(['update-locale', locale]).communicate() - util.render_to_file('default-locale', cfgfile, { 'locale' : locale }) + 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..2b8b2f96 100644 --- a/cloudinit/CloudConfig/cc_mcollective.py +++ b/cloudinit/CloudConfig/cc_mcollective.py @@ -27,49 +27,59 @@ import cloudinit.util as util pubcert_file = "/etc/mcollective/ssl/server-public.pem" pricert_file = "/etc/mcollective/ssl/server-private.pem" + # Our fake header section class FakeSecHead(object): def __init__(self, fp): self.fp = fp 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 'mcollective' not in cfg: + return mcollective_cfg = cfg['mcollective'] # Start by installing the mcollective package ... cc.install_packages(("mcollective",)) # ... and then update the mcollective configuration - if mcollective_cfg.has_key('conf'): + if 'conf' in mcollective_cfg: # 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,9 +89,9 @@ 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 subprocess.check_call(['service', 'mcollective', 'start']) - diff --git a/cloudinit/CloudConfig/cc_mounts.py b/cloudinit/CloudConfig/cc_mounts.py index a3036d5a..2fa57362 100644 --- a/cloudinit/CloudConfig/cc_mounts.py +++ b/cloudinit/CloudConfig/cc_mounts.py @@ -18,30 +18,32 @@ import cloudinit.util as util import os import re -import string +from string import whitespace # pylint: disable=W0402 + def is_mdname(name): # return true if this is a metadata service name - if name in [ "ami", "root", "swap" ]: + if name in ["ami", "root", "swap"]: return True # names 'ephemeral0' or 'ephemeral1' # 'ebs[0-9]' appears when '--block-device-mapping sdf=snap-d4d90bbc' - for enumname in ( "ephemeral", "ebs" ): + for enumname in ("ephemeral", "ebs"): if name.startswith(enumname) and name.find(":") == -1: 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 = [None, None, "auto", "defaults,nobootwait", "0", "2"] defvals = cfg.get("mount_default_fields", defvals) # these are our default set of mounts - defmnts = [ [ "ephemeral0", "/mnt", "auto", defvals[3], "0", "2" ], - [ "swap", "none", "swap", "sw", "0", "0" ] ] + defmnts = [["ephemeral0", "/mnt", "auto", defvals[3], "0", "2"], + ["swap", "none", "swap", "sw", "0", "0"]] - cfgmnt = [ ] - if cfg.has_key("mounts"): + cfgmnt = [] + if "mounts" in cfg: cfgmnt = cfg["mounts"] # shortname matches 'sda', 'sda1', 'xvda', 'hda', 'sdb', xvdb, vda, vdd1 @@ -50,7 +52,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 +78,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 @@ -93,12 +96,12 @@ def handle(_name,cfg,cloud,log,_args): if cfgmnt[j][0] == cfgmnt[i][0]: cfgmnt[j][1] = None - # for each of the "default" mounts, add them only if no other # 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: @@ -109,54 +112,65 @@ def handle(_name,cfg,cloud,log,_args): if cfgm[0] == defmnt[0]: cfgmnt_has = True break - - if cfgmnt_has: continue - cfgmnt.append(defmnt) + if cfgmnt_has: + continue + cfgmnt.append(defmnt) # now, each entry in the cfgmnt list has all fstab values # 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" - cc_lines = [ ] + comment = "comment=cloudconfig" + cc_lines = [] needswap = False - dirs = [ ] + 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+") - ws = re.compile("[%s]+" % string.whitespace) + fstab_lines = [] + fstab = open("/etc/fstab", "r+") + ws = re.compile("[%s]+" % 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) fstab_lines.extend(cc_lines) - + fstab.seek(0) fstab.write("%s\n" % '\n'.join(fstab_lines)) fstab.truncate() 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..73066444 100644 --- a/cloudinit/CloudConfig/cc_phone_home.py +++ b/cloudinit/CloudConfig/cc_phone_home.py @@ -20,7 +20,9 @@ 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/ @@ -30,12 +32,13 @@ post_list_all = [ 'pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', # phone_home: # 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]) + ph_cfg = util.read_conf(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 +47,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: @@ -54,7 +57,7 @@ def handle(_name,cfg,cloud,log,args): if post_list == "all": post_list = post_list_all - all_keys = { } + all_keys = {} all_keys['instance_id'] = cloud.get_instance_id() all_keys['hostname'] = cloud.get_hostname() @@ -72,7 +75,7 @@ def handle(_name,cfg,cloud,log,args): except: log.warn("%s: failed to open in phone_home" % path) - submit_keys = { } + submit_keys = {} for k in post_list: if k in all_keys: submit_keys[k] = all_keys[k] @@ -80,20 +83,22 @@ def handle(_name,cfg,cloud,log,args): submit_keys[k] = "N/A" log.warn("requested key %s from 'post' list not available") - url = util.render_string(url, { 'INSTANCE_ID' : all_keys['instance_id'] }) + url = util.render_string(url, {'INSTANCE_ID': all_keys['instance_id']}) - last_e = None - for i in range(0,tries): + null_exc = object() + last_e = null_exc + for i in range(0, tries): try: util.readurl(url, submit_keys) - log.debug("succeeded submit to %s on try %i" % (url, i+1)) + log.debug("succeeded submit to %s on try %i" % (url, i + 1)) return except Exception as e: - log.debug("failed to post to %s on try %i" % (url, i+1)) + log.debug("failed to post to %s on try %i" % (url, i + 1)) last_e = e sleep(3) log.warn("failed to post to %s in %i tries" % (url, tries)) - if last_e: raise(last_e) - + if last_e is not null_exc: + raise(last_e) + return diff --git a/cloudinit/CloudConfig/cc_puppet.py b/cloudinit/CloudConfig/cc_puppet.py index 3748559a..6db1ed5c 100644 --- a/cloudinit/CloudConfig/cc_puppet.py +++ b/cloudinit/CloudConfig/cc_puppet.py @@ -25,21 +25,25 @@ 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 'puppet' not in cfg: + return puppet_cfg = cfg['puppet'] # Start by installing the puppet package ... cc.install_packages(("puppet",)) # ... and then update the puppet configuration - if puppet_cfg.has_key('conf'): + if 'conf' in puppet_cfg: # Add all sections from the conf object to puppet.conf 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 +67,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 +82,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') @@ -98,4 +103,3 @@ def handle(_name,cfg,cloud,log,_args): log.warn("Do not know how to enable puppet service on this system") # Start puppetd subprocess.check_call(['service', 'puppet', 'start']) - diff --git a/cloudinit/CloudConfig/cc_resizefs.py b/cloudinit/CloudConfig/cc_resizefs.py index adec70be..f29f886d 100644 --- a/cloudinit/CloudConfig/cc_resizefs.py +++ b/cloudinit/CloudConfig/cc_resizefs.py @@ -25,24 +25,26 @@ 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']: + 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() os.unlink(devpth) 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(): @@ -51,9 +53,9 @@ def handle(_name,cfg,_cloud,log,args): log.warn("Failed to make device node to resize /") raise - cmd = [ 'blkid', '-c', '/dev/null', '-sTYPE', '-ovalue', devpth ] + 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)) @@ -61,13 +63,13 @@ def handle(_name,cfg,_cloud,log,args): os.unlink(devpth) raise - log.debug("resizing root filesystem (type=%s, maj=%i, min=%i)" % - (fstype.rstrip("\n"), os.major(st_dev), os.minor(st_dev))) + log.debug("resizing root filesystem (type=%s, maj=%i, min=%i)" % + (str(fstype).rstrip("\n"), os.major(st_dev), os.minor(st_dev))) - if fstype.startswith("ext"): - resize_cmd = [ 'resize2fs', devpth ] + if str(fstype).startswith("ext"): + resize_cmd = ['resize2fs', devpth] elif fstype == "xfs": - resize_cmd = [ 'xfs_growfs', devpth ] + resize_cmd = ['xfs_growfs', devpth] else: os.unlink(devpth) log.debug("not resizing unknown filesystem %s" % fstype) diff --git a/cloudinit/CloudConfig/cc_rightscale_userdata.py b/cloudinit/CloudConfig/cc_rightscale_userdata.py index 2b43023c..d6e93aa3 100644 --- a/cloudinit/CloudConfig/cc_rightscale_userdata.py +++ b/cloudinit/CloudConfig/cc_rightscale_userdata.py @@ -24,9 +24,9 @@ ## for cloud-init support, there will be a key named ## 'CLOUD_INIT_REMOTE_HOOK'. ## -## This cloud-config module will +## This cloud-config module will ## - read the blob of data from raw user data, and parse it as key/value -## - for each key that is found, download the content to +## - for each key that is found, download the content to ## the local instance/scripts directory and set them executable. ## - the files in that directory will be run by the user-scripts module ## Therefore, this must run before that. @@ -42,7 +42,8 @@ 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 +52,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,14 +62,15 @@ 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) - i = i +1 + 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: raise(e) diff --git a/cloudinit/CloudConfig/cc_rsyslog.py b/cloudinit/CloudConfig/cc_rsyslog.py index ab85a6d8..552597a5 100644 --- a/cloudinit/CloudConfig/cc_rsyslog.py +++ b/cloudinit/CloudConfig/cc_rsyslog.py @@ -24,7 +24,8 @@ 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,15 +34,16 @@ 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) - files = [ ] - elst = [ ] + 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 +54,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 @@ -69,7 +71,7 @@ def handle(_name,cfg,_cloud,log,_args): # need to restart syslogd restarted = False try: - # if this config module is running at cloud-init time + # if this config module is running at cloud-init time # (before rsyslog is running) we don't actually have to # restart syslog. # @@ -83,7 +85,7 @@ def handle(_name,cfg,_cloud,log,_args): except Exception as e: elst.append(("restart", str(e))) - + if restarted: # this only needs to run if we *actually* restarted # syslog above. diff --git a/cloudinit/CloudConfig/cc_runcmd.py b/cloudinit/CloudConfig/cc_runcmd.py index d255223b..cb297568 100644 --- a/cloudinit/CloudConfig/cc_runcmd.py +++ b/cloudinit/CloudConfig/cc_runcmd.py @@ -18,12 +18,13 @@ import cloudinit.util as util -def handle(_name,cfg,cloud,log,_args): - if not cfg.has_key("runcmd"): + +def handle(_name, cfg, cloud, log, _args): + if "runcmd" not in cfg: 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..2eb77c18 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_boot.py +++ b/cloudinit/CloudConfig/cc_scripts_per_boot.py @@ -23,7 +23,8 @@ 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..0141c977 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_instance.py +++ b/cloudinit/CloudConfig/cc_scripts_per_instance.py @@ -23,7 +23,8 @@ 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..bbf77dfb 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_once.py +++ b/cloudinit/CloudConfig/cc_scripts_per_once.py @@ -23,7 +23,8 @@ 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..949b4198 100644 --- a/cloudinit/CloudConfig/cc_scripts_user.py +++ b/cloudinit/CloudConfig/cc_scripts_user.py @@ -23,7 +23,8 @@ 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..0b1c8924 100644 --- a/cloudinit/CloudConfig/cc_set_hostname.py +++ b/cloudinit/CloudConfig/cc_set_hostname.py @@ -18,12 +18,13 @@ 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) - ( hostname, _fqdn ) = util.get_hostname_fqdn(cfg, cloud) + (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud) try: set_hostname(hostname, log) except Exception: @@ -32,7 +33,8 @@ def handle(_name,cfg,cloud,log,_args): return(True) + 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..f40544b3 100644 --- a/cloudinit/CloudConfig/cc_set_passwords.py +++ b/cloudinit/CloudConfig/cc_set_passwords.py @@ -19,16 +19,17 @@ import cloudinit.util as util import sys import random -import string +from string import letters, digits # pylint: disable=W0402 -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 +38,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,13 +51,13 @@ 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) try: util.subp(['chpasswd'], ch_in) @@ -67,36 +68,36 @@ def handle(_name,cfg,_cloud,log,args): if len(randlist): sys.stdout.write("%s\n%s\n" % ("Set the following passwords\n", - '\n'.join(randlist) )) + '\n'.join(randlist))) if expire: - enum=len(errors) + enum = len(errors) for u in users: try: util.subp(['passwd', '--expire', u]) except Exception as e: errors.append(e) - log.warn("failed to expire account for %s" % u ) + log.warn("failed to expire account for %s" % u) if enum == len(errors): log.debug("expired passwords for: %s" % u) if 'ssh_pwauth' in cfg: val = str(cfg['ssh_pwauth']).lower() - if val in ( "true", "1", "yes"): - pw_auth="yes" - change_pwauth=True - elif val in ( "false", "0", "no"): - pw_auth="no" - change_pwauth=True + if val in ("true", "1", "yes"): + pw_auth = "yes" + change_pwauth = True + elif val in ("false", "0", "no"): + pw_auth = "no" + change_pwauth = True else: - change_pwauth=False - + change_pwauth = False + if change_pwauth: pa_s = "\(#*\)\(PasswordAuthentication[[:space:]]\+\)\(yes\|no\)" msg = "set PasswordAuthentication to '%s'" % pw_auth try: - cmd = [ 'sed', '-i', 's,%s,\\2%s,' % (pa_s, pw_auth), - '/etc/ssh/sshd_config' ] + cmd = ['sed', '-i', 's,%s,\\2%s,' % (pa_s, pw_auth), + '/etc/ssh/sshd_config'] util.subp(cmd) log.debug(msg) except Exception as e: @@ -104,7 +105,8 @@ def handle(_name,cfg,_cloud,log,args): errors.append(e) try: - p = util.subp(['service', cfg.get('ssh_svcname', 'ssh'), 'restart']) + p = util.subp(['service', cfg.get('ssh_svcname', 'ssh'), + 'restart']) log.debug("restarted sshd") except: log.warn("restart of ssh failed") @@ -114,11 +116,12 @@ def handle(_name,cfg,_cloud,log,args): return -def rand_str(strlen=32, select_from=string.letters+string.digits): + +def rand_str(strlen=32, select_from=letters + 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)) +def rand_user_password(pwlen=9): + selfrom = (letters.translate(None, 'loLOI') + + 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..cdf90bdc 100644 --- a/cloudinit/CloudConfig/cc_ssh.py +++ b/cloudinit/CloudConfig/cc_ssh.py @@ -21,49 +21,50 @@ 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): - global global_log - global_log = log +def handle(_name, cfg, cloud, log, _args): # 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 "ssh_keys" in cfg: # if there are keys in cloud-config, use them key2file = { - "rsa_private" : ("/etc/ssh/ssh_host_rsa_key", 0600), - "rsa_public" : ("/etc/ssh/ssh_host_rsa_key.pub", 0644), - "dsa_private" : ("/etc/ssh/ssh_host_dsa_key", 0600), - "dsa_public" : ("/etc/ssh/ssh_host_dsa_key.pub", 0644), - "ecdsa_private" : ("/etc/ssh/ssh_host_ecdsa_key", 0600), - "ecdsa_public" : ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644), + "rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600), + "rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644), + "dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600), + "dsa_public": ("/etc/ssh/ssh_host_dsa_key.pub", 0644), + "ecdsa_private": ("/etc/ssh/ssh_host_ecdsa_key", 0600), + "ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644), } - for key,val in cfg["ssh_keys"].items(): - if key2file.has_key(key): - util.write_file(key2file[key][0],val,key2file[key][1]) + for key, val in cfg["ssh_keys"].items(): + if key in key2file: + util.write_file(key2file[key][0], val, key2file[key][1]) - priv2pub = { 'rsa_private':'rsa_public', 'dsa_private':'dsa_public', - 'ecdsa_private': 'ecdsa_public', } + 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: # if not, generate them for keytype in util.get_cfg_option_list_or_str(cfg, 'ssh_genkeytypes', - ['rsa', 'dsa', 'ecdsa']): + ['rsa', 'dsa', 'ecdsa']): keyfile = '/etc/ssh/ssh_host_%s_key' % keytype if not os.path.exists(keyfile): subprocess.call(['ssh-keygen', '-t', keytype, '-N', '', @@ -72,30 +73,31 @@ 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) keys = cloud.get_public_ssh_keys() - if cfg.has_key("ssh_authorized_keys"): + if "ssh_authorized_keys" in cfg: 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, log) 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=None): keys = set(keys) if user: sshutil.setup_user_keys(keys, user, '', log) - + if disable_root: key_prefix = disable_root_opts.replace('$USER', user) else: key_prefix = '' sshutil.setup_user_keys(keys, 'root', key_prefix, log) - diff --git a/cloudinit/CloudConfig/cc_ssh_import_id.py b/cloudinit/CloudConfig/cc_ssh_import_id.py index 7e7a54a1..f14a99cd 100644 --- a/cloudinit/CloudConfig/cc_ssh_import_id.py +++ b/cloudinit/CloudConfig/cc_ssh_import_id.py @@ -19,19 +19,21 @@ 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 = [ ] + 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 + cmd = ["sudo", "-Hu", user, "ssh-import-id"] + ids log.debug("importing ssh ids. cmd = %s" % cmd) @@ -39,7 +41,7 @@ def handle(_name,cfg,_cloud,log,args): subprocess.check_call(cmd) except subprocess.CalledProcessError as e: log.debug(traceback.format_exc(e)) - raise Exception("Cmd returned %s: %s" % ( e.returncode, cmd)) + raise Exception("Cmd returned %s: %s" % (e.returncode, cmd)) except OSError as e: log.debug(traceback.format_exc(e)) - raise Exception("Cmd failed to execute: %s" % ( cmd )) + raise Exception("Cmd failed to execute: %s" % (cmd)) diff --git a/cloudinit/CloudConfig/cc_timezone.py b/cloudinit/CloudConfig/cc_timezone.py index 26b2796d..6f0e8f6b 100644 --- a/cloudinit/CloudConfig/cc_timezone.py +++ b/cloudinit/CloudConfig/cc_timezone.py @@ -24,22 +24,24 @@ 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) + tz_file = "%s/%s" % (tz_base, timezone) if not os.path.isfile(tz_file): log.debug("Invalid timezone %s" % tz_file) 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: @@ -58,6 +60,6 @@ def handle(_name,cfg,_cloud,log,args): except: log.debug("failed to copy %s to /etc/localtime" % tz_file) raise - + log.debug("set timezone to %s" % timezone) return diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py index 1c245e67..131e1a1e 100644 --- a/cloudinit/CloudConfig/cc_update_etc_hosts.py +++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py @@ -21,10 +21,11 @@ import StringIO frequency = per_always -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) +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) if manage_hosts in ("True", "true", True, "template"): # render from template file try: @@ -32,8 +33,8 @@ def handle(_name,cfg,cloud,log,_args): log.info("manage_etc_hosts was set, but no hostname found") return - util.render_to_file('hosts', '/etc/hosts', \ - { 'hostname' : hostname, 'fqdn' : fqdn }) + util.render_to_file('hosts', '/etc/hosts', + {'hostname': hostname, 'fqdn': fqdn}) except Exception: log.warn("failed to update /etc/hosts") raise @@ -76,9 +77,8 @@ 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() return - diff --git a/cloudinit/CloudConfig/cc_update_hostname.py b/cloudinit/CloudConfig/cc_update_hostname.py index 4bc1cb2b..2387a8dc 100644 --- a/cloudinit/CloudConfig/cc_update_hostname.py +++ b/cloudinit/CloudConfig/cc_update_hostname.py @@ -22,25 +22,27 @@ 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) + (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") raise + # read hostname from a 'hostname' file # allow for comments and stripping line endings. # 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,9 +53,11 @@ 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): etc_file = "/etc/hostname" @@ -74,22 +78,21 @@ def update_hostname(hostname, prev_file, log): if not hostname_prev or hostname_prev != hostname: update_files.append(prev_file) - if (not hostname_in_etc or + if (not hostname_in_etc or (hostname_in_etc == hostname_prev and hostname_in_etc != hostname)): update_files.append(etc_file) 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) subprocess.Popen(['hostname', hostname]).communicate() - |