~jjo/charms/trusty/ceph-osd/fix-ceph-osd-nrpe

« back to all changes in this revision

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

  • Committer: Corey Bryant
  • Date: 2015-07-02 15:08:10 UTC
  • mfrom: (41.1.13 ceph-osd)
  • Revision ID: corey.bryant@canonical.com-20150702150810-6fg1vreple1d8oms
[beisner,r=corey.bryant] Amulet test updates.

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
20
 
from collections import Iterable
 
19
from inspect import getargspec
 
20
from collections import Iterable, OrderedDict
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 = {}
 
122
        self.services = OrderedDict()
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
 
        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()
 
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()
140
143
 
141
144
    def provide_data(self):
142
145
        """
145
148
        A provider must have a `name` attribute, which indicates which relation
146
149
        to set data on, and a `provide_data()` method, which returns a dict of
147
150
        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.
148
165
        """
149
 
        hook_name = hookenv.hook_name()
150
 
        for service in self.services.values():
 
166
        for service_name, service in self.services.items():
 
167
            service_ready = self.is_ready(service_name)
151
168
            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)
 
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)
157
181
 
158
182
    def reconfigure_services(self, *service_names):
159
183
        """