sailesh1993 / rpms / cloud-init

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