1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
5
# charm-helpers is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# charm-helpers 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 charm-helpers. If not, see <http://www.gnu.org/licenses/>.
27
class AmuletUtils(object):
30
This class provides common utility functions that are used by Amulet
34
def __init__(self, log_level=logging.ERROR):
35
self.log = self.get_logger(level=log_level)
37
def get_logger(self, name="amulet-logger", level=logging.DEBUG):
38
"""Get a logger object that will log to stdout."""
40
logger = log.getLogger(name)
41
fmt = log.Formatter("%(asctime)s %(funcName)s "
42
"%(levelname)s: %(message)s")
44
handler = log.StreamHandler(stream=sys.stdout)
45
handler.setLevel(level)
46
handler.setFormatter(fmt)
48
logger.addHandler(handler)
49
logger.setLevel(level)
53
def valid_ip(self, ip):
54
if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
59
def valid_url(self, url):
62
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # noqa
64
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
73
def validate_services(self, commands):
76
Verify the specified services are running on the corresponding
79
for k, v in six.iteritems(commands):
81
output, code = k.run(cmd)
83
return "command `{}` returned {}".format(cmd, str(code))
86
def _get_config(self, unit, filename):
87
"""Get a ConfigParser object for parsing a unit's config file."""
88
file_contents = unit.file_contents(filename)
89
config = ConfigParser.ConfigParser()
90
config.readfp(io.StringIO(file_contents))
93
def validate_config_data(self, sentry_unit, config_file, section,
95
"""Validate config file data.
97
Verify that the specified section of the config file contains
98
the expected option key:value pairs.
100
config = self._get_config(sentry_unit, config_file)
102
if section != 'DEFAULT' and not config.has_section(section):
103
return "section [{}] does not exist".format(section)
105
for k in expected.keys():
106
if not config.has_option(section, k):
107
return "section [{}] is missing option {}".format(section, k)
108
if config.get(section, k) != expected[k]:
109
return "section [{}] {}:{} != expected {}:{}".format(
110
section, k, config.get(section, k), k, expected[k])
113
def _validate_dict_data(self, expected, actual):
114
"""Validate dictionary data.
116
Compare expected dictionary data vs actual dictionary data.
117
The values in the 'expected' dictionary can be strings, bools, ints,
118
longs, or can be a function that evaluate a variable and returns a
121
for k, v in six.iteritems(expected):
123
if (isinstance(v, six.string_types) or
124
isinstance(v, bool) or
125
isinstance(v, six.integer_types)):
127
return "{}:{}".format(k, actual[k])
128
elif not v(actual[k]):
129
return "{}:{}".format(k, actual[k])
131
return "key '{}' does not exist".format(k)
134
def validate_relation_data(self, sentry_unit, relation, expected):
135
"""Validate actual relation data based on expected relation data."""
136
actual = sentry_unit.relation(relation[0], relation[1])
137
self.log.debug('actual: {}'.format(repr(actual)))
138
return self._validate_dict_data(expected, actual)
140
def _validate_list_data(self, expected, actual):
141
"""Compare expected list vs actual list data."""
144
return "expected item {} not found in actual list".format(e)
147
def not_null(self, string):
148
if string is not None:
153
def _get_file_mtime(self, sentry_unit, filename):
154
"""Get last modification time of file."""
155
return sentry_unit.file_stat(filename)['mtime']
157
def _get_dir_mtime(self, sentry_unit, directory):
158
"""Get last modification time of directory."""
159
return sentry_unit.directory_stat(directory)['mtime']
161
def _get_proc_start_time(self, sentry_unit, service, pgrep_full=False):
162
"""Get process' start time.
164
Determine start time of the process based on the last modification
165
time of the /proc/pid directory. If pgrep_full is True, the process
166
name is matched against the full command line.
169
cmd = 'pgrep -o -f {}'.format(service)
171
cmd = 'pgrep -o {}'.format(service)
172
proc_dir = '/proc/{}'.format(sentry_unit.run(cmd)[0].strip())
173
return self._get_dir_mtime(sentry_unit, proc_dir)
175
def service_restarted(self, sentry_unit, service, filename,
176
pgrep_full=False, sleep_time=20):
177
"""Check if service was restarted.
179
Compare a service's start time vs a file's last modification time
180
(such as a config file for that service) to determine if the service
183
time.sleep(sleep_time)
184
if (self._get_proc_start_time(sentry_unit, service, pgrep_full) >=
185
self._get_file_mtime(sentry_unit, filename)):
190
def relation_error(self, name, data):
191
return 'unexpected relation data in {} - {}'.format(name, data)
193
def endpoint_error(self, name, data):
194
return 'unexpected endpoint data in {} - {}'.format(name, data)