~aisrael/charms/trusty/mysql/trunk

« back to all changes in this revision

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

[hopem, r=gnuoy] refactor to use charmhelpers.contrib.database.mysql

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
6
# This file is part of charm-helpers.
 
7
#
 
8
# charm-helpers is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License version 3 as
 
10
# published by the Free Software Foundation.
 
11
#
 
12
# charm-helpers is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public License
 
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
4
20
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
5
21
 
6
22
import yaml
10
26
from charmhelpers.core.hookenv import (
11
27
    log,
12
28
    DEBUG,
 
29
    ERROR,
13
30
)
14
31
 
15
32
 
16
33
def create(sysctl_dict, sysctl_file):
17
34
    """Creates a sysctl.conf file from a YAML associative array
18
35
 
19
 
    :param sysctl_dict: a dict of sysctl options eg { 'kernel.max_pid': 1337 }
20
 
    :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
21
38
    :param sysctl_file: path to the sysctl file to be saved
22
39
    :type sysctl_file: str or unicode
23
40
    :returns: None
24
41
    """
25
 
    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
26
48
 
27
49
    with open(sysctl_file, "w") as fd:
28
 
        for key, value in sysctl_dict.items():
 
50
        for key, value in sysctl_dict_parsed.items():
29
51
            fd.write("{}={}\n".format(key, value))
30
52
 
31
 
    log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict),
 
53
    log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict_parsed),
32
54
        level=DEBUG)
33
55
 
34
56
    check_call(["sysctl", "-p", sysctl_file])