Merge lp:~brad-marshall/charms/trusty/swift-storage/fix-nagios into lp:~openstack-charmers-archive/charms/trusty/swift-storage/next
- Trusty Tahr (14.04)
- fix-nagios
- Merge into next
Proposed by
Brad Marshall
Status: | Merged |
---|---|
Merged at revision: | 58 |
Proposed branch: | lp:~brad-marshall/charms/trusty/swift-storage/fix-nagios |
Merge into: | lp:~openstack-charmers-archive/charms/trusty/swift-storage/next |
Diff against target: |
1245 lines (+860/-47) 21 files modified
config.yaml (+6/-0) hooks/charmhelpers/contrib/charmsupport/nrpe.py (+41/-7) hooks/charmhelpers/contrib/hahelpers/cluster.py (+5/-1) hooks/charmhelpers/contrib/openstack/amulet/deployment.py (+5/-2) hooks/charmhelpers/contrib/openstack/files/__init__.py (+18/-0) hooks/charmhelpers/contrib/openstack/files/check_haproxy.sh (+32/-0) hooks/charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh (+30/-0) hooks/charmhelpers/contrib/openstack/ip.py (+37/-0) hooks/charmhelpers/contrib/openstack/utils.py (+1/-0) hooks/charmhelpers/contrib/python/packages.py (+2/-2) hooks/charmhelpers/core/fstab.py (+4/-4) hooks/charmhelpers/core/host.py (+5/-5) hooks/charmhelpers/core/strutils.py (+42/-0) hooks/charmhelpers/core/sysctl.py (+13/-7) hooks/charmhelpers/core/templating.py (+3/-3) hooks/charmhelpers/core/unitdata.py (+477/-0) hooks/charmhelpers/fetch/archiveurl.py (+10/-10) hooks/charmhelpers/fetch/giturl.py (+1/-1) tests/basic_deployment.py (+1/-1) tests/charmhelpers/contrib/amulet/utils.py (+122/-2) tests/charmhelpers/contrib/openstack/amulet/deployment.py (+5/-2) |
To merge this branch: | bzr merge lp:~brad-marshall/charms/trusty/swift-storage/fix-nagios |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
OpenStack Charmers | Pending | ||
Review via email: mp+250699@code.launchpad.net |
Commit message
Description of the change
Synced charmhelpers, added nagios_servicegroup config option, and added haproxy nrpe checks.
To post a comment you must log in.
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
charm_unit_test #2004 swift-storage-next for brad-marshall mp250699
UNIT OK: passed
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
charm_amulet_test #2161 swift-storage-next for brad-marshall mp250699
AMULET FAIL: amulet-test failed
AMULET Results (max last 2 lines):
ERROR subprocess encountered error code 1
make: *** [test] Error 1
Full amulet test output: http://
Build: http://
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'config.yaml' | |||
2 | --- config.yaml 2014-10-30 05:52:15 +0000 | |||
3 | +++ config.yaml 2015-02-24 06:05:56 +0000 | |||
4 | @@ -109,3 +109,9 @@ | |||
5 | 109 | juju-myservice-0 | 109 | juju-myservice-0 |
6 | 110 | If you're running multiple environments with the same services in them | 110 | If you're running multiple environments with the same services in them |
7 | 111 | this allows you to differentiate between them. | 111 | this allows you to differentiate between them. |
8 | 112 | nagios_servicegroups: | ||
9 | 113 | default: "" | ||
10 | 114 | type: string | ||
11 | 115 | description: | | ||
12 | 116 | A comma-separated list of nagios servicegroups. | ||
13 | 117 | If left empty, the nagios_context will be used as the servicegroup | ||
14 | 112 | 118 | ||
15 | === modified file 'hooks/charmhelpers/contrib/charmsupport/nrpe.py' | |||
16 | --- hooks/charmhelpers/contrib/charmsupport/nrpe.py 2015-01-26 09:47:56 +0000 | |||
17 | +++ hooks/charmhelpers/contrib/charmsupport/nrpe.py 2015-02-24 06:05:56 +0000 | |||
18 | @@ -24,6 +24,8 @@ | |||
19 | 24 | import pwd | 24 | import pwd |
20 | 25 | import grp | 25 | import grp |
21 | 26 | import os | 26 | import os |
22 | 27 | import glob | ||
23 | 28 | import shutil | ||
24 | 27 | import re | 29 | import re |
25 | 28 | import shlex | 30 | import shlex |
26 | 29 | import yaml | 31 | import yaml |
27 | @@ -161,7 +163,7 @@ | |||
28 | 161 | log('Check command not found: {}'.format(parts[0])) | 163 | log('Check command not found: {}'.format(parts[0])) |
29 | 162 | return '' | 164 | return '' |
30 | 163 | 165 | ||
32 | 164 | def write(self, nagios_context, hostname, nagios_servicegroups=None): | 166 | def write(self, nagios_context, hostname, nagios_servicegroups): |
33 | 165 | nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format( | 167 | nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format( |
34 | 166 | self.command) | 168 | self.command) |
35 | 167 | with open(nrpe_check_file, 'w') as nrpe_check_config: | 169 | with open(nrpe_check_file, 'w') as nrpe_check_config: |
36 | @@ -177,14 +179,11 @@ | |||
37 | 177 | nagios_servicegroups) | 179 | nagios_servicegroups) |
38 | 178 | 180 | ||
39 | 179 | def write_service_config(self, nagios_context, hostname, | 181 | def write_service_config(self, nagios_context, hostname, |
41 | 180 | nagios_servicegroups=None): | 182 | nagios_servicegroups): |
42 | 181 | for f in os.listdir(NRPE.nagios_exportdir): | 183 | for f in os.listdir(NRPE.nagios_exportdir): |
43 | 182 | if re.search('.*{}.cfg'.format(self.command), f): | 184 | if re.search('.*{}.cfg'.format(self.command), f): |
44 | 183 | os.remove(os.path.join(NRPE.nagios_exportdir, f)) | 185 | os.remove(os.path.join(NRPE.nagios_exportdir, f)) |
45 | 184 | 186 | ||
46 | 185 | if not nagios_servicegroups: | ||
47 | 186 | nagios_servicegroups = nagios_context | ||
48 | 187 | |||
49 | 188 | templ_vars = { | 187 | templ_vars = { |
50 | 189 | 'nagios_hostname': hostname, | 188 | 'nagios_hostname': hostname, |
51 | 190 | 'nagios_servicegroup': nagios_servicegroups, | 189 | 'nagios_servicegroup': nagios_servicegroups, |
52 | @@ -211,10 +210,10 @@ | |||
53 | 211 | super(NRPE, self).__init__() | 210 | super(NRPE, self).__init__() |
54 | 212 | self.config = config() | 211 | self.config = config() |
55 | 213 | self.nagios_context = self.config['nagios_context'] | 212 | self.nagios_context = self.config['nagios_context'] |
57 | 214 | if 'nagios_servicegroups' in self.config: | 213 | if 'nagios_servicegroups' in self.config and self.config['nagios_servicegroups']: |
58 | 215 | self.nagios_servicegroups = self.config['nagios_servicegroups'] | 214 | self.nagios_servicegroups = self.config['nagios_servicegroups'] |
59 | 216 | else: | 215 | else: |
61 | 217 | self.nagios_servicegroups = 'juju' | 216 | self.nagios_servicegroups = self.nagios_context |
62 | 218 | self.unit_name = local_unit().replace('/', '-') | 217 | self.unit_name = local_unit().replace('/', '-') |
63 | 219 | if hostname: | 218 | if hostname: |
64 | 220 | self.hostname = hostname | 219 | self.hostname = hostname |
65 | @@ -322,3 +321,38 @@ | |||
66 | 322 | check_cmd='check_status_file.py -f ' | 321 | check_cmd='check_status_file.py -f ' |
67 | 323 | '/var/lib/nagios/service-check-%s.txt' % svc, | 322 | '/var/lib/nagios/service-check-%s.txt' % svc, |
68 | 324 | ) | 323 | ) |
69 | 324 | |||
70 | 325 | |||
71 | 326 | def copy_nrpe_checks(): | ||
72 | 327 | """ | ||
73 | 328 | Copy the nrpe checks into place | ||
74 | 329 | |||
75 | 330 | """ | ||
76 | 331 | NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins' | ||
77 | 332 | nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks', | ||
78 | 333 | 'charmhelpers', 'contrib', 'openstack', | ||
79 | 334 | 'files') | ||
80 | 335 | |||
81 | 336 | if not os.path.exists(NAGIOS_PLUGINS): | ||
82 | 337 | os.makedirs(NAGIOS_PLUGINS) | ||
83 | 338 | for fname in glob.glob(os.path.join(nrpe_files_dir, "check_*")): | ||
84 | 339 | if os.path.isfile(fname): | ||
85 | 340 | shutil.copy2(fname, | ||
86 | 341 | os.path.join(NAGIOS_PLUGINS, os.path.basename(fname))) | ||
87 | 342 | |||
88 | 343 | |||
89 | 344 | def add_haproxy_checks(nrpe, unit_name): | ||
90 | 345 | """ | ||
91 | 346 | Add checks for each service in list | ||
92 | 347 | |||
93 | 348 | :param NRPE nrpe: NRPE object to add check to | ||
94 | 349 | :param str unit_name: Unit name to use in check description | ||
95 | 350 | """ | ||
96 | 351 | nrpe.add_check( | ||
97 | 352 | shortname='haproxy_servers', | ||
98 | 353 | description='Check HAProxy {%s}' % unit_name, | ||
99 | 354 | check_cmd='check_haproxy.sh') | ||
100 | 355 | nrpe.add_check( | ||
101 | 356 | shortname='haproxy_queue', | ||
102 | 357 | description='Check HAProxy queue depth {%s}' % unit_name, | ||
103 | 358 | check_cmd='check_haproxy_queue_depth.sh') | ||
104 | 325 | 359 | ||
105 | === modified file 'hooks/charmhelpers/contrib/hahelpers/cluster.py' | |||
106 | --- hooks/charmhelpers/contrib/hahelpers/cluster.py 2015-01-26 09:47:56 +0000 | |||
107 | +++ hooks/charmhelpers/contrib/hahelpers/cluster.py 2015-02-24 06:05:56 +0000 | |||
108 | @@ -48,6 +48,9 @@ | |||
109 | 48 | from charmhelpers.core.decorators import ( | 48 | from charmhelpers.core.decorators import ( |
110 | 49 | retry_on_exception, | 49 | retry_on_exception, |
111 | 50 | ) | 50 | ) |
112 | 51 | from charmhelpers.core.strutils import ( | ||
113 | 52 | bool_from_string, | ||
114 | 53 | ) | ||
115 | 51 | 54 | ||
116 | 52 | 55 | ||
117 | 53 | class HAIncompleteConfig(Exception): | 56 | class HAIncompleteConfig(Exception): |
118 | @@ -164,7 +167,8 @@ | |||
119 | 164 | . | 167 | . |
120 | 165 | returns: boolean | 168 | returns: boolean |
121 | 166 | ''' | 169 | ''' |
123 | 167 | if config_get('use-https') == "yes": | 170 | use_https = config_get('use-https') |
124 | 171 | if use_https and bool_from_string(use_https): | ||
125 | 168 | return True | 172 | return True |
126 | 169 | if config_get('ssl_cert') and config_get('ssl_key'): | 173 | if config_get('ssl_cert') and config_get('ssl_key'): |
127 | 170 | return True | 174 | return True |
128 | 171 | 175 | ||
129 | === modified file 'hooks/charmhelpers/contrib/openstack/amulet/deployment.py' | |||
130 | --- hooks/charmhelpers/contrib/openstack/amulet/deployment.py 2015-01-26 09:47:56 +0000 | |||
131 | +++ hooks/charmhelpers/contrib/openstack/amulet/deployment.py 2015-02-24 06:05:56 +0000 | |||
132 | @@ -71,16 +71,19 @@ | |||
133 | 71 | services.append(this_service) | 71 | services.append(this_service) |
134 | 72 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', | 72 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', |
135 | 73 | 'ceph-osd', 'ceph-radosgw'] | 73 | 'ceph-osd', 'ceph-radosgw'] |
136 | 74 | # Openstack subordinate charms do not expose an origin option as that | ||
137 | 75 | # is controlled by the principle | ||
138 | 76 | ignore = ['neutron-openvswitch'] | ||
139 | 74 | 77 | ||
140 | 75 | if self.openstack: | 78 | if self.openstack: |
141 | 76 | for svc in services: | 79 | for svc in services: |
143 | 77 | if svc['name'] not in use_source: | 80 | if svc['name'] not in use_source + ignore: |
144 | 78 | config = {'openstack-origin': self.openstack} | 81 | config = {'openstack-origin': self.openstack} |
145 | 79 | self.d.configure(svc['name'], config) | 82 | self.d.configure(svc['name'], config) |
146 | 80 | 83 | ||
147 | 81 | if self.source: | 84 | if self.source: |
148 | 82 | for svc in services: | 85 | for svc in services: |
150 | 83 | if svc['name'] in use_source: | 86 | if svc['name'] in use_source and svc['name'] not in ignore: |
151 | 84 | config = {'source': self.source} | 87 | config = {'source': self.source} |
152 | 85 | self.d.configure(svc['name'], config) | 88 | self.d.configure(svc['name'], config) |
153 | 86 | 89 | ||
154 | 87 | 90 | ||
155 | === added directory 'hooks/charmhelpers/contrib/openstack/files' | |||
156 | === added file 'hooks/charmhelpers/contrib/openstack/files/__init__.py' | |||
157 | --- hooks/charmhelpers/contrib/openstack/files/__init__.py 1970-01-01 00:00:00 +0000 | |||
158 | +++ hooks/charmhelpers/contrib/openstack/files/__init__.py 2015-02-24 06:05:56 +0000 | |||
159 | @@ -0,0 +1,18 @@ | |||
160 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
161 | 2 | # | ||
162 | 3 | # This file is part of charm-helpers. | ||
163 | 4 | # | ||
164 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
165 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
166 | 7 | # published by the Free Software Foundation. | ||
167 | 8 | # | ||
168 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
169 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
170 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
171 | 12 | # GNU Lesser General Public License for more details. | ||
172 | 13 | # | ||
173 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
174 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
175 | 16 | |||
176 | 17 | # dummy __init__.py to fool syncer into thinking this is a syncable python | ||
177 | 18 | # module | ||
178 | 0 | 19 | ||
179 | === added file 'hooks/charmhelpers/contrib/openstack/files/check_haproxy.sh' | |||
180 | --- hooks/charmhelpers/contrib/openstack/files/check_haproxy.sh 1970-01-01 00:00:00 +0000 | |||
181 | +++ hooks/charmhelpers/contrib/openstack/files/check_haproxy.sh 2015-02-24 06:05:56 +0000 | |||
182 | @@ -0,0 +1,32 @@ | |||
183 | 1 | #!/bin/bash | ||
184 | 2 | #-------------------------------------------- | ||
185 | 3 | # This file is managed by Juju | ||
186 | 4 | #-------------------------------------------- | ||
187 | 5 | # | ||
188 | 6 | # Copyright 2009,2012 Canonical Ltd. | ||
189 | 7 | # Author: Tom Haddon | ||
190 | 8 | |||
191 | 9 | CRITICAL=0 | ||
192 | 10 | NOTACTIVE='' | ||
193 | 11 | LOGFILE=/var/log/nagios/check_haproxy.log | ||
194 | 12 | AUTH=$(grep -r "stats auth" /etc/haproxy | head -1 | awk '{print $4}') | ||
195 | 13 | |||
196 | 14 | for appserver in $(grep ' server' /etc/haproxy/haproxy.cfg | awk '{print $2'}); | ||
197 | 15 | do | ||
198 | 16 | output=$(/usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 --regex="class=\"(active|backup)(2|3).*${appserver}" -e ' 200 OK') | ||
199 | 17 | if [ $? != 0 ]; then | ||
200 | 18 | date >> $LOGFILE | ||
201 | 19 | echo $output >> $LOGFILE | ||
202 | 20 | /usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 -v | grep $appserver >> $LOGFILE 2>&1 | ||
203 | 21 | CRITICAL=1 | ||
204 | 22 | NOTACTIVE="${NOTACTIVE} $appserver" | ||
205 | 23 | fi | ||
206 | 24 | done | ||
207 | 25 | |||
208 | 26 | if [ $CRITICAL = 1 ]; then | ||
209 | 27 | echo "CRITICAL:${NOTACTIVE}" | ||
210 | 28 | exit 2 | ||
211 | 29 | fi | ||
212 | 30 | |||
213 | 31 | echo "OK: All haproxy instances looking good" | ||
214 | 32 | exit 0 | ||
215 | 0 | 33 | ||
216 | === added file 'hooks/charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh' | |||
217 | --- hooks/charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh 1970-01-01 00:00:00 +0000 | |||
218 | +++ hooks/charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh 2015-02-24 06:05:56 +0000 | |||
219 | @@ -0,0 +1,30 @@ | |||
220 | 1 | #!/bin/bash | ||
221 | 2 | #-------------------------------------------- | ||
222 | 3 | # This file is managed by Juju | ||
223 | 4 | #-------------------------------------------- | ||
224 | 5 | # | ||
225 | 6 | # Copyright 2009,2012 Canonical Ltd. | ||
226 | 7 | # Author: Tom Haddon | ||
227 | 8 | |||
228 | 9 | # These should be config options at some stage | ||
229 | 10 | CURRQthrsh=0 | ||
230 | 11 | MAXQthrsh=100 | ||
231 | 12 | |||
232 | 13 | AUTH=$(grep -r "stats auth" /etc/haproxy | head -1 | awk '{print $4}') | ||
233 | 14 | |||
234 | 15 | HAPROXYSTATS=$(/usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 -u '/;csv' -v) | ||
235 | 16 | |||
236 | 17 | for BACKEND in $(echo $HAPROXYSTATS| xargs -n1 | grep BACKEND | awk -F , '{print $1}') | ||
237 | 18 | do | ||
238 | 19 | CURRQ=$(echo "$HAPROXYSTATS" | grep $BACKEND | grep BACKEND | cut -d , -f 3) | ||
239 | 20 | MAXQ=$(echo "$HAPROXYSTATS" | grep $BACKEND | grep BACKEND | cut -d , -f 4) | ||
240 | 21 | |||
241 | 22 | if [[ $CURRQ -gt $CURRQthrsh || $MAXQ -gt $MAXQthrsh ]] ; then | ||
242 | 23 | echo "CRITICAL: queue depth for $BACKEND - CURRENT:$CURRQ MAX:$MAXQ" | ||
243 | 24 | exit 2 | ||
244 | 25 | fi | ||
245 | 26 | done | ||
246 | 27 | |||
247 | 28 | echo "OK: All haproxy queue depths looking good" | ||
248 | 29 | exit 0 | ||
249 | 30 | |||
250 | 0 | 31 | ||
251 | === modified file 'hooks/charmhelpers/contrib/openstack/ip.py' | |||
252 | --- hooks/charmhelpers/contrib/openstack/ip.py 2015-01-26 09:47:56 +0000 | |||
253 | +++ hooks/charmhelpers/contrib/openstack/ip.py 2015-02-24 06:05:56 +0000 | |||
254 | @@ -26,6 +26,8 @@ | |||
255 | 26 | ) | 26 | ) |
256 | 27 | from charmhelpers.contrib.hahelpers.cluster import is_clustered | 27 | from charmhelpers.contrib.hahelpers.cluster import is_clustered |
257 | 28 | 28 | ||
258 | 29 | from functools import partial | ||
259 | 30 | |||
260 | 29 | PUBLIC = 'public' | 31 | PUBLIC = 'public' |
261 | 30 | INTERNAL = 'int' | 32 | INTERNAL = 'int' |
262 | 31 | ADMIN = 'admin' | 33 | ADMIN = 'admin' |
263 | @@ -107,3 +109,38 @@ | |||
264 | 107 | "clustered=%s)" % (net_type, clustered)) | 109 | "clustered=%s)" % (net_type, clustered)) |
265 | 108 | 110 | ||
266 | 109 | return resolved_address | 111 | return resolved_address |
267 | 112 | |||
268 | 113 | |||
269 | 114 | def endpoint_url(configs, url_template, port, endpoint_type=PUBLIC, | ||
270 | 115 | override=None): | ||
271 | 116 | """Returns the correct endpoint URL to advertise to Keystone. | ||
272 | 117 | |||
273 | 118 | This method provides the correct endpoint URL which should be advertised to | ||
274 | 119 | the keystone charm for endpoint creation. This method allows for the url to | ||
275 | 120 | be overridden to force a keystone endpoint to have specific URL for any of | ||
276 | 121 | the defined scopes (admin, internal, public). | ||
277 | 122 | |||
278 | 123 | :param configs: OSTemplateRenderer config templating object to inspect | ||
279 | 124 | for a complete https context. | ||
280 | 125 | :param url_template: str format string for creating the url template. Only | ||
281 | 126 | two values will be passed - the scheme+hostname | ||
282 | 127 | returned by the canonical_url and the port. | ||
283 | 128 | :param endpoint_type: str endpoint type to resolve. | ||
284 | 129 | :param override: str the name of the config option which overrides the | ||
285 | 130 | endpoint URL defined by the charm itself. None will | ||
286 | 131 | disable any overrides (default). | ||
287 | 132 | """ | ||
288 | 133 | if override: | ||
289 | 134 | # Return any user-defined overrides for the keystone endpoint URL. | ||
290 | 135 | user_value = config(override) | ||
291 | 136 | if user_value: | ||
292 | 137 | return user_value.strip() | ||
293 | 138 | |||
294 | 139 | return url_template % (canonical_url(configs, endpoint_type), port) | ||
295 | 140 | |||
296 | 141 | |||
297 | 142 | public_endpoint = partial(endpoint_url, endpoint_type=PUBLIC) | ||
298 | 143 | |||
299 | 144 | internal_endpoint = partial(endpoint_url, endpoint_type=INTERNAL) | ||
300 | 145 | |||
301 | 146 | admin_endpoint = partial(endpoint_url, endpoint_type=ADMIN) | ||
302 | 110 | 147 | ||
303 | === modified file 'hooks/charmhelpers/contrib/openstack/utils.py' | |||
304 | --- hooks/charmhelpers/contrib/openstack/utils.py 2015-01-26 09:47:56 +0000 | |||
305 | +++ hooks/charmhelpers/contrib/openstack/utils.py 2015-02-24 06:05:56 +0000 | |||
306 | @@ -103,6 +103,7 @@ | |||
307 | 103 | ('2.1.0', 'juno'), | 103 | ('2.1.0', 'juno'), |
308 | 104 | ('2.2.0', 'juno'), | 104 | ('2.2.0', 'juno'), |
309 | 105 | ('2.2.1', 'kilo'), | 105 | ('2.2.1', 'kilo'), |
310 | 106 | ('2.2.2', 'kilo'), | ||
311 | 106 | ]) | 107 | ]) |
312 | 107 | 108 | ||
313 | 108 | DEFAULT_LOOPBACK_SIZE = '5G' | 109 | DEFAULT_LOOPBACK_SIZE = '5G' |
314 | 109 | 110 | ||
315 | === modified file 'hooks/charmhelpers/contrib/python/packages.py' | |||
316 | --- hooks/charmhelpers/contrib/python/packages.py 2015-01-26 09:47:56 +0000 | |||
317 | +++ hooks/charmhelpers/contrib/python/packages.py 2015-02-24 06:05:56 +0000 | |||
318 | @@ -17,8 +17,6 @@ | |||
319 | 17 | # You should have received a copy of the GNU Lesser General Public License | 17 | # You should have received a copy of the GNU Lesser General Public License |
320 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. |
321 | 19 | 19 | ||
322 | 20 | __author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>" | ||
323 | 21 | |||
324 | 22 | from charmhelpers.fetch import apt_install, apt_update | 20 | from charmhelpers.fetch import apt_install, apt_update |
325 | 23 | from charmhelpers.core.hookenv import log | 21 | from charmhelpers.core.hookenv import log |
326 | 24 | 22 | ||
327 | @@ -29,6 +27,8 @@ | |||
328 | 29 | apt_install('python-pip') | 27 | apt_install('python-pip') |
329 | 30 | from pip import main as pip_execute | 28 | from pip import main as pip_execute |
330 | 31 | 29 | ||
331 | 30 | __author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>" | ||
332 | 31 | |||
333 | 32 | 32 | ||
334 | 33 | def parse_options(given, available): | 33 | def parse_options(given, available): |
335 | 34 | """Given a set of options, check if available""" | 34 | """Given a set of options, check if available""" |
336 | 35 | 35 | ||
337 | === modified file 'hooks/charmhelpers/core/fstab.py' | |||
338 | --- hooks/charmhelpers/core/fstab.py 2015-01-26 09:47:56 +0000 | |||
339 | +++ hooks/charmhelpers/core/fstab.py 2015-02-24 06:05:56 +0000 | |||
340 | @@ -17,11 +17,11 @@ | |||
341 | 17 | # You should have received a copy of the GNU Lesser General Public License | 17 | # You should have received a copy of the GNU Lesser General Public License |
342 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. |
343 | 19 | 19 | ||
344 | 20 | __author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>' | ||
345 | 21 | |||
346 | 22 | import io | 20 | import io |
347 | 23 | import os | 21 | import os |
348 | 24 | 22 | ||
349 | 23 | __author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>' | ||
350 | 24 | |||
351 | 25 | 25 | ||
352 | 26 | class Fstab(io.FileIO): | 26 | class Fstab(io.FileIO): |
353 | 27 | """This class extends file in order to implement a file reader/writer | 27 | """This class extends file in order to implement a file reader/writer |
354 | @@ -77,7 +77,7 @@ | |||
355 | 77 | for line in self.readlines(): | 77 | for line in self.readlines(): |
356 | 78 | line = line.decode('us-ascii') | 78 | line = line.decode('us-ascii') |
357 | 79 | try: | 79 | try: |
359 | 80 | if line.strip() and not line.startswith("#"): | 80 | if line.strip() and not line.strip().startswith("#"): |
360 | 81 | yield self._hydrate_entry(line) | 81 | yield self._hydrate_entry(line) |
361 | 82 | except ValueError: | 82 | except ValueError: |
362 | 83 | pass | 83 | pass |
363 | @@ -104,7 +104,7 @@ | |||
364 | 104 | 104 | ||
365 | 105 | found = False | 105 | found = False |
366 | 106 | for index, line in enumerate(lines): | 106 | for index, line in enumerate(lines): |
368 | 107 | if not line.startswith("#"): | 107 | if line.strip() and not line.strip().startswith("#"): |
369 | 108 | if self._hydrate_entry(line) == entry: | 108 | if self._hydrate_entry(line) == entry: |
370 | 109 | found = True | 109 | found = True |
371 | 110 | break | 110 | break |
372 | 111 | 111 | ||
373 | === modified file 'hooks/charmhelpers/core/host.py' | |||
374 | --- hooks/charmhelpers/core/host.py 2015-01-26 09:47:56 +0000 | |||
375 | +++ hooks/charmhelpers/core/host.py 2015-02-24 06:05:56 +0000 | |||
376 | @@ -191,11 +191,11 @@ | |||
377 | 191 | 191 | ||
378 | 192 | 192 | ||
379 | 193 | def write_file(path, content, owner='root', group='root', perms=0o444): | 193 | def write_file(path, content, owner='root', group='root', perms=0o444): |
381 | 194 | """Create or overwrite a file with the contents of a string""" | 194 | """Create or overwrite a file with the contents of a byte string.""" |
382 | 195 | log("Writing file {} {}:{} {:o}".format(path, owner, group, perms)) | 195 | log("Writing file {} {}:{} {:o}".format(path, owner, group, perms)) |
383 | 196 | uid = pwd.getpwnam(owner).pw_uid | 196 | uid = pwd.getpwnam(owner).pw_uid |
384 | 197 | gid = grp.getgrnam(group).gr_gid | 197 | gid = grp.getgrnam(group).gr_gid |
386 | 198 | with open(path, 'w') as target: | 198 | with open(path, 'wb') as target: |
387 | 199 | os.fchown(target.fileno(), uid, gid) | 199 | os.fchown(target.fileno(), uid, gid) |
388 | 200 | os.fchmod(target.fileno(), perms) | 200 | os.fchmod(target.fileno(), perms) |
389 | 201 | target.write(content) | 201 | target.write(content) |
390 | @@ -305,11 +305,11 @@ | |||
391 | 305 | ceph_client_changed function. | 305 | ceph_client_changed function. |
392 | 306 | """ | 306 | """ |
393 | 307 | def wrap(f): | 307 | def wrap(f): |
395 | 308 | def wrapped_f(*args): | 308 | def wrapped_f(*args, **kwargs): |
396 | 309 | checksums = {} | 309 | checksums = {} |
397 | 310 | for path in restart_map: | 310 | for path in restart_map: |
398 | 311 | checksums[path] = file_hash(path) | 311 | checksums[path] = file_hash(path) |
400 | 312 | f(*args) | 312 | f(*args, **kwargs) |
401 | 313 | restarts = [] | 313 | restarts = [] |
402 | 314 | for path in restart_map: | 314 | for path in restart_map: |
403 | 315 | if checksums[path] != file_hash(path): | 315 | if checksums[path] != file_hash(path): |
404 | @@ -361,7 +361,7 @@ | |||
405 | 361 | ip_output = (line for line in ip_output if line) | 361 | ip_output = (line for line in ip_output if line) |
406 | 362 | for line in ip_output: | 362 | for line in ip_output: |
407 | 363 | if line.split()[1].startswith(int_type): | 363 | if line.split()[1].startswith(int_type): |
409 | 364 | matched = re.search('.*: (bond[0-9]+\.[0-9]+)@.*', line) | 364 | matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line) |
410 | 365 | if matched: | 365 | if matched: |
411 | 366 | interface = matched.groups()[0] | 366 | interface = matched.groups()[0] |
412 | 367 | else: | 367 | else: |
413 | 368 | 368 | ||
414 | === added file 'hooks/charmhelpers/core/strutils.py' | |||
415 | --- hooks/charmhelpers/core/strutils.py 1970-01-01 00:00:00 +0000 | |||
416 | +++ hooks/charmhelpers/core/strutils.py 2015-02-24 06:05:56 +0000 | |||
417 | @@ -0,0 +1,42 @@ | |||
418 | 1 | #!/usr/bin/env python | ||
419 | 2 | # -*- coding: utf-8 -*- | ||
420 | 3 | |||
421 | 4 | # Copyright 2014-2015 Canonical Limited. | ||
422 | 5 | # | ||
423 | 6 | # This file is part of charm-helpers. | ||
424 | 7 | # | ||
425 | 8 | # charm-helpers is free software: you can redistribute it and/or modify | ||
426 | 9 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
427 | 10 | # published by the Free Software Foundation. | ||
428 | 11 | # | ||
429 | 12 | # charm-helpers is distributed in the hope that it will be useful, | ||
430 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
431 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
432 | 15 | # GNU Lesser General Public License for more details. | ||
433 | 16 | # | ||
434 | 17 | # You should have received a copy of the GNU Lesser General Public License | ||
435 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
436 | 19 | |||
437 | 20 | import six | ||
438 | 21 | |||
439 | 22 | |||
440 | 23 | def bool_from_string(value): | ||
441 | 24 | """Interpret string value as boolean. | ||
442 | 25 | |||
443 | 26 | Returns True if value translates to True otherwise False. | ||
444 | 27 | """ | ||
445 | 28 | if isinstance(value, six.string_types): | ||
446 | 29 | value = six.text_type(value) | ||
447 | 30 | else: | ||
448 | 31 | msg = "Unable to interpret non-string value '%s' as boolean" % (value) | ||
449 | 32 | raise ValueError(msg) | ||
450 | 33 | |||
451 | 34 | value = value.strip().lower() | ||
452 | 35 | |||
453 | 36 | if value in ['y', 'yes', 'true', 't']: | ||
454 | 37 | return True | ||
455 | 38 | elif value in ['n', 'no', 'false', 'f']: | ||
456 | 39 | return False | ||
457 | 40 | |||
458 | 41 | msg = "Unable to interpret string value '%s' as boolean" % (value) | ||
459 | 42 | raise ValueError(msg) | ||
460 | 0 | 43 | ||
461 | === modified file 'hooks/charmhelpers/core/sysctl.py' | |||
462 | --- hooks/charmhelpers/core/sysctl.py 2015-01-26 09:47:56 +0000 | |||
463 | +++ hooks/charmhelpers/core/sysctl.py 2015-02-24 06:05:56 +0000 | |||
464 | @@ -17,8 +17,6 @@ | |||
465 | 17 | # You should have received a copy of the GNU Lesser General Public License | 17 | # You should have received a copy of the GNU Lesser General Public License |
466 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. |
467 | 19 | 19 | ||
468 | 20 | __author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>' | ||
469 | 21 | |||
470 | 22 | import yaml | 20 | import yaml |
471 | 23 | 21 | ||
472 | 24 | from subprocess import check_call | 22 | from subprocess import check_call |
473 | @@ -26,25 +24,33 @@ | |||
474 | 26 | from charmhelpers.core.hookenv import ( | 24 | from charmhelpers.core.hookenv import ( |
475 | 27 | log, | 25 | log, |
476 | 28 | DEBUG, | 26 | DEBUG, |
477 | 27 | ERROR, | ||
478 | 29 | ) | 28 | ) |
479 | 30 | 29 | ||
480 | 30 | __author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>' | ||
481 | 31 | |||
482 | 31 | 32 | ||
483 | 32 | def create(sysctl_dict, sysctl_file): | 33 | def create(sysctl_dict, sysctl_file): |
484 | 33 | """Creates a sysctl.conf file from a YAML associative array | 34 | """Creates a sysctl.conf file from a YAML associative array |
485 | 34 | 35 | ||
488 | 35 | :param sysctl_dict: a dict of sysctl options eg { 'kernel.max_pid': 1337 } | 36 | :param sysctl_dict: a YAML-formatted string of sysctl options eg "{ 'kernel.max_pid': 1337 }" |
489 | 36 | :type sysctl_dict: dict | 37 | :type sysctl_dict: str |
490 | 37 | :param sysctl_file: path to the sysctl file to be saved | 38 | :param sysctl_file: path to the sysctl file to be saved |
491 | 38 | :type sysctl_file: str or unicode | 39 | :type sysctl_file: str or unicode |
492 | 39 | :returns: None | 40 | :returns: None |
493 | 40 | """ | 41 | """ |
495 | 41 | sysctl_dict = yaml.load(sysctl_dict) | 42 | try: |
496 | 43 | sysctl_dict_parsed = yaml.safe_load(sysctl_dict) | ||
497 | 44 | except yaml.YAMLError: | ||
498 | 45 | log("Error parsing YAML sysctl_dict: {}".format(sysctl_dict), | ||
499 | 46 | level=ERROR) | ||
500 | 47 | return | ||
501 | 42 | 48 | ||
502 | 43 | with open(sysctl_file, "w") as fd: | 49 | with open(sysctl_file, "w") as fd: |
504 | 44 | for key, value in sysctl_dict.items(): | 50 | for key, value in sysctl_dict_parsed.items(): |
505 | 45 | fd.write("{}={}\n".format(key, value)) | 51 | fd.write("{}={}\n".format(key, value)) |
506 | 46 | 52 | ||
508 | 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), |
509 | 48 | level=DEBUG) | 54 | level=DEBUG) |
510 | 49 | 55 | ||
511 | 50 | check_call(["sysctl", "-p", sysctl_file]) | 56 | check_call(["sysctl", "-p", sysctl_file]) |
512 | 51 | 57 | ||
513 | === modified file 'hooks/charmhelpers/core/templating.py' | |||
514 | --- hooks/charmhelpers/core/templating.py 2015-01-26 09:47:56 +0000 | |||
515 | +++ hooks/charmhelpers/core/templating.py 2015-02-24 06:05:56 +0000 | |||
516 | @@ -21,7 +21,7 @@ | |||
517 | 21 | 21 | ||
518 | 22 | 22 | ||
519 | 23 | def render(source, target, context, owner='root', group='root', | 23 | def render(source, target, context, owner='root', group='root', |
521 | 24 | perms=0o444, templates_dir=None): | 24 | perms=0o444, templates_dir=None, encoding='UTF-8'): |
522 | 25 | """ | 25 | """ |
523 | 26 | Render a template. | 26 | Render a template. |
524 | 27 | 27 | ||
525 | @@ -64,5 +64,5 @@ | |||
526 | 64 | level=hookenv.ERROR) | 64 | level=hookenv.ERROR) |
527 | 65 | raise e | 65 | raise e |
528 | 66 | content = template.render(context) | 66 | content = template.render(context) |
531 | 67 | host.mkdir(os.path.dirname(target), owner, group) | 67 | host.mkdir(os.path.dirname(target), owner, group, perms=0o755) |
532 | 68 | host.write_file(target, content, owner, group, perms) | 68 | host.write_file(target, content.encode(encoding), owner, group, perms) |
533 | 69 | 69 | ||
534 | === added file 'hooks/charmhelpers/core/unitdata.py' | |||
535 | --- hooks/charmhelpers/core/unitdata.py 1970-01-01 00:00:00 +0000 | |||
536 | +++ hooks/charmhelpers/core/unitdata.py 2015-02-24 06:05:56 +0000 | |||
537 | @@ -0,0 +1,477 @@ | |||
538 | 1 | #!/usr/bin/env python | ||
539 | 2 | # -*- coding: utf-8 -*- | ||
540 | 3 | # | ||
541 | 4 | # Copyright 2014-2015 Canonical Limited. | ||
542 | 5 | # | ||
543 | 6 | # This file is part of charm-helpers. | ||
544 | 7 | # | ||
545 | 8 | # charm-helpers is free software: you can redistribute it and/or modify | ||
546 | 9 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
547 | 10 | # published by the Free Software Foundation. | ||
548 | 11 | # | ||
549 | 12 | # charm-helpers is distributed in the hope that it will be useful, | ||
550 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
551 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
552 | 15 | # GNU Lesser General Public License for more details. | ||
553 | 16 | # | ||
554 | 17 | # You should have received a copy of the GNU Lesser General Public License | ||
555 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
556 | 19 | # | ||
557 | 20 | # | ||
558 | 21 | # Authors: | ||
559 | 22 | # Kapil Thangavelu <kapil.foss@gmail.com> | ||
560 | 23 | # | ||
561 | 24 | """ | ||
562 | 25 | Intro | ||
563 | 26 | ----- | ||
564 | 27 | |||
565 | 28 | A simple way to store state in units. This provides a key value | ||
566 | 29 | storage with support for versioned, transactional operation, | ||
567 | 30 | and can calculate deltas from previous values to simplify unit logic | ||
568 | 31 | when processing changes. | ||
569 | 32 | |||
570 | 33 | |||
571 | 34 | Hook Integration | ||
572 | 35 | ---------------- | ||
573 | 36 | |||
574 | 37 | There are several extant frameworks for hook execution, including | ||
575 | 38 | |||
576 | 39 | - charmhelpers.core.hookenv.Hooks | ||
577 | 40 | - charmhelpers.core.services.ServiceManager | ||
578 | 41 | |||
579 | 42 | The storage classes are framework agnostic, one simple integration is | ||
580 | 43 | via the HookData contextmanager. It will record the current hook | ||
581 | 44 | execution environment (including relation data, config data, etc.), | ||
582 | 45 | setup a transaction and allow easy access to the changes from | ||
583 | 46 | previously seen values. One consequence of the integration is the | ||
584 | 47 | reservation of particular keys ('rels', 'unit', 'env', 'config', | ||
585 | 48 | 'charm_revisions') for their respective values. | ||
586 | 49 | |||
587 | 50 | Here's a fully worked integration example using hookenv.Hooks:: | ||
588 | 51 | |||
589 | 52 | from charmhelper.core import hookenv, unitdata | ||
590 | 53 | |||
591 | 54 | hook_data = unitdata.HookData() | ||
592 | 55 | db = unitdata.kv() | ||
593 | 56 | hooks = hookenv.Hooks() | ||
594 | 57 | |||
595 | 58 | @hooks.hook | ||
596 | 59 | def config_changed(): | ||
597 | 60 | # Print all changes to configuration from previously seen | ||
598 | 61 | # values. | ||
599 | 62 | for changed, (prev, cur) in hook_data.conf.items(): | ||
600 | 63 | print('config changed', changed, | ||
601 | 64 | 'previous value', prev, | ||
602 | 65 | 'current value', cur) | ||
603 | 66 | |||
604 | 67 | # Get some unit specific bookeeping | ||
605 | 68 | if not db.get('pkg_key'): | ||
606 | 69 | key = urllib.urlopen('https://example.com/pkg_key').read() | ||
607 | 70 | db.set('pkg_key', key) | ||
608 | 71 | |||
609 | 72 | # Directly access all charm config as a mapping. | ||
610 | 73 | conf = db.getrange('config', True) | ||
611 | 74 | |||
612 | 75 | # Directly access all relation data as a mapping | ||
613 | 76 | rels = db.getrange('rels', True) | ||
614 | 77 | |||
615 | 78 | if __name__ == '__main__': | ||
616 | 79 | with hook_data(): | ||
617 | 80 | hook.execute() | ||
618 | 81 | |||
619 | 82 | |||
620 | 83 | A more basic integration is via the hook_scope context manager which simply | ||
621 | 84 | manages transaction scope (and records hook name, and timestamp):: | ||
622 | 85 | |||
623 | 86 | >>> from unitdata import kv | ||
624 | 87 | >>> db = kv() | ||
625 | 88 | >>> with db.hook_scope('install'): | ||
626 | 89 | ... # do work, in transactional scope. | ||
627 | 90 | ... db.set('x', 1) | ||
628 | 91 | >>> db.get('x') | ||
629 | 92 | 1 | ||
630 | 93 | |||
631 | 94 | |||
632 | 95 | Usage | ||
633 | 96 | ----- | ||
634 | 97 | |||
635 | 98 | Values are automatically json de/serialized to preserve basic typing | ||
636 | 99 | and complex data struct capabilities (dicts, lists, ints, booleans, etc). | ||
637 | 100 | |||
638 | 101 | Individual values can be manipulated via get/set:: | ||
639 | 102 | |||
640 | 103 | >>> kv.set('y', True) | ||
641 | 104 | >>> kv.get('y') | ||
642 | 105 | True | ||
643 | 106 | |||
644 | 107 | # We can set complex values (dicts, lists) as a single key. | ||
645 | 108 | >>> kv.set('config', {'a': 1, 'b': True'}) | ||
646 | 109 | |||
647 | 110 | # Also supports returning dictionaries as a record which | ||
648 | 111 | # provides attribute access. | ||
649 | 112 | >>> config = kv.get('config', record=True) | ||
650 | 113 | >>> config.b | ||
651 | 114 | True | ||
652 | 115 | |||
653 | 116 | |||
654 | 117 | Groups of keys can be manipulated with update/getrange:: | ||
655 | 118 | |||
656 | 119 | >>> kv.update({'z': 1, 'y': 2}, prefix="gui.") | ||
657 | 120 | >>> kv.getrange('gui.', strip=True) | ||
658 | 121 | {'z': 1, 'y': 2} | ||
659 | 122 | |||
660 | 123 | When updating values, its very helpful to understand which values | ||
661 | 124 | have actually changed and how have they changed. The storage | ||
662 | 125 | provides a delta method to provide for this:: | ||
663 | 126 | |||
664 | 127 | >>> data = {'debug': True, 'option': 2} | ||
665 | 128 | >>> delta = kv.delta(data, 'config.') | ||
666 | 129 | >>> delta.debug.previous | ||
667 | 130 | None | ||
668 | 131 | >>> delta.debug.current | ||
669 | 132 | True | ||
670 | 133 | >>> delta | ||
671 | 134 | {'debug': (None, True), 'option': (None, 2)} | ||
672 | 135 | |||
673 | 136 | Note the delta method does not persist the actual change, it needs to | ||
674 | 137 | be explicitly saved via 'update' method:: | ||
675 | 138 | |||
676 | 139 | >>> kv.update(data, 'config.') | ||
677 | 140 | |||
678 | 141 | Values modified in the context of a hook scope retain historical values | ||
679 | 142 | associated to the hookname. | ||
680 | 143 | |||
681 | 144 | >>> with db.hook_scope('config-changed'): | ||
682 | 145 | ... db.set('x', 42) | ||
683 | 146 | >>> db.gethistory('x') | ||
684 | 147 | [(1, u'x', 1, u'install', u'2015-01-21T16:49:30.038372'), | ||
685 | 148 | (2, u'x', 42, u'config-changed', u'2015-01-21T16:49:30.038786')] | ||
686 | 149 | |||
687 | 150 | """ | ||
688 | 151 | |||
689 | 152 | import collections | ||
690 | 153 | import contextlib | ||
691 | 154 | import datetime | ||
692 | 155 | import json | ||
693 | 156 | import os | ||
694 | 157 | import pprint | ||
695 | 158 | import sqlite3 | ||
696 | 159 | import sys | ||
697 | 160 | |||
698 | 161 | __author__ = 'Kapil Thangavelu <kapil.foss@gmail.com>' | ||
699 | 162 | |||
700 | 163 | |||
701 | 164 | class Storage(object): | ||
702 | 165 | """Simple key value database for local unit state within charms. | ||
703 | 166 | |||
704 | 167 | Modifications are automatically committed at hook exit. That's | ||
705 | 168 | currently regardless of exit code. | ||
706 | 169 | |||
707 | 170 | To support dicts, lists, integer, floats, and booleans values | ||
708 | 171 | are automatically json encoded/decoded. | ||
709 | 172 | """ | ||
710 | 173 | def __init__(self, path=None): | ||
711 | 174 | self.db_path = path | ||
712 | 175 | if path is None: | ||
713 | 176 | self.db_path = os.path.join( | ||
714 | 177 | os.environ.get('CHARM_DIR', ''), '.unit-state.db') | ||
715 | 178 | self.conn = sqlite3.connect('%s' % self.db_path) | ||
716 | 179 | self.cursor = self.conn.cursor() | ||
717 | 180 | self.revision = None | ||
718 | 181 | self._closed = False | ||
719 | 182 | self._init() | ||
720 | 183 | |||
721 | 184 | def close(self): | ||
722 | 185 | if self._closed: | ||
723 | 186 | return | ||
724 | 187 | self.flush(False) | ||
725 | 188 | self.cursor.close() | ||
726 | 189 | self.conn.close() | ||
727 | 190 | self._closed = True | ||
728 | 191 | |||
729 | 192 | def _scoped_query(self, stmt, params=None): | ||
730 | 193 | if params is None: | ||
731 | 194 | params = [] | ||
732 | 195 | return stmt, params | ||
733 | 196 | |||
734 | 197 | def get(self, key, default=None, record=False): | ||
735 | 198 | self.cursor.execute( | ||
736 | 199 | *self._scoped_query( | ||
737 | 200 | 'select data from kv where key=?', [key])) | ||
738 | 201 | result = self.cursor.fetchone() | ||
739 | 202 | if not result: | ||
740 | 203 | return default | ||
741 | 204 | if record: | ||
742 | 205 | return Record(json.loads(result[0])) | ||
743 | 206 | return json.loads(result[0]) | ||
744 | 207 | |||
745 | 208 | def getrange(self, key_prefix, strip=False): | ||
746 | 209 | stmt = "select key, data from kv where key like '%s%%'" % key_prefix | ||
747 | 210 | self.cursor.execute(*self._scoped_query(stmt)) | ||
748 | 211 | result = self.cursor.fetchall() | ||
749 | 212 | |||
750 | 213 | if not result: | ||
751 | 214 | return None | ||
752 | 215 | if not strip: | ||
753 | 216 | key_prefix = '' | ||
754 | 217 | return dict([ | ||
755 | 218 | (k[len(key_prefix):], json.loads(v)) for k, v in result]) | ||
756 | 219 | |||
757 | 220 | def update(self, mapping, prefix=""): | ||
758 | 221 | for k, v in mapping.items(): | ||
759 | 222 | self.set("%s%s" % (prefix, k), v) | ||
760 | 223 | |||
761 | 224 | def unset(self, key): | ||
762 | 225 | self.cursor.execute('delete from kv where key=?', [key]) | ||
763 | 226 | if self.revision and self.cursor.rowcount: | ||
764 | 227 | self.cursor.execute( | ||
765 | 228 | 'insert into kv_revisions values (?, ?, ?)', | ||
766 | 229 | [key, self.revision, json.dumps('DELETED')]) | ||
767 | 230 | |||
768 | 231 | def set(self, key, value): | ||
769 | 232 | serialized = json.dumps(value) | ||
770 | 233 | |||
771 | 234 | self.cursor.execute( | ||
772 | 235 | 'select data from kv where key=?', [key]) | ||
773 | 236 | exists = self.cursor.fetchone() | ||
774 | 237 | |||
775 | 238 | # Skip mutations to the same value | ||
776 | 239 | if exists: | ||
777 | 240 | if exists[0] == serialized: | ||
778 | 241 | return value | ||
779 | 242 | |||
780 | 243 | if not exists: | ||
781 | 244 | self.cursor.execute( | ||
782 | 245 | 'insert into kv (key, data) values (?, ?)', | ||
783 | 246 | (key, serialized)) | ||
784 | 247 | else: | ||
785 | 248 | self.cursor.execute(''' | ||
786 | 249 | update kv | ||
787 | 250 | set data = ? | ||
788 | 251 | where key = ?''', [serialized, key]) | ||
789 | 252 | |||
790 | 253 | # Save | ||
791 | 254 | if not self.revision: | ||
792 | 255 | return value | ||
793 | 256 | |||
794 | 257 | self.cursor.execute( | ||
795 | 258 | 'select 1 from kv_revisions where key=? and revision=?', | ||
796 | 259 | [key, self.revision]) | ||
797 | 260 | exists = self.cursor.fetchone() | ||
798 | 261 | |||
799 | 262 | if not exists: | ||
800 | 263 | self.cursor.execute( | ||
801 | 264 | '''insert into kv_revisions ( | ||
802 | 265 | revision, key, data) values (?, ?, ?)''', | ||
803 | 266 | (self.revision, key, serialized)) | ||
804 | 267 | else: | ||
805 | 268 | self.cursor.execute( | ||
806 | 269 | ''' | ||
807 | 270 | update kv_revisions | ||
808 | 271 | set data = ? | ||
809 | 272 | where key = ? | ||
810 | 273 | and revision = ?''', | ||
811 | 274 | [serialized, key, self.revision]) | ||
812 | 275 | |||
813 | 276 | return value | ||
814 | 277 | |||
815 | 278 | def delta(self, mapping, prefix): | ||
816 | 279 | """ | ||
817 | 280 | return a delta containing values that have changed. | ||
818 | 281 | """ | ||
819 | 282 | previous = self.getrange(prefix, strip=True) | ||
820 | 283 | if not previous: | ||
821 | 284 | pk = set() | ||
822 | 285 | else: | ||
823 | 286 | pk = set(previous.keys()) | ||
824 | 287 | ck = set(mapping.keys()) | ||
825 | 288 | delta = DeltaSet() | ||
826 | 289 | |||
827 | 290 | # added | ||
828 | 291 | for k in ck.difference(pk): | ||
829 | 292 | delta[k] = Delta(None, mapping[k]) | ||
830 | 293 | |||
831 | 294 | # removed | ||
832 | 295 | for k in pk.difference(ck): | ||
833 | 296 | delta[k] = Delta(previous[k], None) | ||
834 | 297 | |||
835 | 298 | # changed | ||
836 | 299 | for k in pk.intersection(ck): | ||
837 | 300 | c = mapping[k] | ||
838 | 301 | p = previous[k] | ||
839 | 302 | if c != p: | ||
840 | 303 | delta[k] = Delta(p, c) | ||
841 | 304 | |||
842 | 305 | return delta | ||
843 | 306 | |||
844 | 307 | @contextlib.contextmanager | ||
845 | 308 | def hook_scope(self, name=""): | ||
846 | 309 | """Scope all future interactions to the current hook execution | ||
847 | 310 | revision.""" | ||
848 | 311 | assert not self.revision | ||
849 | 312 | self.cursor.execute( | ||
850 | 313 | 'insert into hooks (hook, date) values (?, ?)', | ||
851 | 314 | (name or sys.argv[0], | ||
852 | 315 | datetime.datetime.utcnow().isoformat())) | ||
853 | 316 | self.revision = self.cursor.lastrowid | ||
854 | 317 | try: | ||
855 | 318 | yield self.revision | ||
856 | 319 | self.revision = None | ||
857 | 320 | except: | ||
858 | 321 | self.flush(False) | ||
859 | 322 | self.revision = None | ||
860 | 323 | raise | ||
861 | 324 | else: | ||
862 | 325 | self.flush() | ||
863 | 326 | |||
864 | 327 | def flush(self, save=True): | ||
865 | 328 | if save: | ||
866 | 329 | self.conn.commit() | ||
867 | 330 | elif self._closed: | ||
868 | 331 | return | ||
869 | 332 | else: | ||
870 | 333 | self.conn.rollback() | ||
871 | 334 | |||
872 | 335 | def _init(self): | ||
873 | 336 | self.cursor.execute(''' | ||
874 | 337 | create table if not exists kv ( | ||
875 | 338 | key text, | ||
876 | 339 | data text, | ||
877 | 340 | primary key (key) | ||
878 | 341 | )''') | ||
879 | 342 | self.cursor.execute(''' | ||
880 | 343 | create table if not exists kv_revisions ( | ||
881 | 344 | key text, | ||
882 | 345 | revision integer, | ||
883 | 346 | data text, | ||
884 | 347 | primary key (key, revision) | ||
885 | 348 | )''') | ||
886 | 349 | self.cursor.execute(''' | ||
887 | 350 | create table if not exists hooks ( | ||
888 | 351 | version integer primary key autoincrement, | ||
889 | 352 | hook text, | ||
890 | 353 | date text | ||
891 | 354 | )''') | ||
892 | 355 | self.conn.commit() | ||
893 | 356 | |||
894 | 357 | def gethistory(self, key, deserialize=False): | ||
895 | 358 | self.cursor.execute( | ||
896 | 359 | ''' | ||
897 | 360 | select kv.revision, kv.key, kv.data, h.hook, h.date | ||
898 | 361 | from kv_revisions kv, | ||
899 | 362 | hooks h | ||
900 | 363 | where kv.key=? | ||
901 | 364 | and kv.revision = h.version | ||
902 | 365 | ''', [key]) | ||
903 | 366 | if deserialize is False: | ||
904 | 367 | return self.cursor.fetchall() | ||
905 | 368 | return map(_parse_history, self.cursor.fetchall()) | ||
906 | 369 | |||
907 | 370 | def debug(self, fh=sys.stderr): | ||
908 | 371 | self.cursor.execute('select * from kv') | ||
909 | 372 | pprint.pprint(self.cursor.fetchall(), stream=fh) | ||
910 | 373 | self.cursor.execute('select * from kv_revisions') | ||
911 | 374 | pprint.pprint(self.cursor.fetchall(), stream=fh) | ||
912 | 375 | |||
913 | 376 | |||
914 | 377 | def _parse_history(d): | ||
915 | 378 | return (d[0], d[1], json.loads(d[2]), d[3], | ||
916 | 379 | datetime.datetime.strptime(d[-1], "%Y-%m-%dT%H:%M:%S.%f")) | ||
917 | 380 | |||
918 | 381 | |||
919 | 382 | class HookData(object): | ||
920 | 383 | """Simple integration for existing hook exec frameworks. | ||
921 | 384 | |||
922 | 385 | Records all unit information, and stores deltas for processing | ||
923 | 386 | by the hook. | ||
924 | 387 | |||
925 | 388 | Sample:: | ||
926 | 389 | |||
927 | 390 | from charmhelper.core import hookenv, unitdata | ||
928 | 391 | |||
929 | 392 | changes = unitdata.HookData() | ||
930 | 393 | db = unitdata.kv() | ||
931 | 394 | hooks = hookenv.Hooks() | ||
932 | 395 | |||
933 | 396 | @hooks.hook | ||
934 | 397 | def config_changed(): | ||
935 | 398 | # View all changes to configuration | ||
936 | 399 | for changed, (prev, cur) in changes.conf.items(): | ||
937 | 400 | print('config changed', changed, | ||
938 | 401 | 'previous value', prev, | ||
939 | 402 | 'current value', cur) | ||
940 | 403 | |||
941 | 404 | # Get some unit specific bookeeping | ||
942 | 405 | if not db.get('pkg_key'): | ||
943 | 406 | key = urllib.urlopen('https://example.com/pkg_key').read() | ||
944 | 407 | db.set('pkg_key', key) | ||
945 | 408 | |||
946 | 409 | if __name__ == '__main__': | ||
947 | 410 | with changes(): | ||
948 | 411 | hook.execute() | ||
949 | 412 | |||
950 | 413 | """ | ||
951 | 414 | def __init__(self): | ||
952 | 415 | self.kv = kv() | ||
953 | 416 | self.conf = None | ||
954 | 417 | self.rels = None | ||
955 | 418 | |||
956 | 419 | @contextlib.contextmanager | ||
957 | 420 | def __call__(self): | ||
958 | 421 | from charmhelpers.core import hookenv | ||
959 | 422 | hook_name = hookenv.hook_name() | ||
960 | 423 | |||
961 | 424 | with self.kv.hook_scope(hook_name): | ||
962 | 425 | self._record_charm_version(hookenv.charm_dir()) | ||
963 | 426 | delta_config, delta_relation = self._record_hook(hookenv) | ||
964 | 427 | yield self.kv, delta_config, delta_relation | ||
965 | 428 | |||
966 | 429 | def _record_charm_version(self, charm_dir): | ||
967 | 430 | # Record revisions.. charm revisions are meaningless | ||
968 | 431 | # to charm authors as they don't control the revision. | ||
969 | 432 | # so logic dependnent on revision is not particularly | ||
970 | 433 | # useful, however it is useful for debugging analysis. | ||
971 | 434 | charm_rev = open( | ||
972 | 435 | os.path.join(charm_dir, 'revision')).read().strip() | ||
973 | 436 | charm_rev = charm_rev or '0' | ||
974 | 437 | revs = self.kv.get('charm_revisions', []) | ||
975 | 438 | if charm_rev not in revs: | ||
976 | 439 | revs.append(charm_rev.strip() or '0') | ||
977 | 440 | self.kv.set('charm_revisions', revs) | ||
978 | 441 | |||
979 | 442 | def _record_hook(self, hookenv): | ||
980 | 443 | data = hookenv.execution_environment() | ||
981 | 444 | self.conf = conf_delta = self.kv.delta(data['conf'], 'config') | ||
982 | 445 | self.rels = rels_delta = self.kv.delta(data['rels'], 'rels') | ||
983 | 446 | self.kv.set('env', data['env']) | ||
984 | 447 | self.kv.set('unit', data['unit']) | ||
985 | 448 | self.kv.set('relid', data.get('relid')) | ||
986 | 449 | return conf_delta, rels_delta | ||
987 | 450 | |||
988 | 451 | |||
989 | 452 | class Record(dict): | ||
990 | 453 | |||
991 | 454 | __slots__ = () | ||
992 | 455 | |||
993 | 456 | def __getattr__(self, k): | ||
994 | 457 | if k in self: | ||
995 | 458 | return self[k] | ||
996 | 459 | raise AttributeError(k) | ||
997 | 460 | |||
998 | 461 | |||
999 | 462 | class DeltaSet(Record): | ||
1000 | 463 | |||
1001 | 464 | __slots__ = () | ||
1002 | 465 | |||
1003 | 466 | |||
1004 | 467 | Delta = collections.namedtuple('Delta', ['previous', 'current']) | ||
1005 | 468 | |||
1006 | 469 | |||
1007 | 470 | _KV = None | ||
1008 | 471 | |||
1009 | 472 | |||
1010 | 473 | def kv(): | ||
1011 | 474 | global _KV | ||
1012 | 475 | if _KV is None: | ||
1013 | 476 | _KV = Storage() | ||
1014 | 477 | return _KV | ||
1015 | 0 | 478 | ||
1016 | === modified file 'hooks/charmhelpers/fetch/archiveurl.py' | |||
1017 | --- hooks/charmhelpers/fetch/archiveurl.py 2015-01-26 09:47:56 +0000 | |||
1018 | +++ hooks/charmhelpers/fetch/archiveurl.py 2015-02-24 06:05:56 +0000 | |||
1019 | @@ -18,6 +18,16 @@ | |||
1020 | 18 | import hashlib | 18 | import hashlib |
1021 | 19 | import re | 19 | import re |
1022 | 20 | 20 | ||
1023 | 21 | from charmhelpers.fetch import ( | ||
1024 | 22 | BaseFetchHandler, | ||
1025 | 23 | UnhandledSource | ||
1026 | 24 | ) | ||
1027 | 25 | from charmhelpers.payload.archive import ( | ||
1028 | 26 | get_archive_handler, | ||
1029 | 27 | extract, | ||
1030 | 28 | ) | ||
1031 | 29 | from charmhelpers.core.host import mkdir, check_hash | ||
1032 | 30 | |||
1033 | 21 | import six | 31 | import six |
1034 | 22 | if six.PY3: | 32 | if six.PY3: |
1035 | 23 | from urllib.request import ( | 33 | from urllib.request import ( |
1036 | @@ -35,16 +45,6 @@ | |||
1037 | 35 | ) | 45 | ) |
1038 | 36 | from urlparse import urlparse, urlunparse, parse_qs | 46 | from urlparse import urlparse, urlunparse, parse_qs |
1039 | 37 | 47 | ||
1040 | 38 | from charmhelpers.fetch import ( | ||
1041 | 39 | BaseFetchHandler, | ||
1042 | 40 | UnhandledSource | ||
1043 | 41 | ) | ||
1044 | 42 | from charmhelpers.payload.archive import ( | ||
1045 | 43 | get_archive_handler, | ||
1046 | 44 | extract, | ||
1047 | 45 | ) | ||
1048 | 46 | from charmhelpers.core.host import mkdir, check_hash | ||
1049 | 47 | |||
1050 | 48 | 48 | ||
1051 | 49 | def splituser(host): | 49 | def splituser(host): |
1052 | 50 | '''urllib.splituser(), but six's support of this seems broken''' | 50 | '''urllib.splituser(), but six's support of this seems broken''' |
1053 | 51 | 51 | ||
1054 | === modified file 'hooks/charmhelpers/fetch/giturl.py' | |||
1055 | --- hooks/charmhelpers/fetch/giturl.py 2015-01-26 09:47:56 +0000 | |||
1056 | +++ hooks/charmhelpers/fetch/giturl.py 2015-02-24 06:05:56 +0000 | |||
1057 | @@ -32,7 +32,7 @@ | |||
1058 | 32 | apt_install("python-git") | 32 | apt_install("python-git") |
1059 | 33 | from git import Repo | 33 | from git import Repo |
1060 | 34 | 34 | ||
1062 | 35 | from git.exc import GitCommandError | 35 | from git.exc import GitCommandError # noqa E402 |
1063 | 36 | 36 | ||
1064 | 37 | 37 | ||
1065 | 38 | class GitUrlFetchHandler(BaseFetchHandler): | 38 | class GitUrlFetchHandler(BaseFetchHandler): |
1066 | 39 | 39 | ||
1067 | === modified file 'tests/basic_deployment.py' | |||
1068 | --- tests/basic_deployment.py 2014-10-09 20:25:51 +0000 | |||
1069 | +++ tests/basic_deployment.py 2015-02-24 06:05:56 +0000 | |||
1070 | @@ -20,7 +20,7 @@ | |||
1071 | 20 | class SwiftStorageBasicDeployment(OpenStackAmuletDeployment): | 20 | class SwiftStorageBasicDeployment(OpenStackAmuletDeployment): |
1072 | 21 | """Amulet tests on a basic swift-storage deployment.""" | 21 | """Amulet tests on a basic swift-storage deployment.""" |
1073 | 22 | 22 | ||
1075 | 23 | def __init__(self, series, openstack=None, source=None, stable=False): | 23 | def __init__(self, series, openstack=None, source=None, stable=True): |
1076 | 24 | """Deploy the entire test environment.""" | 24 | """Deploy the entire test environment.""" |
1077 | 25 | super(SwiftStorageBasicDeployment, self).__init__(series, openstack, | 25 | super(SwiftStorageBasicDeployment, self).__init__(series, openstack, |
1078 | 26 | source, stable) | 26 | source, stable) |
1079 | 27 | 27 | ||
1080 | === modified file 'tests/charmhelpers/contrib/amulet/utils.py' | |||
1081 | --- tests/charmhelpers/contrib/amulet/utils.py 2015-01-26 09:47:56 +0000 | |||
1082 | +++ tests/charmhelpers/contrib/amulet/utils.py 2015-02-24 06:05:56 +0000 | |||
1083 | @@ -169,8 +169,13 @@ | |||
1084 | 169 | cmd = 'pgrep -o -f {}'.format(service) | 169 | cmd = 'pgrep -o -f {}'.format(service) |
1085 | 170 | else: | 170 | else: |
1086 | 171 | cmd = 'pgrep -o {}'.format(service) | 171 | cmd = 'pgrep -o {}'.format(service) |
1089 | 172 | proc_dir = '/proc/{}'.format(sentry_unit.run(cmd)[0].strip()) | 172 | cmd = cmd + ' | grep -v pgrep || exit 0' |
1090 | 173 | return self._get_dir_mtime(sentry_unit, proc_dir) | 173 | cmd_out = sentry_unit.run(cmd) |
1091 | 174 | self.log.debug('CMDout: ' + str(cmd_out)) | ||
1092 | 175 | if cmd_out[0]: | ||
1093 | 176 | self.log.debug('Pid for %s %s' % (service, str(cmd_out[0]))) | ||
1094 | 177 | proc_dir = '/proc/{}'.format(cmd_out[0].strip()) | ||
1095 | 178 | return self._get_dir_mtime(sentry_unit, proc_dir) | ||
1096 | 174 | 179 | ||
1097 | 175 | def service_restarted(self, sentry_unit, service, filename, | 180 | def service_restarted(self, sentry_unit, service, filename, |
1098 | 176 | pgrep_full=False, sleep_time=20): | 181 | pgrep_full=False, sleep_time=20): |
1099 | @@ -187,6 +192,121 @@ | |||
1100 | 187 | else: | 192 | else: |
1101 | 188 | return False | 193 | return False |
1102 | 189 | 194 | ||
1103 | 195 | def service_restarted_since(self, sentry_unit, mtime, service, | ||
1104 | 196 | pgrep_full=False, sleep_time=20, | ||
1105 | 197 | retry_count=2): | ||
1106 | 198 | """Check if service was been started after a given time. | ||
1107 | 199 | |||
1108 | 200 | Args: | ||
1109 | 201 | sentry_unit (sentry): The sentry unit to check for the service on | ||
1110 | 202 | mtime (float): The epoch time to check against | ||
1111 | 203 | service (string): service name to look for in process table | ||
1112 | 204 | pgrep_full (boolean): Use full command line search mode with pgrep | ||
1113 | 205 | sleep_time (int): Seconds to sleep before looking for process | ||
1114 | 206 | retry_count (int): If service is not found, how many times to retry | ||
1115 | 207 | |||
1116 | 208 | Returns: | ||
1117 | 209 | bool: True if service found and its start time it newer than mtime, | ||
1118 | 210 | False if service is older than mtime or if service was | ||
1119 | 211 | not found. | ||
1120 | 212 | """ | ||
1121 | 213 | self.log.debug('Checking %s restarted since %s' % (service, mtime)) | ||
1122 | 214 | time.sleep(sleep_time) | ||
1123 | 215 | proc_start_time = self._get_proc_start_time(sentry_unit, service, | ||
1124 | 216 | pgrep_full) | ||
1125 | 217 | while retry_count > 0 and not proc_start_time: | ||
1126 | 218 | self.log.debug('No pid file found for service %s, will retry %i ' | ||
1127 | 219 | 'more times' % (service, retry_count)) | ||
1128 | 220 | time.sleep(30) | ||
1129 | 221 | proc_start_time = self._get_proc_start_time(sentry_unit, service, | ||
1130 | 222 | pgrep_full) | ||
1131 | 223 | retry_count = retry_count - 1 | ||
1132 | 224 | |||
1133 | 225 | if not proc_start_time: | ||
1134 | 226 | self.log.warn('No proc start time found, assuming service did ' | ||
1135 | 227 | 'not start') | ||
1136 | 228 | return False | ||
1137 | 229 | if proc_start_time >= mtime: | ||
1138 | 230 | self.log.debug('proc start time is newer than provided mtime' | ||
1139 | 231 | '(%s >= %s)' % (proc_start_time, mtime)) | ||
1140 | 232 | return True | ||
1141 | 233 | else: | ||
1142 | 234 | self.log.warn('proc start time (%s) is older than provided mtime ' | ||
1143 | 235 | '(%s), service did not restart' % (proc_start_time, | ||
1144 | 236 | mtime)) | ||
1145 | 237 | return False | ||
1146 | 238 | |||
1147 | 239 | def config_updated_since(self, sentry_unit, filename, mtime, | ||
1148 | 240 | sleep_time=20): | ||
1149 | 241 | """Check if file was modified after a given time. | ||
1150 | 242 | |||
1151 | 243 | Args: | ||
1152 | 244 | sentry_unit (sentry): The sentry unit to check the file mtime on | ||
1153 | 245 | filename (string): The file to check mtime of | ||
1154 | 246 | mtime (float): The epoch time to check against | ||
1155 | 247 | sleep_time (int): Seconds to sleep before looking for process | ||
1156 | 248 | |||
1157 | 249 | Returns: | ||
1158 | 250 | bool: True if file was modified more recently than mtime, False if | ||
1159 | 251 | file was modified before mtime, | ||
1160 | 252 | """ | ||
1161 | 253 | self.log.debug('Checking %s updated since %s' % (filename, mtime)) | ||
1162 | 254 | time.sleep(sleep_time) | ||
1163 | 255 | file_mtime = self._get_file_mtime(sentry_unit, filename) | ||
1164 | 256 | if file_mtime >= mtime: | ||
1165 | 257 | self.log.debug('File mtime is newer than provided mtime ' | ||
1166 | 258 | '(%s >= %s)' % (file_mtime, mtime)) | ||
1167 | 259 | return True | ||
1168 | 260 | else: | ||
1169 | 261 | self.log.warn('File mtime %s is older than provided mtime %s' | ||
1170 | 262 | % (file_mtime, mtime)) | ||
1171 | 263 | return False | ||
1172 | 264 | |||
1173 | 265 | def validate_service_config_changed(self, sentry_unit, mtime, service, | ||
1174 | 266 | filename, pgrep_full=False, | ||
1175 | 267 | sleep_time=20, retry_count=2): | ||
1176 | 268 | """Check service and file were updated after mtime | ||
1177 | 269 | |||
1178 | 270 | Args: | ||
1179 | 271 | sentry_unit (sentry): The sentry unit to check for the service on | ||
1180 | 272 | mtime (float): The epoch time to check against | ||
1181 | 273 | service (string): service name to look for in process table | ||
1182 | 274 | filename (string): The file to check mtime of | ||
1183 | 275 | pgrep_full (boolean): Use full command line search mode with pgrep | ||
1184 | 276 | sleep_time (int): Seconds to sleep before looking for process | ||
1185 | 277 | retry_count (int): If service is not found, how many times to retry | ||
1186 | 278 | |||
1187 | 279 | Typical Usage: | ||
1188 | 280 | u = OpenStackAmuletUtils(ERROR) | ||
1189 | 281 | ... | ||
1190 | 282 | mtime = u.get_sentry_time(self.cinder_sentry) | ||
1191 | 283 | self.d.configure('cinder', {'verbose': 'True', 'debug': 'True'}) | ||
1192 | 284 | if not u.validate_service_config_changed(self.cinder_sentry, | ||
1193 | 285 | mtime, | ||
1194 | 286 | 'cinder-api', | ||
1195 | 287 | '/etc/cinder/cinder.conf') | ||
1196 | 288 | amulet.raise_status(amulet.FAIL, msg='update failed') | ||
1197 | 289 | Returns: | ||
1198 | 290 | bool: True if both service and file where updated/restarted after | ||
1199 | 291 | mtime, False if service is older than mtime or if service was | ||
1200 | 292 | not found or if filename was modified before mtime. | ||
1201 | 293 | """ | ||
1202 | 294 | self.log.debug('Checking %s restarted since %s' % (service, mtime)) | ||
1203 | 295 | time.sleep(sleep_time) | ||
1204 | 296 | service_restart = self.service_restarted_since(sentry_unit, mtime, | ||
1205 | 297 | service, | ||
1206 | 298 | pgrep_full=pgrep_full, | ||
1207 | 299 | sleep_time=0, | ||
1208 | 300 | retry_count=retry_count) | ||
1209 | 301 | config_update = self.config_updated_since(sentry_unit, filename, mtime, | ||
1210 | 302 | sleep_time=0) | ||
1211 | 303 | return service_restart and config_update | ||
1212 | 304 | |||
1213 | 305 | def get_sentry_time(self, sentry_unit): | ||
1214 | 306 | """Return current epoch time on a sentry""" | ||
1215 | 307 | cmd = "date +'%s'" | ||
1216 | 308 | return float(sentry_unit.run(cmd)[0]) | ||
1217 | 309 | |||
1218 | 190 | def relation_error(self, name, data): | 310 | def relation_error(self, name, data): |
1219 | 191 | return 'unexpected relation data in {} - {}'.format(name, data) | 311 | return 'unexpected relation data in {} - {}'.format(name, data) |
1220 | 192 | 312 | ||
1221 | 193 | 313 | ||
1222 | === modified file 'tests/charmhelpers/contrib/openstack/amulet/deployment.py' | |||
1223 | --- tests/charmhelpers/contrib/openstack/amulet/deployment.py 2015-01-26 09:47:56 +0000 | |||
1224 | +++ tests/charmhelpers/contrib/openstack/amulet/deployment.py 2015-02-24 06:05:56 +0000 | |||
1225 | @@ -71,16 +71,19 @@ | |||
1226 | 71 | services.append(this_service) | 71 | services.append(this_service) |
1227 | 72 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', | 72 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', |
1228 | 73 | 'ceph-osd', 'ceph-radosgw'] | 73 | 'ceph-osd', 'ceph-radosgw'] |
1229 | 74 | # Openstack subordinate charms do not expose an origin option as that | ||
1230 | 75 | # is controlled by the principle | ||
1231 | 76 | ignore = ['neutron-openvswitch'] | ||
1232 | 74 | 77 | ||
1233 | 75 | if self.openstack: | 78 | if self.openstack: |
1234 | 76 | for svc in services: | 79 | for svc in services: |
1236 | 77 | if svc['name'] not in use_source: | 80 | if svc['name'] not in use_source + ignore: |
1237 | 78 | config = {'openstack-origin': self.openstack} | 81 | config = {'openstack-origin': self.openstack} |
1238 | 79 | self.d.configure(svc['name'], config) | 82 | self.d.configure(svc['name'], config) |
1239 | 80 | 83 | ||
1240 | 81 | if self.source: | 84 | if self.source: |
1241 | 82 | for svc in services: | 85 | for svc in services: |
1243 | 83 | if svc['name'] in use_source: | 86 | if svc['name'] in use_source and svc['name'] not in ignore: |
1244 | 84 | config = {'source': self.source} | 87 | config = {'source': self.source} |
1245 | 85 | self.d.configure(svc['name'], config) | 88 | self.d.configure(svc['name'], config) |
1246 | 86 | 89 |
charm_lint_check #2215 swift-storage-next for brad-marshall mp250699
LINT OK: passed
Build: http:// 10.245. 162.77: 8080/job/ charm_lint_ check/2215/