59e311
From 53e3f8ab9008fec8400f96918c2129f7defe6a70 Mon Sep 17 00:00:00 2001
c188f5
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
59e311
Date: Fri, 10 Jun 2022 20:51:55 +0200
59e311
Subject: [PATCH 1/3] Honor system locale for RHEL (#1355)
c188f5
c188f5
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
59e311
RH-MergeRequest: 29: Honor system locale for RHEL (#1355)
59e311
RH-Commit: [1/1] d571126fe6add8dc34a22c869d4e1a07a7373d8d (eesposit/cloud-init-centos-)
59e311
RH-Bugzilla: 2061604
c188f5
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
59e311
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
c188f5
c188f5
commit 58da7d856274e9ca2b507128d6f186e0e6abfe06
59e311
Author: Wei Shi <wshi@redhat.com>
59e311
Date:   Wed Mar 30 23:55:30 2022 +0800
c188f5
c188f5
    Honor system locale for RHEL (#1355)
c188f5
c188f5
    Make sure to use system locale as default on RHEL if locale is not
c188f5
    set in cloud-config.
c188f5
c188f5
    RHEL has a pre-installed cloud image using C.UTF-8 for system locale
c188f5
    just like ubuntu-minimal cloud image, without this patch, locale
c188f5
    module will set it to en_US.UTF-8 from ds default value during config
c188f5
    stage.
c188f5
c188f5
    Authored-by: Wei Shi <shi2wei3@hotmail.com>
c188f5
c188f5
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c188f5
---
59e311
 cloudinit/distros/rhel.py               | 32 +++++++++++++++++++++++++
59e311
 tests/unittests/distros/test_generic.py | 10 ++++----
59e311
 tools/.github-cla-signers               |  1 +
c188f5
 3 files changed, 39 insertions(+), 4 deletions(-)
c188f5
c188f5
diff --git a/cloudinit/distros/rhel.py b/cloudinit/distros/rhel.py
59e311
index 84744ece..320f4ba1 100644
c188f5
--- a/cloudinit/distros/rhel.py
c188f5
+++ b/cloudinit/distros/rhel.py
c188f5
@@ -7,6 +7,7 @@
c188f5
 # Author: Joshua Harlow <harlowja@yahoo-inc.com>
c188f5
 #
c188f5
 # This file is part of cloud-init. See LICENSE file for license information.
c188f5
+import os
c188f5
 
59e311
 from cloudinit import distros, helpers
59e311
 from cloudinit import log as logging
59e311
@@ -57,11 +58,25 @@ class Distro(distros.Distro):
c188f5
         # should only happen say once per instance...)
c188f5
         self._runner = helpers.Runners(paths)
59e311
         self.osfamily = "redhat"
c188f5
+        self.default_locale = "en_US.UTF-8"
c188f5
+        self.system_locale = None
59e311
         cfg["ssh_svcname"] = "sshd"
c188f5
 
c188f5
     def install_packages(self, pkglist):
59e311
         self.package_command("install", pkgs=pkglist)
c188f5
 
c188f5
+    def get_locale(self):
c188f5
+        """Return the default locale if set, else use system locale"""
c188f5
+
c188f5
+        # read system locale value
c188f5
+        if not self.system_locale:
c188f5
+            self.system_locale = self._read_system_locale()
c188f5
+
c188f5
+        # Return system_locale setting if valid, else use default locale
c188f5
+        return (
c188f5
+            self.system_locale if self.system_locale else self.default_locale
c188f5
+        )
c188f5
+
c188f5
     def apply_locale(self, locale, out_fn=None):
c188f5
         if self.uses_systemd():
c188f5
             if not out_fn:
59e311
@@ -75,6 +90,23 @@ class Distro(distros.Distro):
c188f5
         }
c188f5
         rhel_util.update_sysconfig_file(out_fn, locale_cfg)
c188f5
 
c188f5
+    def _read_system_locale(self, keyname="LANG"):
c188f5
+        """Read system default locale setting, if present"""
c188f5
+        if self.uses_systemd():
c188f5
+            locale_fn = self.systemd_locale_conf_fn
c188f5
+        else:
c188f5
+            locale_fn = self.locale_conf_fn
c188f5
+
c188f5
+        if not locale_fn:
c188f5
+            raise ValueError("Invalid path: %s" % locale_fn)
c188f5
+
c188f5
+        if os.path.exists(locale_fn):
c188f5
+            (_exists, contents) = rhel_util.read_sysconfig_file(locale_fn)
c188f5
+            if keyname in contents:
c188f5
+                return contents[keyname]
c188f5
+            else:
c188f5
+                return None
c188f5
+
59e311
     def _write_hostname(self, hostname, filename):
c188f5
         # systemd will never update previous-hostname for us, so
c188f5
         # we need to do it ourselves
59e311
diff --git a/tests/unittests/distros/test_generic.py b/tests/unittests/distros/test_generic.py
59e311
index 93c5395c..fedc7300 100644
59e311
--- a/tests/unittests/distros/test_generic.py
59e311
+++ b/tests/unittests/distros/test_generic.py
59e311
@@ -187,12 +187,14 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase):
c188f5
         locale = d.get_locale()
59e311
         self.assertEqual("C.UTF-8", locale)
c188f5
 
c188f5
-    def test_get_locale_rhel(self):
c188f5
-        """Test rhel distro returns NotImplementedError exception"""
c188f5
+    @mock.patch("cloudinit.distros.rhel.Distro._read_system_locale")
c188f5
+    def test_get_locale_rhel(self, m_locale):
c188f5
+        """Test rhel distro returns locale set to C.UTF-8"""
c188f5
+        m_locale.return_value = "C.UTF-8"
c188f5
         cls = distros.fetch("rhel")
c188f5
         d = cls("rhel", {}, None)
c188f5
-        with self.assertRaises(NotImplementedError):
c188f5
-            d.get_locale()
c188f5
+        locale = d.get_locale()
c188f5
+        self.assertEqual("C.UTF-8", locale)
c188f5
 
c188f5
     def test_expire_passwd_uses_chpasswd(self):
c188f5
         """Test ubuntu.expire_passwd uses the passwd command."""
c188f5
diff --git a/tools/.github-cla-signers b/tools/.github-cla-signers
59e311
index 9f71ea0c..9eb2ae38 100644
c188f5
--- a/tools/.github-cla-signers
c188f5
+++ b/tools/.github-cla-signers
59e311
@@ -70,6 +70,7 @@ renanrodrigo
59e311
 rhansen
c188f5
 riedel
59e311
 sarahwzadara
c188f5
+shi2wei3
59e311
 slingamn
c188f5
 slyon
c188f5
 smoser
c188f5
-- 
59e311
2.35.1
c188f5