2
# -*- coding: utf-8 -*-
4
# Copyright 2014-2015 Canonical Limited.
6
# This file is part of charm-helpers.
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.
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.
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/>.
22
from subprocess import check_call
24
from charmhelpers.core.hookenv import (
30
__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
33
def create(sysctl_dict, sysctl_file):
34
"""Creates a sysctl.conf file from a YAML associative array
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
43
sysctl_dict_parsed = yaml.safe_load(sysctl_dict)
44
except yaml.YAMLError:
45
log("Error parsing YAML sysctl_dict: {}".format(sysctl_dict),
49
with open(sysctl_file, "w") as fd:
50
for key, value in sysctl_dict_parsed.items():
51
fd.write("{}={}\n".format(key, value))
53
log("Updating sysctl_file: %s values: %s" % (sysctl_file, sysctl_dict_parsed),
56
check_call(["sysctl", "-p", sysctl_file])