Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for create_hostname_file key before writing /etc/hostname #4330

Merged
merged 9 commits into from
Sep 12, 2023
7 changes: 7 additions & 0 deletions cloudinit/config/cc_set_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
if hostname_fqdn is not None:
cloud.distro.set_option("prefer_fqdn_over_hostname", hostname_fqdn)

# Set create_hostname_file in distro
create_hostname_file = util.get_cfg_option_bool(
cfg, "create_hostname_file", None
)
if create_hostname_file is not None:
cloud.distro.set_option("create_hostname_file", create_hostname_file)

(hostname, fqdn, is_default) = util.get_hostname_fqdn(cfg, cloud)
# Check for previous successful invocation of set_hostname

Expand Down
7 changes: 7 additions & 0 deletions cloudinit/config/cc_update_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
if hostname_fqdn is not None:
cloud.distro.set_option("prefer_fqdn_over_hostname", hostname_fqdn)

# Set create_hostname_file in distro
create_hostname_file = util.get_cfg_option_bool(
cfg, "create_hostname_file", None
)
if create_hostname_file is not None:
cloud.distro.set_option("create_hostname_file", create_hostname_file)

(hostname, fqdn, is_default) = util.get_hostname_fqdn(cfg, cloud)
if is_default and hostname == "localhost":
# https://github.com/systemd/systemd/commit/d39079fcaa05e23540d2b1f0270fa31c22a7e9f1
Expand Down
8 changes: 7 additions & 1 deletion cloudinit/distros/alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ def _write_hostname(self, hostname, filename):
# so lets see if we can read it first.
conf = self._read_hostname_conf(filename)
except IOError:
pass
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
pass
else:
return
if not conf:
conf = HostnameConf("")
conf.set_hostname(hostname)
Expand Down
8 changes: 7 additions & 1 deletion cloudinit/distros/arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ def _write_hostname(self, hostname, filename):
# so lets see if we can read it first.
conf = self._read_hostname_conf(filename)
except IOError:
pass
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
pass
else:
return
if not conf:
conf = HostnameConf("")
conf.set_hostname(hostname)
Expand Down
8 changes: 7 additions & 1 deletion cloudinit/distros/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ def _write_hostname(self, hostname, filename):
# so lets see if we can read it first.
conf = self._read_hostname_conf(filename)
except IOError:
pass
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
pass
else:
return
if not conf:
conf = HostnameConf("")
conf.set_hostname(hostname)
Expand Down
8 changes: 7 additions & 1 deletion cloudinit/distros/gentoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ def _write_hostname(self, hostname, filename):
# so lets see if we can read it first.
conf = self._read_hostname_conf(filename)
except IOError:
pass
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
pass
else:
return
if not conf:
conf = HostnameConf("")

Expand Down
20 changes: 18 additions & 2 deletions cloudinit/distros/opensuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,34 @@ def _set_update_method(self):
self.update_method = "zypper"

def _write_hostname(self, hostname, filename):
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file"
aciba90 marked this conversation as resolved.
Show resolved Hide resolved
)
if self.uses_systemd() and filename.endswith("/previous-hostname"):
util.write_file(filename, hostname)
elif self.uses_systemd():
subp.subp(["hostnamectl", "set-hostname", str(hostname)])
if not create_hostname_file:
aciba90 marked this conversation as resolved.
Show resolved Hide resolved
subp.subp(["hostnamectl", "set-hostname", str(hostname)])
else:
subp.subp(
[
"hostnamectl",
"set-hostname",
"--transient",
str(hostname),
]
)
else:
conf = None
try:
# Try to update the previous one
# so lets see if we can read it first.
conf = self._read_hostname_conf(filename)
except IOError:
pass
if not create_hostname_file:
pass
else:
return
if not conf:
conf = HostnameConf("")
conf.set_hostname(hostname)
Expand Down
18 changes: 16 additions & 2 deletions cloudinit/distros/photon.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,23 @@ def _write_hostname(self, hostname, filename):
if filename and filename.endswith("/previous-hostname"):
util.write_file(filename, hostname)
else:
ret, _out, err = self.exec_cmd(
["hostnamectl", "set-hostname", str(hostname)]
ret = None
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
ret, _out, err = self.exec_cmd(
["hostnamectl", "set-hostname", str(hostname)]
)
else:
ret, _out, err = self.exec_cmd(
[
"hostnamectl",
"set-hostname",
"--transient",
str(hostname),
]
)
if ret:
LOG.warning(
(
Expand Down
15 changes: 14 additions & 1 deletion cloudinit/distros/rhel.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ def _write_hostname(self, hostname, filename):
conf.set_hostname(hostname)
util.write_file(filename, str(conf), 0o644)
elif self.uses_systemd():
subp.subp(["hostnamectl", "set-hostname", str(hostname)])
create_hostname_file = util.get_cfg_option_bool(
self._cfg, "create_hostname_file", True
)
if create_hostname_file:
subp.subp(["hostnamectl", "set-hostname", str(hostname)])
else:
subp.subp(
[
"hostnamectl",
"set-hostname",
"--transient",
str(hostname),
]
)
else:
host_cfg = {
"HOSTNAME": hostname,
Expand Down
90 changes: 90 additions & 0 deletions tests/unittests/config/test_cc_set_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,95 @@ def test_ignore_empty_previous_artifact_file(self):
contents = util.load_file("/etc/hostname")
self.assertEqual("blah", contents.strip())

def test_create_hostname_file_false(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("debian")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")

def test_create_hostname_file_false_arch(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("arch")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")

def test_create_hostname_file_false_alpine(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("alpine")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")

def test_create_hostname_file_false_gentoo(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("gentoo")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")

def test_create_hostname_file_false_photon(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("photon")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")

def test_create_hostname_file_false_rhel(self):
cfg = {
"hostname": "foo",
"fqdn": "foo.blah.yahoo.com",
"create_hostname_file": False,
}
distro = self._fetch_distro("rhel")
paths = helpers.Paths({"cloud_dir": self.tmp})
ds = None
cc = cloud.Cloud(ds, paths, {}, distro, None)
self.patchUtils(self.tmp)
cc_set_hostname.handle("cc_set_hostname", cfg, cc, [])
with self.assertRaises(FileNotFoundError):
util.load_file("/etc/hostname")


# vi: ts=4 expandtab
1 change: 1 addition & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bmhughes
brianphaley
CalvoM
candlerb
catmsred
cawamata
cclauss
chifac08
Expand Down
Loading