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