diff options
author | Chad Smith <chad.smith@canonical.com> | 2018-03-14 23:38:07 -0600 |
---|---|---|
committer | Chad Smith <chad.smith@canonical.com> | 2018-03-14 23:38:07 -0600 |
commit | 133ad2cb327ad17b7b81319fac8f9f14577c04df (patch) | |
tree | ca14c7c958537e00f1c7ce93405eb77cee85b81e /cloudinit/util.py | |
parent | 76460b63f9c310c7de4e5f0c11d1525bedd277e1 (diff) | |
download | vyos-cloud-init-133ad2cb327ad17b7b81319fac8f9f14577c04df.tar.gz vyos-cloud-init-133ad2cb327ad17b7b81319fac8f9f14577c04df.zip |
set_hostname: When present in metadata, set it before network bringup.
When instance meta-data provides hostname information, run
cc_set_hostname in the init-local or init-net stage before network
comes up.
Prevent an initial DHCP request which leaks the stock cloud-image default
hostname before the meta-data provided hostname was processed.
A leaked cloud-image hostname adversely affects Dynamic DNS which
would reallocate 'ubuntu' hostname in DNS to every instance brought up by
cloud-init. These instances would only update DNS to the cloud-init
configured hostname upon DHCP lease renewal.
This branch extends the get_hostname methods in datasource, cloud and
util to limit results to metadata_only to avoid extra cost of querying
the distro for hostname information if metadata does not provide that
information.
LP: #1746455
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r-- | cloudinit/util.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index 083a8efe..4504f053 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1025,9 +1025,16 @@ def dos2unix(contents): return contents.replace('\r\n', '\n') -def get_hostname_fqdn(cfg, cloud): - # return the hostname and fqdn from 'cfg'. If not found in cfg, - # then fall back to data from cloud +def get_hostname_fqdn(cfg, cloud, metadata_only=False): + """Get hostname and fqdn from config if present and fallback to cloud. + + @param cfg: Dictionary of merged user-data configuration (from init.cfg). + @param cloud: Cloud instance from init.cloudify(). + @param metadata_only: Boolean, set True to only query cloud meta-data, + returning None if not present in meta-data. + @return: a Tuple of strings <hostname>, <fqdn>. Values can be none when + metadata_only is True and no cfg or metadata provides hostname info. + """ if "fqdn" in cfg: # user specified a fqdn. Default hostname then is based off that fqdn = cfg['fqdn'] @@ -1041,11 +1048,11 @@ def get_hostname_fqdn(cfg, cloud): else: # no fqdn set, get fqdn from cloud. # get hostname from cfg if available otherwise cloud - fqdn = cloud.get_hostname(fqdn=True) + fqdn = cloud.get_hostname(fqdn=True, metadata_only=metadata_only) if "hostname" in cfg: hostname = cfg['hostname'] else: - hostname = cloud.get_hostname() + hostname = cloud.get_hostname(metadata_only=metadata_only) return (hostname, fqdn) |