~openstack-charmers/charms/trusty/heat/keystonev3

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2015-02-24 11:04:31 UTC
  • Revision ID: edward.hope-morley@canonical.com-20150224110431-pngo7g3nf5mut81t
[trivial] charmhelpers sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# You should have received a copy of the GNU Lesser General Public License
18
18
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
 
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
21
 
 
22
20
import yaml
23
21
 
24
22
from subprocess import check_call
26
24
from charmhelpers.core.hookenv import (
27
25
    log,
28
26
    DEBUG,
 
27
    ERROR,
29
28
)
30
29
 
 
30
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
 
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])