~le-charmers/charms/trusty/swift-proxy/leadership-election

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-06-04 08:44:50 UTC
  • Revision ID: liam.young@canonical.com-20150604084450-6542hlpg58av4zw1
Resync le charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import os
18
 
import re
19
18
import json
 
19
from inspect import getargspec
20
20
from collections import Iterable, OrderedDict
21
21
 
22
22
from charmhelpers.core import host
132
132
        if hook_name == 'stop':
133
133
            self.stop_services()
134
134
        else:
 
135
            self.reconfigure_services()
135
136
            self.provide_data()
136
 
            self.reconfigure_services()
137
137
        cfg = hookenv.config()
138
138
        if cfg.implicit_save:
139
139
            cfg.save()
145
145
        A provider must have a `name` attribute, which indicates which relation
146
146
        to set data on, and a `provide_data()` method, which returns a dict of
147
147
        data to set.
 
148
 
 
149
        The `provide_data()` method can optionally accept two parameters:
 
150
 
 
151
          * ``remote_service`` The name of the remote service that the data will
 
152
            be provided to.  The `provide_data()` method will be called once
 
153
            for each connected service (not unit).  This allows the method to
 
154
            tailor its data to the given service.
 
155
          * ``service_ready`` Whether or not the service definition had all of
 
156
            its requirements met, and thus the ``data_ready`` callbacks run.
 
157
 
 
158
        Note that the ``provided_data`` methods are now called **after** the
 
159
        ``data_ready`` callbacks are run.  This gives the ``data_ready`` callbacks
 
160
        a chance to generate any data necessary for the providing to the remote
 
161
        services.
148
162
        """
149
 
        hook_name = hookenv.hook_name()
150
 
        for service in self.services.values():
 
163
        for service_name, service in self.services.items():
 
164
            service_ready = self.is_ready(service_name)
151
165
            for provider in service.get('provided_data', []):
152
 
                if re.match(r'{}-relation-(joined|changed)'.format(provider.name), hook_name):
153
 
                    data = provider.provide_data()
154
 
                    _ready = provider._is_ready(data) if hasattr(provider, '_is_ready') else data
155
 
                    if _ready:
156
 
                        hookenv.relation_set(None, data)
 
166
                for relid in hookenv.relation_ids(provider.name):
 
167
                    units = hookenv.related_units(relid)
 
168
                    if not units:
 
169
                        continue
 
170
                    remote_service = units[0].split('/')[0]
 
171
                    argspec = getargspec(provider.provide_data)
 
172
                    if len(argspec.args) > 1:
 
173
                        data = provider.provide_data(remote_service, service_ready)
 
174
                    else:
 
175
                        data = provider.provide_data()
 
176
                    if data:
 
177
                        hookenv.relation_set(relid, data)
157
178
 
158
179
    def reconfigure_services(self, *service_names):
159
180
        """