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
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
37"""37"""
3838
39NETWORK_CONF_FN = "/etc/network/interfaces.d/50-cloud-init.cfg"39NETWORK_CONF_FN = "/etc/network/interfaces.d/50-cloud-init.cfg"
40LOCALE_CONF_FN = "/etc/default/locale"
4041
4142
42class Distro(distros.Distro):43class Distro(distros.Distro):
43 hostname_conf_fn = "/etc/hostname"44 hostname_conf_fn = "/etc/hostname"
44 locale_conf_fn = "/etc/default/locale"
45 network_conf_fn = {45 network_conf_fn = {
46 "eni": "/etc/network/interfaces.d/50-cloud-init.cfg",46 "eni": "/etc/network/interfaces.d/50-cloud-init.cfg",
47 "netplan": "/etc/netplan/50-cloud-init.yaml"47 "netplan": "/etc/netplan/50-cloud-init.yaml"
@@ -64,7 +64,12 @@ class Distro(distros.Distro):
6464
65 def apply_locale(self, locale, out_fn=None):65 def apply_locale(self, locale, out_fn=None):
66 if not out_fn:66 if not out_fn:
67 out_fn = self.locale_conf_fn67 out_fn = LOCALE_CONF_FN
68
69 if not _maybe_regen_locale(locale, path=out_fn):
70 LOG.debug('Skipping regen, locale already set to %s', locale)
71 return
72
68 util.subp(['locale-gen', locale], capture=False)73 util.subp(['locale-gen', locale], capture=False)
69 util.subp(['update-locale', locale], capture=False)74 util.subp(['update-locale', locale], capture=False)
70 # "" provides trailing newline during join75 # "" provides trailing newline during join
@@ -225,4 +230,39 @@ def _maybe_remove_legacy_eth0(path="/etc/network/interfaces.d/eth0.cfg"):
225230
226 LOG.warning(msg)231 LOG.warning(msg)
227232
233
234def _maybe_regen_locale(locale, path=None):
235 """Check if we need to regenerate locale by checking in a specified
236 configuration file. In the file, check if LANG=<locale> matches
237 the locale to be set value and if not LANG value from config
238 file is: not found, unset, or mismatch then cloud-init will regen.
239 """
240 if not locale:
241 raise ValueError('Failed to provide locale value.')
242
243 if not path:
244 path = LOCALE_CONF_FN
245
246 # locale config file not present, regen
247 if not os.path.exists(path):
248 return True
249
250 locale_content = util.load_file(path)
251 shell_content = [line for line in locale_content.splitlines()
252 if not line.startswith("#")]
253 # LANG unset, regen
254 if len(shell_content) == 0:
255 return True
256
257 # if LANG isn't present, regen
258 current_locale = util.load_shell_content(locale_content).get('LANG', None)
259 if not current_locale:
260 return True
261
262 # locale mismatch, regen
263 if locale.lower() != current_locale.lower():
264 return True
265
266 return False
267
228# vi: ts=4 expandtab268# vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches