Skip to content

Commit

Permalink
feat(gentoo): Add compatibility for Gentoo with systemd (canonical#5918)
Browse files Browse the repository at this point in the history
  • Loading branch information
akhuettel authored and holmanb committed Feb 11, 2025
1 parent ef8e890 commit 2adc476
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
37 changes: 24 additions & 13 deletions cloudinit/distros/gentoo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright (C) 2014 Rackspace, US Inc.
# Copyright (C) 2016 Matthew Thode.
# Copyright (C) 2024 Andreas K. Huettel
#
# Author: Nate House <nathan.house@rackspace.com>
# Author: Matthew Thode <prometheanfire@gentoo.org>
# Author: Andreas K. Huettel <dilfridge@gentoo.org>
#
# This file is part of cloud-init. See LICENSE file for license information.

Expand All @@ -18,7 +20,6 @@

class Distro(distros.Distro):
locale_gen_fn = "/etc/locale.gen"
hostname_conf_fn = "/etc/conf.d/hostname"
default_locale = "en_US.UTF-8"

# C.UTF8 makes sense to generate, but is not selected
Expand All @@ -27,28 +28,31 @@ class Distro(distros.Distro):

def __init__(self, name, cfg, paths):
distros.Distro.__init__(self, name, cfg, paths)

if distros.uses_systemd():
self.hostname_conf_fn = "/etc/hostname"
else:
self.hostname_conf_fn = "/etc/conf.d/hostname"

# This will be used to restrict certain
# calls from repeatedly happening (when they
# should only happen say once per instance...)
self._runner = helpers.Runners(paths)
self.osfamily = "gentoo"
# Fix sshd restarts
cfg["ssh_svcname"] = "/etc/init.d/sshd"
if distros.uses_systemd():
LOG.error("Cloud-init does not support systemd with gentoo")
if not distros.uses_systemd():
# Fix sshd restarts (openrc-specific?)
cfg["ssh_svcname"] = "/etc/init.d/sshd"

def apply_locale(self, _, out_fn=None):
"""rc-only - not compatible with systemd
Locales need to be added to /etc/locale.gen and generated prior
"""Locales need to be added to /etc/locale.gen and generated prior
to selection. Default to en_US.UTF-8 for simplicity.
"""
util.write_file(self.locale_gen_fn, "\n".join(self.locales), mode=644)

# generate locales
subp.subp(["locale-gen"], capture=False)

# select locale
# select locale, works for both openrc and systemd
subp.subp(
["eselect", "locale", "set", self.default_locale], capture=False
)
Expand Down Expand Up @@ -77,10 +81,17 @@ def _write_hostname(self, hostname, filename):
if not conf:
conf = HostnameConf("")

# Many distro's format is the hostname by itself, and that is the
# way HostnameConf works but gentoo expects it to be in
# hostname="the-actual-hostname"
conf.set_hostname('hostname="%s"' % hostname)
if distros.uses_systemd():
# Gentoo uses the same format for /etc/hostname as everyone else-
# only the hostname by itself. Works for openrc and systemd, but
# openrc has its own config file and /etc/hostname is generated.
conf.set_hostname(hostname)
else:
# Openrc generates /etc/hostname from /etc/conf.d/hostname with the
# differing format
# hostname="the-actual-hostname"
conf.set_hostname('hostname="%s"' % hostname)

util.write_file(filename, str(conf), 0o644)

def _read_system_hostname(self):
Expand Down
34 changes: 24 additions & 10 deletions tests/unittests/distros/test_gentoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@

from cloudinit import atomic_helper, util
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase
from tests.unittests.helpers import CiTestCase, mock


class TestGentoo(CiTestCase):
def test_write_hostname(self):
def test_write_hostname(self, whatever=False):
distro = _get_distro("gentoo")
hostname = "myhostname"
hostfile = self.tmp_path("hostfile")
distro._write_hostname(hostname, hostfile)
self.assertEqual(
'hostname="myhostname"\n', util.load_text_file(hostfile)
)
if distro.uses_systemd():
self.assertEqual("myhostname\n", util.load_text_file(hostfile))
else:
self.assertEqual(
'hostname="myhostname"\n', util.load_text_file(hostfile)
)

def test_write_existing_hostname_with_comments(self):
def test_write_existing_hostname_with_comments(self, whatever=False):
distro = _get_distro("gentoo")
hostname = "myhostname"
contents = '#This is the hostname\nhostname="localhost"'
hostfile = self.tmp_path("hostfile")
atomic_helper.write_file(hostfile, contents, omode="w")
distro._write_hostname(hostname, hostfile)
self.assertEqual(
'#This is the hostname\nhostname="myhostname"\n',
util.load_text_file(hostfile),
)
if distro.uses_systemd():
self.assertEqual(
"#This is the hostname\nmyhostname\n",
util.load_text_file(hostfile),
)
else:
self.assertEqual(
'#This is the hostname\nhostname="myhostname"\n',
util.load_text_file(hostfile),
)


@mock.patch("cloudinit.distros.uses_systemd", return_value=False)
class TestGentooOpenRC(TestGentoo):
pass

0 comments on commit 2adc476

Please sign in to comment.