~corey.bryant/charms/trusty/cinder-ceph/charm-proof-next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/services/base.py

  • Committer: Liam Young
  • Date: 2014-08-26 13:26:28 UTC
  • Revision ID: liam.young@canonical.com-20140826132628-3ylj4pw8mff7v4oj
Sync charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
        """
18
18
        Register a list of services, given their definitions.
19
19
 
20
 
        Traditional charm authoring is focused on implementing hooks.  That is,
21
 
        the charm author is thinking in terms of "What hook am I handling; what
22
 
        does this hook need to do?"  However, in most cases, the real question
23
 
        should be "Do I have the information I need to configure and start this
24
 
        piece of software and, if so, what are the steps for doing so?"  The
25
 
        ServiceManager framework tries to bring the focus to the data and the
26
 
        setup tasks, in the most declarative way possible.
27
 
 
28
20
        Service definitions are dicts in the following formats (all keys except
29
21
        'service' are optional)::
30
22
 
31
23
            {
32
24
                "service": <service name>,
33
25
                "required_data": <list of required data contexts>,
 
26
                "provided_data": <list of provided data contexts>,
34
27
                "data_ready": <one or more callbacks>,
35
28
                "data_lost": <one or more callbacks>,
36
29
                "start": <one or more callbacks>,
44
37
        of 'data_ready' and 'start' callbacks executed.  See `is_ready()` for more
45
38
        information.
46
39
 
 
40
        The 'provided_data' list should contain relation data providers, most likely
 
41
        a subclass of :class:`charmhelpers.core.services.helpers.RelationContext`,
 
42
        that will indicate a set of data to set on a given relation.
 
43
 
47
44
        The 'data_ready' value should be either a single callback, or a list of
48
45
        callbacks, to be called when all items in 'required_data' pass `is_ready()`.
49
46
        Each callback will be called with the service name as the only parameter.
123
120
            self.reconfigure_services()
124
121
 
125
122
    def provide_data(self):
 
123
        """
 
124
        Set the relation data for each provider in the ``provided_data`` list.
 
125
 
 
126
        A provider must have a `name` attribute, which indicates which relation
 
127
        to set data on, and a `provide_data()` method, which returns a dict of
 
128
        data to set.
 
129
        """
126
130
        hook_name = hookenv.hook_name()
127
131
        for service in self.services.values():
128
132
            for provider in service.get('provided_data', []):
129
133
                if re.match(r'{}-relation-(joined|changed)'.format(provider.name), hook_name):
130
134
                    data = provider.provide_data()
131
 
                    if provider._is_ready(data):
 
135
                    _ready = provider._is_ready(data) if hasattr(provider, '_is_ready') else data
 
136
                    if _ready:
132
137
                        hookenv.relation_set(None, data)
133
138
 
134
139
    def reconfigure_services(self, *service_names):