1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2016 Canonical Ltd.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License as published by
7
# the Free Software Foundation; version 3.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
from autopilot import logging as autopilot_logging
23
logger = logging.getLogger(__name__)
26
def is_initctl_env_var_set(variable, global_=False):
27
"""Check True if an initctl environment variable is set.
29
:param variable: The name of the variable to check.
30
:param global: if True, the method will operate on the global environment
31
table. Default is False.
32
:return: True if the variable is set. False otherwise.
36
get_initctl_env_var(variable, global_)
38
except subprocess.CalledProcessError:
42
def get_initctl_env_var(variable, global_=False):
43
"""Return the value of an initctl environment variable."""
44
command = ['/sbin/initctl', 'get-env', variable]
46
command += ['--global']
47
output = subprocess.check_output(
48
command, stderr=subprocess.STDOUT, universal_newlines=True)
49
return output.rstrip()
52
@autopilot_logging.log_action(logger.info)
53
def set_initctl_env_var(variable, value, global_=False):
54
"""Set the value of an initctl environment variable."""
55
command = ['/sbin/initctl', 'set-env', '%s=%s' % (variable, value)]
57
command += ['--global']
58
subprocess.call(command, stderr=subprocess.STDOUT, universal_newlines=True)
61
@autopilot_logging.log_action(logger.info)
62
def unset_initctl_env_var(variable, global_=False):
63
"""Remove an initctl environment variable."""
64
command = ['/sbin/initctl', 'unset-env', variable]
66
command += ['--global']
68
command, stderr=subprocess.STDOUT, universal_newlines=True)