Blob Blame History Raw
From 2e3f17d06a61f4fa05abe186b30e6aba7e23d1ba Mon Sep 17 00:00:00 2001
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Date: Fri, 10 Jun 2022 20:59:09 +0200
Subject: [PATCH] Honor system locale for RHEL (#1355)

RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
RH-MergeRequest: 78: Honor system locale for RHEL (#1355)
RH-Commit: [1/1] cf4e8ebe6828a05ec135cfbd58e54418aa28a02d (eesposit/cloud-init)
RH-Bugzilla: 2096196
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>

commit 58da7d856274e9ca2b507128d6f186e0e6abfe06
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Date:   Fri Jun 10 20:57:32 2022 +0200

    Honor system locale for RHEL (#1355)

    Make sure to use system locale as default on RHEL if locale is not
    set in cloud-config.

    RHEL has a pre-installed cloud image using C.UTF-8 for system locale
    just like ubuntu-minimal cloud image, without this patch, locale
    module will set it to en_US.UTF-8 from ds default value during config
    stage.

    Authored-by: Wei Shi <shi2wei3@hotmail.com>

Conflicts:
	cloudinit/distros/rhel.py: single quotes instead of double, and
	_write_hostname also has out_fn parameter instead of filename
	.github-cla-signers: names from other people not yet present

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 cloudinit/distros/rhel.py                    | 32 ++++++++++++++++++++
 tests/unittests/test_distros/test_generic.py | 10 +++---
 tools/.github-cla-signers                    |  1 +
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py
index c72f7c17..9fbfc3ce 100644
--- a/cloudinit/distros/rhel.py
+++ b/cloudinit/distros/rhel.py
@@ -7,6 +7,7 @@
 # Author: Joshua Harlow <harlowja@yahoo-inc.com>
 #
 # This file is part of cloud-init. See LICENSE file for license information.
+import os
 
 from cloudinit import distros
 from cloudinit import helpers
@@ -57,6 +58,8 @@ class Distro(distros.Distro):
         # 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):
@@ -65,6 +68,18 @@ class Distro(distros.Distro):
     def _write_network_config(self, netconfig):
         return self._supported_write_network_config(netconfig)
 
+    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:
@@ -78,6 +93,23 @@ class Distro(distros.Distro):
         }
         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, out_fn):
         # systemd will never update previous-hostname for us, so
         # we need to do it ourselves
diff --git a/tests/unittests/test_distros/test_generic.py b/tests/unittests/test_distros/test_generic.py
index 336150bc..2c3cdc59 100644
--- a/tests/unittests/test_distros/test_generic.py
+++ b/tests/unittests/test_distros/test_generic.py
@@ -174,12 +174,14 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase):
         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 cbfa883c..c58761d4 100644
--- a/tools/.github-cla-signers
+++ b/tools/.github-cla-signers
@@ -34,6 +34,7 @@ omBratteng
 onitake
 qubidt
 riedel
+shi2wei3
 slyon
 smoser
 sshedi
-- 
2.31.1