Merge lp:~lazypower/charms/trusty/mongodb/charmhelpers_merge into lp:charms/mongodb
- Trusty Tahr (14.04)
- charmhelpers_merge
- Merge into trunk
Proposed by
Charles Butler
Status: | Merged |
---|---|
Merged at revision: | 50 |
Proposed branch: | lp:~lazypower/charms/trusty/mongodb/charmhelpers_merge |
Merge into: | lp:charms/mongodb |
Diff against target: |
1655 lines (+605/-490) 13 files modified
.bzrignore (+3/-0) Makefile (+9/-1) config.yaml (+1/-0) hooks/charmhelpers/core/fstab.py (+116/-0) hooks/charmhelpers/core/hookenv.py (+103/-5) hooks/charmhelpers/core/host.py (+38/-8) hooks/charmhelpers/fetch/__init__.py (+130/-81) hooks/charmhelpers/fetch/bzrurl.py (+2/-1) hooks/hooks.py (+150/-382) hooks/install (+0/-5) metadata.yaml (+5/-2) tests/00-setup (+5/-5) tests/200_relate_ceilometer.test (+43/-0) |
To merge this branch: | bzr merge lp:~lazypower/charms/trusty/mongodb/charmhelpers_merge |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Marco Ceppi (community) | Approve | ||
Cory Johns (community) | Approve | ||
Review via email: mp+228758@code.launchpad.net |
Commit message
Description of the change
Re-submission of charm-helpers migration. Adds validation test for Ceilometer. (note: when running tests, you will need to point teh cluster test at lp:~lazypower/charms/trusty/mongodb/charmhelpers_merge - as the store version will fail 100% of the time)
Please take a look
To post a comment you must log in.
- 55. By Charles Butler
-
adds missing import relation_ids
- 56. By Charles Butler
-
Fixes missing default on key, strips check around apt-add-repository
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file '.bzrignore' | |||
2 | --- .bzrignore 1970-01-01 00:00:00 +0000 | |||
3 | +++ .bzrignore 2014-07-30 17:48:38 +0000 | |||
4 | @@ -0,0 +1,3 @@ | |||
5 | 1 | .git | ||
6 | 2 | bin/* | ||
7 | 3 | scripts/charm-helpers-sync.py | ||
8 | 0 | 4 | ||
9 | === modified file 'Makefile' | |||
10 | --- Makefile 2014-04-11 20:55:42 +0000 | |||
11 | +++ Makefile 2014-07-30 17:48:38 +0000 | |||
12 | @@ -13,9 +13,17 @@ | |||
13 | 13 | # You should have received a copy of the GNU Affero General Public License | 13 | # You should have received a copy of the GNU Affero General Public License |
14 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 | 15 | ||
16 | 16 | PYTHON := /usr/bin/env python | ||
17 | 16 | 17 | ||
18 | 17 | unittest: | 18 | unittest: |
19 | 18 | tests/10-unit.test | 19 | tests/10-unit.test |
20 | 19 | 20 | ||
21 | 20 | sync: | 21 | sync: |
23 | 21 | @charm-helper-sync -c charm-helpers-sync.yaml | 22 | @mkdir -p bin |
24 | 23 | @bzr cat lp:charm-helpers/tools/charm_helpers_sync/charm_helpers_sync.py > bin/charm_helpers_sync.py | ||
25 | 24 | @$(PYTHON) bin/charm_helpers_sync.py -c charm-helpers-sync.yaml | ||
26 | 25 | |||
27 | 26 | clean: | ||
28 | 27 | @find . -name \*.pyc -delete | ||
29 | 28 | @find . -name '*.bak' -delete | ||
30 | 29 | |||
31 | 22 | 30 | ||
32 | === added file '__init__.py' | |||
33 | === added directory 'bin' | |||
34 | === modified file 'config.yaml' | |||
35 | --- config.yaml 2014-06-18 11:13:54 +0000 | |||
36 | +++ config.yaml 2014-07-30 17:48:38 +0000 | |||
37 | @@ -211,6 +211,7 @@ | |||
38 | 211 | option. | 211 | option. |
39 | 212 | key: | 212 | key: |
40 | 213 | type: string | 213 | type: string |
41 | 214 | default: | ||
42 | 214 | description: > | 215 | description: > |
43 | 215 | Key ID to import to the apt keyring to support use with arbitary source | 216 | Key ID to import to the apt keyring to support use with arbitary source |
44 | 216 | configuration from outside of Launchpad archives or PPA's. | 217 | configuration from outside of Launchpad archives or PPA's. |
45 | 217 | 218 | ||
46 | === added file 'hooks/charmhelpers/core/fstab.py' | |||
47 | --- hooks/charmhelpers/core/fstab.py 1970-01-01 00:00:00 +0000 | |||
48 | +++ hooks/charmhelpers/core/fstab.py 2014-07-30 17:48:38 +0000 | |||
49 | @@ -0,0 +1,116 @@ | |||
50 | 1 | #!/usr/bin/env python | ||
51 | 2 | # -*- coding: utf-8 -*- | ||
52 | 3 | |||
53 | 4 | __author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>' | ||
54 | 5 | |||
55 | 6 | import os | ||
56 | 7 | |||
57 | 8 | |||
58 | 9 | class Fstab(file): | ||
59 | 10 | """This class extends file in order to implement a file reader/writer | ||
60 | 11 | for file `/etc/fstab` | ||
61 | 12 | """ | ||
62 | 13 | |||
63 | 14 | class Entry(object): | ||
64 | 15 | """Entry class represents a non-comment line on the `/etc/fstab` file | ||
65 | 16 | """ | ||
66 | 17 | def __init__(self, device, mountpoint, filesystem, | ||
67 | 18 | options, d=0, p=0): | ||
68 | 19 | self.device = device | ||
69 | 20 | self.mountpoint = mountpoint | ||
70 | 21 | self.filesystem = filesystem | ||
71 | 22 | |||
72 | 23 | if not options: | ||
73 | 24 | options = "defaults" | ||
74 | 25 | |||
75 | 26 | self.options = options | ||
76 | 27 | self.d = d | ||
77 | 28 | self.p = p | ||
78 | 29 | |||
79 | 30 | def __eq__(self, o): | ||
80 | 31 | return str(self) == str(o) | ||
81 | 32 | |||
82 | 33 | def __str__(self): | ||
83 | 34 | return "{} {} {} {} {} {}".format(self.device, | ||
84 | 35 | self.mountpoint, | ||
85 | 36 | self.filesystem, | ||
86 | 37 | self.options, | ||
87 | 38 | self.d, | ||
88 | 39 | self.p) | ||
89 | 40 | |||
90 | 41 | DEFAULT_PATH = os.path.join(os.path.sep, 'etc', 'fstab') | ||
91 | 42 | |||
92 | 43 | def __init__(self, path=None): | ||
93 | 44 | if path: | ||
94 | 45 | self._path = path | ||
95 | 46 | else: | ||
96 | 47 | self._path = self.DEFAULT_PATH | ||
97 | 48 | file.__init__(self, self._path, 'r+') | ||
98 | 49 | |||
99 | 50 | def _hydrate_entry(self, line): | ||
100 | 51 | # NOTE: use split with no arguments to split on any | ||
101 | 52 | # whitespace including tabs | ||
102 | 53 | return Fstab.Entry(*filter( | ||
103 | 54 | lambda x: x not in ('', None), | ||
104 | 55 | line.strip("\n").split())) | ||
105 | 56 | |||
106 | 57 | @property | ||
107 | 58 | def entries(self): | ||
108 | 59 | self.seek(0) | ||
109 | 60 | for line in self.readlines(): | ||
110 | 61 | try: | ||
111 | 62 | if not line.startswith("#"): | ||
112 | 63 | yield self._hydrate_entry(line) | ||
113 | 64 | except ValueError: | ||
114 | 65 | pass | ||
115 | 66 | |||
116 | 67 | def get_entry_by_attr(self, attr, value): | ||
117 | 68 | for entry in self.entries: | ||
118 | 69 | e_attr = getattr(entry, attr) | ||
119 | 70 | if e_attr == value: | ||
120 | 71 | return entry | ||
121 | 72 | return None | ||
122 | 73 | |||
123 | 74 | def add_entry(self, entry): | ||
124 | 75 | if self.get_entry_by_attr('device', entry.device): | ||
125 | 76 | return False | ||
126 | 77 | |||
127 | 78 | self.write(str(entry) + '\n') | ||
128 | 79 | self.truncate() | ||
129 | 80 | return entry | ||
130 | 81 | |||
131 | 82 | def remove_entry(self, entry): | ||
132 | 83 | self.seek(0) | ||
133 | 84 | |||
134 | 85 | lines = self.readlines() | ||
135 | 86 | |||
136 | 87 | found = False | ||
137 | 88 | for index, line in enumerate(lines): | ||
138 | 89 | if not line.startswith("#"): | ||
139 | 90 | if self._hydrate_entry(line) == entry: | ||
140 | 91 | found = True | ||
141 | 92 | break | ||
142 | 93 | |||
143 | 94 | if not found: | ||
144 | 95 | return False | ||
145 | 96 | |||
146 | 97 | lines.remove(line) | ||
147 | 98 | |||
148 | 99 | self.seek(0) | ||
149 | 100 | self.write(''.join(lines)) | ||
150 | 101 | self.truncate() | ||
151 | 102 | return True | ||
152 | 103 | |||
153 | 104 | @classmethod | ||
154 | 105 | def remove_by_mountpoint(cls, mountpoint, path=None): | ||
155 | 106 | fstab = cls(path=path) | ||
156 | 107 | entry = fstab.get_entry_by_attr('mountpoint', mountpoint) | ||
157 | 108 | if entry: | ||
158 | 109 | return fstab.remove_entry(entry) | ||
159 | 110 | return False | ||
160 | 111 | |||
161 | 112 | @classmethod | ||
162 | 113 | def add(cls, device, mountpoint, filesystem, options=None, path=None): | ||
163 | 114 | return cls(path=path).add_entry(Fstab.Entry(device, | ||
164 | 115 | mountpoint, filesystem, | ||
165 | 116 | options=options)) | ||
166 | 0 | 117 | ||
167 | === modified file 'hooks/charmhelpers/core/hookenv.py' | |||
168 | --- hooks/charmhelpers/core/hookenv.py 2014-04-11 20:55:42 +0000 | |||
169 | +++ hooks/charmhelpers/core/hookenv.py 2014-07-30 17:48:38 +0000 | |||
170 | @@ -25,7 +25,7 @@ | |||
171 | 25 | def cached(func): | 25 | def cached(func): |
172 | 26 | """Cache return values for multiple executions of func + args | 26 | """Cache return values for multiple executions of func + args |
173 | 27 | 27 | ||
175 | 28 | For example: | 28 | For example:: |
176 | 29 | 29 | ||
177 | 30 | @cached | 30 | @cached |
178 | 31 | def unit_get(attribute): | 31 | def unit_get(attribute): |
179 | @@ -155,6 +155,100 @@ | |||
180 | 155 | return os.path.basename(sys.argv[0]) | 155 | return os.path.basename(sys.argv[0]) |
181 | 156 | 156 | ||
182 | 157 | 157 | ||
183 | 158 | class Config(dict): | ||
184 | 159 | """A Juju charm config dictionary that can write itself to | ||
185 | 160 | disk (as json) and track which values have changed since | ||
186 | 161 | the previous hook invocation. | ||
187 | 162 | |||
188 | 163 | Do not instantiate this object directly - instead call | ||
189 | 164 | ``hookenv.config()`` | ||
190 | 165 | |||
191 | 166 | Example usage:: | ||
192 | 167 | |||
193 | 168 | >>> # inside a hook | ||
194 | 169 | >>> from charmhelpers.core import hookenv | ||
195 | 170 | >>> config = hookenv.config() | ||
196 | 171 | >>> config['foo'] | ||
197 | 172 | 'bar' | ||
198 | 173 | >>> config['mykey'] = 'myval' | ||
199 | 174 | >>> config.save() | ||
200 | 175 | |||
201 | 176 | |||
202 | 177 | >>> # user runs `juju set mycharm foo=baz` | ||
203 | 178 | >>> # now we're inside subsequent config-changed hook | ||
204 | 179 | >>> config = hookenv.config() | ||
205 | 180 | >>> config['foo'] | ||
206 | 181 | 'baz' | ||
207 | 182 | >>> # test to see if this val has changed since last hook | ||
208 | 183 | >>> config.changed('foo') | ||
209 | 184 | True | ||
210 | 185 | >>> # what was the previous value? | ||
211 | 186 | >>> config.previous('foo') | ||
212 | 187 | 'bar' | ||
213 | 188 | >>> # keys/values that we add are preserved across hooks | ||
214 | 189 | >>> config['mykey'] | ||
215 | 190 | 'myval' | ||
216 | 191 | >>> # don't forget to save at the end of hook! | ||
217 | 192 | >>> config.save() | ||
218 | 193 | |||
219 | 194 | """ | ||
220 | 195 | CONFIG_FILE_NAME = '.juju-persistent-config' | ||
221 | 196 | |||
222 | 197 | def __init__(self, *args, **kw): | ||
223 | 198 | super(Config, self).__init__(*args, **kw) | ||
224 | 199 | self._prev_dict = None | ||
225 | 200 | self.path = os.path.join(charm_dir(), Config.CONFIG_FILE_NAME) | ||
226 | 201 | if os.path.exists(self.path): | ||
227 | 202 | self.load_previous() | ||
228 | 203 | |||
229 | 204 | def load_previous(self, path=None): | ||
230 | 205 | """Load previous copy of config from disk so that current values | ||
231 | 206 | can be compared to previous values. | ||
232 | 207 | |||
233 | 208 | :param path: | ||
234 | 209 | |||
235 | 210 | File path from which to load the previous config. If `None`, | ||
236 | 211 | config is loaded from the default location. If `path` is | ||
237 | 212 | specified, subsequent `save()` calls will write to the same | ||
238 | 213 | path. | ||
239 | 214 | |||
240 | 215 | """ | ||
241 | 216 | self.path = path or self.path | ||
242 | 217 | with open(self.path) as f: | ||
243 | 218 | self._prev_dict = json.load(f) | ||
244 | 219 | |||
245 | 220 | def changed(self, key): | ||
246 | 221 | """Return true if the value for this key has changed since | ||
247 | 222 | the last save. | ||
248 | 223 | |||
249 | 224 | """ | ||
250 | 225 | if self._prev_dict is None: | ||
251 | 226 | return True | ||
252 | 227 | return self.previous(key) != self.get(key) | ||
253 | 228 | |||
254 | 229 | def previous(self, key): | ||
255 | 230 | """Return previous value for this key, or None if there | ||
256 | 231 | is no "previous" value. | ||
257 | 232 | |||
258 | 233 | """ | ||
259 | 234 | if self._prev_dict: | ||
260 | 235 | return self._prev_dict.get(key) | ||
261 | 236 | return None | ||
262 | 237 | |||
263 | 238 | def save(self): | ||
264 | 239 | """Save this config to disk. | ||
265 | 240 | |||
266 | 241 | Preserves items in _prev_dict that do not exist in self. | ||
267 | 242 | |||
268 | 243 | """ | ||
269 | 244 | if self._prev_dict: | ||
270 | 245 | for k, v in self._prev_dict.iteritems(): | ||
271 | 246 | if k not in self: | ||
272 | 247 | self[k] = v | ||
273 | 248 | with open(self.path, 'w') as f: | ||
274 | 249 | json.dump(self, f) | ||
275 | 250 | |||
276 | 251 | |||
277 | 158 | @cached | 252 | @cached |
278 | 159 | def config(scope=None): | 253 | def config(scope=None): |
279 | 160 | """Juju charm configuration""" | 254 | """Juju charm configuration""" |
280 | @@ -163,7 +257,10 @@ | |||
281 | 163 | config_cmd_line.append(scope) | 257 | config_cmd_line.append(scope) |
282 | 164 | config_cmd_line.append('--format=json') | 258 | config_cmd_line.append('--format=json') |
283 | 165 | try: | 259 | try: |
285 | 166 | return json.loads(subprocess.check_output(config_cmd_line)) | 260 | config_data = json.loads(subprocess.check_output(config_cmd_line)) |
286 | 261 | if scope is not None: | ||
287 | 262 | return config_data | ||
288 | 263 | return Config(config_data) | ||
289 | 167 | except ValueError: | 264 | except ValueError: |
290 | 168 | return None | 265 | return None |
291 | 169 | 266 | ||
292 | @@ -348,18 +445,19 @@ | |||
293 | 348 | class Hooks(object): | 445 | class Hooks(object): |
294 | 349 | """A convenient handler for hook functions. | 446 | """A convenient handler for hook functions. |
295 | 350 | 447 | ||
297 | 351 | Example: | 448 | Example:: |
298 | 449 | |||
299 | 352 | hooks = Hooks() | 450 | hooks = Hooks() |
300 | 353 | 451 | ||
301 | 354 | # register a hook, taking its name from the function name | 452 | # register a hook, taking its name from the function name |
302 | 355 | @hooks.hook() | 453 | @hooks.hook() |
303 | 356 | def install(): | 454 | def install(): |
305 | 357 | ... | 455 | pass # your code here |
306 | 358 | 456 | ||
307 | 359 | # register a hook, providing a custom hook name | 457 | # register a hook, providing a custom hook name |
308 | 360 | @hooks.hook("config-changed") | 458 | @hooks.hook("config-changed") |
309 | 361 | def config_changed(): | 459 | def config_changed(): |
311 | 362 | ... | 460 | pass # your code here |
312 | 363 | 461 | ||
313 | 364 | if __name__ == "__main__": | 462 | if __name__ == "__main__": |
314 | 365 | # execute a hook based on the name the program is called by | 463 | # execute a hook based on the name the program is called by |
315 | 366 | 464 | ||
316 | === modified file 'hooks/charmhelpers/core/host.py' | |||
317 | --- hooks/charmhelpers/core/host.py 2014-04-11 20:55:42 +0000 | |||
318 | +++ hooks/charmhelpers/core/host.py 2014-07-30 17:48:38 +0000 | |||
319 | @@ -16,6 +16,7 @@ | |||
320 | 16 | from collections import OrderedDict | 16 | from collections import OrderedDict |
321 | 17 | 17 | ||
322 | 18 | from hookenv import log | 18 | from hookenv import log |
323 | 19 | from fstab import Fstab | ||
324 | 19 | 20 | ||
325 | 20 | 21 | ||
326 | 21 | def service_start(service_name): | 22 | def service_start(service_name): |
327 | @@ -34,7 +35,8 @@ | |||
328 | 34 | 35 | ||
329 | 35 | 36 | ||
330 | 36 | def service_reload(service_name, restart_on_failure=False): | 37 | def service_reload(service_name, restart_on_failure=False): |
332 | 37 | """Reload a system service, optionally falling back to restart if reload fails""" | 38 | """Reload a system service, optionally falling back to restart if |
333 | 39 | reload fails""" | ||
334 | 38 | service_result = service('reload', service_name) | 40 | service_result = service('reload', service_name) |
335 | 39 | if not service_result and restart_on_failure: | 41 | if not service_result and restart_on_failure: |
336 | 40 | service_result = service('restart', service_name) | 42 | service_result = service('restart', service_name) |
337 | @@ -143,7 +145,19 @@ | |||
338 | 143 | target.write(content) | 145 | target.write(content) |
339 | 144 | 146 | ||
340 | 145 | 147 | ||
342 | 146 | def mount(device, mountpoint, options=None, persist=False): | 148 | def fstab_remove(mp): |
343 | 149 | """Remove the given mountpoint entry from /etc/fstab | ||
344 | 150 | """ | ||
345 | 151 | return Fstab.remove_by_mountpoint(mp) | ||
346 | 152 | |||
347 | 153 | |||
348 | 154 | def fstab_add(dev, mp, fs, options=None): | ||
349 | 155 | """Adds the given device entry to the /etc/fstab file | ||
350 | 156 | """ | ||
351 | 157 | return Fstab.add(dev, mp, fs, options=options) | ||
352 | 158 | |||
353 | 159 | |||
354 | 160 | def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"): | ||
355 | 147 | """Mount a filesystem at a particular mountpoint""" | 161 | """Mount a filesystem at a particular mountpoint""" |
356 | 148 | cmd_args = ['mount'] | 162 | cmd_args = ['mount'] |
357 | 149 | if options is not None: | 163 | if options is not None: |
358 | @@ -154,9 +168,9 @@ | |||
359 | 154 | except subprocess.CalledProcessError, e: | 168 | except subprocess.CalledProcessError, e: |
360 | 155 | log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output)) | 169 | log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output)) |
361 | 156 | return False | 170 | return False |
362 | 171 | |||
363 | 157 | if persist: | 172 | if persist: |
366 | 158 | # TODO: update fstab | 173 | return fstab_add(device, mountpoint, filesystem, options=options) |
365 | 159 | pass | ||
367 | 160 | return True | 174 | return True |
368 | 161 | 175 | ||
369 | 162 | 176 | ||
370 | @@ -168,9 +182,9 @@ | |||
371 | 168 | except subprocess.CalledProcessError, e: | 182 | except subprocess.CalledProcessError, e: |
372 | 169 | log('Error unmounting {}\n{}'.format(mountpoint, e.output)) | 183 | log('Error unmounting {}\n{}'.format(mountpoint, e.output)) |
373 | 170 | return False | 184 | return False |
374 | 185 | |||
375 | 171 | if persist: | 186 | if persist: |
378 | 172 | # TODO: update fstab | 187 | return fstab_remove(mountpoint) |
377 | 173 | pass | ||
379 | 174 | return True | 188 | return True |
380 | 175 | 189 | ||
381 | 176 | 190 | ||
382 | @@ -197,13 +211,13 @@ | |||
383 | 197 | def restart_on_change(restart_map, stopstart=False): | 211 | def restart_on_change(restart_map, stopstart=False): |
384 | 198 | """Restart services based on configuration files changing | 212 | """Restart services based on configuration files changing |
385 | 199 | 213 | ||
387 | 200 | This function is used a decorator, for example | 214 | This function is used a decorator, for example:: |
388 | 201 | 215 | ||
389 | 202 | @restart_on_change({ | 216 | @restart_on_change({ |
390 | 203 | '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ] | 217 | '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ] |
391 | 204 | }) | 218 | }) |
392 | 205 | def ceph_client_changed(): | 219 | def ceph_client_changed(): |
394 | 206 | ... | 220 | pass # your code here |
395 | 207 | 221 | ||
396 | 208 | In this example, the cinder-api and cinder-volume services | 222 | In this example, the cinder-api and cinder-volume services |
397 | 209 | would be restarted if /etc/ceph/ceph.conf is changed by the | 223 | would be restarted if /etc/ceph/ceph.conf is changed by the |
398 | @@ -295,3 +309,19 @@ | |||
399 | 295 | if 'link/ether' in words: | 309 | if 'link/ether' in words: |
400 | 296 | hwaddr = words[words.index('link/ether') + 1] | 310 | hwaddr = words[words.index('link/ether') + 1] |
401 | 297 | return hwaddr | 311 | return hwaddr |
402 | 312 | |||
403 | 313 | |||
404 | 314 | def cmp_pkgrevno(package, revno, pkgcache=None): | ||
405 | 315 | '''Compare supplied revno with the revno of the installed package | ||
406 | 316 | |||
407 | 317 | * 1 => Installed revno is greater than supplied arg | ||
408 | 318 | * 0 => Installed revno is the same as supplied arg | ||
409 | 319 | * -1 => Installed revno is less than supplied arg | ||
410 | 320 | |||
411 | 321 | ''' | ||
412 | 322 | import apt_pkg | ||
413 | 323 | if not pkgcache: | ||
414 | 324 | apt_pkg.init() | ||
415 | 325 | pkgcache = apt_pkg.Cache() | ||
416 | 326 | pkg = pkgcache[package] | ||
417 | 327 | return apt_pkg.version_compare(pkg.current_ver.ver_str, revno) | ||
418 | 298 | 328 | ||
419 | === modified file 'hooks/charmhelpers/fetch/__init__.py' | |||
420 | --- hooks/charmhelpers/fetch/__init__.py 2014-04-11 20:55:42 +0000 | |||
421 | +++ hooks/charmhelpers/fetch/__init__.py 2014-07-30 17:48:38 +0000 | |||
422 | @@ -1,4 +1,5 @@ | |||
423 | 1 | import importlib | 1 | import importlib |
424 | 2 | import time | ||
425 | 2 | from yaml import safe_load | 3 | from yaml import safe_load |
426 | 3 | from charmhelpers.core.host import ( | 4 | from charmhelpers.core.host import ( |
427 | 4 | lsb_release | 5 | lsb_release |
428 | @@ -12,9 +13,9 @@ | |||
429 | 12 | config, | 13 | config, |
430 | 13 | log, | 14 | log, |
431 | 14 | ) | 15 | ) |
432 | 15 | import apt_pkg | ||
433 | 16 | import os | 16 | import os |
434 | 17 | 17 | ||
435 | 18 | |||
436 | 18 | CLOUD_ARCHIVE = """# Ubuntu Cloud Archive | 19 | CLOUD_ARCHIVE = """# Ubuntu Cloud Archive |
437 | 19 | deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main | 20 | deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main |
438 | 20 | """ | 21 | """ |
439 | @@ -54,12 +55,74 @@ | |||
440 | 54 | 'icehouse/proposed': 'precise-proposed/icehouse', | 55 | 'icehouse/proposed': 'precise-proposed/icehouse', |
441 | 55 | 'precise-icehouse/proposed': 'precise-proposed/icehouse', | 56 | 'precise-icehouse/proposed': 'precise-proposed/icehouse', |
442 | 56 | 'precise-proposed/icehouse': 'precise-proposed/icehouse', | 57 | 'precise-proposed/icehouse': 'precise-proposed/icehouse', |
443 | 58 | # Juno | ||
444 | 59 | 'juno': 'trusty-updates/juno', | ||
445 | 60 | 'trusty-juno': 'trusty-updates/juno', | ||
446 | 61 | 'trusty-juno/updates': 'trusty-updates/juno', | ||
447 | 62 | 'trusty-updates/juno': 'trusty-updates/juno', | ||
448 | 63 | 'juno/proposed': 'trusty-proposed/juno', | ||
449 | 64 | 'juno/proposed': 'trusty-proposed/juno', | ||
450 | 65 | 'trusty-juno/proposed': 'trusty-proposed/juno', | ||
451 | 66 | 'trusty-proposed/juno': 'trusty-proposed/juno', | ||
452 | 57 | } | 67 | } |
453 | 58 | 68 | ||
454 | 69 | # The order of this list is very important. Handlers should be listed in from | ||
455 | 70 | # least- to most-specific URL matching. | ||
456 | 71 | FETCH_HANDLERS = ( | ||
457 | 72 | 'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler', | ||
458 | 73 | 'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler', | ||
459 | 74 | ) | ||
460 | 75 | |||
461 | 76 | APT_NO_LOCK = 100 # The return code for "couldn't acquire lock" in APT. | ||
462 | 77 | APT_NO_LOCK_RETRY_DELAY = 10 # Wait 10 seconds between apt lock checks. | ||
463 | 78 | APT_NO_LOCK_RETRY_COUNT = 30 # Retry to acquire the lock X times. | ||
464 | 79 | |||
465 | 80 | |||
466 | 81 | class SourceConfigError(Exception): | ||
467 | 82 | pass | ||
468 | 83 | |||
469 | 84 | |||
470 | 85 | class UnhandledSource(Exception): | ||
471 | 86 | pass | ||
472 | 87 | |||
473 | 88 | |||
474 | 89 | class AptLockError(Exception): | ||
475 | 90 | pass | ||
476 | 91 | |||
477 | 92 | |||
478 | 93 | class BaseFetchHandler(object): | ||
479 | 94 | |||
480 | 95 | """Base class for FetchHandler implementations in fetch plugins""" | ||
481 | 96 | |||
482 | 97 | def can_handle(self, source): | ||
483 | 98 | """Returns True if the source can be handled. Otherwise returns | ||
484 | 99 | a string explaining why it cannot""" | ||
485 | 100 | return "Wrong source type" | ||
486 | 101 | |||
487 | 102 | def install(self, source): | ||
488 | 103 | """Try to download and unpack the source. Return the path to the | ||
489 | 104 | unpacked files or raise UnhandledSource.""" | ||
490 | 105 | raise UnhandledSource("Wrong source type {}".format(source)) | ||
491 | 106 | |||
492 | 107 | def parse_url(self, url): | ||
493 | 108 | return urlparse(url) | ||
494 | 109 | |||
495 | 110 | def base_url(self, url): | ||
496 | 111 | """Return url without querystring or fragment""" | ||
497 | 112 | parts = list(self.parse_url(url)) | ||
498 | 113 | parts[4:] = ['' for i in parts[4:]] | ||
499 | 114 | return urlunparse(parts) | ||
500 | 115 | |||
501 | 59 | 116 | ||
502 | 60 | def filter_installed_packages(packages): | 117 | def filter_installed_packages(packages): |
503 | 61 | """Returns a list of packages that require installation""" | 118 | """Returns a list of packages that require installation""" |
504 | 119 | import apt_pkg | ||
505 | 62 | apt_pkg.init() | 120 | apt_pkg.init() |
506 | 121 | |||
507 | 122 | # Tell apt to build an in-memory cache to prevent race conditions (if | ||
508 | 123 | # another process is already building the cache). | ||
509 | 124 | apt_pkg.config.set("Dir::Cache::pkgcache", "") | ||
510 | 125 | |||
511 | 63 | cache = apt_pkg.Cache() | 126 | cache = apt_pkg.Cache() |
512 | 64 | _pkgs = [] | 127 | _pkgs = [] |
513 | 65 | for package in packages: | 128 | for package in packages: |
514 | @@ -87,14 +150,7 @@ | |||
515 | 87 | cmd.extend(packages) | 150 | cmd.extend(packages) |
516 | 88 | log("Installing {} with options: {}".format(packages, | 151 | log("Installing {} with options: {}".format(packages, |
517 | 89 | options)) | 152 | options)) |
526 | 90 | env = os.environ.copy() | 153 | _run_apt_command(cmd, fatal) |
519 | 91 | if 'DEBIAN_FRONTEND' not in env: | ||
520 | 92 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
521 | 93 | |||
522 | 94 | if fatal: | ||
523 | 95 | subprocess.check_call(cmd, env=env) | ||
524 | 96 | else: | ||
525 | 97 | subprocess.call(cmd, env=env) | ||
527 | 98 | 154 | ||
528 | 99 | 155 | ||
529 | 100 | def apt_upgrade(options=None, fatal=False, dist=False): | 156 | def apt_upgrade(options=None, fatal=False, dist=False): |
530 | @@ -109,24 +165,13 @@ | |||
531 | 109 | else: | 165 | else: |
532 | 110 | cmd.append('upgrade') | 166 | cmd.append('upgrade') |
533 | 111 | log("Upgrading with options: {}".format(options)) | 167 | log("Upgrading with options: {}".format(options)) |
543 | 112 | 168 | _run_apt_command(cmd, fatal) | |
535 | 113 | env = os.environ.copy() | ||
536 | 114 | if 'DEBIAN_FRONTEND' not in env: | ||
537 | 115 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
538 | 116 | |||
539 | 117 | if fatal: | ||
540 | 118 | subprocess.check_call(cmd, env=env) | ||
541 | 119 | else: | ||
542 | 120 | subprocess.call(cmd, env=env) | ||
544 | 121 | 169 | ||
545 | 122 | 170 | ||
546 | 123 | def apt_update(fatal=False): | 171 | def apt_update(fatal=False): |
547 | 124 | """Update local apt cache""" | 172 | """Update local apt cache""" |
548 | 125 | cmd = ['apt-get', 'update'] | 173 | cmd = ['apt-get', 'update'] |
553 | 126 | if fatal: | 174 | _run_apt_command(cmd, fatal) |
550 | 127 | subprocess.check_call(cmd) | ||
551 | 128 | else: | ||
552 | 129 | subprocess.call(cmd) | ||
554 | 130 | 175 | ||
555 | 131 | 176 | ||
556 | 132 | def apt_purge(packages, fatal=False): | 177 | def apt_purge(packages, fatal=False): |
557 | @@ -137,10 +182,7 @@ | |||
558 | 137 | else: | 182 | else: |
559 | 138 | cmd.extend(packages) | 183 | cmd.extend(packages) |
560 | 139 | log("Purging {}".format(packages)) | 184 | log("Purging {}".format(packages)) |
565 | 140 | if fatal: | 185 | _run_apt_command(cmd, fatal) |
562 | 141 | subprocess.check_call(cmd) | ||
563 | 142 | else: | ||
564 | 143 | subprocess.call(cmd) | ||
566 | 144 | 186 | ||
567 | 145 | 187 | ||
568 | 146 | def apt_hold(packages, fatal=False): | 188 | def apt_hold(packages, fatal=False): |
569 | @@ -151,6 +193,7 @@ | |||
570 | 151 | else: | 193 | else: |
571 | 152 | cmd.extend(packages) | 194 | cmd.extend(packages) |
572 | 153 | log("Holding {}".format(packages)) | 195 | log("Holding {}".format(packages)) |
573 | 196 | |||
574 | 154 | if fatal: | 197 | if fatal: |
575 | 155 | subprocess.check_call(cmd) | 198 | subprocess.check_call(cmd) |
576 | 156 | else: | 199 | else: |
577 | @@ -184,57 +227,50 @@ | |||
578 | 184 | apt.write(PROPOSED_POCKET.format(release)) | 227 | apt.write(PROPOSED_POCKET.format(release)) |
579 | 185 | if key: | 228 | if key: |
580 | 186 | subprocess.check_call(['apt-key', 'adv', '--keyserver', | 229 | subprocess.check_call(['apt-key', 'adv', '--keyserver', |
582 | 187 | 'keyserver.ubuntu.com', '--recv', | 230 | 'hkp://keyserver.ubuntu.com:80', '--recv', |
583 | 188 | key]) | 231 | key]) |
584 | 189 | 232 | ||
585 | 190 | 233 | ||
586 | 191 | class SourceConfigError(Exception): | ||
587 | 192 | pass | ||
588 | 193 | |||
589 | 194 | |||
590 | 195 | def configure_sources(update=False, | 234 | def configure_sources(update=False, |
591 | 196 | sources_var='install_sources', | 235 | sources_var='install_sources', |
592 | 197 | keys_var='install_keys'): | 236 | keys_var='install_keys'): |
593 | 198 | """ | 237 | """ |
595 | 199 | Configure multiple sources from charm configuration | 238 | Configure multiple sources from charm configuration. |
596 | 239 | |||
597 | 240 | The lists are encoded as yaml fragments in the configuration. | ||
598 | 241 | The frament needs to be included as a string. | ||
599 | 200 | 242 | ||
600 | 201 | Example config: | 243 | Example config: |
602 | 202 | install_sources: | 244 | install_sources: | |
603 | 203 | - "ppa:foo" | 245 | - "ppa:foo" |
604 | 204 | - "http://example.com/repo precise main" | 246 | - "http://example.com/repo precise main" |
606 | 205 | install_keys: | 247 | install_keys: | |
607 | 206 | - null | 248 | - null |
608 | 207 | - "a1b2c3d4" | 249 | - "a1b2c3d4" |
609 | 208 | 250 | ||
610 | 209 | Note that 'null' (a.k.a. None) should not be quoted. | 251 | Note that 'null' (a.k.a. None) should not be quoted. |
611 | 210 | """ | 252 | """ |
619 | 211 | sources = safe_load(config(sources_var)) | 253 | sources = safe_load((config(sources_var) or '').strip()) or [] |
620 | 212 | keys = config(keys_var) | 254 | keys = safe_load((config(keys_var) or '').strip()) or None |
621 | 213 | if keys is not None: | 255 | |
622 | 214 | keys = safe_load(keys) | 256 | if isinstance(sources, basestring): |
623 | 215 | if isinstance(sources, basestring) and ( | 257 | sources = [sources] |
624 | 216 | keys is None or isinstance(keys, basestring)): | 258 | |
625 | 217 | add_source(sources, keys) | 259 | if keys is None: |
626 | 260 | for source in sources: | ||
627 | 261 | add_source(source, None) | ||
628 | 218 | else: | 262 | else: |
634 | 219 | if not len(sources) == len(keys): | 263 | if isinstance(keys, basestring): |
635 | 220 | msg = 'Install sources and keys lists are different lengths' | 264 | keys = [keys] |
636 | 221 | raise SourceConfigError(msg) | 265 | |
637 | 222 | for src_num in range(len(sources)): | 266 | if len(sources) != len(keys): |
638 | 223 | add_source(sources[src_num], keys[src_num]) | 267 | raise SourceConfigError( |
639 | 268 | 'Install sources and keys lists are different lengths') | ||
640 | 269 | for source, key in zip(sources, keys): | ||
641 | 270 | add_source(source, key) | ||
642 | 224 | if update: | 271 | if update: |
643 | 225 | apt_update(fatal=True) | 272 | apt_update(fatal=True) |
644 | 226 | 273 | ||
645 | 227 | # The order of this list is very important. Handlers should be listed in from | ||
646 | 228 | # least- to most-specific URL matching. | ||
647 | 229 | FETCH_HANDLERS = ( | ||
648 | 230 | 'charmhelpers.fetch.archiveurl.ArchiveUrlFetchHandler', | ||
649 | 231 | 'charmhelpers.fetch.bzrurl.BzrUrlFetchHandler', | ||
650 | 232 | ) | ||
651 | 233 | |||
652 | 234 | |||
653 | 235 | class UnhandledSource(Exception): | ||
654 | 236 | pass | ||
655 | 237 | |||
656 | 238 | 274 | ||
657 | 239 | def install_remote(source): | 275 | def install_remote(source): |
658 | 240 | """ | 276 | """ |
659 | @@ -265,30 +301,6 @@ | |||
660 | 265 | return install_remote(source) | 301 | return install_remote(source) |
661 | 266 | 302 | ||
662 | 267 | 303 | ||
663 | 268 | class BaseFetchHandler(object): | ||
664 | 269 | |||
665 | 270 | """Base class for FetchHandler implementations in fetch plugins""" | ||
666 | 271 | |||
667 | 272 | def can_handle(self, source): | ||
668 | 273 | """Returns True if the source can be handled. Otherwise returns | ||
669 | 274 | a string explaining why it cannot""" | ||
670 | 275 | return "Wrong source type" | ||
671 | 276 | |||
672 | 277 | def install(self, source): | ||
673 | 278 | """Try to download and unpack the source. Return the path to the | ||
674 | 279 | unpacked files or raise UnhandledSource.""" | ||
675 | 280 | raise UnhandledSource("Wrong source type {}".format(source)) | ||
676 | 281 | |||
677 | 282 | def parse_url(self, url): | ||
678 | 283 | return urlparse(url) | ||
679 | 284 | |||
680 | 285 | def base_url(self, url): | ||
681 | 286 | """Return url without querystring or fragment""" | ||
682 | 287 | parts = list(self.parse_url(url)) | ||
683 | 288 | parts[4:] = ['' for i in parts[4:]] | ||
684 | 289 | return urlunparse(parts) | ||
685 | 290 | |||
686 | 291 | |||
687 | 292 | def plugins(fetch_handlers=None): | 304 | def plugins(fetch_handlers=None): |
688 | 293 | if not fetch_handlers: | 305 | if not fetch_handlers: |
689 | 294 | fetch_handlers = FETCH_HANDLERS | 306 | fetch_handlers = FETCH_HANDLERS |
690 | @@ -306,3 +318,40 @@ | |||
691 | 306 | log("FetchHandler {} not found, skipping plugin".format( | 318 | log("FetchHandler {} not found, skipping plugin".format( |
692 | 307 | handler_name)) | 319 | handler_name)) |
693 | 308 | return plugin_list | 320 | return plugin_list |
694 | 321 | |||
695 | 322 | |||
696 | 323 | def _run_apt_command(cmd, fatal=False): | ||
697 | 324 | """ | ||
698 | 325 | Run an APT command, checking output and retrying if the fatal flag is set | ||
699 | 326 | to True. | ||
700 | 327 | |||
701 | 328 | :param: cmd: str: The apt command to run. | ||
702 | 329 | :param: fatal: bool: Whether the command's output should be checked and | ||
703 | 330 | retried. | ||
704 | 331 | """ | ||
705 | 332 | env = os.environ.copy() | ||
706 | 333 | |||
707 | 334 | if 'DEBIAN_FRONTEND' not in env: | ||
708 | 335 | env['DEBIAN_FRONTEND'] = 'noninteractive' | ||
709 | 336 | |||
710 | 337 | if fatal: | ||
711 | 338 | retry_count = 0 | ||
712 | 339 | result = None | ||
713 | 340 | |||
714 | 341 | # If the command is considered "fatal", we need to retry if the apt | ||
715 | 342 | # lock was not acquired. | ||
716 | 343 | |||
717 | 344 | while result is None or result == APT_NO_LOCK: | ||
718 | 345 | try: | ||
719 | 346 | result = subprocess.check_call(cmd, env=env) | ||
720 | 347 | except subprocess.CalledProcessError, e: | ||
721 | 348 | retry_count = retry_count + 1 | ||
722 | 349 | if retry_count > APT_NO_LOCK_RETRY_COUNT: | ||
723 | 350 | raise | ||
724 | 351 | result = e.returncode | ||
725 | 352 | log("Couldn't acquire DPKG lock. Will retry in {} seconds." | ||
726 | 353 | "".format(APT_NO_LOCK_RETRY_DELAY)) | ||
727 | 354 | time.sleep(APT_NO_LOCK_RETRY_DELAY) | ||
728 | 355 | |||
729 | 356 | else: | ||
730 | 357 | subprocess.call(cmd, env=env) | ||
731 | 309 | 358 | ||
732 | === modified file 'hooks/charmhelpers/fetch/bzrurl.py' | |||
733 | --- hooks/charmhelpers/fetch/bzrurl.py 2014-04-11 20:55:42 +0000 | |||
734 | +++ hooks/charmhelpers/fetch/bzrurl.py 2014-07-30 17:48:38 +0000 | |||
735 | @@ -39,7 +39,8 @@ | |||
736 | 39 | def install(self, source): | 39 | def install(self, source): |
737 | 40 | url_parts = self.parse_url(source) | 40 | url_parts = self.parse_url(source) |
738 | 41 | branch_name = url_parts.path.strip("/").split("/")[-1] | 41 | branch_name = url_parts.path.strip("/").split("/")[-1] |
740 | 42 | dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched", branch_name) | 42 | dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched", |
741 | 43 | branch_name) | ||
742 | 43 | if not os.path.exists(dest_dir): | 44 | if not os.path.exists(dest_dir): |
743 | 44 | mkdir(dest_dir, perms=0755) | 45 | mkdir(dest_dir, perms=0755) |
744 | 45 | try: | 46 | try: |
745 | 46 | 47 | ||
746 | === modified file 'hooks/hooks.py' | |||
747 | --- hooks/hooks.py 2014-07-29 10:22:55 +0000 | |||
748 | +++ hooks/hooks.py 2014-07-30 17:48:38 +0000 | |||
749 | @@ -6,7 +6,6 @@ | |||
750 | 6 | ''' | 6 | ''' |
751 | 7 | 7 | ||
752 | 8 | import commands | 8 | import commands |
753 | 9 | import json | ||
754 | 10 | import os | 9 | import os |
755 | 11 | import re | 10 | import re |
756 | 12 | import signal | 11 | import signal |
757 | @@ -15,7 +14,6 @@ | |||
758 | 15 | import sys | 14 | import sys |
759 | 16 | import time | 15 | import time |
760 | 17 | import yaml | 16 | import yaml |
761 | 18 | import argparse | ||
762 | 19 | 17 | ||
763 | 20 | from os import chmod | 18 | from os import chmod |
764 | 21 | from os import remove | 19 | from os import remove |
765 | @@ -29,10 +27,27 @@ | |||
766 | 29 | apt_update, | 27 | apt_update, |
767 | 30 | apt_install | 28 | apt_install |
768 | 31 | ) | 29 | ) |
769 | 30 | |||
770 | 32 | from charmhelpers.core.hookenv import ( | 31 | from charmhelpers.core.hookenv import ( |
774 | 33 | config | 32 | config, |
775 | 34 | ) | 33 | unit_get, |
776 | 35 | 34 | relation_get, | |
777 | 35 | relation_set, | ||
778 | 36 | relations_of_type, | ||
779 | 37 | relation_id, | ||
780 | 38 | relation_ids, | ||
781 | 39 | open_port, | ||
782 | 40 | close_port, | ||
783 | 41 | Hooks, | ||
784 | 42 | ) | ||
785 | 43 | |||
786 | 44 | from charmhelpers.core.hookenv import log as juju_log | ||
787 | 45 | |||
788 | 46 | from charmhelpers.core.host import ( | ||
789 | 47 | service, | ||
790 | 48 | ) | ||
791 | 49 | |||
792 | 50 | hooks = Hooks() | ||
793 | 36 | 51 | ||
794 | 37 | ############################################################################### | 52 | ############################################################################### |
795 | 38 | # Global variables | 53 | # Global variables |
796 | @@ -40,222 +55,14 @@ | |||
797 | 40 | default_mongodb_config = "/etc/mongodb.conf" | 55 | default_mongodb_config = "/etc/mongodb.conf" |
798 | 41 | default_mongodb_init_config = "/etc/init/mongodb.conf" | 56 | default_mongodb_init_config = "/etc/init/mongodb.conf" |
799 | 42 | default_mongos_list = "/etc/mongos.list" | 57 | default_mongos_list = "/etc/mongos.list" |
802 | 43 | default_wait_for = 20 | 58 | default_wait_for = 10 |
803 | 44 | default_max_tries = 20 | 59 | default_max_tries = 5 |
804 | 45 | 60 | ||
805 | 46 | ############################################################################### | 61 | ############################################################################### |
806 | 47 | # Supporting functions | 62 | # Supporting functions |
807 | 48 | ############################################################################### | 63 | ############################################################################### |
808 | 49 | 64 | ||
809 | 50 | 65 | ||
810 | 51 | #------------------------------------------------------------------------------ | ||
811 | 52 | # juju_log: calls juju-log and records the message defined by the message | ||
812 | 53 | # variable | ||
813 | 54 | #------------------------------------------------------------------------------ | ||
814 | 55 | def juju_log(message=None): | ||
815 | 56 | return (subprocess.call(['juju-log', str(message)]) == 0) | ||
816 | 57 | |||
817 | 58 | |||
818 | 59 | #------------------------------------------------------------------------------ | ||
819 | 60 | # service: Analogous to calling service on the command line to start/stop | ||
820 | 61 | # and get status of a service/daemon. | ||
821 | 62 | # Parameters: | ||
822 | 63 | # service_name: The name of the service to act on. | ||
823 | 64 | # service_action: The action (start, stop, status, etc.) | ||
824 | 65 | # Returns: True if the command was successfully executed or False on | ||
825 | 66 | # error. | ||
826 | 67 | #------------------------------------------------------------------------------ | ||
827 | 68 | def service(service_name=None, service_action=None): | ||
828 | 69 | juju_log("service: %s, action: %s" % (service_name, service_action)) | ||
829 | 70 | if service_name is not None and service_action is not None: | ||
830 | 71 | retVal = subprocess.call( | ||
831 | 72 | ["service", service_name, service_action]) == 0 | ||
832 | 73 | else: | ||
833 | 74 | retVal = False | ||
834 | 75 | juju_log("service %s %s returns: %s" % | ||
835 | 76 | (service_name, service_action, retVal)) | ||
836 | 77 | return(retVal) | ||
837 | 78 | |||
838 | 79 | |||
839 | 80 | #------------------------------------------------------------------------------ | ||
840 | 81 | # unit_get: Convenience function wrapping the juju command unit-get | ||
841 | 82 | # Parameter: | ||
842 | 83 | # setting_name: The setting to get out of unit_get | ||
843 | 84 | # Returns: The requested information or None on error | ||
844 | 85 | #------------------------------------------------------------------------------ | ||
845 | 86 | def unit_get(setting_name=None): | ||
846 | 87 | juju_log("unit_get: %s" % setting_name) | ||
847 | 88 | try: | ||
848 | 89 | cmd_line = ['unit-get', '--format=json'] | ||
849 | 90 | if setting_name is not None: | ||
850 | 91 | cmd_line.append(setting_name) | ||
851 | 92 | unit_data = json.loads(subprocess.check_output(cmd_line)) | ||
852 | 93 | except Exception, e: | ||
853 | 94 | subprocess.call(['juju-log', str(e)]) | ||
854 | 95 | unit_data = None | ||
855 | 96 | finally: | ||
856 | 97 | juju_log("unit_get %s returns: %s" % (setting_name, unit_data)) | ||
857 | 98 | return(unit_data) | ||
858 | 99 | |||
859 | 100 | |||
860 | 101 | #------------------------------------------------------------------------------ | ||
861 | 102 | # config_get: Returns a dictionary containing all of the config information | ||
862 | 103 | # Optional parameter: scope | ||
863 | 104 | # scope: limits the scope of the returned configuration to the | ||
864 | 105 | # desired config item. | ||
865 | 106 | #------------------------------------------------------------------------------ | ||
866 | 107 | def config_get(scope=None): | ||
867 | 108 | juju_log("config_get: %s" % scope) | ||
868 | 109 | try: | ||
869 | 110 | config_cmd_line = ['config-get'] | ||
870 | 111 | if scope is not None: | ||
871 | 112 | config_cmd_line.append(scope) | ||
872 | 113 | config_cmd_line.append('--format=json') | ||
873 | 114 | config_data = json.loads(subprocess.check_output(config_cmd_line)) | ||
874 | 115 | except Exception, e: | ||
875 | 116 | juju_log(str(e)) | ||
876 | 117 | config_data = None | ||
877 | 118 | finally: | ||
878 | 119 | juju_log("config_get: %s returns: %s" % (scope, config_data)) | ||
879 | 120 | return(config_data) | ||
880 | 121 | |||
881 | 122 | |||
882 | 123 | #------------------------------------------------------------------------------ | ||
883 | 124 | # relation_get: Returns a dictionary containing the relation information | ||
884 | 125 | # Optional parameters: scope, relation_id | ||
885 | 126 | # scope: limits the scope of the returned data to the | ||
886 | 127 | # desired item. | ||
887 | 128 | # unit_name: limits the data ( and optionally the scope ) | ||
888 | 129 | # to the specified unit | ||
889 | 130 | # relation_id: specify relation id for out of context usage. | ||
890 | 131 | #------------------------------------------------------------------------------ | ||
891 | 132 | def relation_get(scope=None, unit_name=None, relation_id=None, | ||
892 | 133 | wait_for=default_wait_for, max_tries=default_max_tries): | ||
893 | 134 | juju_log("relation_get: scope: %s, unit_name: %s, relation_id: %s" % | ||
894 | 135 | (scope, unit_name, relation_id)) | ||
895 | 136 | current_try = 0 | ||
896 | 137 | try: | ||
897 | 138 | relation_cmd_line = ['relation-get', '--format=json'] | ||
898 | 139 | if relation_id is not None: | ||
899 | 140 | relation_cmd_line.extend(('-r', relation_id)) | ||
900 | 141 | if scope is not None: | ||
901 | 142 | relation_cmd_line.append(scope) | ||
902 | 143 | else: | ||
903 | 144 | relation_cmd_line.append('') | ||
904 | 145 | if unit_name is not None: | ||
905 | 146 | relation_cmd_line.append(unit_name) | ||
906 | 147 | relation_data = json.loads(subprocess.check_output(relation_cmd_line)) | ||
907 | 148 | |||
908 | 149 | # while relation_data is None and current_try < max_tries: | ||
909 | 150 | # time.sleep(wait_for) | ||
910 | 151 | # relation_data = json.loads(subprocess.check_output(relation_cmd_line)) | ||
911 | 152 | # current_try += 1 | ||
912 | 153 | |||
913 | 154 | except Exception, e: | ||
914 | 155 | juju_log(str(e)) | ||
915 | 156 | relation_data = None | ||
916 | 157 | finally: | ||
917 | 158 | juju_log("relation_get returns: %s" % relation_data) | ||
918 | 159 | return(relation_data) | ||
919 | 160 | |||
920 | 161 | |||
921 | 162 | #------------------------------------------------------------------------------ | ||
922 | 163 | # relation_set: Convenience function wrapping the juju command relation-set | ||
923 | 164 | # Parameters: | ||
924 | 165 | # key_value_pairs: A dictionary containing the key/value pairs | ||
925 | 166 | # to be set. | ||
926 | 167 | # Optional Parameter: | ||
927 | 168 | # relation_id: The relation id to use | ||
928 | 169 | # Returns: True on success or False on failure | ||
929 | 170 | #------------------------------------------------------------------------------ | ||
930 | 171 | def relation_set(key_value_pairs=None, relation_id=None): | ||
931 | 172 | juju_log("relation_set: kv: %s, relation_id: %s" % | ||
932 | 173 | (key_value_pairs, relation_id)) | ||
933 | 174 | if key_value_pairs is None or not isinstance(key_value_pairs, dict): | ||
934 | 175 | juju_log("relation_set: Invalid key_value_pais.") | ||
935 | 176 | return(False) | ||
936 | 177 | try: | ||
937 | 178 | relation_cmd_line = ['relation-set', '--format=json'] | ||
938 | 179 | if relation_id is not None: | ||
939 | 180 | relation_cmd_line.append('-r %s' % relation_id) | ||
940 | 181 | for (key, value) in key_value_pairs.items(): | ||
941 | 182 | relation_cmd_line.append('%s=%s' % (key, value)) | ||
942 | 183 | retVal = (subprocess.call(relation_cmd_line) == 0) | ||
943 | 184 | except Exception, e: | ||
944 | 185 | juju_log(str(e)) | ||
945 | 186 | retVal = False | ||
946 | 187 | finally: | ||
947 | 188 | juju_log("relation_set returns: %s" % retVal) | ||
948 | 189 | return(retVal) | ||
949 | 190 | |||
950 | 191 | |||
951 | 192 | def relation_list(relation_id=None, wait_for=default_wait_for, | ||
952 | 193 | max_tries=default_max_tries): | ||
953 | 194 | juju_log("relation_list: relation_id: %s" % relation_id) | ||
954 | 195 | current_try = 0 | ||
955 | 196 | try: | ||
956 | 197 | relation_cmd_line = ['relation-list', '--format=json'] | ||
957 | 198 | if relation_id is not None: | ||
958 | 199 | relation_cmd_line.append('-r %s' % relation_id) | ||
959 | 200 | relation_data = json.loads(subprocess.check_output(relation_cmd_line)) | ||
960 | 201 | |||
961 | 202 | # while relation_data is None and current_try < max_tries: | ||
962 | 203 | # time.sleep(wait_for) | ||
963 | 204 | # relation_data = json.loads(subprocess.check_output(relation_cmd_line)) | ||
964 | 205 | # current_try += 1 | ||
965 | 206 | |||
966 | 207 | except Exception, e: | ||
967 | 208 | juju_log(str(e)) | ||
968 | 209 | relation_data = None | ||
969 | 210 | finally: | ||
970 | 211 | juju_log("relation_id %s returns: %s" % (relation_id, relation_data)) | ||
971 | 212 | return(relation_data) | ||
972 | 213 | |||
973 | 214 | |||
974 | 215 | def relation_ids(relation_name=None): | ||
975 | 216 | juju_log("relation_ids: relation_name: %s" % relation_name) | ||
976 | 217 | try: | ||
977 | 218 | relation_cmd_line = ['relation-ids', '--format=json'] | ||
978 | 219 | if relation_name is not None: | ||
979 | 220 | relation_cmd_line.append(relation_name) | ||
980 | 221 | relation_data = json.loads(subprocess.check_output(relation_cmd_line)) | ||
981 | 222 | except Exception, e: | ||
982 | 223 | juju_log(str(e)) | ||
983 | 224 | relation_data = None | ||
984 | 225 | finally: | ||
985 | 226 | juju_log("relation_ids %s returns: %s" % (relation_name, relation_data)) | ||
986 | 227 | return(relation_data) | ||
987 | 228 | |||
988 | 229 | #------------------------------------------------------------------------------ | ||
989 | 230 | # open_port: Convenience function to open a port in juju to | ||
990 | 231 | # expose a service | ||
991 | 232 | #------------------------------------------------------------------------------ | ||
992 | 233 | def open_port(port=None, protocol="TCP"): | ||
993 | 234 | juju_log("open_port: port: %d protocol: %s" % (int(port), protocol)) | ||
994 | 235 | if port is None: | ||
995 | 236 | retVal = False | ||
996 | 237 | else: | ||
997 | 238 | retVal = subprocess.call(['open-port', "%d/%s" % | ||
998 | 239 | (int(port), protocol)]) == 0 | ||
999 | 240 | juju_log("open_port %d/%s returns: %s" % (int(port), protocol, retVal)) | ||
1000 | 241 | return(retVal) | ||
1001 | 242 | |||
1002 | 243 | |||
1003 | 244 | #------------------------------------------------------------------------------ | ||
1004 | 245 | # close_port: Convenience function to close a port in juju to | ||
1005 | 246 | # unexpose a service | ||
1006 | 247 | #------------------------------------------------------------------------------ | ||
1007 | 248 | def close_port(port=None, protocol="TCP"): | ||
1008 | 249 | juju_log("close_port: port: %d protocol: %s" % (int(port), protocol)) | ||
1009 | 250 | if port is None: | ||
1010 | 251 | retVal = False | ||
1011 | 252 | else: | ||
1012 | 253 | retVal = subprocess.call(['close-port', "%d/%s" % | ||
1013 | 254 | (int(port), protocol)]) == 0 | ||
1014 | 255 | juju_log("close_port %d/%s returns: %s" % (int(port), protocol, retVal)) | ||
1015 | 256 | return(retVal) | ||
1016 | 257 | |||
1017 | 258 | |||
1018 | 259 | def port_check(host=None, port=None, protocol='TCP'): | 66 | def port_check(host=None, port=None, protocol='TCP'): |
1019 | 260 | if host is None or port is None: | 67 | if host is None or port is None: |
1020 | 261 | juju_log("port_check: host and port must be defined.") | 68 | juju_log("port_check: host and port must be defined.") |
1021 | @@ -494,7 +301,7 @@ | |||
1022 | 494 | config.append("") | 301 | config.append("") |
1023 | 495 | 302 | ||
1024 | 496 | # arbiter | 303 | # arbiter |
1026 | 497 | if config_data['arbiter'] != "disabled" and\ | 304 | if config_data['arbiter'] != "disabled" and \ |
1027 | 498 | config_data['arbiter'] != "enabled": | 305 | config_data['arbiter'] != "enabled": |
1028 | 499 | config.append("arbiter = %s" % config_data['arbiter']) | 306 | config.append("arbiter = %s" % config_data['arbiter']) |
1029 | 500 | config.append("") | 307 | config.append("") |
1030 | @@ -657,7 +464,7 @@ | |||
1031 | 657 | 464 | ||
1032 | 658 | 465 | ||
1033 | 659 | def configsvr_status(wait_for=default_wait_for, max_tries=default_max_tries): | 466 | def configsvr_status(wait_for=default_wait_for, max_tries=default_max_tries): |
1035 | 660 | config_data = config_get() | 467 | config_data = config() |
1036 | 661 | current_try = 0 | 468 | current_try = 0 |
1037 | 662 | while (process_check_pidfile('/var/run/mongodb/configsvr.pid') != | 469 | while (process_check_pidfile('/var/run/mongodb/configsvr.pid') != |
1038 | 663 | (None, None)) and not port_check( | 470 | (None, None)) and not port_check( |
1039 | @@ -685,7 +492,7 @@ | |||
1040 | 685 | juju_log("disable_configsvr: port not defined.") | 492 | juju_log("disable_configsvr: port not defined.") |
1041 | 686 | return(False) | 493 | return(False) |
1042 | 687 | try: | 494 | try: |
1044 | 688 | config_server_port = config_get('config_server_port') | 495 | config_server_port = config('config_server_port') |
1045 | 689 | pid = open('/var/run/mongodb/configsvr.pid').read() | 496 | pid = open('/var/run/mongodb/configsvr.pid').read() |
1046 | 690 | os.kill(int(pid), signal.SIGTERM) | 497 | os.kill(int(pid), signal.SIGTERM) |
1047 | 691 | os.unlink('/var/run/mongodb/configsvr.pid') | 498 | os.unlink('/var/run/mongodb/configsvr.pid') |
1048 | @@ -747,7 +554,7 @@ | |||
1049 | 747 | 554 | ||
1050 | 748 | 555 | ||
1051 | 749 | def mongos_status(wait_for=default_wait_for, max_tries=default_max_tries): | 556 | def mongos_status(wait_for=default_wait_for, max_tries=default_max_tries): |
1053 | 750 | config_data = config_get() | 557 | config_data = config() |
1054 | 751 | current_try = 0 | 558 | current_try = 0 |
1055 | 752 | while (process_check_pidfile('/var/run/mongodb/mongos.pid') != | 559 | while (process_check_pidfile('/var/run/mongodb/mongos.pid') != |
1056 | 753 | (None, None)) and not port_check( | 560 | (None, None)) and not port_check( |
1057 | @@ -839,17 +646,17 @@ | |||
1058 | 839 | 646 | ||
1059 | 840 | def restart_mongod(wait_for=default_wait_for, max_tries=default_max_tries): | 647 | def restart_mongod(wait_for=default_wait_for, max_tries=default_max_tries): |
1060 | 841 | my_hostname = unit_get('public-address') | 648 | my_hostname = unit_get('public-address') |
1062 | 842 | my_port = config_get('port') | 649 | my_port = config('port') |
1063 | 843 | current_try = 0 | 650 | current_try = 0 |
1064 | 844 | 651 | ||
1066 | 845 | service('mongodb', 'stop') | 652 | service('stop', 'mongodb') |
1067 | 846 | if os.path.exists('/var/lib/mongodb/mongod.lock'): | 653 | if os.path.exists('/var/lib/mongodb/mongod.lock'): |
1068 | 847 | os.remove('/var/lib/mongodb/mongod.lock') | 654 | os.remove('/var/lib/mongodb/mongod.lock') |
1069 | 848 | 655 | ||
1071 | 849 | if not service('mongodb', 'start'): | 656 | if not service('start', 'mongodb'): |
1072 | 850 | return False | 657 | return False |
1073 | 851 | 658 | ||
1075 | 852 | while (service('mongodb', 'status') and | 659 | while (service('status', 'mongodb') and |
1076 | 853 | not port_check(my_hostname, my_port) and | 660 | not port_check(my_hostname, my_port) and |
1077 | 854 | current_try < max_tries): | 661 | current_try < max_tries): |
1078 | 855 | juju_log( | 662 | juju_log( |
1079 | @@ -859,14 +666,14 @@ | |||
1080 | 859 | current_try += 1 | 666 | current_try += 1 |
1081 | 860 | 667 | ||
1082 | 861 | return( | 668 | return( |
1084 | 862 | (service('mongodb', 'status') == port_check(my_hostname, my_port)) | 669 | (service('status', 'mongodb') == port_check(my_hostname, my_port)) |
1085 | 863 | is True) | 670 | is True) |
1086 | 864 | 671 | ||
1087 | 865 | 672 | ||
1088 | 866 | def backup_cronjob(disable=False): | 673 | def backup_cronjob(disable=False): |
1089 | 867 | """Generate the cronjob to backup with mongodbump.""" | 674 | """Generate the cronjob to backup with mongodbump.""" |
1090 | 868 | juju_log('Setting up cronjob') | 675 | juju_log('Setting up cronjob') |
1092 | 869 | config_data = config_get() | 676 | config_data = config() |
1093 | 870 | backupdir = config_data['backup_directory'] | 677 | backupdir = config_data['backup_directory'] |
1094 | 871 | bind_ip = config_data['bind_ip'] | 678 | bind_ip = config_data['bind_ip'] |
1095 | 872 | cron_file = '/etc/cron.d/mongodb' | 679 | cron_file = '/etc/cron.d/mongodb' |
1096 | @@ -915,18 +722,19 @@ | |||
1097 | 915 | ############################################################################### | 722 | ############################################################################### |
1098 | 916 | # Hook functions | 723 | # Hook functions |
1099 | 917 | ############################################################################### | 724 | ############################################################################### |
1100 | 725 | @hooks.hook('install') | ||
1101 | 918 | def install_hook(): | 726 | def install_hook(): |
1102 | 919 | juju_log("Installing mongodb") | 727 | juju_log("Installing mongodb") |
1103 | 920 | add_source(config('source'), config('key')) | 728 | add_source(config('source'), config('key')) |
1104 | 921 | apt_update(fatal=True) | 729 | apt_update(fatal=True) |
1105 | 922 | apt_install(packages=['mongodb', 'python-yaml'], fatal=True) | 730 | apt_install(packages=['mongodb', 'python-yaml'], fatal=True) |
1109 | 923 | return True | 731 | |
1110 | 924 | 732 | ||
1111 | 925 | 733 | @hooks.hook('config-changed') | |
1112 | 926 | def config_changed(): | 734 | def config_changed(): |
1113 | 927 | juju_log("Entering config_changed") | 735 | juju_log("Entering config_changed") |
1114 | 928 | print "Entering config_changed" | 736 | print "Entering config_changed" |
1116 | 929 | config_data = config_get() | 737 | config_data = config() |
1117 | 930 | print "config_data: ", config_data | 738 | print "config_data: ", config_data |
1118 | 931 | mongodb_config = open(default_mongodb_config).read() | 739 | mongodb_config = open(default_mongodb_config).read() |
1119 | 932 | 740 | ||
1120 | @@ -1048,7 +856,7 @@ | |||
1121 | 1048 | juju_log("config_changed: Exceptions: %s" % str(e)) | 856 | juju_log("config_changed: Exceptions: %s" % str(e)) |
1122 | 1049 | 857 | ||
1123 | 1050 | if mongos_pid is not None: | 858 | if mongos_pid is not None: |
1125 | 1051 | mongos_port = re.search('--port (\w+)', mongos_cmd_line).group(2) | 859 | mongos_port = re.search('--port (\w+)', mongos_cmd_line).group(1) |
1126 | 1052 | disable_mongos(mongos_port) | 860 | disable_mongos(mongos_port) |
1127 | 1053 | enable_mongos(config_data['mongos_port']) | 861 | enable_mongos(config_data['mongos_port']) |
1128 | 1054 | else: | 862 | else: |
1129 | @@ -1058,6 +866,7 @@ | |||
1130 | 1058 | return(True) | 866 | return(True) |
1131 | 1059 | 867 | ||
1132 | 1060 | 868 | ||
1133 | 869 | @hooks.hook('start') | ||
1134 | 1061 | def start_hook(): | 870 | def start_hook(): |
1135 | 1062 | juju_log("start_hook") | 871 | juju_log("start_hook") |
1136 | 1063 | retVal = restart_mongod() | 872 | retVal = restart_mongod() |
1137 | @@ -1065,10 +874,11 @@ | |||
1138 | 1065 | return(retVal) | 874 | return(retVal) |
1139 | 1066 | 875 | ||
1140 | 1067 | 876 | ||
1141 | 877 | @hooks.hook('stop') | ||
1142 | 1068 | def stop_hook(): | 878 | def stop_hook(): |
1143 | 1069 | juju_log("stop_hook") | 879 | juju_log("stop_hook") |
1144 | 1070 | try: | 880 | try: |
1146 | 1071 | retVal = service('mongodb', 'stop') | 881 | retVal = service('stop', 'mongodb') |
1147 | 1072 | os.remove('/var/lib/mongodb/mongod.lock') | 882 | os.remove('/var/lib/mongodb/mongod.lock') |
1148 | 1073 | #FIXME Need to check if this is still needed | 883 | #FIXME Need to check if this is still needed |
1149 | 1074 | except Exception, e: | 884 | except Exception, e: |
1150 | @@ -1079,15 +889,16 @@ | |||
1151 | 1079 | return(retVal) | 889 | return(retVal) |
1152 | 1080 | 890 | ||
1153 | 1081 | 891 | ||
1154 | 892 | @hooks.hook('database-relation-joined') | ||
1155 | 1082 | def database_relation_joined(): | 893 | def database_relation_joined(): |
1156 | 1083 | juju_log("database_relation_joined") | 894 | juju_log("database_relation_joined") |
1157 | 1084 | my_hostname = unit_get('public-address') | 895 | my_hostname = unit_get('public-address') |
1160 | 1085 | my_port = config_get('port') | 896 | my_port = config('port') |
1161 | 1086 | my_replset = config_get('replicaset') | 897 | my_replset = config('replicaset') |
1162 | 1087 | juju_log("my_hostname: %s" % my_hostname) | 898 | juju_log("my_hostname: %s" % my_hostname) |
1163 | 1088 | juju_log("my_port: %s" % my_port) | 899 | juju_log("my_port: %s" % my_port) |
1164 | 1089 | juju_log("my_replset: %s" % my_replset) | 900 | juju_log("my_replset: %s" % my_replset) |
1166 | 1090 | return(relation_set( | 901 | return(relation_set(relation_id(), |
1167 | 1091 | { | 902 | { |
1168 | 1092 | 'hostname': my_hostname, | 903 | 'hostname': my_hostname, |
1169 | 1093 | 'port': my_port, | 904 | 'port': my_port, |
1170 | @@ -1096,34 +907,36 @@ | |||
1171 | 1096 | })) | 907 | })) |
1172 | 1097 | 908 | ||
1173 | 1098 | 909 | ||
1174 | 910 | @hooks.hook('replicaset-relation-joined') | ||
1175 | 1099 | def replica_set_relation_joined(): | 911 | def replica_set_relation_joined(): |
1176 | 1100 | juju_log("replica_set_relation_joined") | 912 | juju_log("replica_set_relation_joined") |
1177 | 1101 | my_hostname = unit_get('public-address') | 913 | my_hostname = unit_get('public-address') |
1180 | 1102 | my_port = config_get('port') | 914 | my_port = config('port') |
1181 | 1103 | my_replset = config_get('replicaset') | 915 | my_replset = config('replicaset') |
1182 | 1104 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] | 916 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] |
1183 | 1105 | juju_log("my_hostname: %s" % my_hostname) | 917 | juju_log("my_hostname: %s" % my_hostname) |
1184 | 1106 | juju_log("my_port: %s" % my_port) | 918 | juju_log("my_port: %s" % my_port) |
1185 | 1107 | juju_log("my_replset: %s" % my_replset) | 919 | juju_log("my_replset: %s" % my_replset) |
1186 | 1108 | juju_log("my_install_order: %s" % my_install_order) | 920 | juju_log("my_install_order: %s" % my_install_order) |
1199 | 1109 | return(enable_replset(my_replset) == | 921 | enable_replset(my_replset) |
1200 | 1110 | restart_mongod() == | 922 | restart_mongod() |
1201 | 1111 | relation_set( | 923 | |
1202 | 1112 | { | 924 | relation_set(relation_id(), { |
1203 | 1113 | 'hostname': my_hostname, | 925 | 'hostname': my_hostname, |
1204 | 1114 | 'port': my_port, | 926 | 'port': my_port, |
1205 | 1115 | 'replset': my_replset, | 927 | 'replset': my_replset, |
1206 | 1116 | 'install-order': my_install_order, | 928 | 'install-order': my_install_order, |
1207 | 1117 | 'type': 'replset', | 929 | 'type': 'replset', |
1208 | 1118 | })) | 930 | }) |
1209 | 1119 | 931 | ||
1210 | 1120 | 932 | ||
1211 | 933 | @hooks.hook('replicaset-relation-changed') | ||
1212 | 1121 | def replica_set_relation_changed(): | 934 | def replica_set_relation_changed(): |
1213 | 1122 | juju_log("replica_set_relation_changed") | 935 | juju_log("replica_set_relation_changed") |
1214 | 1123 | my_hostname = unit_get('public-address') | 936 | my_hostname = unit_get('public-address') |
1216 | 1124 | my_port = config_get('port') | 937 | my_port = config('port') |
1217 | 1125 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] | 938 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] |
1219 | 1126 | my_replicaset_master = config_get('replicaset_master') | 939 | my_replicaset_master = config('replicaset_master') |
1220 | 1127 | 940 | ||
1221 | 1128 | # If we are joining an existing replicaset cluster, just join and leave. | 941 | # If we are joining an existing replicaset cluster, just join and leave. |
1222 | 1129 | if my_replicaset_master != "auto": | 942 | if my_replicaset_master != "auto": |
1223 | @@ -1135,44 +948,44 @@ | |||
1224 | 1135 | master_install_order = my_install_order | 948 | master_install_order = my_install_order |
1225 | 1136 | 949 | ||
1226 | 1137 | # Check the nodes in the relation to find the master | 950 | # Check the nodes in the relation to find the master |
1228 | 1138 | for member in relation_list(): | 951 | for member in relations_of_type('replica-set'): |
1229 | 952 | member = member['__unit__'] | ||
1230 | 1139 | juju_log("replica_set_relation_changed: member: %s" % member) | 953 | juju_log("replica_set_relation_changed: member: %s" % member) |
1231 | 1140 | hostname = relation_get('hostname', member) | 954 | hostname = relation_get('hostname', member) |
1232 | 1141 | port = relation_get('port', member) | 955 | port = relation_get('port', member) |
1237 | 1142 | install_order = relation_get('install-order', member) | 956 | inst_ordr = relation_get('install-order', member) |
1238 | 1143 | juju_log("replica_set_relation_changed: install_order: %s" % install_order) | 957 | juju_log("replica_set_relation_changed: install_order: %s" % inst_ordr) |
1239 | 1144 | if install_order is None: | 958 | if inst_ordr is None: |
1240 | 1145 | juju_log("replica_set_relation_changed: install_order is None. relation is not ready") | 959 | juju_log("replica_set_relation_changed: install_order is None." |
1241 | 960 | " relation is not ready") | ||
1242 | 1146 | break | 961 | break |
1244 | 1147 | if int(install_order) < int(master_install_order): | 962 | if int(inst_ordr) < int(master_install_order): |
1245 | 1148 | master_hostname = hostname | 963 | master_hostname = hostname |
1246 | 1149 | master_port = port | 964 | master_port = port |
1248 | 1150 | master_install_order = install_order | 965 | master_install_order = inst_ordr |
1249 | 1151 | 966 | ||
1250 | 1152 | # Initiate the replset | 967 | # Initiate the replset |
1251 | 1153 | init_replset("%s:%s" % (master_hostname, master_port)) | 968 | init_replset("%s:%s" % (master_hostname, master_port)) |
1252 | 1154 | 969 | ||
1253 | 1155 | # Add the rest of the nodes to the replset | 970 | # Add the rest of the nodes to the replset |
1257 | 1156 | for member in relation_list(): | 971 | for member in relations_of_type('replica-set'): |
1258 | 1157 | hostname = relation_get('hostname', member) | 972 | hostname = relation_get('hostname', member['__unit__']) |
1259 | 1158 | port = relation_get('port', member) | 973 | port = relation_get('port', member['__unit__']) |
1260 | 1159 | if master_hostname != hostname: | 974 | if master_hostname != hostname: |
1261 | 1160 | if hostname == my_hostname: | 975 | if hostname == my_hostname: |
1265 | 1161 | subprocess.call(['mongo', | 976 | subprocess.call(['mongo', '--eval', |
1266 | 1162 | '--eval', | 977 | "rs.add(\"%s\")" % hostname]) |
1264 | 1163 | "rs.add(\"%s\")" % hostname]) | ||
1267 | 1164 | else: | 978 | else: |
1268 | 1165 | join_replset("%s:%s" % (master_hostname, master_port), | 979 | join_replset("%s:%s" % (master_hostname, master_port), |
1270 | 1166 | "%s:%s" % (hostname, port)) | 980 | "%s:%s" % (hostname, port)) |
1271 | 1167 | 981 | ||
1272 | 1168 | # Add this node to the replset ( if needed ) | 982 | # Add this node to the replset ( if needed ) |
1273 | 1169 | if master_hostname != my_hostname: | 983 | if master_hostname != my_hostname: |
1274 | 1170 | join_replset("%s:%s" % (master_hostname, master_port), | 984 | join_replset("%s:%s" % (master_hostname, master_port), |
1280 | 1171 | "%s:%s" % (my_hostname, my_port)) | 985 | "%s:%s" % (my_hostname, my_port)) |
1281 | 1172 | 986 | ||
1282 | 1173 | return(True) | 987 | |
1283 | 1174 | 988 | @hooks.hook('data-relation-joined') | |
1279 | 1175 | |||
1284 | 1176 | def data_relation_joined(): | 989 | def data_relation_joined(): |
1285 | 1177 | juju_log("data_relation_joined") | 990 | juju_log("data_relation_joined") |
1286 | 1178 | 991 | ||
1287 | @@ -1182,6 +995,7 @@ | |||
1288 | 1182 | })) | 995 | })) |
1289 | 1183 | 996 | ||
1290 | 1184 | 997 | ||
1291 | 998 | @hooks.hook('data-relation-changed') | ||
1292 | 1185 | def data_relation_changed(): | 999 | def data_relation_changed(): |
1293 | 1186 | juju_log("data_relation_changed") | 1000 | juju_log("data_relation_changed") |
1294 | 1187 | 1001 | ||
1295 | @@ -1189,60 +1003,63 @@ | |||
1296 | 1189 | juju_log("mountpoint from storage subordinate not ready, let's wait") | 1003 | juju_log("mountpoint from storage subordinate not ready, let's wait") |
1297 | 1190 | return(True) | 1004 | return(True) |
1298 | 1191 | 1005 | ||
1302 | 1192 | return(config_changed()) | 1006 | config_changed() |
1303 | 1193 | 1007 | ||
1304 | 1194 | 1008 | ||
1305 | 1009 | @hooks.hook('data-relation-departed') | ||
1306 | 1195 | def data_relation_departed(): | 1010 | def data_relation_departed(): |
1307 | 1196 | juju_log("data_relation_departed") | 1011 | juju_log("data_relation_departed") |
1308 | 1197 | return(config_changed()) | 1012 | return(config_changed()) |
1309 | 1198 | 1013 | ||
1311 | 1199 | 1014 | @hooks.hook('configsvr-relation-joined') | |
1312 | 1200 | def configsvr_relation_joined(): | 1015 | def configsvr_relation_joined(): |
1313 | 1201 | juju_log("configsvr_relation_joined") | 1016 | juju_log("configsvr_relation_joined") |
1314 | 1202 | my_hostname = unit_get('public-address') | 1017 | my_hostname = unit_get('public-address') |
1316 | 1203 | my_port = config_get('config_server_port') | 1018 | my_port = config('config_server_port') |
1317 | 1204 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] | 1019 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] |
1327 | 1205 | return(relation_set( | 1020 | relation_set(relation_id(), |
1328 | 1206 | { | 1021 | { |
1329 | 1207 | 'hostname': my_hostname, | 1022 | 'hostname': my_hostname, |
1330 | 1208 | 'port': my_port, | 1023 | 'port': my_port, |
1331 | 1209 | 'install-order': my_install_order, | 1024 | 'install-order': my_install_order, |
1332 | 1210 | 'type': 'configsvr', | 1025 | 'type': 'configsvr', |
1333 | 1211 | })) | 1026 | }) |
1334 | 1212 | 1027 | ||
1335 | 1213 | 1028 | ||
1336 | 1029 | @hooks.hook('configsvr-relation-changed') | ||
1337 | 1214 | def configsvr_relation_changed(): | 1030 | def configsvr_relation_changed(): |
1338 | 1215 | juju_log("configsvr_relation_changed") | 1031 | juju_log("configsvr_relation_changed") |
1340 | 1216 | config_data = config_get() | 1032 | config_data = config() |
1341 | 1217 | my_port = config_data['config_server_port'] | 1033 | my_port = config_data['config_server_port'] |
1342 | 1218 | disable_configsvr(my_port) | 1034 | disable_configsvr(my_port) |
1348 | 1219 | retVal = enable_configsvr(config_data) | 1035 | |
1349 | 1220 | juju_log("configsvr_relation_changed returns: %s" % retVal) | 1036 | |
1350 | 1221 | return(retVal) | 1037 | @hooks.hook('mongos-cfg-relation-joined') |
1351 | 1222 | 1038 | @hooks.hook('mongos-relation-joined') | |
1347 | 1223 | |||
1352 | 1224 | def mongos_relation_joined(): | 1039 | def mongos_relation_joined(): |
1353 | 1225 | juju_log("mongos_relation_joined") | 1040 | juju_log("mongos_relation_joined") |
1354 | 1226 | my_hostname = unit_get('public-address') | 1041 | my_hostname = unit_get('public-address') |
1356 | 1227 | my_port = config_get('mongos_port') | 1042 | my_port = config('mongos_port') |
1357 | 1228 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] | 1043 | my_install_order = os.environ['JUJU_UNIT_NAME'].split('/')[1] |
1367 | 1229 | return(relation_set( | 1044 | relation_set(relation_id(), |
1368 | 1230 | { | 1045 | { |
1369 | 1231 | 'hostname': my_hostname, | 1046 | 'hostname': my_hostname, |
1370 | 1232 | 'port': my_port, | 1047 | 'port': my_port, |
1371 | 1233 | 'install-order': my_install_order, | 1048 | 'install-order': my_install_order, |
1372 | 1234 | 'type': 'mongos' | 1049 | 'type': 'mongos' |
1373 | 1235 | })) | 1050 | }) |
1374 | 1236 | 1051 | ||
1375 | 1237 | 1052 | ||
1376 | 1053 | @hooks.hook('mongos-cfg-relation-changed') | ||
1377 | 1054 | @hooks.hook('mongos-relation-changed') | ||
1378 | 1238 | def mongos_relation_changed(): | 1055 | def mongos_relation_changed(): |
1379 | 1239 | juju_log("mongos_relation_changed") | 1056 | juju_log("mongos_relation_changed") |
1381 | 1240 | config_data = config_get() | 1057 | config_data = config() |
1382 | 1241 | retVal = False | 1058 | retVal = False |
1387 | 1242 | for member in relation_list(): | 1059 | for member in relations_of_type('mongos-cfg'): |
1388 | 1243 | hostname = relation_get('hostname', member) | 1060 | hostname = relation_get('hostname', member['__unit__']) |
1389 | 1244 | port = relation_get('port', member) | 1061 | port = relation_get('port', member['__unit__']) |
1390 | 1245 | rel_type = relation_get('type', member) | 1062 | rel_type = relation_get('type', member['__unit__']) |
1391 | 1246 | if hostname is None or port is None or rel_type is None: | 1063 | if hostname is None or port is None or rel_type is None: |
1392 | 1247 | juju_log("mongos_relation_changed: relation data not ready.") | 1064 | juju_log("mongos_relation_changed: relation data not ready.") |
1393 | 1248 | break | 1065 | break |
1394 | @@ -1264,34 +1081,33 @@ | |||
1395 | 1264 | if mongos_ready(): | 1081 | if mongos_ready(): |
1396 | 1265 | mongos_host = "%s:%s" % ( | 1082 | mongos_host = "%s:%s" % ( |
1397 | 1266 | unit_get('public-address'), | 1083 | unit_get('public-address'), |
1399 | 1267 | config_get('mongos_port')) | 1084 | config('mongos_port')) |
1400 | 1268 | shard_command1 = "sh.addShard(\"%s:%s\")" % (hostname, port) | 1085 | shard_command1 = "sh.addShard(\"%s:%s\")" % (hostname, port) |
1402 | 1269 | retVal1 = mongo_client(mongos_host, shard_command1) | 1086 | mongo_client(mongos_host, shard_command1) |
1403 | 1270 | replicaset = relation_get('replset', member) | 1087 | replicaset = relation_get('replset', member) |
1405 | 1271 | shard_command2 = "sh.addShard(\"%s/%s:%s\")" % \ | 1088 | shard_command2 = "sh.addShard(\"%s/%s:%s\")" % \ |
1406 | 1272 | (replicaset, hostname, port) | 1089 | (replicaset, hostname, port) |
1412 | 1273 | retVal2 = mongo_client(mongos_host, shard_command2) | 1090 | mongo_client(mongos_host, shard_command2) |
1413 | 1274 | retVal = retVal1 is True and retVal2 is True | 1091 | |
1414 | 1275 | else: | 1092 | |
1410 | 1276 | juju_log("Not enough config server for mongos yet.") | ||
1411 | 1277 | retVal = True | ||
1415 | 1278 | else: | 1093 | else: |
1416 | 1279 | juju_log("mongos_relation_change: undefined rel_type: %s" % | 1094 | juju_log("mongos_relation_change: undefined rel_type: %s" % |
1418 | 1280 | rel_type) | 1095 | rel_type) |
1419 | 1281 | return(False) | 1096 | return(False) |
1420 | 1282 | juju_log("mongos_relation_changed returns: %s" % retVal) | 1097 | juju_log("mongos_relation_changed returns: %s" % retVal) |
1424 | 1283 | return(retVal) | 1098 | |
1425 | 1284 | 1099 | ||
1426 | 1285 | 1100 | ||
1427 | 1101 | @hooks.hook('mongos-relation-broken') | ||
1428 | 1286 | def mongos_relation_broken(): | 1102 | def mongos_relation_broken(): |
1437 | 1287 | # config_servers = load_config_servers(default_mongos_list) | 1103 | config_servers = load_config_servers(default_mongos_list) |
1438 | 1288 | # for member in relation_list(): | 1104 | for member in relations_of_type('mongos'): |
1439 | 1289 | # hostname = relation_get('hostname', member) | 1105 | hostname = relation_get('hostname', member) |
1440 | 1290 | # port = relation_get('port', member) | 1106 | port = relation_get('port', member) |
1441 | 1291 | # if '%s:%s' % (hostname, port) in config_servers: | 1107 | if '%s:%s' % (hostname, port) in config_servers: |
1442 | 1292 | # config_servers.remove('%s:%s' % (hostname, port)) | 1108 | config_servers.remove('%s:%s' % (hostname, port)) |
1443 | 1293 | # return(update_file(default_mongos_list, '\n'.join(config_servers))) | 1109 | |
1444 | 1294 | return(True) | 1110 | update_file(default_mongos_list, '\n'.join(config_servers)) |
1445 | 1295 | 1111 | ||
1446 | 1296 | 1112 | ||
1447 | 1297 | def run(command, exit_on_error=True): | 1113 | def run(command, exit_on_error=True): |
1448 | @@ -1318,7 +1134,7 @@ | |||
1449 | 1318 | # | 1134 | # |
1450 | 1319 | #------------------------------ | 1135 | #------------------------------ |
1451 | 1320 | def volume_get_volid_from_volume_map(): | 1136 | def volume_get_volid_from_volume_map(): |
1453 | 1321 | config_data = config_get() | 1137 | config_data = config() |
1454 | 1322 | volume_map = {} | 1138 | volume_map = {} |
1455 | 1323 | try: | 1139 | try: |
1456 | 1324 | volume_map = yaml.load(config_data['volume-map'].strip()) | 1140 | volume_map = yaml.load(config_data['volume-map'].strip()) |
1457 | @@ -1379,19 +1195,21 @@ | |||
1458 | 1379 | # None config state is invalid - we should not serve | 1195 | # None config state is invalid - we should not serve |
1459 | 1380 | def volume_get_volume_id(): | 1196 | def volume_get_volume_id(): |
1460 | 1381 | 1197 | ||
1461 | 1198 | config_data = config() | ||
1462 | 1199 | |||
1463 | 1200 | |||
1464 | 1382 | 1201 | ||
1465 | 1383 | volid = volume_get_id_for_storage_subordinate() | 1202 | volid = volume_get_id_for_storage_subordinate() |
1466 | 1384 | if volid: | 1203 | if volid: |
1467 | 1385 | return volid | 1204 | return volid |
1468 | 1386 | 1205 | ||
1469 | 1387 | config_data = config_get() | ||
1470 | 1388 | ephemeral_storage = config_data['volume-ephemeral-storage'] | 1206 | ephemeral_storage = config_data['volume-ephemeral-storage'] |
1471 | 1389 | volid = volume_get_volid_from_volume_map() | 1207 | volid = volume_get_volid_from_volume_map() |
1472 | 1390 | juju_unit_name = os.environ['JUJU_UNIT_NAME'] | 1208 | juju_unit_name = os.environ['JUJU_UNIT_NAME'] |
1473 | 1391 | if ephemeral_storage in [True, 'yes', 'Yes', 'true', 'True']: | 1209 | if ephemeral_storage in [True, 'yes', 'Yes', 'true', 'True']: |
1474 | 1392 | if volid: | 1210 | if volid: |
1475 | 1393 | juju_log( | 1211 | juju_log( |
1477 | 1394 | "volume-ephemeral-storage is True, but " + | 1212 | "volume-ephemeral-storage is True, but" |
1478 | 1395 | "volume-map[{!r}] -> {}".format(juju_unit_name, volid)) | 1213 | "volume-map[{!r}] -> {}".format(juju_unit_name, volid)) |
1479 | 1396 | return None | 1214 | return None |
1480 | 1397 | else: | 1215 | else: |
1481 | @@ -1424,6 +1242,7 @@ | |||
1482 | 1424 | return None | 1242 | return None |
1483 | 1425 | return output | 1243 | return output |
1484 | 1426 | 1244 | ||
1485 | 1245 | |||
1486 | 1427 | #------------------------------------------------------------------------------ | 1246 | #------------------------------------------------------------------------------ |
1487 | 1428 | # Core logic for permanent storage changes: | 1247 | # Core logic for permanent storage changes: |
1488 | 1429 | # NOTE the only 2 "True" return points: | 1248 | # NOTE the only 2 "True" return points: |
1489 | @@ -1435,7 +1254,7 @@ | |||
1490 | 1435 | # - manipulate /var/lib/mongodb/VERSION/CLUSTER symlink | 1254 | # - manipulate /var/lib/mongodb/VERSION/CLUSTER symlink |
1491 | 1436 | #------------------------------------------------------------------------------ | 1255 | #------------------------------------------------------------------------------ |
1492 | 1437 | def config_changed_volume_apply(): | 1256 | def config_changed_volume_apply(): |
1494 | 1438 | config_data = config_get() | 1257 | config_data = config() |
1495 | 1439 | data_directory_path = config_data["dbpath"] | 1258 | data_directory_path = config_data["dbpath"] |
1496 | 1440 | assert(data_directory_path) | 1259 | assert(data_directory_path) |
1497 | 1441 | volid = volume_get_volume_id() | 1260 | volid = volume_get_volume_id() |
1498 | @@ -1548,57 +1367,6 @@ | |||
1499 | 1548 | ############################################################################### | 1367 | ############################################################################### |
1500 | 1549 | # Main section | 1368 | # Main section |
1501 | 1550 | ############################################################################### | 1369 | ############################################################################### |
1556 | 1551 | if __name__ == '__main__': | 1370 | if __name__ == "__main__": |
1557 | 1552 | parser = argparse.ArgumentParser() | 1371 | # execute a hook based on the name the program is called by |
1558 | 1553 | parser.add_argument('-H', '--hook_name', dest='hook_name', | 1372 | hooks.execute(sys.argv) |
1505 | 1554 | help='hook to call') | ||
1506 | 1555 | args = parser.parse_args() | ||
1507 | 1556 | if args.hook_name is not None: | ||
1508 | 1557 | hook_name = args.hook_name | ||
1509 | 1558 | else: | ||
1510 | 1559 | hook_name = os.path.basename(sys.argv[0]) | ||
1511 | 1560 | |||
1512 | 1561 | if hook_name == "install": | ||
1513 | 1562 | retVal = install_hook() | ||
1514 | 1563 | elif hook_name == "config-changed": | ||
1515 | 1564 | retVal = config_changed() | ||
1516 | 1565 | elif hook_name == "start": | ||
1517 | 1566 | retVal = start_hook() | ||
1518 | 1567 | elif hook_name == "stop": | ||
1519 | 1568 | retVal = stop_hook() | ||
1520 | 1569 | elif hook_name == "database-relation-joined": | ||
1521 | 1570 | retVal = database_relation_joined() | ||
1522 | 1571 | elif hook_name == "replica-set-relation-joined": | ||
1523 | 1572 | retVal = replica_set_relation_joined() | ||
1524 | 1573 | elif hook_name == "replica-set-relation-changed": | ||
1525 | 1574 | retVal = replica_set_relation_changed() | ||
1526 | 1575 | elif hook_name == "configsvr-relation-joined": | ||
1527 | 1576 | retVal = configsvr_relation_joined() | ||
1528 | 1577 | elif hook_name == "configsvr-relation-changed": | ||
1529 | 1578 | retVal = configsvr_relation_changed() | ||
1530 | 1579 | elif hook_name == "mongos-cfg-relation-joined": | ||
1531 | 1580 | retVal = mongos_relation_joined() | ||
1532 | 1581 | elif hook_name == "mongos-cfg-relation-changed": | ||
1533 | 1582 | retVal = mongos_relation_changed() | ||
1534 | 1583 | elif hook_name == "mongos-cfg-relation-broken": | ||
1535 | 1584 | retVal = mongos_relation_broken() | ||
1536 | 1585 | elif hook_name == "mongos-relation-joined": | ||
1537 | 1586 | retVal = mongos_relation_joined() | ||
1538 | 1587 | elif hook_name == "mongos-relation-changed": | ||
1539 | 1588 | retVal = mongos_relation_changed() | ||
1540 | 1589 | elif hook_name == "mongos-relation-broken": | ||
1541 | 1590 | retVal = mongos_relation_broken() | ||
1542 | 1591 | elif hook_name == "data-relation-joined": | ||
1543 | 1592 | retVal = data_relation_joined() | ||
1544 | 1593 | elif hook_name == "data-relation-changed": | ||
1545 | 1594 | retVal = data_relation_changed() | ||
1546 | 1595 | elif hook_name == "data-relation-departed": | ||
1547 | 1596 | retVal = data_relation_departed() | ||
1548 | 1597 | else: | ||
1549 | 1598 | print "Unknown hook" | ||
1550 | 1599 | retVal = False | ||
1551 | 1600 | |||
1552 | 1601 | if retVal is True: | ||
1553 | 1602 | sys.exit(0) | ||
1554 | 1603 | else: | ||
1555 | 1604 | sys.exit(1) | ||
1559 | 1605 | \ No newline at end of file | 1373 | \ No newline at end of file |
1560 | 1606 | 1374 | ||
1561 | === modified file 'hooks/install' | |||
1562 | --- hooks/install 2013-11-25 19:48:00 +0000 | |||
1563 | +++ hooks/install 1970-01-01 00:00:00 +0000 | |||
1564 | @@ -1,5 +0,0 @@ | |||
1565 | 1 | #!/bin/bash | ||
1566 | 2 | |||
1567 | 3 | sudo apt-get install "python-yaml" | ||
1568 | 4 | |||
1569 | 5 | hooks/hooks.py -H install | ||
1570 | 6 | 0 | ||
1571 | === target is u'hooks.py' | |||
1572 | === modified file 'metadata.yaml' | |||
1573 | --- metadata.yaml 2014-06-18 11:13:54 +0000 | |||
1574 | +++ metadata.yaml 2014-07-30 17:48:38 +0000 | |||
1575 | @@ -1,6 +1,9 @@ | |||
1576 | 1 | name: mongodb | 1 | name: mongodb |
1579 | 2 | maintainer: Juan Negron <juan.negron@canonical.com> | 2 | summary: An open-source document database, and the leading NoSQL database |
1580 | 3 | summary: MongoDB (from humongous) is an open-source document database | 3 | maintainers: |
1581 | 4 | - Juan Negron <juan.negron@canonical.com> | ||
1582 | 5 | - Marco Ceppi <marco@ceppi.net> | ||
1583 | 6 | - Charles Butler <chuck@dasroot.net> | ||
1584 | 4 | description: | | 7 | description: | |
1585 | 5 | MongoDB is a high-performance, open source, schema-free document- | 8 | MongoDB is a high-performance, open source, schema-free document- |
1586 | 6 | oriented data store that's easy to deploy, manage and use. It's | 9 | oriented data store that's easy to deploy, manage and use. It's |
1587 | 7 | 10 | ||
1588 | === modified file 'tests/00-setup' | |||
1589 | --- tests/00-setup 2014-02-25 21:37:07 +0000 | |||
1590 | +++ tests/00-setup 2014-07-30 17:48:38 +0000 | |||
1591 | @@ -1,11 +1,11 @@ | |||
1592 | 1 | #!/bin/bash | 1 | #!/bin/bash |
1593 | 2 | 2 | ||
1594 | 3 | set -e | ||
1595 | 4 | |||
1596 | 3 | sudo apt-get install python-setuptools -y | 5 | sudo apt-get install python-setuptools -y |
1601 | 4 | 6 | sudo add-apt-repository ppa:juju/stable -y | |
1598 | 5 | if [ -f '/etc/apt.d/sources.list.d/juju-stable-precise.list' ]; then | ||
1599 | 6 | sudo add-apt-repository ppa:juju/stable -y | ||
1600 | 7 | fi | ||
1602 | 8 | 7 | ||
1603 | 9 | sudo apt-get update | 8 | sudo apt-get update |
1604 | 10 | 9 | ||
1606 | 11 | sudo apt-get install amulet python3 python3-requests python3-pymongo juju-core charm-tools python-mock -y | 10 | |
1607 | 11 | sudo apt-get install amulet python3 python3-requests python3-pymongo juju-core charm-tools python-mock python-pymongo -y | ||
1608 | 12 | 12 | ||
1609 | === added file 'tests/200_relate_ceilometer.test' | |||
1610 | --- tests/200_relate_ceilometer.test 1970-01-01 00:00:00 +0000 | |||
1611 | +++ tests/200_relate_ceilometer.test 2014-07-30 17:48:38 +0000 | |||
1612 | @@ -0,0 +1,43 @@ | |||
1613 | 1 | #!/usr/bin/env python3 | ||
1614 | 2 | |||
1615 | 3 | import amulet | ||
1616 | 4 | import pdb | ||
1617 | 5 | |||
1618 | 6 | class TestDeploy(object): | ||
1619 | 7 | |||
1620 | 8 | def __init__(self, time=2500): | ||
1621 | 9 | # Attempt to load the deployment topology from a bundle. | ||
1622 | 10 | self.deploy = amulet.Deployment(series="trusty") | ||
1623 | 11 | |||
1624 | 12 | # If something errored out, attempt to continue by | ||
1625 | 13 | # manually specifying a standalone deployment | ||
1626 | 14 | self.deploy.add('mongodb') | ||
1627 | 15 | self.deploy.add('ceilometer', 'cs:trusty/ceilometer') | ||
1628 | 16 | # send blank configs to finalize the objects in the deployment map | ||
1629 | 17 | self.deploy.configure('mongodb', {}) | ||
1630 | 18 | self.deploy.configure('ceilometer', {}) | ||
1631 | 19 | |||
1632 | 20 | self.deploy.relate('mongodb:database', 'ceilometer:shared-db') | ||
1633 | 21 | |||
1634 | 22 | try: | ||
1635 | 23 | self.deploy.setup(time) | ||
1636 | 24 | self.deploy.sentry.wait(time) | ||
1637 | 25 | except: | ||
1638 | 26 | amulet.raise_status(amulet.FAIL, msg="Environment standup timeout") | ||
1639 | 27 | # sentry = self.deploy.sentry | ||
1640 | 28 | |||
1641 | 29 | def run(self): | ||
1642 | 30 | for test in dir(self): | ||
1643 | 31 | if test.startswith('test_'): | ||
1644 | 32 | getattr(self, test)() | ||
1645 | 33 | |||
1646 | 34 | def test_mongo_relation(self): | ||
1647 | 35 | unit = self.deploy.sentry.unit['ceilometer/0'] | ||
1648 | 36 | mongo = self.deploy.sentry.unit['mongodb/0'].info['public-address'] | ||
1649 | 37 | cont = unit.file_contents('/etc/ceilometer/ceilometer.conf') | ||
1650 | 38 | if mongo not in cont: | ||
1651 | 39 | amulet.raise_status(amulet.FAIL, "Unable to verify ceilometer cfg") | ||
1652 | 40 | |||
1653 | 41 | if __name__ == '__main__': | ||
1654 | 42 | runner = TestDeploy() | ||
1655 | 43 | runner.run() |
Charles,
With the fixes as we discussed on IRC, I give this refactor my +1.
The tests all pass, with the caveat mentioned above (which will be resolved once it is merged), and the charm is overall cleaner. I particularly like the pattern for pulling in charm_helpers_ sync.py and wonder if that could be leveraged into a pattern for pulling in charmhelpers at deploy time.