diff options
author | Scott Moser <smoser@ubuntu.com> | 2011-12-16 16:42:58 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2011-12-16 16:42:58 -0500 |
commit | 5c84e5972c4e363da1b3b874d7e5af5b1cd2221b (patch) | |
tree | 0f181b03c8e81e5ac070c187d36f3472b4249844 /cloudinit/CloudConfig | |
parent | 1f292d297e368a8dce0122736aa2d140a275caba (diff) | |
parent | 32e3e2f3d637051fb79f63f791270eeb05bc721b (diff) | |
download | vyos-cloud-init-5c84e5972c4e363da1b3b874d7e5af5b1cd2221b.tar.gz vyos-cloud-init-5c84e5972c4e363da1b3b874d7e5af5b1cd2221b.zip |
add support for reading configuration of mirror and proxy
This adds support for configuration of a mirror by looking
for dns names like 'ubuntu-mirror', and setting a
'apt_mirror_search' configuration variable to find the first
mirror in the list.
Also, allows configuration of an apt proxy via 'apt_proxy'
in cloud-config.
LP: #897688
Diffstat (limited to 'cloudinit/CloudConfig')
-rw-r--r-- | cloudinit/CloudConfig/cc_apt_update_upgrade.py | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/cloudinit/CloudConfig/cc_apt_update_upgrade.py b/cloudinit/CloudConfig/cc_apt_update_upgrade.py index 495b8522..a60512e1 100644 --- a/cloudinit/CloudConfig/cc_apt_update_upgrade.py +++ b/cloudinit/CloudConfig/cc_apt_update_upgrade.py @@ -27,10 +27,10 @@ def handle(name,cfg,cloud,log,args): upgrade = util.get_cfg_option_bool(cfg, 'apt_upgrade', False) release = get_release() - if cfg.has_key("apt_mirror"): - mirror = cfg["apt_mirror"] - else: - mirror = cloud.get_mirror() + + mirror = find_apt_mirror(cloud, cfg) + + log.debug("selected mirror at: %s" % mirror) if not util.get_cfg_option_bool(cfg, \ 'apt_preserve_sources_list', False): @@ -39,6 +39,20 @@ def handle(name,cfg,cloud,log,args): "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: + fp.write(contents % proxy) + except Exception as e: + log.warn("Failed to write proxy to %s" % proxy_filename) + elif os.path.isfile(proxy_filename): + os.unlink(proxy_filename) + # process 'apt_sources' if cfg.has_key('apt_sources'): errors = add_sources(cfg['apt_sources'], @@ -162,3 +176,47 @@ def add_sources(srclist, searchList={ }): return(elst) +def find_apt_mirror(cloud, cfg): + """ find an apt_mirror given the cloud and cfg provided """ + + # TODO: distro and defaults should be configurable + distro = "ubuntu" + defaults = { + 'ubuntu': "http://archive.ubuntu.com/ubuntu", + 'debian': "http://archive.debian.org/debian", + } + mirror = None + + cfg_mirror = cfg.get("apt_mirror",None) + if cfg_mirror: + mirror = cfg["apt_mirror"] + elif cfg.has_key("apt_mirror_search"): + mirror = util.search_for_mirror(cfg['apt_mirror_search']) + else: + if cloud: + mirror = cloud.get_mirror() + + mydom = "" + + doms = [] + + if not mirror and cloud: + # if we have a fqdn, then search its domain portion first + ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) + mydom = ".".join(fqdn.split(".")[1:]) + if mydom: + doms.append(".%s" % mydom) + + doms.extend((".localdomain", "",)) + + mirror_list = [] + mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro ) + for post in doms: + mirror_list.append(mirrorfmt % post) + + mirror = util.search_for_mirror(mirror_list) + + if not mirror: + mirror = defaults[distro] + + return mirror |