Fix a bunch of == None cases

This commit is contained in:
harlowja 2012-06-23 15:06:26 -07:00
commit 612a9ba80d
8 changed files with 45 additions and 37 deletions

View File

@ -29,6 +29,21 @@ distros = ['ubuntu', 'debian']
PROXY_TPL = "Acquire::HTTP::Proxy \"%s\";\n" PROXY_TPL = "Acquire::HTTP::Proxy \"%s\";\n"
PROXY_FN = "/etc/apt/apt.conf.d/95cloud-init-proxy" 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): def handle(_name, cfg, cloud, log, _args):
update = util.get_cfg_option_bool(cfg, 'apt_update', False) update = util.get_cfg_option_bool(cfg, 'apt_update', False)
@ -106,9 +121,19 @@ def handle(_name, cfg, cloud, log, _args):
raise errors[-1] 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): def mirror2lists_fileprefix(mirror):
string = mirror string = mirror
# take of http:// or ftp:// # take off http:// or ftp://
if string.endswith("/"): if string.endswith("/"):
string = string[0:-1] string = string[0:-1]
pos = string.find("://") pos = string.find("://")
@ -119,8 +144,8 @@ def mirror2lists_fileprefix(mirror):
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)) oprefix = os.path.join(lists_d, mirror2lists_fileprefix(omirror))
nprefix = "%s/%s" % (lists_d, mirror2lists_fileprefix(new_mirror)) nprefix = os.path.join(lists_d, mirror2lists_fileprefix(new_mirror))
if oprefix == nprefix: if oprefix == nprefix:
return return
olen = len(oprefix) olen = len(oprefix)
@ -181,7 +206,7 @@ def add_sources(cloud, srclist, template_params=None):
if 'keyserver' in ent: if 'keyserver' in ent:
ks = ent['keyserver'] ks = ent['keyserver']
try: try:
ent['key'] = util.getkeybyid(ent['keyid'], ks) ent['key'] = getkeybyid(ent['keyid'], ks)
except: except:
errorlist.append([source, "failed to get key from %s" % ks]) errorlist.append([source, "failed to get key from %s" % ks])
continue continue

View File

@ -19,7 +19,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import tempfile
from cloudinit import util from cloudinit import util
from cloudinit.settings import PER_ALWAYS from cloudinit.settings import PER_ALWAYS
@ -34,7 +33,7 @@ def handle(name, cfg, cloud, log, _args):
" no 'bootcmd' key in configuration"), name) " no 'bootcmd' key in configuration"), name)
return return
with tempfile.NamedTemporaryFile(suffix=".sh") as tmpf: with util.ExtendedTemporaryFile(suffix=".sh") as tmpf:
try: try:
content = util.shellify(cfg["bootcmd"]) content = util.shellify(cfg["bootcmd"])
tmpf.write(content) tmpf.write(content)

View File

@ -37,14 +37,14 @@ def handle(_name, cfg, _cloud, log, _args):
if ((os.path.exists("/dev/sda1") and not os.path.exists("/dev/sda")) or 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"))): (os.path.exists("/dev/xvda1") and not os.path.exists("/dev/xvda"))):
if idevs == None: if idevs is None:
idevs = "" idevs = ""
if idevs_empty == None: if idevs_empty is None:
idevs_empty = "true" idevs_empty = "true"
else: else:
if idevs_empty == None: if idevs_empty is None:
idevs_empty = "false" idevs_empty = "false"
if idevs == None: if idevs is None:
idevs = "/dev/sda" 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): if os.path.exists(dev):

View File

@ -79,8 +79,8 @@ def handle(name, cfg, cloud, log, args):
# TODO: allow what is to be resized to be configurable?? # TODO: allow what is to be resized to be configurable??
resize_what = cloud.paths.join(False, "/") resize_what = cloud.paths.join(False, "/")
with util.SilentTemporaryFile(prefix="cloudinit.resizefs.", with util.ExtendedTemporaryFile(prefix="cloudinit.resizefs.",
dir=resize_root_d, delete=True) as tfh: dir=resize_root_d, delete=True) as tfh:
devpth = tfh.name devpth = tfh.name
# Delete the file so that mknod will work # Delete the file so that mknod will work

View File

@ -186,7 +186,7 @@ def transport_iso9660(require_iso=True):
fstype = info['fstype'] fstype = info['fstype']
if fstype != "iso9660" and require_iso: if fstype != "iso9660" and require_iso:
continue continue
if cdmatch.match(dev[5:]) == None: # take off '/dev/' if cdmatch.match(dev[5:]) is None: # take off '/dev/'
continue continue
mp = info['mountpoint'] mp = info['mountpoint']
(fname, contents) = get_ovf_env(mp) (fname, contents) = get_ovf_env(mp)

View File

@ -171,10 +171,13 @@ class Init(object):
return None return None
def _write_to_cache(self): def _write_to_cache(self):
if not self.datasource:
return False
pickled_fn = self.paths.get_ipath_cur("obj_pkl") pickled_fn = self.paths.get_ipath_cur("obj_pkl")
try: try:
contents = pickle.dumps(self.datasource) contents = pickle.dumps(self.datasource)
util.write_file(pickled_fn, contents, mode=0400) util.write_file(pickled_fn, contents, mode=0400)
return True
except Exception: except Exception:
util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn) util.logexc(LOG, "Failed pickling datasource to %s", pickled_fn)
return False return False
@ -285,7 +288,8 @@ class Init(object):
self.distro, helpers.Runners(self.paths)) self.distro, helpers.Runners(self.paths))
def update(self): def update(self):
self._write_to_cache() if not self._write_to_cache():
return
self._store_userdata() self._store_userdata()
def _store_userdata(self): def _store_userdata(self):

View File

@ -157,7 +157,7 @@ class MountFailedError(Exception):
pass pass
def SilentTemporaryFile(**kwargs): def ExtendedTemporaryFile(**kwargs):
fh = tempfile.NamedTemporaryFile(**kwargs) fh = tempfile.NamedTemporaryFile(**kwargs)
# Replace its unlink with a quiet version # Replace its unlink with a quiet version
# that does not raise errors when the # that does not raise errors when the
@ -517,26 +517,6 @@ def del_dir(path):
shutil.rmtree(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): def runparts(dirp, skip_no_exist=True):
if skip_no_exist and not os.path.isdir(dirp): if skip_no_exist and not os.path.isdir(dirp):
return return
@ -928,7 +908,7 @@ def pipe_in_out(in_fh, out_fh, chunk_size=1024, chunk_cb=None):
def chownbyid(fname, uid=None, gid=None): def chownbyid(fname, uid=None, gid=None):
if uid == None and gid == None: if uid is None and gid is None:
return return
LOG.debug("Changing the ownership of %s to %s:%s", fname, uid, gid) LOG.debug("Changing the ownership of %s to %s:%s", fname, uid, gid)
os.chown(fname, uid, gid) os.chown(fname, uid, gid)