~simpoir/landscape-client-charm/charm_push_target

« back to all changes in this revision

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

  • Committer: 🤖 Landscape Builder
  • Author(s): Chad Smith
  • Date: 2017-03-03 23:19:24 UTC
  • mfrom: (62.1.4 landscape-client)
  • Revision ID: _landscape_builder-20170303231924-c0380jqvms8gi7cj
Merge add-apt-repository-retries [f=] [r=landscape-builder,ericsnowcurrently] [a=Chad Smith]
Sync charmhelpers for add_source retries to avoid hook errors on network timeouts.  Because of charmhelpers sync fetch._run_apt_command moved to fetch.ubuntu._run_apt_command and fetch has new dependency on osplatform

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014-2015 Canonical Limited.
2
2
#
3
 
# This file is part of charm-helpers.
4
 
#
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.
8
 
#
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.
13
 
#
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
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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.
16
14
 
17
15
import os
18
16
import yaml
 
17
 
19
18
from charmhelpers.core import hookenv
 
19
from charmhelpers.core import host
20
20
from charmhelpers.core import templating
21
21
 
22
22
from charmhelpers.core.services.base import ManagerCallback
239
239
    action.
240
240
 
241
241
    :param str source: The template source file, relative to
242
 
    `$CHARM_DIR/templates`
 
242
        `$CHARM_DIR/templates`
243
243
 
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
 
251
 
 
252
    :return str: The rendered template
248
253
    """
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
256
264
 
257
265
    def __call__(self, manager, service_name, event_name):
 
266
        pre_checksum = ''
 
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)
259
 
        context = {}
 
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)
 
274
 
 
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):
 
280
                hookenv.log(
 
281
                    'No change detected: {}'.format(self.target),
 
282
                    hookenv.DEBUG)
 
283
            else:
 
284
                self.on_change_action()
 
285
 
 
286
        return result
264
287
 
265
288
 
266
289
# Convenience aliases for templates