sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
c188f5
From 2e3f17d06a61f4fa05abe186b30e6aba7e23d1ba Mon Sep 17 00:00:00 2001
c188f5
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c188f5
Date: Fri, 10 Jun 2022 20:59:09 +0200
c188f5
Subject: [PATCH] Honor system locale for RHEL (#1355)
c188f5
c188f5
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c188f5
RH-MergeRequest: 78: Honor system locale for RHEL (#1355)
c188f5
RH-Commit: [1/1] cf4e8ebe6828a05ec135cfbd58e54418aa28a02d (eesposit/cloud-init)
c188f5
RH-Bugzilla: 2096196
c188f5
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
c188f5
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
c188f5
c188f5
commit 58da7d856274e9ca2b507128d6f186e0e6abfe06
c188f5
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c188f5
Date:   Fri Jun 10 20:57:32 2022 +0200
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
Conflicts:
c188f5
	cloudinit/distros/rhel.py: single quotes instead of double, and
c188f5
	_write_hostname also has out_fn parameter instead of filename
c188f5
	.github-cla-signers: names from other people not yet present
c188f5
c188f5
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c188f5
---
c188f5
 cloudinit/distros/rhel.py                    | 32 ++++++++++++++++++++
c188f5
 tests/unittests/test_distros/test_generic.py | 10 +++---
c188f5
 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
c188f5
index c72f7c17..9fbfc3ce 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
 
c188f5
 from cloudinit import distros
c188f5
 from cloudinit import helpers
c188f5
@@ -57,6 +58,8 @@ class Distro(distros.Distro):
c188f5
         # should only happen say once per instance...)
c188f5
         self._runner = helpers.Runners(paths)
c188f5
         self.osfamily = 'redhat'
c188f5
+        self.default_locale = "en_US.UTF-8"
c188f5
+        self.system_locale = None
c188f5
         cfg['ssh_svcname'] = 'sshd'
c188f5
 
c188f5
     def install_packages(self, pkglist):
c188f5
@@ -65,6 +68,18 @@ class Distro(distros.Distro):
c188f5
     def _write_network_config(self, netconfig):
c188f5
         return self._supported_write_network_config(netconfig)
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:
c188f5
@@ -78,6 +93,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
+
c188f5
     def _write_hostname(self, hostname, out_fn):
c188f5
         # systemd will never update previous-hostname for us, so
c188f5
         # we need to do it ourselves
c188f5
diff --git a/tests/unittests/test_distros/test_generic.py b/tests/unittests/test_distros/test_generic.py
c188f5
index 336150bc..2c3cdc59 100644
c188f5
--- a/tests/unittests/test_distros/test_generic.py
c188f5
+++ b/tests/unittests/test_distros/test_generic.py
c188f5
@@ -174,12 +174,14 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase):
c188f5
         locale = d.get_locale()
c188f5
         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
c188f5
index cbfa883c..c58761d4 100644
c188f5
--- a/tools/.github-cla-signers
c188f5
+++ b/tools/.github-cla-signers
c188f5
@@ -34,6 +34,7 @@ omBratteng
c188f5
 onitake
c188f5
 qubidt
c188f5
 riedel
c188f5
+shi2wei3
c188f5
 slyon
c188f5
 smoser
c188f5
 sshedi
c188f5
-- 
c188f5
2.31.1
c188f5