From bd0c29c2f298ad060bba88ddbb4d2d11ab07cafe Mon Sep 17 00:00:00 2001 From: Ben Howard Date: Wed, 14 Jan 2015 07:29:57 -0700 Subject: Use the short name for GCE hostnames per GCE's request (LP: #1383794). --- cloudinit/sources/DataSourceGCE.py | 3 ++- tests/unittests/test_datasource/test_gce.py | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index 92e5a28e..f877b3d1 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -124,7 +124,8 @@ class DataSourceGCE(sources.DataSource): return self.metadata['public-keys'] def get_hostname(self, fqdn=False, _resolve_ip=False): - return self.metadata['local-hostname'] + # GCE has long FDQN's and has asked for short hostnames + return self.metadata['local-hostname'].split('.')[0] def get_userdata_raw(self): return self.metadata['user-data'] diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py index 89251b0e..06050bb1 100644 --- a/tests/unittests/test_datasource/test_gce.py +++ b/tests/unittests/test_datasource/test_gce.py @@ -99,7 +99,8 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase): body=_new_request_callback()) self.ds.get_data() - self.assertEqual(GCE_META.get('instance/hostname'), + shostname = GCE_META.get('instance/hostname').split('.')[0] + self.assertEqual(shostname, self.ds.get_hostname()) self.assertEqual(GCE_META.get('instance/id'), @@ -126,8 +127,8 @@ class TestDataSourceGCE(test_helpers.HttprettyTestCase): self.assertEqual(GCE_META_PARTIAL.get('instance/id'), self.ds.get_instance_id()) - self.assertEqual(GCE_META_PARTIAL.get('instance/hostname'), - self.ds.get_hostname()) + shostname = GCE_META_PARTIAL.get('instance/hostname').split('.')[0] + self.assertEqual(shostname, self.ds.get_hostname()) @httpretty.activate def test_metadata_encoding(self): -- cgit v1.2.3 From 72d4be8b5d8650f1f3c1c32f2a728a22c7f6ad1f Mon Sep 17 00:00:00 2001 From: Steve Langasek Date: Thu, 15 Jan 2015 09:14:34 -0800 Subject: Ensure that when running under systemd, cloud-init.service is run before we display any getty on the console so that the cloud-init output doesn't get written on top of a login prompt. --- systemd/cloud-init.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systemd/cloud-init.service b/systemd/cloud-init.service index 6b0c7229..64471cdf 100644 --- a/systemd/cloud-init.service +++ b/systemd/cloud-init.service @@ -1,7 +1,7 @@ [Unit] Description=Initial cloud-init job (metadata service crawler) After=local-fs.target network.target cloud-init-local.service -Before=sshd.service sshd-keygen.service +Before=sshd.service sshd-keygen.service getty.target Requires=network.target Wants=local-fs.target cloud-init-local.service sshd.service sshd-keygen.service -- cgit v1.2.3 From df288bf65a436f42509b10b604a1e169c44c49e6 Mon Sep 17 00:00:00 2001 From: Steve Langasek Date: Fri, 16 Jan 2015 08:36:30 -0800 Subject: Use systemd-user-sessions.service for sequencing, not getty.target, for correct race-free ordering --- systemd/cloud-init.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systemd/cloud-init.service b/systemd/cloud-init.service index 64471cdf..398b90ea 100644 --- a/systemd/cloud-init.service +++ b/systemd/cloud-init.service @@ -1,7 +1,7 @@ [Unit] Description=Initial cloud-init job (metadata service crawler) After=local-fs.target network.target cloud-init-local.service -Before=sshd.service sshd-keygen.service getty.target +Before=sshd.service sshd-keygen.service systemd-user-sessions.service Requires=network.target Wants=local-fs.target cloud-init-local.service sshd.service sshd-keygen.service -- cgit v1.2.3 From ae2f5123faab45e71e4dcf3237d7dac59a4c66b7 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 16 Jan 2015 13:21:08 -0500 Subject: pep8 fixes --- cloudinit/sources/DataSourceGCE.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index f877b3d1..2cf8fdcd 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -29,6 +29,7 @@ BUILTIN_DS_CONFIG = { } REQUIRED_FIELDS = ('instance-id', 'availability-zone', 'local-hostname') + class DataSourceGCE(sources.DataSource): def __init__(self, sys_cfg, distro, paths): sources.DataSource.__init__(self, sys_cfg, distro, paths) @@ -59,7 +60,8 @@ class DataSourceGCE(sources.DataSource): ('local-hostname', 'instance/hostname', True), ('public-keys', 'project/attributes/sshKeys', False), ('user-data', 'instance/attributes/user-data', False), - ('user-data-encoding', 'instance/attributes/user-data-encoding', False), + ('user-data-encoding', 'instance/attributes/user-data-encoding', + False), ] # if we cannot resolve the metadata server, then no point in trying @@ -106,7 +108,8 @@ class DataSourceGCE(sources.DataSource): encoding = self.metadata.get('user-data-encoding') if encoding: if encoding == 'base64': - self.metadata['user-data'] = b64decode(self.metadata['user-data']) + self.metadata['user-data'] = b64decode( + self.metadata['user-data']) else: LOG.warn('unknown user-data-encoding: %s, ignoring', encoding) -- cgit v1.2.3 From 8d453d2a4da4492857a4487b14fe7b11a014115b Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 16 Jan 2015 14:29:48 -0500 Subject: hostname: apply hostname same as is written on RHEL, we were writing to persistent configuration the fqdn, but invoking 'hostname' on the first boot with just the shortname. On 'reboot', then the hostname would differ. Now, whatever we write, invoke hostname with. Also remove some duplicate code. LP: #1246485 --- ChangeLog | 2 ++ cloudinit/distros/__init__.py | 9 ++++++--- cloudinit/distros/arch.py | 7 ------- cloudinit/distros/debian.py | 7 ------- cloudinit/distros/freebsd.py | 5 ----- cloudinit/distros/gentoo.py | 7 ------- cloudinit/distros/sles.py | 7 ------- 7 files changed, 8 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index da2a5a0c..ddde383a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,8 @@ - GCE: Allow base64 encoded user-data (LP: #1404311) [Wayne Witzell III] - GCE: use short hostname rather than fqdn (LP: #1383794) [Ben Howard] - systemd: make init stage run before login prompts shown [Steve Langasek] + - hostname: on first boot apply hostname to be same as is written for + persistent hostname. (LP: #1246485) 0.7.6: - open 0.7.6 - Enable vendordata on CloudSigma datasource (LP: #1303986) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index bf465442..5eab780b 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -86,7 +86,7 @@ class Distro(object): def set_hostname(self, hostname, fqdn=None): writeable_hostname = self._select_hostname(hostname, fqdn) self._write_hostname(writeable_hostname, self.hostname_conf_fn) - self._apply_hostname(hostname) + self._apply_hostname(writeable_hostname) @abc.abstractmethod def package_command(self, cmd, args=None, pkgs=None): @@ -160,9 +160,12 @@ class Distro(object): util.logexc(LOG, "Failed to non-persistently adjust the system " "hostname to %s", hostname) - @abc.abstractmethod def _select_hostname(self, hostname, fqdn): - raise NotImplementedError() + # Prefer the short hostname over the long + # fully qualified domain name + if not hostname: + return fqdn + return hostname @staticmethod def expand_osfamily(family_list): diff --git a/cloudinit/distros/arch.py b/cloudinit/distros/arch.py index 005a0dd4..68bf1aab 100644 --- a/cloudinit/distros/arch.py +++ b/cloudinit/distros/arch.py @@ -118,13 +118,6 @@ class Distro(distros.Distro): return False return True - def _select_hostname(self, hostname, fqdn): - # Prefer the short hostname over the long - # fully qualified domain name - if not hostname: - return fqdn - return hostname - def _write_hostname(self, your_hostname, out_fn): conf = None try: diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 010be67d..b09eb094 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -86,13 +86,6 @@ class Distro(distros.Distro): else: return distros.Distro._bring_up_interfaces(self, device_names) - def _select_hostname(self, hostname, fqdn): - # Prefer the short hostname over the long - # fully qualified domain name - if not hostname: - return fqdn - return hostname - def _write_hostname(self, your_hostname, out_fn): conf = None try: diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 9216510e..f1b4a256 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -150,11 +150,6 @@ class Distro(distros.Distro): return default return hostname - def _select_hostname(self, hostname, fqdn): - if not hostname: - return fqdn - return hostname - def _write_hostname(self, hostname, filename): self.updatercconf('hostname', hostname) diff --git a/cloudinit/distros/gentoo.py b/cloudinit/distros/gentoo.py index 45c2e658..09dd0d73 100644 --- a/cloudinit/distros/gentoo.py +++ b/cloudinit/distros/gentoo.py @@ -97,13 +97,6 @@ class Distro(distros.Distro): else: return distros.Distro._bring_up_interfaces(self, device_names) - def _select_hostname(self, hostname, fqdn): - # Prefer the short hostname over the long - # fully qualified domain name - if not hostname: - return fqdn - return hostname - def _write_hostname(self, your_hostname, out_fn): conf = None try: diff --git a/cloudinit/distros/sles.py b/cloudinit/distros/sles.py index 9788a1ba..43682a12 100644 --- a/cloudinit/distros/sles.py +++ b/cloudinit/distros/sles.py @@ -115,13 +115,6 @@ class Distro(distros.Distro): conf.set_hostname(hostname) util.write_file(out_fn, str(conf), 0644) - def _select_hostname(self, hostname, fqdn): - # Prefer the short hostname over the long - # fully qualified domain name - if not hostname: - return fqdn - return hostname - def _read_system_hostname(self): host_fn = self.hostname_conf_fn return (host_fn, self._read_hostname(host_fn)) -- cgit v1.2.3