~tribaal/charms/trusty/landscape-server/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/sysctl.py

  • Committer: Free Ekanayaka
  • Date: 2015-01-30 11:16:09 UTC
  • Revision ID: free.ekanayaka@canonical.com-20150130111609-kubupb768v7xltre
Add hahelpers to charmhelpers config [trivial] [r=tealeg]

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from charmhelpers.core.hookenv import (
27
27
    log,
28
28
    DEBUG,
 
29
    ERROR,
29
30
)
30
31
 
31
32
 
32
33
def create(sysctl_dict, sysctl_file):
33
34
    """Creates a sysctl.conf file from a YAML associative array
34
35
 
35
 
    :param sysctl_dict: a dict of sysctl options eg { 'kernel.max_pid': 1337 }
36
 
    :type sysctl_dict: dict
 
36
    :param sysctl_dict: a YAML-formatted string of sysctl options eg "{ 'kernel.max_pid': 1337 }"
 
37
    :type sysctl_dict: str
37
38
    :param sysctl_file: path to the sysctl file to be saved
38
39
    :type sysctl_file: str or unicode
39
40
    :returns: None
40
41
    """
41
 
    sysctl_dict = yaml.load(sysctl_dict)
 
42
    try:
 
43
        sysctl_dict_parsed = yaml.safe_load(sysctl_dict)
 
44
    except yaml.YAMLError:
 
45
        log("Error parsing YAML sysctl_dict: {}".format(sysctl_dict),
 
46
            level=ERROR)
 
47
        return
42
48
 
43
49
    with open(sysctl_file, "w") as fd:
44
 
        for key, value in sysctl_dict.items():
 
50
        for key, value in sysctl_dict_parsed.items():
45
51
            fd.write("{}={}\n".format(key, value))
46
52
 
47
 
    log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict),
 
53
    log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict_parsed),
48
54
        level=DEBUG)
49
55
 
50
56
    check_call(["sysctl", "-p", sysctl_file])