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

Proposed by Ryan Harper
Status: Rejected
Rejected by: Scott Moser
Proposed branch: ~raharper/cloud-init:pregen-locale
Merge into: cloud-init:master
Diff against target: 71 lines (+42/-2)
1 file modified
cloudinit/distros/debian.py (+42/-2)
Reviewer Review Type Date Requested Status
Scott Moser Needs Fixing
Server Team CI bot continuous-integration Approve
Review via email: mp+325406@code.launchpad.net

Description of the change

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.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

I would like to update util.load_shell_content to support the comment char.
and then perhaps to just skip blank lines (or only white space lines) entirely

then i think this looks good.

Revision history for this message
Scott Moser (smoser) wrote :

And then please a unit test for this.

review: Needs Fixing
Revision history for this message
Scott Moser (smoser) wrote :

Ryan,
I added support for comment characters in load_shell_content at
 https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/327525

so go ahead and assume that works in trunk and update this MP.

Then, we still need a test for the Debian Distro for this feature.

Revision history for this message
Scott Moser (smoser) wrote :
Revision history for this message
Scott Moser (smoser) wrote :

I've marked this rejected as I think my merge proposal superseeds it.
please review mine listed above.

Unmerged commits

0f9ec26... by Ryan Harper

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.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py
2index d06d46a..51f3dca 100644
3--- a/cloudinit/distros/debian.py
4+++ b/cloudinit/distros/debian.py
5@@ -37,11 +37,11 @@ ENI_HEADER = """# This file is generated from information provided by
6 """
7
8 NETWORK_CONF_FN = "/etc/network/interfaces.d/50-cloud-init.cfg"
9+LOCALE_CONF_FN = "/etc/default/locale"
10
11
12 class Distro(distros.Distro):
13 hostname_conf_fn = "/etc/hostname"
14- locale_conf_fn = "/etc/default/locale"
15 network_conf_fn = {
16 "eni": "/etc/network/interfaces.d/50-cloud-init.cfg",
17 "netplan": "/etc/netplan/50-cloud-init.yaml"
18@@ -64,7 +64,12 @@ class Distro(distros.Distro):
19
20 def apply_locale(self, locale, out_fn=None):
21 if not out_fn:
22- out_fn = self.locale_conf_fn
23+ out_fn = LOCALE_CONF_FN
24+
25+ if not _maybe_regen_locale(locale, path=out_fn):
26+ LOG.debug('Skipping regen, locale already set to %s', locale)
27+ return
28+
29 util.subp(['locale-gen', locale], capture=False)
30 util.subp(['update-locale', locale], capture=False)
31 # "" provides trailing newline during join
32@@ -225,4 +230,39 @@ def _maybe_remove_legacy_eth0(path="/etc/network/interfaces.d/eth0.cfg"):
33
34 LOG.warning(msg)
35
36+
37+def _maybe_regen_locale(locale, path=None):
38+ """Check if we need to regenerate locale by checking in a specified
39+ configuration file. In the file, check if LANG=<locale> matches
40+ the locale to be set value and if not LANG value from config
41+ file is: not found, unset, or mismatch then cloud-init will regen.
42+ """
43+ if not locale:
44+ raise ValueError('Failed to provide locale value.')
45+
46+ if not path:
47+ path = LOCALE_CONF_FN
48+
49+ # locale config file not present, regen
50+ if not os.path.exists(path):
51+ return True
52+
53+ locale_content = util.load_file(path)
54+ shell_content = [line for line in locale_content.splitlines()
55+ if not line.startswith("#")]
56+ # LANG unset, regen
57+ if len(shell_content) == 0:
58+ return True
59+
60+ # if LANG isn't present, regen
61+ current_locale = util.load_shell_content(locale_content).get('LANG', None)
62+ if not current_locale:
63+ return True
64+
65+ # locale mismatch, regen
66+ if locale.lower() != current_locale.lower():
67+ return True
68+
69+ return False
70+
71 # vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches