← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~raharper/cloud-init:pregen-locale into cloud-init:master

 

Ryan Harper has proposed merging ~raharper/cloud-init:pregen-locale into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~raharper/cloud-init/+git/cloud-init/+merge/325406

Check before attempting to regenerate locales

Avoid unnecessary IO incurred when generating locales by
testing if the system has already configured a LANG setting.
If a user supplied locale or the default locale defined in
in cloud-init differs from what is presently configured
then cloud-init will regenerate locales based on the supplied
value.
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~raharper/cloud-init:pregen-locale into cloud-init:master.
diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py
index d06d46a..51f3dca 100644
--- a/cloudinit/distros/debian.py
+++ b/cloudinit/distros/debian.py
@@ -37,11 +37,11 @@ ENI_HEADER = """# This file is generated from information provided by
 """
 
 NETWORK_CONF_FN = "/etc/network/interfaces.d/50-cloud-init.cfg"
+LOCALE_CONF_FN = "/etc/default/locale"
 
 
 class Distro(distros.Distro):
     hostname_conf_fn = "/etc/hostname"
-    locale_conf_fn = "/etc/default/locale"
     network_conf_fn = {
         "eni": "/etc/network/interfaces.d/50-cloud-init.cfg",
         "netplan": "/etc/netplan/50-cloud-init.yaml"
@@ -64,7 +64,12 @@ class Distro(distros.Distro):
 
     def apply_locale(self, locale, out_fn=None):
         if not out_fn:
-            out_fn = self.locale_conf_fn
+            out_fn = LOCALE_CONF_FN
+
+        if not _maybe_regen_locale(locale, path=out_fn):
+            LOG.debug('Skipping regen, locale already set to %s', locale)
+            return
+
         util.subp(['locale-gen', locale], capture=False)
         util.subp(['update-locale', locale], capture=False)
         # "" provides trailing newline during join
@@ -225,4 +230,39 @@ def _maybe_remove_legacy_eth0(path="/etc/network/interfaces.d/eth0.cfg"):
 
     LOG.warning(msg)
 
+
+def _maybe_regen_locale(locale, path=None):
+    """Check if we need to regenerate locale by checking in a specified
+       configuration file.  In the file, check if LANG=<locale> matches
+       the locale to be set value and if not LANG value from config
+       file is: not found, unset, or mismatch then cloud-init will regen.
+    """
+    if not locale:
+        raise ValueError('Failed to provide locale value.')
+
+    if not path:
+        path = LOCALE_CONF_FN
+
+    # locale config file not present, regen
+    if not os.path.exists(path):
+        return True
+
+    locale_content = util.load_file(path)
+    shell_content = [line for line in locale_content.splitlines()
+                     if not line.startswith("#")]
+    # LANG unset, regen
+    if len(shell_content) == 0:
+        return True
+
+    # if LANG isn't present, regen
+    current_locale = util.load_shell_content(locale_content).get('LANG', None)
+    if not current_locale:
+        return True
+
+    # locale mismatch, regen
+    if locale.lower() != current_locale.lower():
+        return True
+
+    return False
+
 # vi: ts=4 expandtab

Follow ups