summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig/cc_apt_update_upgrade.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2010-06-17 22:22:39 -0400
committerScott Moser <smoser@ubuntu.com>2010-06-17 22:22:39 -0400
commit22184b7ca3f78808d8025bab5981ed92f8ad99f5 (patch)
tree89c9f4b1991d5f453b7fec5d9239c09adeb7f0f3 /cloudinit/CloudConfig/cc_apt_update_upgrade.py
parent257da18b16434360e656fe1ef94e4cf8ba3a5210 (diff)
downloadvyos-cloud-init-22184b7ca3f78808d8025bab5981ed92f8ad99f5.tar.gz
vyos-cloud-init-22184b7ca3f78808d8025bab5981ed92f8ad99f5.zip
make cloud-config modules configurable by cloud-config
The list of cloud-config modules is now kept in cloud config itself. There is a builtin list in cloudinit, which is overrideable by /etc/cloud/cloud.cfg or user data cloud-config. This should make the modules more easily added or removed (as no code needs to be edited now) Basic summary of changes: - move CloudConfig.py -> cloudinit/CloudConfig/__init__.py - split cloud-config modules into their own files named cloudinit/CloudConfig/cc_<name>.py - remove all the upstart/cloud-config-* scripts, replacing them with upstart/cloud-config.conf
Diffstat (limited to 'cloudinit/CloudConfig/cc_apt_update_upgrade.py')
-rw-r--r--cloudinit/CloudConfig/cc_apt_update_upgrade.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/cloudinit/CloudConfig/cc_apt_update_upgrade.py b/cloudinit/CloudConfig/cc_apt_update_upgrade.py
new file mode 100644
index 00000000..ab2ece93
--- /dev/null
+++ b/cloudinit/CloudConfig/cc_apt_update_upgrade.py
@@ -0,0 +1,120 @@
+import cloudinit.util as util
+import subprocess
+import os
+
+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)
+
+ if not util.get_cfg_option_bool(cfg, \
+ 'apt_preserve_sources_list', False):
+ if cfg.has_key("apt_mirror"):
+ mirror = cfg["apt_mirror"]
+ else:
+ mirror = cloud.get_mirror()
+ generate_sources_list(mirror)
+ old_mir = util.get_cfg_option_str(cfg,'apt_old_mirror', \
+ "archive.ubuntu.com/ubuntu")
+ rename_apt_lists(old_mir, mirror)
+
+ # process 'apt_sources'
+ if cfg.has_key('apt_sources'):
+ errors = add_sources(cfg['apt_sources'])
+ for e in errors:
+ log.warn("Source Error: %s\n" % ':'.join(e))
+
+ pkglist = []
+ if 'packages' in cfg:
+ if isinstance(cfg['packages'],list):
+ pkglist = cfg['packages']
+ else: pkglist.append(cfg['packages'])
+
+ if update or upgrade or pkglist:
+ #retcode = subprocess.call(list)
+ subprocess.Popen(['apt-get', 'update']).communicate()
+
+ e=os.environ.copy()
+ e['DEBIAN_FRONTEND']='noninteractive'
+
+ if upgrade:
+ subprocess.Popen(['apt-get', 'upgrade', '--assume-yes'], env=e).communicate()
+
+ if pkglist:
+ cmd=['apt-get', 'install', '--assume-yes']
+ cmd.extend(pkglist)
+ subprocess.Popen(cmd, env=e).communicate()
+
+ return(True)
+
+def mirror2lists_fileprefix(mirror):
+ file=mirror
+ # take of http:// or ftp://
+ if file.endswith("/"): file=file[0:-1]
+ pos=file.find("://")
+ if pos >= 0:
+ file=file[pos+3:]
+ file=file.replace("/","_")
+ return file
+
+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 file in glob.glob("%s_*" % oprefix):
+ os.rename(file,"%s%s" % (nprefix, file[olen:]))
+
+def generate_sources_list(mirror):
+ stdout, stderr = subprocess.Popen(['lsb_release', '-cs'], stdout=subprocess.PIPE).communicate()
+ codename = stdout.strip()
+
+ util.render_to_file('sources.list', '/etc/apt/sources.list', \
+ { '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):
+ elst = []
+
+ for ent in srclist:
+ if not ent.has_key('source'):
+ elst.append([ "", "missing source" ])
+ continue
+
+ source=ent['source']
+ if source.startswith("ppa:"):
+ try: util.subp(["add-apt-repository",source])
+ except:
+ elst.append([source, "add-apt-repository failed"])
+ continue
+
+ if not ent.has_key('filename'):
+ 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') ):
+ ks = "keyserver.ubuntu.com"
+ if ent.has_key('keyserver'): ks = ent['keyserver']
+ try:
+ ent['key'] = util.getkeybyid(ent['keyid'], ks)
+ except:
+ elst.append([source,"failed to get key from %s" % ks])
+ continue
+
+ if ent.has_key('key'):
+ try: util.subp(('apt-key', 'add', '-'), ent['key'])
+ except:
+ elst.append([source, "failed add key"])
+
+ try: util.write_file(ent['filename'], source + "\n")
+ except:
+ elst.append([source, "failed write to file %s" % ent['filename']])
+
+ return(elst)
+
+