2013-12-24 12:29:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
from subprocess import Popen, PIPE, call
|
2015-01-03 22:24:27 +01:00
|
|
|
import re
|
2013-12-24 12:29:02 +01:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2014-03-11 21:13:47 +01:00
|
|
|
module: locale_gen
|
2014-10-02 16:42:13 +02:00
|
|
|
short_description: Creates or removes locales.
|
2013-12-24 12:29:02 +01:00
|
|
|
description:
|
|
|
|
- Manages locales by editing /etc/locale.gen and invoking locale-gen.
|
2014-03-11 20:56:30 +01:00
|
|
|
version_added: "1.6"
|
2015-05-14 07:06:16 +02:00
|
|
|
author: "Augustus Kling (@AugustusKling)"
|
2013-12-24 12:29:02 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name and encoding of the locale, such as "en_GB.UTF-8".
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the locale shall be present.
|
|
|
|
required: false
|
|
|
|
choices: ["present", "absent"]
|
|
|
|
default: "present"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Ensure a locale exists.
|
2014-03-11 21:13:47 +01:00
|
|
|
- locale_gen: name=de_CH.UTF-8 state=present
|
2013-12-24 12:29:02 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-22 04:07:10 +01:00
|
|
|
LOCALE_NORMALIZATION = {
|
|
|
|
".utf8": ".UTF-8",
|
|
|
|
".eucjp": ".EUC-JP",
|
2015-07-21 21:08:02 +02:00
|
|
|
".iso885915": ".ISO-8859-15",
|
2015-01-22 04:07:10 +01:00
|
|
|
}
|
|
|
|
|
2013-12-24 12:29:02 +01:00
|
|
|
# ===========================================
|
|
|
|
# location module specific support methods.
|
|
|
|
#
|
|
|
|
|
2015-01-03 18:01:13 +01:00
|
|
|
def is_available(name, ubuntuMode):
|
|
|
|
"""Check if the given locale is available on the system. This is done by
|
|
|
|
checking either :
|
|
|
|
* if the locale is present in /etc/locales.gen
|
|
|
|
* or if the locale is present in /usr/share/i18n/SUPPORTED"""
|
|
|
|
if ubuntuMode:
|
|
|
|
__regexp = '^(?P<locale>\S+_\S+) (?P<charset>\S+)\s*$'
|
|
|
|
__locales_available = '/usr/share/i18n/SUPPORTED'
|
|
|
|
else:
|
|
|
|
__regexp = '^#{0,1}\s*(?P<locale>\S+_\S+) (?P<charset>\S+)\s*$'
|
|
|
|
__locales_available = '/etc/locale.gen'
|
|
|
|
|
|
|
|
re_compiled = re.compile(__regexp)
|
2015-05-08 23:36:15 +02:00
|
|
|
fd = open(__locales_available, 'r')
|
|
|
|
for line in fd:
|
|
|
|
result = re_compiled.match(line)
|
|
|
|
if result and result.group('locale') == name:
|
|
|
|
return True
|
|
|
|
fd.close()
|
2015-01-03 18:01:13 +01:00
|
|
|
return False
|
|
|
|
|
2013-12-24 12:29:02 +01:00
|
|
|
def is_present(name):
|
|
|
|
"""Checks if the given locale is currently installed."""
|
|
|
|
output = Popen(["locale", "-a"], stdout=PIPE).communicate()[0]
|
|
|
|
return any(fix_case(name) == fix_case(line) for line in output.splitlines())
|
|
|
|
|
|
|
|
def fix_case(name):
|
|
|
|
"""locale -a might return the encoding in either lower or upper case.
|
|
|
|
Passing through this function makes them uniform for comparisons."""
|
2015-01-22 04:07:10 +01:00
|
|
|
for s, r in LOCALE_NORMALIZATION.iteritems():
|
|
|
|
name = name.replace(s, r)
|
|
|
|
return name
|
2013-12-24 12:29:02 +01:00
|
|
|
|
|
|
|
def replace_line(existing_line, new_line):
|
|
|
|
"""Replaces lines in /etc/locale.gen"""
|
2015-05-11 17:29:28 +02:00
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "r")
|
|
|
|
lines = [line.replace(existing_line, new_line) for line in f]
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "w")
|
|
|
|
f.write("".join(lines))
|
|
|
|
finally:
|
|
|
|
f.close()
|
2013-12-24 12:29:02 +01:00
|
|
|
|
2015-01-03 18:01:13 +01:00
|
|
|
def set_locale(name, enabled=True):
|
|
|
|
""" Sets the state of the locale. Defaults to enabled. """
|
|
|
|
search_string = '#{0,1}\s*%s (?P<charset>.+)' % name
|
|
|
|
if enabled:
|
|
|
|
new_string = '%s \g<charset>' % (name)
|
|
|
|
else:
|
|
|
|
new_string = '# %s \g<charset>' % (name)
|
2015-05-11 17:29:28 +02:00
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "r")
|
|
|
|
lines = [re.sub(search_string, new_string, line) for line in f]
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "w")
|
|
|
|
f.write("".join(lines))
|
|
|
|
finally:
|
|
|
|
f.close()
|
2015-01-03 18:01:13 +01:00
|
|
|
|
|
|
|
def apply_change(targetState, name):
|
2014-01-19 23:35:10 +01:00
|
|
|
"""Create or remove locale.
|
2015-01-03 18:01:13 +01:00
|
|
|
|
2014-01-19 23:35:10 +01:00
|
|
|
Keyword arguments:
|
2014-04-29 16:41:05 +02:00
|
|
|
targetState -- Desired state, either present or absent.
|
2014-01-19 23:35:10 +01:00
|
|
|
name -- Name including encoding such as de_CH.UTF-8.
|
|
|
|
"""
|
|
|
|
if targetState=="present":
|
|
|
|
# Create locale.
|
2015-01-03 18:01:13 +01:00
|
|
|
set_locale(name, enabled=True)
|
2014-01-19 23:35:10 +01:00
|
|
|
else:
|
|
|
|
# Delete locale.
|
2015-01-03 18:01:13 +01:00
|
|
|
set_locale(name, enabled=False)
|
2014-01-19 23:35:10 +01:00
|
|
|
|
|
|
|
localeGenExitValue = call("locale-gen")
|
|
|
|
if localeGenExitValue!=0:
|
2014-01-20 00:11:01 +01:00
|
|
|
raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned "+str(localeGenExitValue))
|
2014-01-19 23:35:10 +01:00
|
|
|
|
2015-01-03 18:01:13 +01:00
|
|
|
def apply_change_ubuntu(targetState, name):
|
2014-01-19 23:35:10 +01:00
|
|
|
"""Create or remove locale.
|
|
|
|
|
|
|
|
Keyword arguments:
|
2014-04-29 16:41:05 +02:00
|
|
|
targetState -- Desired state, either present or absent.
|
2014-01-19 23:35:10 +01:00
|
|
|
name -- Name including encoding such as de_CH.UTF-8.
|
|
|
|
"""
|
|
|
|
if targetState=="present":
|
|
|
|
# Create locale.
|
|
|
|
# Ubuntu's patched locale-gen automatically adds the new locale to /var/lib/locales/supported.d/local
|
|
|
|
localeGenExitValue = call(["locale-gen", name])
|
|
|
|
else:
|
|
|
|
# Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales.
|
2015-05-11 17:29:28 +02:00
|
|
|
try:
|
|
|
|
f = open("/var/lib/locales/supported.d/local", "r")
|
|
|
|
content = f.readlines()
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f = open("/var/lib/locales/supported.d/local", "w")
|
|
|
|
for line in content:
|
|
|
|
locale, charset = line.split(' ')
|
|
|
|
if locale != name:
|
|
|
|
f.write(line)
|
|
|
|
finally:
|
|
|
|
f.close()
|
2014-01-19 23:35:10 +01:00
|
|
|
# Purge locales and regenerate.
|
|
|
|
# Please provide a patch if you know how to avoid regenerating the locales to keep!
|
|
|
|
localeGenExitValue = call(["locale-gen", "--purge"])
|
|
|
|
|
|
|
|
if localeGenExitValue!=0:
|
|
|
|
raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned "+str(localeGenExitValue))
|
|
|
|
|
2013-12-24 12:29:02 +01:00
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True),
|
2015-03-07 09:24:32 +01:00
|
|
|
state = dict(choices=['present','absent'], default='present'),
|
2013-12-24 12:29:02 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
if not os.path.exists("/etc/locale.gen"):
|
2015-07-25 11:57:21 +02:00
|
|
|
if os.path.exists("/var/lib/locales/supported.d/"):
|
2014-01-19 23:35:10 +01:00
|
|
|
# Ubuntu created its own system to manage locales.
|
|
|
|
ubuntuMode = True
|
|
|
|
else:
|
2015-03-26 19:36:21 +01:00
|
|
|
module.fail_json(msg="/etc/locale.gen and /var/lib/locales/supported.d/local are missing. Is the package \"locales\" installed?")
|
2014-01-19 23:35:10 +01:00
|
|
|
else:
|
|
|
|
# We found the common way to manage locales.
|
|
|
|
ubuntuMode = False
|
2015-01-03 18:01:13 +01:00
|
|
|
|
|
|
|
if not is_available(name, ubuntuMode):
|
|
|
|
module.fail_json(msg="The locales you've entered is not available "
|
|
|
|
"on your system.")
|
|
|
|
|
2015-05-08 23:36:15 +02:00
|
|
|
if is_present(name):
|
|
|
|
prev_state = "present"
|
|
|
|
else:
|
|
|
|
prev_state = "absent"
|
2013-12-24 12:29:02 +01:00
|
|
|
changed = (prev_state!=state)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
else:
|
|
|
|
if changed:
|
2014-01-19 23:35:10 +01:00
|
|
|
try:
|
|
|
|
if ubuntuMode==False:
|
2015-01-03 18:01:13 +01:00
|
|
|
apply_change(state, name)
|
2014-01-19 23:35:10 +01:00
|
|
|
else:
|
2015-01-03 18:01:13 +01:00
|
|
|
apply_change_ubuntu(state, name)
|
2015-05-08 23:36:15 +02:00
|
|
|
except EnvironmentError, e:
|
2014-01-19 23:35:10 +01:00
|
|
|
module.fail_json(msg=e.strerror, exitValue=e.errno)
|
2015-01-03 18:01:13 +01:00
|
|
|
|
2013-12-24 12:29:02 +01:00
|
|
|
module.exit_json(name=name, changed=changed, msg="OK")
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
2014-03-11 20:56:30 +01:00
|
|
|
main()
|