diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py index 84744eceffe..320f4ba1dc3 100644 --- a/cloudinit/distros/rhel.py +++ b/cloudinit/distros/rhel.py @@ -7,6 +7,7 @@ # Author: Joshua Harlow # # This file is part of cloud-init. See LICENSE file for license information. +import os from cloudinit import distros, helpers from cloudinit import log as logging @@ -57,11 +58,25 @@ def __init__(self, name, cfg, paths): # should only happen say once per instance...) self._runner = helpers.Runners(paths) self.osfamily = "redhat" + self.default_locale = "en_US.UTF-8" + self.system_locale = None cfg["ssh_svcname"] = "sshd" def install_packages(self, pkglist): self.package_command("install", pkgs=pkglist) + def get_locale(self): + """Return the default locale if set, else use system locale""" + + # read system locale value + if not self.system_locale: + self.system_locale = self._read_system_locale() + + # Return system_locale setting if valid, else use default locale + return ( + self.system_locale if self.system_locale else self.default_locale + ) + def apply_locale(self, locale, out_fn=None): if self.uses_systemd(): if not out_fn: @@ -75,6 +90,23 @@ def apply_locale(self, locale, out_fn=None): } rhel_util.update_sysconfig_file(out_fn, locale_cfg) + def _read_system_locale(self, keyname="LANG"): + """Read system default locale setting, if present""" + if self.uses_systemd(): + locale_fn = self.systemd_locale_conf_fn + else: + locale_fn = self.locale_conf_fn + + if not locale_fn: + raise ValueError("Invalid path: %s" % locale_fn) + + if os.path.exists(locale_fn): + (_exists, contents) = rhel_util.read_sysconfig_file(locale_fn) + if keyname in contents: + return contents[keyname] + else: + return None + def _write_hostname(self, hostname, filename): # systemd will never update previous-hostname for us, so # we need to do it ourselves diff --git a/tests/unittests/distros/test_generic.py b/tests/unittests/distros/test_generic.py index 93c5395c52e..fedc7300d38 100644 --- a/tests/unittests/distros/test_generic.py +++ b/tests/unittests/distros/test_generic.py @@ -187,12 +187,14 @@ def test_get_locale_ubuntu(self, m_locale): locale = d.get_locale() self.assertEqual("C.UTF-8", locale) - def test_get_locale_rhel(self): - """Test rhel distro returns NotImplementedError exception""" + @mock.patch("cloudinit.distros.rhel.Distro._read_system_locale") + def test_get_locale_rhel(self, m_locale): + """Test rhel distro returns locale set to C.UTF-8""" + m_locale.return_value = "C.UTF-8" cls = distros.fetch("rhel") d = cls("rhel", {}, None) - with self.assertRaises(NotImplementedError): - d.get_locale() + locale = d.get_locale() + self.assertEqual("C.UTF-8", locale) def test_expire_passwd_uses_chpasswd(self): """Test ubuntu.expire_passwd uses the passwd command.""" diff --git a/tools/.github-cla-signers b/tools/.github-cla-signers index a9ede0cb47d..55272218ce3 100644 --- a/tools/.github-cla-signers +++ b/tools/.github-cla-signers @@ -73,6 +73,7 @@ renanrodrigo rhansen riedel sarahwzadara +shi2wei3 slingamn slyon smoser