~gnuoy/charms/trusty/keystone/restart-horror

« back to all changes in this revision

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

  • Committer: Corey Bryant
  • Date: 2015-08-18 17:34:34 UTC
  • Revision ID: corey.bryant@canonical.com-20150818173434-hp6flkiy622hjrsy
[corey.bryant,r=trivial] Sync charm-helpers to pick up Liberty support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import os
18
18
import yaml
 
19
 
19
20
from charmhelpers.core import hookenv
 
21
from charmhelpers.core import host
20
22
from charmhelpers.core import templating
21
23
 
22
24
from charmhelpers.core.services.base import ManagerCallback
240
242
 
241
243
    :param str source: The template source file, relative to
242
244
        `$CHARM_DIR/templates`
 
245
 
243
246
    :param str target: The target to write the rendered template to
244
247
    :param str owner: The owner of the rendered file
245
248
    :param str group: The group of the rendered file
246
249
    :param int perms: The permissions of the rendered file
247
 
 
 
250
    :param partial on_change_action: functools partial to be executed when
 
251
                                     rendered file changes
248
252
    """
249
253
    def __init__(self, source, target,
250
 
                 owner='root', group='root', perms=0o444):
 
254
                 owner='root', group='root', perms=0o444,
 
255
                 on_change_action=None):
251
256
        self.source = source
252
257
        self.target = target
253
258
        self.owner = owner
254
259
        self.group = group
255
260
        self.perms = perms
 
261
        self.on_change_action = on_change_action
256
262
 
257
263
    def __call__(self, manager, service_name, event_name):
 
264
        pre_checksum = ''
 
265
        if self.on_change_action and os.path.isfile(self.target):
 
266
            pre_checksum = host.file_hash(self.target)
258
267
        service = manager.get_service(service_name)
259
268
        context = {}
260
269
        for ctx in service.get('required_data', []):
261
270
            context.update(ctx)
262
271
        templating.render(self.source, self.target, context,
263
272
                          self.owner, self.group, self.perms)
 
273
        if self.on_change_action:
 
274
            if pre_checksum == host.file_hash(self.target):
 
275
                hookenv.log(
 
276
                    'No change detected: {}'.format(self.target),
 
277
                    hookenv.DEBUG)
 
278
            else:
 
279
                self.on_change_action()
264
280
 
265
281
 
266
282
# Convenience aliases for templates