~openstack-charm-sync/charms/trusty/cinder-backup/stable

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2016-01-26 12:23:55 UTC
  • mfrom: (1.1.1 stable.remote)
  • Revision ID: edward.hope-morley@canonical.com-20160126122355-9ko3yaf807xe3w9l
[trivial] sync charmhelpers and add amulet tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
243
243
    :param str source: The template source file, relative to
244
244
        `$CHARM_DIR/templates`
245
245
 
246
 
    :param str target: The target to write the rendered template to
 
246
    :param str target: The target to write the rendered template to (or None)
247
247
    :param str owner: The owner of the rendered file
248
248
    :param str group: The group of the rendered file
249
249
    :param int perms: The permissions of the rendered file
250
250
    :param partial on_change_action: functools partial to be executed when
251
251
                                     rendered file changes
 
252
    :param jinja2 loader template_loader: A jinja2 template loader
 
253
 
 
254
    :return str: The rendered template
252
255
    """
253
256
    def __init__(self, source, target,
254
257
                 owner='root', group='root', perms=0o444,
255
 
                 on_change_action=None):
 
258
                 on_change_action=None, template_loader=None):
256
259
        self.source = source
257
260
        self.target = target
258
261
        self.owner = owner
259
262
        self.group = group
260
263
        self.perms = perms
261
264
        self.on_change_action = on_change_action
 
265
        self.template_loader = template_loader
262
266
 
263
267
    def __call__(self, manager, service_name, event_name):
264
268
        pre_checksum = ''
265
269
        if self.on_change_action and os.path.isfile(self.target):
266
270
            pre_checksum = host.file_hash(self.target)
267
271
        service = manager.get_service(service_name)
268
 
        context = {}
 
272
        context = {'ctx': {}}
269
273
        for ctx in service.get('required_data', []):
270
274
            context.update(ctx)
271
 
        templating.render(self.source, self.target, context,
272
 
                          self.owner, self.group, self.perms)
 
275
            context['ctx'].update(ctx)
 
276
 
 
277
        result = templating.render(self.source, self.target, context,
 
278
                                   self.owner, self.group, self.perms,
 
279
                                   template_loader=self.template_loader)
273
280
        if self.on_change_action:
274
281
            if pre_checksum == host.file_hash(self.target):
275
282
                hookenv.log(
278
285
            else:
279
286
                self.on_change_action()
280
287
 
 
288
        return result
 
289
 
281
290
 
282
291
# Convenience aliases for templates
283
292
render_template = template = TemplateCallback