241
243
:param str source: The template source file, relative to
242
244
`$CHARM_DIR/templates`
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
250
:param partial on_change_action: functools partial to be executed when
251
rendered file changes
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
257
263
def __call__(self, manager, service_name, event_name):
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)
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):
276
'No change detected: {}'.format(self.target),
279
self.on_change_action()
266
282
# Convenience aliases for templates