1
1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
5
# charm-helpers is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# charm-helpers is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
19
18
from charmhelpers.core import hookenv
19
from charmhelpers.core import host
20
20
from charmhelpers.core import templating
22
22
from charmhelpers.core.services.base import ManagerCallback
241
241
:param str source: The template source file, relative to
242
`$CHARM_DIR/templates`
242
`$CHARM_DIR/templates`
244
:param str target: The target to write the rendered template to
244
:param str target: The target to write the rendered template to (or None)
245
245
:param str owner: The owner of the rendered file
246
246
:param str group: The group of the rendered file
247
247
:param int perms: The permissions of the rendered file
248
:param partial on_change_action: functools partial to be executed when
249
rendered file changes
250
:param jinja2 loader template_loader: A jinja2 template loader
252
:return str: The rendered template
249
254
def __init__(self, source, target,
250
owner='root', group='root', perms=0o444):
255
owner='root', group='root', perms=0o444,
256
on_change_action=None, template_loader=None):
251
257
self.source = source
252
258
self.target = target
253
259
self.owner = owner
254
260
self.group = group
255
261
self.perms = perms
262
self.on_change_action = on_change_action
263
self.template_loader = template_loader
257
265
def __call__(self, manager, service_name, event_name):
267
if self.on_change_action and os.path.isfile(self.target):
268
pre_checksum = host.file_hash(self.target)
258
269
service = manager.get_service(service_name)
270
context = {'ctx': {}}
260
271
for ctx in service.get('required_data', []):
261
272
context.update(ctx)
262
templating.render(self.source, self.target, context,
263
self.owner, self.group, self.perms)
273
context['ctx'].update(ctx)
275
result = templating.render(self.source, self.target, context,
276
self.owner, self.group, self.perms,
277
template_loader=self.template_loader)
278
if self.on_change_action:
279
if pre_checksum == host.file_hash(self.target):
281
'No change detected: {}'.format(self.target),
284
self.on_change_action()
266
289
# Convenience aliases for templates