~tribaal/charms/trusty/nova-compute/enable-api-rate-limiting

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2015-03-16 15:38:04 UTC
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: edward.hope-morley@canonical.com-20150316153804-du1szvv0gwfve4g6
cleanup

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
18
19
import json
19
 
from inspect import getargspec
20
 
from collections import Iterable, OrderedDict
 
20
from collections import Iterable
21
21
 
22
22
from charmhelpers.core import host
23
23
from charmhelpers.core import hookenv
119
119
        """
120
120
        self._ready_file = os.path.join(hookenv.charm_dir(), 'READY-SERVICES.json')
121
121
        self._ready = None
122
 
        self.services = OrderedDict()
 
122
        self.services = {}
123
123
        for service in services or []:
124
124
            service_name = service['service']
125
125
            self.services[service_name] = service
128
128
        """
129
129
        Handle the current hook by doing The Right Thing with the registered services.
130
130
        """
131
 
        hookenv._run_atstart()
132
 
        try:
133
 
            hook_name = hookenv.hook_name()
134
 
            if hook_name == 'stop':
135
 
                self.stop_services()
136
 
            else:
137
 
                self.reconfigure_services()
138
 
                self.provide_data()
139
 
        except SystemExit as x:
140
 
            if x.code is None or x.code == 0:
141
 
                hookenv._run_atexit()
142
 
        hookenv._run_atexit()
 
131
        hook_name = hookenv.hook_name()
 
132
        if hook_name == 'stop':
 
133
            self.stop_services()
 
134
        else:
 
135
            self.provide_data()
 
136
            self.reconfigure_services()
 
137
        cfg = hookenv.config()
 
138
        if cfg.implicit_save:
 
139
            cfg.save()
143
140
 
144
141
    def provide_data(self):
145
142
        """
148
145
        A provider must have a `name` attribute, which indicates which relation
149
146
        to set data on, and a `provide_data()` method, which returns a dict of
150
147
        data to set.
151
 
 
152
 
        The `provide_data()` method can optionally accept two parameters:
153
 
 
154
 
          * ``remote_service`` The name of the remote service that the data will
155
 
            be provided to.  The `provide_data()` method will be called once
156
 
            for each connected service (not unit).  This allows the method to
157
 
            tailor its data to the given service.
158
 
          * ``service_ready`` Whether or not the service definition had all of
159
 
            its requirements met, and thus the ``data_ready`` callbacks run.
160
 
 
161
 
        Note that the ``provided_data`` methods are now called **after** the
162
 
        ``data_ready`` callbacks are run.  This gives the ``data_ready`` callbacks
163
 
        a chance to generate any data necessary for the providing to the remote
164
 
        services.
165
148
        """
166
 
        for service_name, service in self.services.items():
167
 
            service_ready = self.is_ready(service_name)
 
149
        hook_name = hookenv.hook_name()
 
150
        for service in self.services.values():
168
151
            for provider in service.get('provided_data', []):
169
 
                for relid in hookenv.relation_ids(provider.name):
170
 
                    units = hookenv.related_units(relid)
171
 
                    if not units:
172
 
                        continue
173
 
                    remote_service = units[0].split('/')[0]
174
 
                    argspec = getargspec(provider.provide_data)
175
 
                    if len(argspec.args) > 1:
176
 
                        data = provider.provide_data(remote_service, service_ready)
177
 
                    else:
178
 
                        data = provider.provide_data()
179
 
                    if data:
180
 
                        hookenv.relation_set(relid, 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)
181
157
 
182
158
    def reconfigure_services(self, *service_names):
183
159
        """