summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig/cc_apt_update_upgrade.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/CloudConfig/cc_apt_update_upgrade.py')
-rw-r--r--cloudinit/CloudConfig/cc_apt_update_upgrade.py97
1 files changed, 55 insertions, 42 deletions
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)