~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

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

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
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
 
 
20
 
import yaml
21
 
 
22
 
from subprocess import check_call
23
 
 
24
 
from charmhelpers.core.hookenv import (
25
 
    log,
26
 
    DEBUG,
27
 
    ERROR,
28
 
)
29
 
 
30
 
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
31
 
 
32
 
 
33
 
def create(sysctl_dict, sysctl_file):
34
 
    """Creates a sysctl.conf file from a YAML associative array
35
 
 
36
 
    :param sysctl_dict: a YAML-formatted string of sysctl options eg "{ 'kernel.max_pid': 1337 }"
37
 
    :type sysctl_dict: str
38
 
    :param sysctl_file: path to the sysctl file to be saved
39
 
    :type sysctl_file: str or unicode
40
 
    :returns: None
41
 
    """
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
48
 
 
49
 
    with open(sysctl_file, "w") as fd:
50
 
        for key, value in sysctl_dict_parsed.items():
51
 
            fd.write("{}={}\n".format(key, value))
52
 
 
53
 
    log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict_parsed),
54
 
        level=DEBUG)
55
 
 
56
 
    check_call(["sysctl", "-p", sysctl_file])