From f8413af9168adc0ad7c730b9adea9eba67949ba5 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 22 Jun 2012 23:26:50 -0700 Subject: 1. Move the getkeybyid function back here but add some slight adjustments a. Instead of executing a bash string, write out a temporary file and then just execute '/bin/sh' on that file with the right arguments instead. 2. Rename util.SilentTemporaryFile to util.ExtendedTemporaryFile and update the usages of the previous name accordingly, this better reflects what this temp file is. 3. More teenie pep8 line length fixings --- cloudinit/config/cc_apt_update_upgrade.py | 27 ++++++++++++++++++++++++++- cloudinit/config/cc_bootcmd.py | 3 +-- cloudinit/config/cc_chef.py | 2 +- cloudinit/config/cc_resizefs.py | 4 ++-- cloudinit/util.py | 22 +--------------------- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/cloudinit/config/cc_apt_update_upgrade.py b/cloudinit/config/cc_apt_update_upgrade.py index 6719da52..8ecd9c94 100644 --- a/cloudinit/config/cc_apt_update_upgrade.py +++ b/cloudinit/config/cc_apt_update_upgrade.py @@ -29,6 +29,21 @@ distros = ['ubuntu', 'debian'] PROXY_TPL = "Acquire::HTTP::Proxy \"%s\";\n" PROXY_FN = "/etc/apt/apt.conf.d/95cloud-init-proxy" +# A temporary shell program to get a given gpg key +# from a given keyserver +EXPORT_GPG_KEYID = """ + k=${1} ks=${2}; + exec 2>/dev/null + [ -n "$k" ] || exit 1; + armour=$(gpg --list-keys --armour "${k}") + if [ -z "${armour}" ]; then + gpg --keyserver ${ks} --recv $k >/dev/null && + armour=$(gpg --export --armour "${k}") && + gpg --batch --yes --delete-keys "${k}" + fi + [ -n "${armour}" ] && echo "${armour}" +""" + def handle(_name, cfg, cloud, log, _args): update = util.get_cfg_option_bool(cfg, 'apt_update', False) @@ -106,6 +121,16 @@ def handle(_name, cfg, cloud, log, _args): raise errors[-1] +# get gpg keyid from keyserver +def getkeybyid(keyid, keyserver): + with util.ExtendedTemporaryFile(suffix='.sh') as fh: + fh.write(EXPORT_GPG_KEYID) + fh.flush() + cmd = ['/bin/sh', fh.name, keyid, keyserver] + (stdout, _stderr) = util.subp(cmd) + return stdout.strip() + + def mirror2lists_fileprefix(mirror): string = mirror # take of http:// or ftp:// @@ -181,7 +206,7 @@ def add_sources(cloud, srclist, template_params=None): if 'keyserver' in ent: ks = ent['keyserver'] try: - ent['key'] = util.getkeybyid(ent['keyid'], ks) + ent['key'] = getkeybyid(ent['keyid'], ks) except: errorlist.append([source, "failed to get key from %s" % ks]) continue diff --git a/cloudinit/config/cc_bootcmd.py b/cloudinit/config/cc_bootcmd.py index 89ccf3f1..bae1ea54 100644 --- a/cloudinit/config/cc_bootcmd.py +++ b/cloudinit/config/cc_bootcmd.py @@ -19,7 +19,6 @@ # along with this program. If not, see . import os -import tempfile from cloudinit import util from cloudinit.settings import PER_ALWAYS @@ -34,7 +33,7 @@ def handle(name, cfg, cloud, log, _args): " no 'bootcmd' key in configuration"), name) return - with tempfile.NamedTemporaryFile(suffix=".sh") as tmpf: + with util.ExtendedTemporaryFile(suffix=".sh") as tmpf: try: content = util.shellify(cfg["bootcmd"]) tmpf.write(content) diff --git a/cloudinit/config/cc_chef.py b/cloudinit/config/cc_chef.py index d682398a..6f568261 100644 --- a/cloudinit/config/cc_chef.py +++ b/cloudinit/config/cc_chef.py @@ -96,7 +96,7 @@ def handle(name, cfg, cloud, log, _args): install_chef_from_gems(cloud.distro, ruby_version, chef_version) # and finally, run chef-client log.debug('Running chef-client') - util.subp(['/usr/bin/chef-client', + util.subp(['/usr/bin/chef-client', '-d', '-i', '1800', '-s', '20'], capture=False) elif install_type == 'packages': # this will install and run the chef-client from packages diff --git a/cloudinit/config/cc_resizefs.py b/cloudinit/config/cc_resizefs.py index 7e1428e9..69cd8872 100644 --- a/cloudinit/config/cc_resizefs.py +++ b/cloudinit/config/cc_resizefs.py @@ -79,8 +79,8 @@ def handle(name, cfg, cloud, log, args): # TODO: allow what is to be resized to be configurable?? resize_what = cloud.paths.join(False, "/") - with util.SilentTemporaryFile(prefix="cloudinit.resizefs.", - dir=resize_root_d, delete=True) as tfh: + with util.ExtendedTemporaryFile(prefix="cloudinit.resizefs.", + dir=resize_root_d, delete=True) as tfh: devpth = tfh.name # Delete the file so that mknod will work diff --git a/cloudinit/util.py b/cloudinit/util.py index baa3def1..6cdf9ff3 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -157,7 +157,7 @@ class MountFailedError(Exception): pass -def SilentTemporaryFile(**kwargs): +def ExtendedTemporaryFile(**kwargs): fh = tempfile.NamedTemporaryFile(**kwargs) # Replace its unlink with a quiet version # that does not raise errors when the @@ -517,26 +517,6 @@ def del_dir(path): shutil.rmtree(path) -# get gpg keyid from keyserver -def getkeybyid(keyid, keyserver): - # TODO fix this... - shcmd = """ - k=${1} ks=${2}; - exec 2>/dev/null - [ -n "$k" ] || exit 1; - armour=$(gpg --list-keys --armour "${k}") - if [ -z "${armour}" ]; then - gpg --keyserver ${ks} --recv $k >/dev/null && - armour=$(gpg --export --armour "${k}") && - gpg --batch --yes --delete-keys "${k}" - fi - [ -n "${armour}" ] && echo "${armour}" - """ - args = ['sh', '-c', shcmd, "export-gpg-keyid", keyid, keyserver] - (stdout, _stderr) = subp(args) - return stdout - - def runparts(dirp, skip_no_exist=True): if skip_no_exist and not os.path.isdir(dirp): return -- cgit v1.2.3 From fdbd341aae7a41d3985e544fe40375087056b1f9 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 22 Jun 2012 23:36:08 -0700 Subject: Use os.path.join instead of custom string path formation --- cloudinit/config/cc_apt_update_upgrade.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cloudinit/config/cc_apt_update_upgrade.py b/cloudinit/config/cc_apt_update_upgrade.py index 8ecd9c94..42b6f3e9 100644 --- a/cloudinit/config/cc_apt_update_upgrade.py +++ b/cloudinit/config/cc_apt_update_upgrade.py @@ -133,7 +133,7 @@ def getkeybyid(keyid, keyserver): def mirror2lists_fileprefix(mirror): string = mirror - # take of http:// or ftp:// + # take off http:// or ftp:// if string.endswith("/"): string = string[0:-1] pos = string.find("://") @@ -144,8 +144,8 @@ def mirror2lists_fileprefix(mirror): 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 = os.path.join(lists_d, mirror2lists_fileprefix(omirror)) + nprefix = os.path.join(lists_d, mirror2lists_fileprefix(new_mirror)) if oprefix == nprefix: return olen = len(oprefix) -- cgit v1.2.3 From a0f42d8f96248d519e70a86e470868616dea3d6e Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 22 Jun 2012 23:43:48 -0700 Subject: Don't complete the update if there is no datasource or if writing to cache fails --- cloudinit/stages.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cloudinit/stages.py b/cloudinit/stages.py index cfe1c071..3ed13009 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -178,10 +178,13 @@ class Init(object): return None def _write_to_cache(self): + if not self.datasource: + return False pickled_fn = self.paths.get_ipath_cur("obj_pkl") try: contents = pickle.dumps(self.datasource) util.write_file(pickled_fn, contents, mode=0400) + return True except Exception: util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn) return False @@ -292,7 +295,8 @@ class Init(object): self.distro, helpers.Runners(self.paths)) def update(self): - self._write_to_cache() + if not self._write_to_cache(): + return self._store_userdata() def _store_userdata(self): -- cgit v1.2.3