aboutsummaryrefslogtreecommitdiffstats
path: root/fabtools/require/system.py
blob: bb1a651096bbfece128a92dc3fd292ab344998b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
System settings
===============
"""
from __future__ import with_statement

from fabric.api import sudo, warn
from fabric.contrib.files import append

from fabtools.files import watch
from fabtools.system import (
    get_hostname, set_hostname,
    get_sysctl, set_sysctl,
    supported_locales,
    )


def sysctl(key, value, persist=True):
    """
    Require a kernel parameter to have a specific value.
    """
    if get_sysctl(key) != value:
        set_sysctl(key, value)

    if persist:
        from fabtools import require
        filename = '/etc/sysctl.d/60-%s.conf' % key
        with watch(filename, use_sudo=True) as config:
            require.file(filename,
                contents='%(key)s = %(value)s\n' % locals(),
                use_sudo=True)
        if config.changed:
            sudo('service procps start')


def hostname(name):
    """
    Require the hostname to have a specific value.
    """
    if get_hostname() != name:
        set_hostname(name)


def locales(names):
    """
    Require the list of locales to be available.
    """

    config_file = '/var/lib/locales/supported.d/local'

    # Regenerate locales if config file changes
    with watch(config_file, use_sudo=True) as config:

        # Add valid locale names to the config file
        supported = dict(supported_locales())
        for name in names:
            if name in supported:
                charset = supported[name]
                locale = "%s %s" % (name, charset)
                append(config_file, locale, use_sudo=True)
            else:
                warn('Unsupported locale name "%s"' % name)

    if config.changed:
        sudo('dpkg-reconfigure locales')


def locale(name):
    """
    Require the locale to be available.
    """
    locales([name])


def default_locale(name):
    """
    Require the locale to be the default.
    """
    from fabtools import require

    # Ensure the locale is available
    locale(name)

    # Make it the default
    contents = 'LANG="%s"\n' % name
    require.file('/etc/default/locale', contents, use_sudo=True)