Merge lp:~johnsca/charms/trusty/cf-cloud-controller/services-callback-fu into lp:~cf-charmers/charms/trusty/cf-cloud-controller/trunk
- Trusty Tahr (14.04)
- services-callback-fu
- Merge into trunk
Status: | Merged |
---|---|
Merged at revision: | 29 |
Proposed branch: | lp:~johnsca/charms/trusty/cf-cloud-controller/services-callback-fu |
Merge into: | lp:~cf-charmers/charms/trusty/cf-cloud-controller/trunk |
Diff against target: |
950 lines (+493/-337) 12 files modified
hooks/charmhelpers/contrib/cloudfoundry/contexts.py (+41/-32) hooks/charmhelpers/core/host.py (+5/-0) hooks/charmhelpers/core/services.py (+316/-79) hooks/charmhelpers/core/templating.py (+38/-145) hooks/config-changed (+9/-0) hooks/config.py (+39/-0) hooks/db-relation-changed (+9/-0) hooks/hooks.py (+0/-81) hooks/nats-relation-changed (+9/-0) hooks/router-relation-changed (+9/-0) hooks/stop (+9/-0) hooks/upgrade-charm (+9/-0) |
To merge this branch: | bzr merge lp:~johnsca/charms/trusty/cf-cloud-controller/services-callback-fu |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Cloud Foundry Charmers | Pending | ||
Review via email: mp+220741@code.launchpad.net |
Commit message
Description of the change
Fixed relation ordering issue
Fixed an relation hook ordering dependency by refactoring to use
callbacks in charmhelpers.
Cory Johns (johnsca) wrote : | # |
Benjamin Saller (bcsaller) wrote : | # |
Thanks I like this, I'll approve it pending the answer to the one
question
https:/
File hooks/hooks.py (right):
https:/
hooks/hooks.py:18: def register():
why the containing method? it seems fine to register with hooks on
import
https:/
hooks/hooks.py:41:
'router-
The other pattern is more familiar but I think we could get people used
to this and it has the advantage of being very concise. :)
- 30. By Cory Johns
-
Use refactored services API and remove symlink hooks
Cory Johns (johnsca) wrote : | # |
Please take a look.
Whit Morriss (whitmo) wrote : | # |
LGTM. nice and concise.
https:/
File hooks/charmhelp
https:/
hooks/charmhelp
As discussed with Cory: we are abusing module scope and globals to
emulate object behavior. I feel the code will age and extend better if
we sacrificed a tiny bit of api convenience and used idiomatic python
classes here.
- 31. By Cory Johns
-
Updated charm-helpers and switched to class API for ServiceManager
Cory Johns (johnsca) wrote : | # |
Please take a look.
Benjamin Saller (bcsaller) wrote : | # |
So this is reviewed in the context of a charm, but its nice to see the
top level singleton thing play out.
I have some more bikeshedding suggestions, but they are only that. Feel
free to land this and the helpers changes. They
LGTM +1
Thanks
https:/
File hooks/charmhelp
https:/
hooks/charmhelp
Even though we only use this as a Singleton this is almost certainly the
proper way to evolve the code. Better to do this now (because its a
minor api change) than later, thanks.
https:/
hooks/charmhelp
callbacks>,
Not sure I love data_ready/
shedding and whit already reviewed this and was ok with the naming so
I'll pass if you don't like these better
'requires': [],
'complete': []
'incomplete': []
another option is to keep with the naming we use in relations though
I'll admit 'joined' is a little hard to understand in this context, so
maybe
'requires', 'changed', 'broken'
I do think 'broken' is a good name, but it needs something that fits
conceptually with it. At this point though I do think what you have is
understandable and nicely documented.
https:/
File hooks/config.py (right):
https:/
hooks/config.py:8:
Yeah, not bad.
Cory Johns (johnsca) wrote : | # |
On 2014/05/27 18:06:56, benjamin.saller wrote:
> Not sure I love data_ready/
shedding and
> whit already reviewed this and was ok with the naming so I'll pass if
you don't
> like these better
> 'requires': [],
> 'complete': []
> 'incomplete': []
> another option is to keep with the naming we use in relations though
I'll admit
> 'joined' is a little hard to understand in this context, so maybe
> 'requires', 'changed', 'broken'
> I do think 'broken' is a good name, but it needs something that fits
> conceptually with it. At this point though I do think what you have is
> understandable and nicely documented.
Yeah, we talked about nomenclature quite a bit on Friday. Whit's point
was that our "dependencies" list was overloaded and actually serves two
purposes:
1) Define what the service needs to operate that it can't get for
itself (relations, config data, etc)
2) Collect all of that information into a unified view
Naming it "dependencies" or "requirements" makes that second aspect
non-obvious, and it would be best for the API to be, as much as
possible, self-documenting. Given that, "required_data" was the best we
could come up with, as it conveys both that this is a list of required
things, and that those things provide data somehow. This also plays
into the ability to, instead of having a semi-opaque StaticContext
class, you can now just drop a plain dict (or the results of config())
in there directly and it will do what you expect.
Then, I also realized that I wanted to separate the "ready" (or
"complete," or whatever) event from the "start" event, so that the
charms can just worry about defining what happens between everything the
service needs being available and spinning up the service itself,
without having to worry about the details of spinning it up by manually
specifying host.service_start every time, since 90% of charms should be
using Upstart or some other SysV-compatible service manager.
So, then we end up with the events of interest:
* All of the requirements have been satisfied and the data is
available to configure and start the service
* Something changed, and we no longer have complete information for
running the service
The names we chose, "data_ready" and "data_lost" seemed to capture those
ideas while matching up well to "required_data." Additionally,
"data_lost" specially calls out the fact that it doesn't need to be
triggered in the case that we're still working to get set up and we've
never had the service set up, so nothing needs to be done to tear it
down.
I'm not opposed to "complete" and "incomplete," or "complete" and
"broken." But I also think that it will not be difficult to change the
keys later in a backwards-
a set of keys, and it would be all encapsulated in fire_event).
So, my inclination is to leave it as-is until / if we get more feedback
that "required_data," "data_ready," and "data_lost" are confusing.
- 32. By Cory Johns
-
Updated charm-helpers and removed implicit target from templates
Cory Johns (johnsca) wrote : | # |
Please take a look.
Cory Johns (johnsca) wrote : | # |
Please take a look.
Cory Johns (johnsca) wrote : | # |
*** Submitted:
Fixed relation ordering issue
Fixed an relation hook ordering dependency by refactoring to use
callbacks in charmhelpers.
R=benjamin.saller, whit.morriss
CC=
https:/
Preview Diff
1 | === modified file 'hooks/charmhelpers/contrib/cloudfoundry/contexts.py' | |||
2 | --- hooks/charmhelpers/contrib/cloudfoundry/contexts.py 2014-05-16 23:33:25 +0000 | |||
3 | +++ hooks/charmhelpers/contrib/cloudfoundry/contexts.py 2014-05-29 17:26:14 +0000 | |||
4 | @@ -1,55 +1,64 @@ | |||
5 | 1 | import os | 1 | import os |
17 | 2 | 2 | import yaml | |
18 | 3 | from charmhelpers.core.templating import ( | 3 | |
19 | 4 | ContextGenerator, | 4 | from charmhelpers.core.services import RelationContext |
20 | 5 | RelationContext, | 5 | |
21 | 6 | StorableContext, | 6 | |
22 | 7 | ) | 7 | class StoredContext(dict): |
23 | 8 | 8 | """ | |
24 | 9 | 9 | A data context that always returns the data that it was first created with. | |
25 | 10 | # Stores `config_data` hash into yaml file with `file_name` as a name | 10 | """ |
15 | 11 | # if `file_name` already exists, then it loads data from `file_name`. | ||
16 | 12 | class StoredContext(ContextGenerator, StorableContext): | ||
26 | 13 | def __init__(self, file_name, config_data): | 11 | def __init__(self, file_name, config_data): |
27 | 12 | """ | ||
28 | 13 | If the file exists, populate `self` with the data from the file. | ||
29 | 14 | Otherwise, populate with the given data and persist it to the file. | ||
30 | 15 | """ | ||
31 | 14 | if os.path.exists(file_name): | 16 | if os.path.exists(file_name): |
33 | 15 | self.data = self.read_context(file_name) | 17 | self.update(self.read_context(file_name)) |
34 | 16 | else: | 18 | else: |
35 | 17 | self.store_context(file_name, config_data) | 19 | self.store_context(file_name, config_data) |
43 | 18 | self.data = config_data | 20 | self.update(config_data) |
44 | 19 | 21 | ||
45 | 20 | def __call__(self): | 22 | def store_context(self, file_name, config_data): |
46 | 21 | return self.data | 23 | with open(file_name, 'w') as file_stream: |
47 | 22 | 24 | yaml.dump(config_data, file_stream) | |
48 | 23 | 25 | ||
49 | 24 | class NatsContext(RelationContext): | 26 | def read_context(self, file_name): |
50 | 27 | with open(file_name, 'r') as file_stream: | ||
51 | 28 | data = yaml.load(file_stream) | ||
52 | 29 | if not data: | ||
53 | 30 | raise OSError("%s is empty" % file_name) | ||
54 | 31 | return data | ||
55 | 32 | |||
56 | 33 | |||
57 | 34 | class NatsRelation(RelationContext): | ||
58 | 25 | interface = 'nats' | 35 | interface = 'nats' |
59 | 26 | required_keys = ['nats_port', 'nats_address', 'nats_user', 'nats_password'] | 36 | required_keys = ['nats_port', 'nats_address', 'nats_user', 'nats_password'] |
60 | 27 | 37 | ||
61 | 28 | 38 | ||
63 | 29 | class MysqlDSNContext(RelationContext): | 39 | class MysqlRelation(RelationContext): |
64 | 30 | interface = 'db' | 40 | interface = 'db' |
65 | 31 | required_keys = ['user', 'password', 'host', 'database'] | 41 | required_keys = ['user', 'password', 'host', 'database'] |
66 | 32 | dsn_template = "mysql2://{user}:{password}@{host}:{port}/{database}" | 42 | dsn_template = "mysql2://{user}:{password}@{host}:{port}/{database}" |
67 | 33 | 43 | ||
78 | 34 | def __call__(self): | 44 | def get_data(self): |
79 | 35 | ctx = RelationContext.__call__(self) | 45 | RelationContext.get_data(self) |
80 | 36 | if ctx: | 46 | if self.is_ready(): |
81 | 37 | if 'port' not in ctx: | 47 | if 'port' not in self['db']: |
82 | 38 | ctx['db']['port'] = '3306' | 48 | self['db']['port'] = '3306' |
83 | 39 | ctx['db']['dsn'] = self.dsn_template.format(**ctx['db']) | 49 | self['db']['dsn'] = self.dsn_template.format(**self['db']) |
84 | 40 | return ctx | 50 | |
85 | 41 | 51 | ||
86 | 42 | 52 | class RouterRelation(RelationContext): | |
77 | 43 | class RouterContext(RelationContext): | ||
87 | 44 | interface = 'router' | 53 | interface = 'router' |
88 | 45 | required_keys = ['domain'] | 54 | required_keys = ['domain'] |
89 | 46 | 55 | ||
90 | 47 | 56 | ||
92 | 48 | class LogRouterContext(RelationContext): | 57 | class LogRouterRelation(RelationContext): |
93 | 49 | interface = 'logrouter' | 58 | interface = 'logrouter' |
94 | 50 | required_keys = ['shared-secret', 'logrouter-address'] | 59 | required_keys = ['shared-secret', 'logrouter-address'] |
95 | 51 | 60 | ||
96 | 52 | 61 | ||
98 | 53 | class LoggregatorContext(RelationContext): | 62 | class LoggregatorRelation(RelationContext): |
99 | 54 | interface = 'loggregator' | 63 | interface = 'loggregator' |
100 | 55 | required_keys = ['shared_secret', 'loggregator_address'] | 64 | required_keys = ['shared_secret', 'loggregator_address'] |
101 | 56 | 65 | ||
102 | === modified file 'hooks/charmhelpers/core/host.py' | |||
103 | --- hooks/charmhelpers/core/host.py 2014-05-20 19:43:29 +0000 | |||
104 | +++ hooks/charmhelpers/core/host.py 2014-05-29 17:26:14 +0000 | |||
105 | @@ -63,6 +63,11 @@ | |||
106 | 63 | return False | 63 | return False |
107 | 64 | 64 | ||
108 | 65 | 65 | ||
109 | 66 | def service_available(service_name): | ||
110 | 67 | """Determine whether a system service is available""" | ||
111 | 68 | return service('status', service_name) | ||
112 | 69 | |||
113 | 70 | |||
114 | 66 | def adduser(username, password=None, shell='/bin/bash', system_user=False): | 71 | def adduser(username, password=None, shell='/bin/bash', system_user=False): |
115 | 67 | """Add a user to the system""" | 72 | """Add a user to the system""" |
116 | 68 | try: | 73 | try: |
117 | 69 | 74 | ||
118 | === modified file 'hooks/charmhelpers/core/services.py' | |||
119 | --- hooks/charmhelpers/core/services.py 2014-05-16 22:44:17 +0000 | |||
120 | +++ hooks/charmhelpers/core/services.py 2014-05-29 17:26:14 +0000 | |||
121 | @@ -1,84 +1,321 @@ | |||
122 | 1 | import os | ||
123 | 2 | import sys | ||
124 | 3 | from collections import Iterable | ||
125 | 1 | from charmhelpers.core import templating | 4 | from charmhelpers.core import templating |
126 | 2 | from charmhelpers.core import host | 5 | from charmhelpers.core import host |
194 | 3 | 6 | from charmhelpers.core import hookenv | |
195 | 4 | 7 | ||
196 | 5 | SERVICES = {} | 8 | |
197 | 6 | 9 | class ServiceManager(object): | |
198 | 7 | 10 | def __init__(self, services=None): | |
199 | 8 | def register(services, templates_dir=None): | 11 | """ |
200 | 9 | """ | 12 | Register a list of services, given their definitions. |
201 | 10 | Register a list of service configs. | 13 | |
202 | 11 | 14 | Service definitions are dicts in the following formats (all keys except | |
203 | 12 | Service Configs are dicts in the following formats: | 15 | 'service' are optional): |
204 | 13 | 16 | ||
205 | 14 | { | 17 | { |
206 | 15 | "service": <service name>, | 18 | "service": <service name>, |
207 | 16 | "templates": [ { | 19 | "required_data": <list of required data contexts>, |
208 | 17 | 'target': <render target of template>, | 20 | "data_ready": <one or more callbacks>, |
209 | 18 | 'source': <optional name of template in passed in templates_dir> | 21 | "data_lost": <one or more callbacks>, |
210 | 19 | 'file_properties': <optional dict taking owner and octal mode> | 22 | "start": <one or more callbacks>, |
211 | 20 | 'contexts': [ context generators, see contexts.py ] | 23 | "stop": <one or more callbacks>, |
212 | 21 | } | 24 | "ports": <list of ports to manage>, |
213 | 22 | ] } | 25 | } |
214 | 23 | 26 | ||
215 | 24 | Either `source` or `target` must be provided. | 27 | The 'required_data' list should contain dicts of required data (or |
216 | 25 | 28 | dependency managers that act like dicts and know how to collect the data). | |
217 | 26 | If 'source' is not provided for a template the templates_dir will | 29 | Only when all items in the 'required_data' list are populated are the list |
218 | 27 | be consulted for ``basename(target).j2``. | 30 | of 'data_ready' and 'start' callbacks executed. See `is_ready()` for more |
219 | 28 | 31 | information. | |
220 | 29 | If `target` is not provided, it will be assumed to be | 32 | |
221 | 30 | ``/etc/init/<service name>.conf``. | 33 | The 'data_ready' value should be either a single callback, or a list of |
222 | 31 | """ | 34 | callbacks, to be called when all items in 'required_data' pass `is_ready()`. |
223 | 32 | for service in services: | 35 | Each callback will be called with the service name as the only parameter. |
224 | 33 | service.setdefault('templates_dir', templates_dir) | 36 | After these all of the 'data_ready' callbacks are called, the 'start' |
225 | 34 | SERVICES[service['service']] = service | 37 | callbacks are fired. |
226 | 35 | 38 | ||
227 | 36 | 39 | The 'data_lost' value should be either a single callback, or a list of | |
228 | 37 | def reconfigure_services(restart=True): | 40 | callbacks, to be called when a 'required_data' item no longer passes |
229 | 38 | """ | 41 | `is_ready()`. Each callback will be called with the service name as the |
230 | 39 | Update all files for all services and optionally restart them, if ready. | 42 | only parameter. After these all of the 'data_ready' callbacks are called, |
231 | 40 | """ | 43 | the 'stop' callbacks are fired. |
232 | 41 | for service_name in SERVICES.keys(): | 44 | |
233 | 42 | reconfigure_service(service_name, restart=restart) | 45 | The 'start' value should be either a single callback, or a list of |
234 | 43 | 46 | callbacks, to be called when starting the service, after the 'data_ready' | |
235 | 44 | 47 | callbacks are complete. Each callback will be called with the service | |
236 | 45 | def reconfigure_service(service_name, restart=True): | 48 | name as the only parameter. This defaults to |
237 | 46 | """ | 49 | `[host.service_start, services.open_ports]`. |
238 | 47 | Update all files for a single service and optionally restart it, if ready. | 50 | |
239 | 48 | """ | 51 | The 'stop' value should be either a single callback, or a list of |
240 | 49 | service = SERVICES.get(service_name) | 52 | callbacks, to be called when stopping the service. If the service is |
241 | 50 | if not service or service['service'] != service_name: | 53 | being stopped because it no longer has all of its 'required_data', this |
242 | 51 | raise KeyError('Service not registered: %s' % service_name) | 54 | will be called after all of the 'data_lost' callbacks are complete. |
243 | 52 | 55 | Each callback will be called with the service name as the only parameter. | |
244 | 53 | manager_type = service.get('type', UpstartService) | 56 | This defaults to `[services.close_ports, host.service_stop]`. |
245 | 54 | manager_type(service).reconfigure(restart) | 57 | |
246 | 55 | 58 | The 'ports' value should be a list of ports to manage. The default | |
247 | 56 | 59 | 'start' handler will open the ports after the service is started, | |
248 | 57 | def stop_services(): | 60 | and the default 'stop' handler will close the ports prior to stopping |
249 | 58 | for service_name in SERVICES.keys(): | 61 | the service. |
250 | 59 | if host.service_running(service_name): | 62 | |
251 | 60 | host.service_stop(service_name) | 63 | |
252 | 61 | 64 | Examples: | |
253 | 62 | 65 | ||
254 | 63 | class ServiceTypeManager(object): | 66 | The following registers an Upstart service called bingod that depends on |
255 | 64 | def __init__(self, service_definition): | 67 | a mongodb relation and which runs a custom `db_migrate` function prior to |
256 | 65 | self.service_name = service_definition['service'] | 68 | restarting the service, and a Runit serivce called spadesd. |
257 | 66 | self.templates = service_definition['templates'] | 69 | |
258 | 67 | self.templates_dir = service_definition['templates_dir'] | 70 | >>> manager = services.ServiceManager([ |
259 | 68 | 71 | ... { | |
260 | 69 | def reconfigure(self, restart=True): | 72 | ... 'service': 'bingod', |
261 | 73 | ... 'ports': [80, 443], | ||
262 | 74 | ... 'required_data': [MongoRelation(), config()], | ||
263 | 75 | ... 'data_ready': [ | ||
264 | 76 | ... services.template(source='bingod.conf'), | ||
265 | 77 | ... services.template(source='bingod.ini', | ||
266 | 78 | ... target='/etc/bingod.ini', | ||
267 | 79 | ... owner='bingo', perms=0400), | ||
268 | 80 | ... ], | ||
269 | 81 | ... }, | ||
270 | 82 | ... { | ||
271 | 83 | ... 'service': 'spadesd', | ||
272 | 84 | ... 'data_ready': services.template(source='spadesd_run.j2', | ||
273 | 85 | ... target='/etc/sv/spadesd/run', | ||
274 | 86 | ... perms=0555), | ||
275 | 87 | ... 'start': runit_start, | ||
276 | 88 | ... 'stop': runit_stop, | ||
277 | 89 | ... }, | ||
278 | 90 | ... ]) | ||
279 | 91 | ... manager.manage() | ||
280 | 92 | """ | ||
281 | 93 | self.services = {} | ||
282 | 94 | for service in services or []: | ||
283 | 95 | service_name = service['service'] | ||
284 | 96 | self.services[service_name] = service | ||
285 | 97 | |||
286 | 98 | def manage(self): | ||
287 | 99 | """ | ||
288 | 100 | Handle the current hook by doing The Right Thing with the registered services. | ||
289 | 101 | """ | ||
290 | 102 | hook_name = os.path.basename(sys.argv[0]) | ||
291 | 103 | if hook_name == 'stop': | ||
292 | 104 | self.stop_services() | ||
293 | 105 | else: | ||
294 | 106 | self.reconfigure_services() | ||
295 | 107 | |||
296 | 108 | def reconfigure_services(self, *service_names): | ||
297 | 109 | """ | ||
298 | 110 | Update all files for one or more registered services, and, | ||
299 | 111 | if ready, optionally restart them. | ||
300 | 112 | |||
301 | 113 | If no service names are given, reconfigures all registered services. | ||
302 | 114 | """ | ||
303 | 115 | for service_name in service_names or self.services.keys(): | ||
304 | 116 | if self.is_ready(service_name): | ||
305 | 117 | self.fire_event('data_ready', service_name) | ||
306 | 118 | self.fire_event('start', service_name, default=[ | ||
307 | 119 | host.service_restart, | ||
308 | 120 | open_ports]) | ||
309 | 121 | self.save_ready(service_name) | ||
310 | 122 | else: | ||
311 | 123 | if self.was_ready(service_name): | ||
312 | 124 | self.fire_event('data_lost', service_name) | ||
313 | 125 | self.fire_event('stop', service_name, default=[ | ||
314 | 126 | close_ports, | ||
315 | 127 | host.service_stop]) | ||
316 | 128 | self.save_lost(service_name) | ||
317 | 129 | |||
318 | 130 | def stop_services(self, *service_names): | ||
319 | 131 | """ | ||
320 | 132 | Stop one or more registered services, by name. | ||
321 | 133 | |||
322 | 134 | If no service names are given, stops all registered services. | ||
323 | 135 | """ | ||
324 | 136 | for service_name in service_names or self.services.keys(): | ||
325 | 137 | self.fire_event('stop', service_name, default=[ | ||
326 | 138 | close_ports, | ||
327 | 139 | host.service_stop]) | ||
328 | 140 | |||
329 | 141 | def get_service(self, service_name): | ||
330 | 142 | """ | ||
331 | 143 | Given the name of a registered service, return its service definition. | ||
332 | 144 | """ | ||
333 | 145 | service = self.services.get(service_name) | ||
334 | 146 | if not service: | ||
335 | 147 | raise KeyError('Service not registered: %s' % service_name) | ||
336 | 148 | return service | ||
337 | 149 | |||
338 | 150 | def fire_event(self, event_name, service_name, default=None): | ||
339 | 151 | """ | ||
340 | 152 | Fire a data_ready, data_lost, start, or stop event on a given service. | ||
341 | 153 | """ | ||
342 | 154 | service = self.get_service(service_name) | ||
343 | 155 | callbacks = service.get(event_name, default) | ||
344 | 156 | if not callbacks: | ||
345 | 157 | return | ||
346 | 158 | if not isinstance(callbacks, Iterable): | ||
347 | 159 | callbacks = [callbacks] | ||
348 | 160 | for callback in callbacks: | ||
349 | 161 | if isinstance(callback, ManagerCallback): | ||
350 | 162 | callback(self, service_name, event_name) | ||
351 | 163 | else: | ||
352 | 164 | callback(service_name) | ||
353 | 165 | |||
354 | 166 | def is_ready(self, service_name): | ||
355 | 167 | """ | ||
356 | 168 | Determine if a registered service is ready, by checking its 'required_data'. | ||
357 | 169 | |||
358 | 170 | A 'required_data' item can be any mapping type, and is considered ready | ||
359 | 171 | if `bool(item)` evaluates as True. | ||
360 | 172 | """ | ||
361 | 173 | service = self.get_service(service_name) | ||
362 | 174 | reqs = service.get('required_data', []) | ||
363 | 175 | return all(bool(req) for req in reqs) | ||
364 | 176 | |||
365 | 177 | def save_ready(self, service_name): | ||
366 | 178 | """ | ||
367 | 179 | Save an indicator that the given service is now data_ready. | ||
368 | 180 | """ | ||
369 | 181 | ready_file = '{}/.ready.{}'.format(hookenv.charm_dir(), service_name) | ||
370 | 182 | with open(ready_file, 'a'): | ||
371 | 183 | pass | ||
372 | 184 | |||
373 | 185 | def save_lost(self, service_name): | ||
374 | 186 | """ | ||
375 | 187 | Save an indicator that the given service is no longer data_ready. | ||
376 | 188 | """ | ||
377 | 189 | ready_file = '{}/.ready.{}'.format(hookenv.charm_dir(), service_name) | ||
378 | 190 | if os.path.exists(ready_file): | ||
379 | 191 | os.remove(ready_file) | ||
380 | 192 | |||
381 | 193 | def was_ready(self, service_name): | ||
382 | 194 | """ | ||
383 | 195 | Determine if the given service was previously data_ready. | ||
384 | 196 | """ | ||
385 | 197 | ready_file = '{}/.ready.{}'.format(hookenv.charm_dir(), service_name) | ||
386 | 198 | return os.path.exists(ready_file) | ||
387 | 199 | |||
388 | 200 | |||
389 | 201 | class RelationContext(dict): | ||
390 | 202 | """ | ||
391 | 203 | Base class for a context generator that gets relation data from juju. | ||
392 | 204 | |||
393 | 205 | Subclasses must provide `interface`, which is the interface type of interest, | ||
394 | 206 | and `required_keys`, which is the set of keys required for the relation to | ||
395 | 207 | be considered complete. The first relation for the interface that is complete | ||
396 | 208 | will be used to populate the data for template. | ||
397 | 209 | |||
398 | 210 | The generated context will be namespaced under the interface type, to prevent | ||
399 | 211 | potential naming conflicts. | ||
400 | 212 | """ | ||
401 | 213 | interface = None | ||
402 | 214 | required_keys = [] | ||
403 | 215 | |||
404 | 216 | def __bool__(self): | ||
405 | 217 | """ | ||
406 | 218 | Updates the data and returns True if all of the required_keys are available. | ||
407 | 219 | """ | ||
408 | 220 | self.get_data() | ||
409 | 221 | return self.is_ready() | ||
410 | 222 | |||
411 | 223 | __nonzero__ = __bool__ | ||
412 | 224 | |||
413 | 225 | def is_ready(self): | ||
414 | 226 | """ | ||
415 | 227 | Returns True if all of the required_keys are available. | ||
416 | 228 | """ | ||
417 | 229 | return set(self.get(self.interface, {}).keys()).issuperset(set(self.required_keys)) | ||
418 | 230 | |||
419 | 231 | def get_data(self): | ||
420 | 232 | """ | ||
421 | 233 | Retrieve the relation data and store it under `self[self.interface]`. | ||
422 | 234 | |||
423 | 235 | If there are more than one units related on the desired interface, | ||
424 | 236 | then each unit will have its data stored under `self[self.interface][unit_id]` | ||
425 | 237 | and one of the units with complete information will chosen at random | ||
426 | 238 | to fill the values at `self[self.interface]`. | ||
427 | 239 | |||
428 | 240 | |||
429 | 241 | For example: | ||
430 | 242 | |||
431 | 243 | { | ||
432 | 244 | 'foo': 'bar', | ||
433 | 245 | 'unit/0': { | ||
434 | 246 | 'foo': 'bar', | ||
435 | 247 | }, | ||
436 | 248 | 'unit/1': { | ||
437 | 249 | 'foo': 'baz', | ||
438 | 250 | }, | ||
439 | 251 | } | ||
440 | 252 | """ | ||
441 | 253 | if not hookenv.relation_ids(self.interface): | ||
442 | 254 | return | ||
443 | 255 | |||
444 | 256 | ns = self.setdefault(self.interface, {}) | ||
445 | 257 | required = set(self.required_keys) | ||
446 | 258 | for rid in hookenv.relation_ids(self.interface): | ||
447 | 259 | for unit in hookenv.related_units(rid): | ||
448 | 260 | reldata = hookenv.relation_get(rid=rid, unit=unit) | ||
449 | 261 | unit_ns = ns.setdefault(unit, {}) | ||
450 | 262 | unit_ns.update(reldata) | ||
451 | 263 | if set(reldata.keys()).issuperset(required): | ||
452 | 264 | ns.update(reldata) | ||
453 | 265 | |||
454 | 266 | |||
455 | 267 | class ManagerCallback(object): | ||
456 | 268 | """ | ||
457 | 269 | Special case of a callback that takes the `ServiceManager` instance | ||
458 | 270 | in addition to the service name. | ||
459 | 271 | |||
460 | 272 | Subclasses should implement `__call__` which should accept two parameters: | ||
461 | 273 | |||
462 | 274 | * `manager` The `ServiceManager` instance | ||
463 | 275 | * `service_name` The name of the service it's being triggered for | ||
464 | 276 | * `event_name` The name of the event that this callback is handling | ||
465 | 277 | """ | ||
466 | 278 | def __call__(self, manager, service_name, event_name): | ||
467 | 70 | raise NotImplementedError() | 279 | raise NotImplementedError() |
468 | 71 | 280 | ||
469 | 72 | 281 | ||
482 | 73 | class UpstartService(ServiceTypeManager): | 282 | class TemplateCallback(ManagerCallback): |
483 | 74 | def __init__(self, service_definition): | 283 | """ |
484 | 75 | super(UpstartService, self).__init__(service_definition) | 284 | Callback class that will render a template, for use as a ready action. |
485 | 76 | for tmpl in self.templates: | 285 | |
486 | 77 | if 'target' not in tmpl: | 286 | The `target` param, if omitted, will default to `/etc/init/<service name>`. |
487 | 78 | tmpl['target'] = '/etc/init/%s.conf' % self.service_name | 287 | """ |
488 | 79 | 288 | def __init__(self, source, target, owner='root', group='root', perms=0444): | |
489 | 80 | def reconfigure(self, restart): | 289 | self.source = source |
490 | 81 | complete = templating.render(self.templates, self.templates_dir) | 290 | self.target = target |
491 | 82 | 291 | self.owner = owner | |
492 | 83 | if restart and complete: | 292 | self.group = group |
493 | 84 | host.service_restart(self.service_name) | 293 | self.perms = perms |
494 | 294 | |||
495 | 295 | def __call__(self, manager, service_name, event_name): | ||
496 | 296 | service = manager.get_service(service_name) | ||
497 | 297 | context = {} | ||
498 | 298 | for ctx in service.get('required_data', []): | ||
499 | 299 | context.update(ctx) | ||
500 | 300 | templating.render(self.source, self.target, context, | ||
501 | 301 | self.owner, self.group, self.perms) | ||
502 | 302 | |||
503 | 303 | |||
504 | 304 | class PortManagerCallback(ManagerCallback): | ||
505 | 305 | """ | ||
506 | 306 | Callback class that will open or close ports, for use as either | ||
507 | 307 | a start or stop action. | ||
508 | 308 | """ | ||
509 | 309 | def __call__(self, manager, service_name, event_name): | ||
510 | 310 | service = manager.get_service(service_name) | ||
511 | 311 | for port in service.get('ports', []): | ||
512 | 312 | if event_name == 'start': | ||
513 | 313 | hookenv.open_port(port) | ||
514 | 314 | elif event_name == 'stop': | ||
515 | 315 | hookenv.close_port(port) | ||
516 | 316 | |||
517 | 317 | |||
518 | 318 | # Convenience aliases | ||
519 | 319 | template = TemplateCallback | ||
520 | 320 | open_ports = PortManagerCallback() | ||
521 | 321 | close_ports = PortManagerCallback() | ||
522 | 85 | 322 | ||
523 | === modified file 'hooks/charmhelpers/core/templating.py' | |||
524 | --- hooks/charmhelpers/core/templating.py 2014-05-20 19:43:29 +0000 | |||
525 | +++ hooks/charmhelpers/core/templating.py 2014-05-29 17:26:14 +0000 | |||
526 | @@ -1,158 +1,51 @@ | |||
527 | 1 | import os | 1 | import os |
528 | 2 | import yaml | ||
529 | 3 | 2 | ||
530 | 4 | from charmhelpers.core import host | 3 | from charmhelpers.core import host |
531 | 5 | from charmhelpers.core import hookenv | 4 | from charmhelpers.core import hookenv |
532 | 6 | 5 | ||
533 | 7 | 6 | ||
648 | 8 | class ContextGenerator(object): | 7 | def render(source, target, context, owner='root', group='root', perms=0444, templates_dir=None): |
649 | 9 | """ | 8 | """ |
650 | 10 | Base interface for template context container generators. | 9 | Render a template. |
651 | 11 | 10 | ||
652 | 12 | A template context is a dictionary that contains data needed to populate | 11 | The `source` path, if not absolute, is relative to the `templates_dir`. |
539 | 13 | the template. The generator instance should produce the context when | ||
540 | 14 | called (without arguments) by collecting information from juju (config-get, | ||
541 | 15 | relation-get, etc), the system, or whatever other sources are appropriate. | ||
542 | 16 | |||
543 | 17 | A context generator should only return any values if it has enough information | ||
544 | 18 | to provide all of its values. Any context that is missing data is considered | ||
545 | 19 | incomplete and will cause that template to not render until it has all of its | ||
546 | 20 | necessary data. | ||
547 | 21 | |||
548 | 22 | The template may receive several contexts, which will be merged together, | ||
549 | 23 | so care should be taken in the key names. | ||
550 | 24 | """ | ||
551 | 25 | def __call__(self): | ||
552 | 26 | raise NotImplementedError | ||
553 | 27 | |||
554 | 28 | |||
555 | 29 | class StorableContext(object): | ||
556 | 30 | """ | ||
557 | 31 | A mixin for persisting a context to disk. | ||
558 | 32 | """ | ||
559 | 33 | def store_context(self, file_name, config_data): | ||
560 | 34 | with open(file_name, 'w') as file_stream: | ||
561 | 35 | yaml.dump(config_data, file_stream) | ||
562 | 36 | |||
563 | 37 | def read_context(self, file_name): | ||
564 | 38 | with open(file_name, 'r') as file_stream: | ||
565 | 39 | data = yaml.load(file_stream) | ||
566 | 40 | if not data: | ||
567 | 41 | raise OSError("%s is empty" % file_name) | ||
568 | 42 | return data | ||
569 | 43 | |||
570 | 44 | |||
571 | 45 | class ConfigContext(ContextGenerator): | ||
572 | 46 | """ | ||
573 | 47 | A context generator that generates a context containing all of the | ||
574 | 48 | juju config values. | ||
575 | 49 | """ | ||
576 | 50 | def __call__(self): | ||
577 | 51 | return hookenv.config() | ||
578 | 52 | |||
579 | 53 | |||
580 | 54 | class RelationContext(ContextGenerator): | ||
581 | 55 | """ | ||
582 | 56 | Base class for a context generator that gets relation data from juju. | ||
583 | 57 | |||
584 | 58 | Subclasses must provide `interface`, which is the interface type of interest, | ||
585 | 59 | and `required_keys`, which is the set of keys required for the relation to | ||
586 | 60 | be considered complete. The first relation for the interface that is complete | ||
587 | 61 | will be used to populate the data for template. | ||
588 | 62 | |||
589 | 63 | The generated context will be namespaced under the interface type, to prevent | ||
590 | 64 | potential naming conflicts. | ||
591 | 65 | """ | ||
592 | 66 | interface = None | ||
593 | 67 | required_keys = [] | ||
594 | 68 | |||
595 | 69 | def __call__(self): | ||
596 | 70 | if not hookenv.relation_ids(self.interface): | ||
597 | 71 | return {} | ||
598 | 72 | |||
599 | 73 | ctx = {} | ||
600 | 74 | for rid in hookenv.relation_ids(self.interface): | ||
601 | 75 | for unit in hookenv.related_units(rid): | ||
602 | 76 | reldata = hookenv.relation_get(rid=rid, unit=unit) | ||
603 | 77 | required = set(self.required_keys) | ||
604 | 78 | if set(reldata.keys()).issuperset(required): | ||
605 | 79 | ns = ctx.setdefault(self.interface, {}) | ||
606 | 80 | for k, v in reldata.items(): | ||
607 | 81 | ns[k] = v | ||
608 | 82 | return ctx | ||
609 | 83 | |||
610 | 84 | return {} | ||
611 | 85 | |||
612 | 86 | |||
613 | 87 | class StaticContext(ContextGenerator): | ||
614 | 88 | def __init__(self, data): | ||
615 | 89 | self.data = data | ||
616 | 90 | |||
617 | 91 | def __call__(self): | ||
618 | 92 | return self.data | ||
619 | 93 | |||
620 | 94 | |||
621 | 95 | def _collect_contexts(context_providers): | ||
622 | 96 | """ | ||
623 | 97 | Helper function to collect and merge contexts from a list of providers. | ||
624 | 98 | |||
625 | 99 | If any of the contexts are incomplete (i.e., they return an empty dict), | ||
626 | 100 | the template is considered incomplete and will not render. | ||
627 | 101 | """ | ||
628 | 102 | ctx = {} | ||
629 | 103 | for provider in context_providers: | ||
630 | 104 | c = provider() | ||
631 | 105 | if not c: | ||
632 | 106 | return False | ||
633 | 107 | ctx.update(c) | ||
634 | 108 | return ctx | ||
635 | 109 | |||
636 | 110 | |||
637 | 111 | def render(template_definitions, templates_dir=None): | ||
638 | 112 | """ | ||
639 | 113 | Render one or more templates, given a list of template definitions. | ||
640 | 114 | |||
641 | 115 | The template definitions should be dicts with the keys: `source`, `target`, | ||
642 | 116 | `file_properties`, and `contexts`. | ||
643 | 117 | |||
644 | 118 | The `source` path, if not absolute, is relative to the `templates_dir` | ||
645 | 119 | given when the rendered was created. If `source` is not provided | ||
646 | 120 | for a template the `template_dir` will be consulted for | ||
647 | 121 | ``basename(target).j2``. | ||
653 | 122 | 12 | ||
654 | 123 | The `target` path should be absolute. | 13 | The `target` path should be absolute. |
655 | 124 | 14 | ||
666 | 125 | The `file_properties` should be a dict optionally containing | 15 | The context should be a dict containing the values to be replaced in the |
667 | 126 | `owner`, `group`, or `perms` options, to be passed to `write_file`. | 16 | template. |
668 | 127 | 17 | ||
669 | 128 | The `contexts` should be a list containing zero or more ContextGenerators. | 18 | The `owner`, `group`, and `perms` options will be passed to `write_file`. |
670 | 129 | 19 | ||
671 | 130 | The `template_dir` defaults to `$CHARM_DIR/templates` | 20 | If omitted, `templates_dir` defaults to the `templates` folder in the charm. |
672 | 131 | 21 | ||
673 | 132 | Returns True if all of the templates were "complete" (i.e., the context | 22 | Note: Using this requires python-jinja2; if it is not installed, calling |
674 | 133 | generators were able to collect the information needed to render the | 23 | this will attempt to use charmhelpers.fetch.apt_install to install it. |
665 | 134 | template) and were rendered. | ||
675 | 135 | """ | 24 | """ |
679 | 136 | # lazy import jinja2 in case templating is needed in install hook | 25 | try: |
680 | 137 | from jinja2 import FileSystemLoader, Environment, exceptions | 26 | from jinja2 import FileSystemLoader, Environment, exceptions |
681 | 138 | all_complete = True | 27 | except ImportError: |
682 | 28 | try: | ||
683 | 29 | from charmhelpers.fetch import apt_install | ||
684 | 30 | except ImportError: | ||
685 | 31 | hookenv.log('Could not import jinja2, and could not import ' | ||
686 | 32 | 'charmhelpers.fetch to install it', | ||
687 | 33 | level=hookenv.ERROR) | ||
688 | 34 | raise | ||
689 | 35 | apt_install('python-jinja2', fatal=True) | ||
690 | 36 | from jinja2 import FileSystemLoader, Environment, exceptions | ||
691 | 37 | |||
692 | 139 | if templates_dir is None: | 38 | if templates_dir is None: |
693 | 140 | templates_dir = os.path.join(hookenv.charm_dir(), 'templates') | 39 | templates_dir = os.path.join(hookenv.charm_dir(), 'templates') |
694 | 141 | loader = Environment(loader=FileSystemLoader(templates_dir)) | 40 | loader = Environment(loader=FileSystemLoader(templates_dir)) |
712 | 142 | for tmpl in template_definitions: | 41 | try: |
713 | 143 | ctx = _collect_contexts(tmpl.get('contexts', [])) | 42 | source = source |
714 | 144 | if ctx is False: | 43 | template = loader.get_template(source) |
715 | 145 | all_complete = False | 44 | except exceptions.TemplateNotFound as e: |
716 | 146 | continue | 45 | hookenv.log('Could not load template %s from %s.' % |
717 | 147 | try: | 46 | (source, templates_dir), |
718 | 148 | source = tmpl.get('source', os.path.basename(tmpl['target'])+'.j2') | 47 | level=hookenv.ERROR) |
719 | 149 | template = loader.get_template(source) | 48 | raise e |
720 | 150 | except exceptions.TemplateNotFound as e: | 49 | content = template.render(context) |
721 | 151 | hookenv.log('Could not load template %s from %s.' % | 50 | host.mkdir(os.path.dirname(target)) |
722 | 152 | (tmpl['source'], templates_dir), | 51 | host.write_file(target, content, owner, group, perms) |
706 | 153 | level=hookenv.ERROR) | ||
707 | 154 | raise e | ||
708 | 155 | content = template.render(ctx) | ||
709 | 156 | host.mkdir(os.path.dirname(tmpl['target'])) | ||
710 | 157 | host.write_file(tmpl['target'], content, **tmpl.get('file_properties', {})) | ||
711 | 158 | return all_complete | ||
723 | 159 | 52 | ||
724 | === modified symlink 'hooks/config-changed' (properties changed: -x to +x) | |||
725 | === target was u'hooks.py' | |||
726 | --- hooks/config-changed 1970-01-01 00:00:00 +0000 | |||
727 | +++ hooks/config-changed 2014-05-29 17:26:14 +0000 | |||
728 | @@ -0,0 +1,9 @@ | |||
729 | 1 | #!/usr/bin/env python | ||
730 | 2 | # vim: et ai ts=4 sw=4: | ||
731 | 3 | from charmhelpers.core import services | ||
732 | 4 | |||
733 | 5 | import config | ||
734 | 6 | |||
735 | 7 | |||
736 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
737 | 9 | manager.manage() | ||
738 | 0 | 10 | ||
739 | === modified file 'hooks/config.py' | |||
740 | --- hooks/config.py 2014-05-20 19:50:35 +0000 | |||
741 | +++ hooks/config.py 2014-05-29 17:26:14 +0000 | |||
742 | @@ -1,4 +1,10 @@ | |||
743 | 1 | import os | 1 | import os |
744 | 2 | import subprocess | ||
745 | 3 | |||
746 | 4 | from charmhelpers.core import host | ||
747 | 5 | from charmhelpers.core import hookenv | ||
748 | 6 | from charmhelpers.core import services | ||
749 | 7 | from charmhelpers.contrib.cloudfoundry import contexts | ||
750 | 2 | 8 | ||
751 | 3 | 9 | ||
752 | 4 | __all__ = ['CF_DIR', 'CC_PACKAGES', 'CC_DIR', 'CC_CONFIG_DIR', | 10 | __all__ = ['CF_DIR', 'CC_PACKAGES', 'CC_DIR', 'CC_CONFIG_DIR', |
753 | @@ -23,3 +29,36 @@ | |||
754 | 23 | NGINX_RUN_DIR = '/var/vcap/sys/run/nginx_ccng' | 29 | NGINX_RUN_DIR = '/var/vcap/sys/run/nginx_ccng' |
755 | 24 | NGINX_LOG_DIR = '/var/vcap/sys/log/nginx_ccng' | 30 | NGINX_LOG_DIR = '/var/vcap/sys/log/nginx_ccng' |
756 | 25 | FOG_CONNECTION = '/var/vcap/nfs/store' | 31 | FOG_CONNECTION = '/var/vcap/nfs/store' |
757 | 32 | |||
758 | 33 | |||
759 | 34 | def db_migrate(service_name): | ||
760 | 35 | hookenv.log("Starting db:migrate...", hookenv.DEBUG) | ||
761 | 36 | with host.chdir(CC_DIR): | ||
762 | 37 | subprocess.check_call([ | ||
763 | 38 | 'sudo', '-u', 'vcap', '-g', 'vcap', | ||
764 | 39 | 'CLOUD_CONTROLLER_NG_CONFIG={}'.format(CC_CONFIG_FILE), | ||
765 | 40 | 'bundle', 'exec', 'rake', 'db:migrate']) | ||
766 | 41 | hookenv.log("Finished db:migrate", hookenv.DEBUG) | ||
767 | 42 | |||
768 | 43 | |||
769 | 44 | SERVICES = [ | ||
770 | 45 | { | ||
771 | 46 | 'service': 'cf-cloudcontroller', | ||
772 | 47 | 'required_data': [contexts.NatsRelation(), | ||
773 | 48 | contexts.RouterRelation(), | ||
774 | 49 | contexts.MysqlRelation()], | ||
775 | 50 | 'data_ready': [ | ||
776 | 51 | services.template(source='cf-cloudcontroller.conf', | ||
777 | 52 | target='/etc/init/cf-cloudcontroller.conf'), | ||
778 | 53 | services.template(source='cloud_controller.yml', | ||
779 | 54 | target=CC_CONFIG_FILE, | ||
780 | 55 | owner='vcap'), | ||
781 | 56 | db_migrate, | ||
782 | 57 | ], | ||
783 | 58 | }, | ||
784 | 59 | { | ||
785 | 60 | 'service': 'cf-cloudcontroller-job', | ||
786 | 61 | 'data_ready': services.template(source='cf-cloudcontroller-job.conf', | ||
787 | 62 | target='/etc/init/cf-cloudcontroller-job.conf'), | ||
788 | 63 | }, | ||
789 | 64 | ] | ||
790 | 26 | 65 | ||
791 | === modified symlink 'hooks/db-relation-changed' (properties changed: -x to +x) | |||
792 | === target was u'hooks.py' | |||
793 | --- hooks/db-relation-changed 1970-01-01 00:00:00 +0000 | |||
794 | +++ hooks/db-relation-changed 2014-05-29 17:26:14 +0000 | |||
795 | @@ -0,0 +1,9 @@ | |||
796 | 1 | #!/usr/bin/env python | ||
797 | 2 | # vim: et ai ts=4 sw=4: | ||
798 | 3 | from charmhelpers.core import services | ||
799 | 4 | |||
800 | 5 | import config | ||
801 | 6 | |||
802 | 7 | |||
803 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
804 | 9 | manager.manage() | ||
805 | 0 | 10 | ||
806 | === removed file 'hooks/hooks.py' | |||
807 | --- hooks/hooks.py 2014-05-20 19:50:35 +0000 | |||
808 | +++ hooks/hooks.py 1970-01-01 00:00:00 +0000 | |||
809 | @@ -1,81 +0,0 @@ | |||
810 | 1 | #!/usr/bin/env python | ||
811 | 2 | # vim: et ai ts=4 sw=4: | ||
812 | 3 | import os | ||
813 | 4 | import sys | ||
814 | 5 | import subprocess | ||
815 | 6 | |||
816 | 7 | from charmhelpers.core import host | ||
817 | 8 | from charmhelpers.core import hookenv | ||
818 | 9 | from charmhelpers.core.hookenv import log | ||
819 | 10 | from charmhelpers.core import services | ||
820 | 11 | from charmhelpers.contrib.cloudfoundry import contexts | ||
821 | 12 | |||
822 | 13 | import config | ||
823 | 14 | |||
824 | 15 | hooks = hookenv.Hooks() | ||
825 | 16 | fileproperties = {'owner': 'vcap'} | ||
826 | 17 | |||
827 | 18 | services.register([ | ||
828 | 19 | { | ||
829 | 20 | 'service': 'cf-cloudcontroller', | ||
830 | 21 | 'templates': [ | ||
831 | 22 | {'source': 'cf-cloudcontroller.conf'}, | ||
832 | 23 | {'source': 'cloud_controller.yml', | ||
833 | 24 | 'target': config.CC_CONFIG_FILE, | ||
834 | 25 | 'file_properties': fileproperties, | ||
835 | 26 | 'contexts': [contexts.NatsContext(), | ||
836 | 27 | contexts.RouterContext(), | ||
837 | 28 | contexts.MysqlDSNContext()]} | ||
838 | 29 | ], | ||
839 | 30 | }, | ||
840 | 31 | { | ||
841 | 32 | 'service': 'cf-cloudcontroller-job', | ||
842 | 33 | 'templates': [{'source': 'cf-cloudcontroller-job.conf'}], | ||
843 | 34 | }, | ||
844 | 35 | ]) | ||
845 | 36 | |||
846 | 37 | |||
847 | 38 | @hooks.hook('upgrade-charm') | ||
848 | 39 | def upgrade_charm(): | ||
849 | 40 | pass | ||
850 | 41 | |||
851 | 42 | |||
852 | 43 | @hooks.hook("config-changed") | ||
853 | 44 | def config_changed(): | ||
854 | 45 | services.reconfigure_services() | ||
855 | 46 | |||
856 | 47 | |||
857 | 48 | @hooks.hook() | ||
858 | 49 | def stop(): | ||
859 | 50 | services.stop_services() | ||
860 | 51 | |||
861 | 52 | |||
862 | 53 | @hooks.hook('db-relation-changed') | ||
863 | 54 | def db_relation_changed(): | ||
864 | 55 | services.reconfigure_services() | ||
865 | 56 | hookenv.log("Starting db:migrate...", hookenv.DEBUG) | ||
866 | 57 | with host.chdir(config.CC_DIR) as dir: | ||
867 | 58 | subprocess.check_call([ | ||
868 | 59 | 'sudo', '-u', 'vcap', '-g', 'vcap', | ||
869 | 60 | 'CLOUD_CONTROLLER_NG_CONFIG={}'.format(config.CC_CONFIG_FILE), | ||
870 | 61 | 'bundle', 'exec', 'rake', 'db:migrate']) | ||
871 | 62 | hookenv.log("Finished db:migrate in %s." % (dir)) | ||
872 | 63 | |||
873 | 64 | |||
874 | 65 | @hooks.hook('nats-relation-changed') | ||
875 | 66 | def nats_relation_changed(): | ||
876 | 67 | services.reconfigure_services() | ||
877 | 68 | |||
878 | 69 | |||
879 | 70 | @hooks.hook('router-relation-changed') | ||
880 | 71 | def router_relation_changed(): | ||
881 | 72 | services.reconfigure_services() | ||
882 | 73 | |||
883 | 74 | |||
884 | 75 | if __name__ == '__main__': | ||
885 | 76 | hook_name = os.path.basename(sys.argv[0]) | ||
886 | 77 | log("Running {} hook".format(hook_name)) | ||
887 | 78 | if hookenv.relation_id(): | ||
888 | 79 | log("Relation {} with {}".format( | ||
889 | 80 | hookenv.relation_id(), hookenv.remote_unit())) | ||
890 | 81 | hooks.execute(sys.argv) | ||
891 | 82 | 0 | ||
892 | === modified symlink 'hooks/nats-relation-changed' (properties changed: -x to +x) | |||
893 | === target was u'hooks.py' | |||
894 | --- hooks/nats-relation-changed 1970-01-01 00:00:00 +0000 | |||
895 | +++ hooks/nats-relation-changed 2014-05-29 17:26:14 +0000 | |||
896 | @@ -0,0 +1,9 @@ | |||
897 | 1 | #!/usr/bin/env python | ||
898 | 2 | # vim: et ai ts=4 sw=4: | ||
899 | 3 | from charmhelpers.core import services | ||
900 | 4 | |||
901 | 5 | import config | ||
902 | 6 | |||
903 | 7 | |||
904 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
905 | 9 | manager.manage() | ||
906 | 0 | 10 | ||
907 | === modified symlink 'hooks/router-relation-changed' (properties changed: -x to +x) | |||
908 | === target was u'hooks.py' | |||
909 | --- hooks/router-relation-changed 1970-01-01 00:00:00 +0000 | |||
910 | +++ hooks/router-relation-changed 2014-05-29 17:26:14 +0000 | |||
911 | @@ -0,0 +1,9 @@ | |||
912 | 1 | #!/usr/bin/env python | ||
913 | 2 | # vim: et ai ts=4 sw=4: | ||
914 | 3 | from charmhelpers.core import services | ||
915 | 4 | |||
916 | 5 | import config | ||
917 | 6 | |||
918 | 7 | |||
919 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
920 | 9 | manager.manage() | ||
921 | 0 | 10 | ||
922 | === modified symlink 'hooks/stop' (properties changed: -x to +x) | |||
923 | === target was u'hooks.py' | |||
924 | --- hooks/stop 1970-01-01 00:00:00 +0000 | |||
925 | +++ hooks/stop 2014-05-29 17:26:14 +0000 | |||
926 | @@ -0,0 +1,9 @@ | |||
927 | 1 | #!/usr/bin/env python | ||
928 | 2 | # vim: et ai ts=4 sw=4: | ||
929 | 3 | from charmhelpers.core import services | ||
930 | 4 | |||
931 | 5 | import config | ||
932 | 6 | |||
933 | 7 | |||
934 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
935 | 9 | manager.manage() | ||
936 | 0 | 10 | ||
937 | === modified symlink 'hooks/upgrade-charm' (properties changed: -x to +x) | |||
938 | === target was u'hooks.py' | |||
939 | --- hooks/upgrade-charm 1970-01-01 00:00:00 +0000 | |||
940 | +++ hooks/upgrade-charm 2014-05-29 17:26:14 +0000 | |||
941 | @@ -0,0 +1,9 @@ | |||
942 | 1 | #!/usr/bin/env python | ||
943 | 2 | # vim: et ai ts=4 sw=4: | ||
944 | 3 | from charmhelpers.core import services | ||
945 | 4 | |||
946 | 5 | import config | ||
947 | 6 | |||
948 | 7 | |||
949 | 8 | manager = services.ServiceManager(config.SERVICES) | ||
950 | 9 | manager.manage() |
Reviewers: mp+220741_ code.launchpad. net,
Message:
Please take a look.
Description:
Fixed relation ordering issue
Fixed an relation hook ordering dependency by refactoring to use core.services
callbacks in charmhelpers.
https:/ /code.launchpad .net/~johnsca/ charms/ trusty/ cf-cloud- controller/ services- callback- fu/+merge/ 220741
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/100680044/
Affected files (+134, -112 lines): ers/core/ host.py ers/core/ services. py ers/core/ templating. py
A [revision details]
M hooks/charmhelp
M hooks/charmhelp
M hooks/charmhelp
M hooks/hooks.py