~openstack-charmers-next/charms/trusty/cisco-vpp/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/templating.py

  • Committer: Liam Young
  • Date: 2015-12-02 10:33:20 UTC
  • mfrom: (115.1.9 dhcp)
  • Revision ID: liam.young@canonical.com-20151202103320-0cjg1te17yovqyu3
[gnuoy, r=james-page] Adds support for serving dhcp and metadata requests to guests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import six
20
20
 
21
 
from charmhelpers.fetch import apt_install
 
21
from charmhelpers.fetch import apt_install, apt_update
22
22
from charmhelpers.core.hookenv import (
23
23
    log,
24
24
    ERROR,
29
29
try:
30
30
    from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions
31
31
except ImportError:
32
 
    # python-jinja2 may not be installed yet, or we're running unittests.
33
 
    FileSystemLoader = ChoiceLoader = Environment = exceptions = None
 
32
    apt_update(fatal=True)
 
33
    apt_install('python-jinja2', fatal=True)
 
34
    from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions
34
35
 
35
36
 
36
37
class OSConfigException(Exception):
37
38
    pass
38
39
 
39
40
 
40
 
def os_template_dirs(templates_dir, os_release):
41
 
    tmpl_dirs = [(rel, os.path.join(templates_dir, rel))
42
 
                 for rel in six.itervalues(OPENSTACK_CODENAMES)]
43
 
 
44
 
    if not os.path.isdir(templates_dir):
45
 
        log('Templates directory not found @ %s.' % templates_dir,
46
 
            level=ERROR)
47
 
        raise OSConfigException
48
 
    dirs = [templates_dir]
49
 
    helper_templates = os.path.join(os.path.dirname(__file__), 'templates')
50
 
    if os.path.isdir(helper_templates):
51
 
        dirs.append(helper_templates)
52
 
 
53
 
    for rel, tmpl_dir in tmpl_dirs:
54
 
        if os.path.isdir(tmpl_dir):
55
 
            dirs.insert(0, tmpl_dir)
56
 
        if rel == os_release:
57
 
            break
58
 
    ch_templates = os.path.dirname(__file__) + '/charmhelpers/contrib/openstack/templates'
59
 
    dirs.append(ch_templates)
60
 
    log('Template search path: %s' %
61
 
        ' '.join(dirs), level=INFO)
62
 
    return dirs
63
 
 
64
 
 
65
41
def get_loader(templates_dir, os_release):
66
42
    """
67
43
    Create a jinja2.ChoiceLoader containing template dirs up to
137
113
 
138
114
    def complete_contexts(self):
139
115
        '''
140
 
        Return a list of interfaces that have atisfied contexts.
 
116
        Return a list of interfaces that have satisfied contexts.
141
117
        '''
142
118
        if self._complete_contexts:
143
119
            return self._complete_contexts
318
294
        [interfaces.extend(i.complete_contexts())
319
295
         for i in six.itervalues(self.templates)]
320
296
        return interfaces
 
297
 
 
298
    def get_incomplete_context_data(self, interfaces):
 
299
        '''
 
300
        Return dictionary of relation status of interfaces and any missing
 
301
        required context data. Example:
 
302
            {'amqp': {'missing_data': ['rabbitmq_password'], 'related': True},
 
303
             'zeromq-configuration': {'related': False}}
 
304
        '''
 
305
        incomplete_context_data = {}
 
306
 
 
307
        for i in six.itervalues(self.templates):
 
308
            for context in i.contexts:
 
309
                for interface in interfaces:
 
310
                    related = False
 
311
                    if interface in context.interfaces:
 
312
                        related = context.get_related()
 
313
                        missing_data = context.missing_data
 
314
                        if missing_data:
 
315
                            incomplete_context_data[interface] = {'missing_data': missing_data}
 
316
                        if related:
 
317
                            if incomplete_context_data.get(interface):
 
318
                                incomplete_context_data[interface].update({'related': True})
 
319
                            else:
 
320
                                incomplete_context_data[interface] = {'related': True}
 
321
                        else:
 
322
                            incomplete_context_data[interface] = {'related': False}
 
323
        return incomplete_context_data