2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) Ansible project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: locale_gen
|
|
|
|
short_description: Creates or removes locales
|
|
|
|
description:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Manages locales by editing /etc/locale.gen and invoking locale-gen.
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Augustus Kling (@AugustusKling)
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
2024-08-12 07:33:54 +02:00
|
|
|
type: list
|
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2024-08-12 07:33:54 +02:00
|
|
|
- Name and encoding of the locales, such as V(en_GB.UTF-8).
|
|
|
|
- Before community.general 9.3.0, this was a string. Using a string still works.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
state:
|
2023-02-20 17:28:47 +01:00
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Whether the locale shall be present.
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
2023-06-30 06:39:11 +02:00
|
|
|
notes:
|
|
|
|
- This module does not support RHEL-based systems.
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Ensure a locale exists
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.locale_gen:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: de_CH.UTF-8
|
|
|
|
state: present
|
2024-08-12 07:33:54 +02:00
|
|
|
|
|
|
|
- name: Ensure multiple locales exist
|
|
|
|
community.general.locale_gen:
|
|
|
|
name:
|
|
|
|
- en_GB.UTF-8
|
|
|
|
- nl_NL.UTF-8
|
|
|
|
state: present
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.mh.deco import check_mode_skip
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.locale_gen import locale_runner, locale_gen_runner
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-06 21:05:34 +02:00
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
class LocaleGen(StateModuleHelper):
|
|
|
|
LOCALE_NORMALIZATION = {
|
|
|
|
".utf8": ".UTF-8",
|
|
|
|
".eucjp": ".EUC-JP",
|
|
|
|
".iso885915": ".ISO-8859-15",
|
|
|
|
".cp1251": ".CP1251",
|
|
|
|
".koi8r": ".KOI8-R",
|
|
|
|
".armscii8": ".ARMSCII-8",
|
|
|
|
".euckr": ".EUC-KR",
|
|
|
|
".gbk": ".GBK",
|
|
|
|
".gb18030": ".GB18030",
|
|
|
|
".euctw": ".EUC-TW",
|
|
|
|
}
|
|
|
|
LOCALE_GEN = "/etc/locale.gen"
|
|
|
|
LOCALE_SUPPORTED = "/var/lib/locales/supported.d/"
|
|
|
|
|
|
|
|
output_params = ["name"]
|
|
|
|
module = dict(
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec=dict(
|
2024-08-12 07:33:54 +02:00
|
|
|
name=dict(type="list", elements="str", required=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2024-05-26 13:56:49 +02:00
|
|
|
use_old_vardict = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
def __init_module__(self):
|
|
|
|
self.vars.set("ubuntu_mode", False)
|
|
|
|
if os.path.exists(self.LOCALE_SUPPORTED):
|
|
|
|
self.vars.ubuntu_mode = True
|
|
|
|
else:
|
|
|
|
if not os.path.exists(self.LOCALE_GEN):
|
|
|
|
self.do_raise("{0} and {1} are missing. Is the package \"locales\" installed?".format(
|
|
|
|
self.LOCALE_SUPPORTED, self.LOCALE_GEN
|
|
|
|
))
|
|
|
|
|
2024-08-12 07:33:54 +02:00
|
|
|
self.assert_available()
|
2023-07-15 12:54:53 +02:00
|
|
|
self.vars.set("is_present", self.is_present(), output=False)
|
|
|
|
self.vars.set("state_tracking", self._state_name(self.vars.is_present), output=False, change=True)
|
|
|
|
|
|
|
|
def __quit_module__(self):
|
|
|
|
self.vars.state_tracking = self._state_name(self.is_present())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _state_name(present):
|
|
|
|
return "present" if present else "absent"
|
|
|
|
|
2024-08-12 07:33:54 +02:00
|
|
|
def assert_available(self):
|
|
|
|
"""Check if the given locales are available on the system. This is done by
|
2023-07-15 12:54:53 +02:00
|
|
|
checking either :
|
|
|
|
* if the locale is present in /etc/locales.gen
|
|
|
|
* or if the locale is present in /usr/share/i18n/SUPPORTED"""
|
|
|
|
__regexp = r'^#?\s*(?P<locale>\S+[\._\S]+) (?P<charset>\S+)\s*$'
|
|
|
|
if self.vars.ubuntu_mode:
|
|
|
|
__locales_available = '/usr/share/i18n/SUPPORTED'
|
|
|
|
else:
|
|
|
|
__locales_available = '/etc/locale.gen'
|
|
|
|
|
|
|
|
re_compiled = re.compile(__regexp)
|
|
|
|
with open(__locales_available, 'r') as fd:
|
|
|
|
lines = fd.readlines()
|
|
|
|
res = [re_compiled.match(line) for line in lines]
|
|
|
|
if self.verbosity >= 4:
|
|
|
|
self.vars.available_lines = lines
|
2024-08-12 07:33:54 +02:00
|
|
|
|
|
|
|
locales_not_found = []
|
|
|
|
for locale in self.vars.name:
|
|
|
|
# Check if the locale is not found in any of the matches
|
|
|
|
if not any(match and match.group("locale") == locale for match in res):
|
|
|
|
locales_not_found.append(locale)
|
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
# locale may be installed but not listed in the file, for example C.UTF-8 in some systems
|
2024-08-12 07:33:54 +02:00
|
|
|
locales_not_found = self.locale_get_not_present(locales_not_found)
|
|
|
|
|
|
|
|
if locales_not_found:
|
|
|
|
self.do_raise("The following locales you've entered are not available on your system: {0}".format(', '.join(locales_not_found)))
|
2023-07-15 12:54:53 +02:00
|
|
|
|
|
|
|
def is_present(self):
|
2024-08-12 07:33:54 +02:00
|
|
|
return not self.locale_get_not_present(self.vars.name)
|
|
|
|
|
|
|
|
def locale_get_not_present(self, locales):
|
2023-07-15 12:54:53 +02:00
|
|
|
runner = locale_runner(self.module)
|
|
|
|
with runner() as ctx:
|
|
|
|
rc, out, err = ctx.run()
|
|
|
|
if self.verbosity >= 4:
|
|
|
|
self.vars.locale_run_info = ctx.run_info
|
2024-08-12 07:33:54 +02:00
|
|
|
|
|
|
|
not_found = []
|
|
|
|
for locale in locales:
|
|
|
|
if not any(self.fix_case(locale) == self.fix_case(line) for line in out.splitlines()):
|
|
|
|
not_found.append(locale)
|
|
|
|
|
|
|
|
return not_found
|
2023-07-15 12:54:53 +02:00
|
|
|
|
|
|
|
def fix_case(self, name):
|
|
|
|
"""locale -a might return the encoding in either lower or upper case.
|
|
|
|
Passing through this function makes them uniform for comparisons."""
|
|
|
|
for s, r in self.LOCALE_NORMALIZATION.items():
|
|
|
|
name = name.replace(s, r)
|
|
|
|
return name
|
|
|
|
|
2024-08-12 07:33:54 +02:00
|
|
|
def set_locale(self, names, enabled=True):
|
2023-07-15 12:54:53 +02:00
|
|
|
""" Sets the state of the locale. Defaults to enabled. """
|
2024-08-12 07:33:54 +02:00
|
|
|
with open("/etc/locale.gen", 'r') as fr:
|
|
|
|
lines = fr.readlines()
|
|
|
|
|
|
|
|
locale_regexes = []
|
|
|
|
|
|
|
|
for name in names:
|
|
|
|
search_string = r'^#?\s*%s (?P<charset>.+)' % re.escape(name)
|
|
|
|
if enabled:
|
|
|
|
new_string = r'%s \g<charset>' % (name)
|
|
|
|
else:
|
|
|
|
new_string = r'# %s \g<charset>' % (name)
|
|
|
|
re_search = re.compile(search_string)
|
|
|
|
locale_regexes.append([re_search, new_string])
|
|
|
|
|
|
|
|
for i in range(len(lines)):
|
|
|
|
for [search, replace] in locale_regexes:
|
|
|
|
lines[i] = search.sub(replace, lines[i])
|
|
|
|
|
|
|
|
# Write the modified content back to the file
|
|
|
|
with open("/etc/locale.gen", 'w') as fw:
|
|
|
|
fw.writelines(lines)
|
|
|
|
|
|
|
|
def apply_change(self, targetState, names):
|
2023-07-15 12:54:53 +02:00
|
|
|
"""Create or remove locale.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
targetState -- Desired state, either present or absent.
|
2024-08-12 07:33:54 +02:00
|
|
|
names -- Names list including encoding such as de_CH.UTF-8.
|
2023-07-15 12:54:53 +02:00
|
|
|
"""
|
|
|
|
|
2024-08-12 07:33:54 +02:00
|
|
|
self.set_locale(names, enabled=(targetState == "present"))
|
2023-07-15 12:54:53 +02:00
|
|
|
|
|
|
|
runner = locale_gen_runner(self.module)
|
|
|
|
with runner() as ctx:
|
|
|
|
ctx.run()
|
|
|
|
|
2024-08-12 07:33:54 +02:00
|
|
|
def apply_change_ubuntu(self, targetState, names):
|
2023-07-15 12:54:53 +02:00
|
|
|
"""Create or remove locale.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
targetState -- Desired state, either present or absent.
|
2024-08-12 07:33:54 +02:00
|
|
|
names -- Name list including encoding such as de_CH.UTF-8.
|
2023-07-15 12:54:53 +02:00
|
|
|
"""
|
|
|
|
runner = locale_gen_runner(self.module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-07-15 12:54:53 +02:00
|
|
|
if targetState == "present":
|
|
|
|
# Create locale.
|
|
|
|
# Ubuntu's patched locale-gen automatically adds the new locale to /var/lib/locales/supported.d/local
|
|
|
|
with runner() as ctx:
|
|
|
|
ctx.run()
|
|
|
|
else:
|
|
|
|
# Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales.
|
|
|
|
with open("/var/lib/locales/supported.d/local", "r") as fr:
|
|
|
|
content = fr.readlines()
|
|
|
|
with open("/var/lib/locales/supported.d/local", "w") as fw:
|
|
|
|
for line in content:
|
|
|
|
locale, charset = line.split(' ')
|
2024-08-12 07:33:54 +02:00
|
|
|
if locale not in names:
|
2023-07-15 12:54:53 +02:00
|
|
|
fw.write(line)
|
|
|
|
# Purge locales and regenerate.
|
|
|
|
# Please provide a patch if you know how to avoid regenerating the locales to keep!
|
|
|
|
with runner("purge") as ctx:
|
|
|
|
ctx.run()
|
|
|
|
|
|
|
|
@check_mode_skip
|
|
|
|
def __state_fallback__(self):
|
|
|
|
if self.vars.state_tracking == self.vars.state:
|
|
|
|
return
|
|
|
|
if self.vars.ubuntu_mode:
|
|
|
|
self.apply_change_ubuntu(self.vars.state, self.vars.name)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2023-07-15 12:54:53 +02:00
|
|
|
self.apply_change(self.vars.state, self.vars.name)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
LocaleGen.execute()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|