~celebdor/charms/trusty/neutron-agents-midonet/trunk

« back to all changes in this revision

Viewing changes to hooks/midonet_helpers/puppet.py

  • Committer: Antoni Segura Puimedon
  • Date: 2016-01-26 12:07:26 UTC
  • Revision ID: toni@midokura.com-20160126120726-02b9xjta30x1pcwj
sync midonet_helpers to get the support for puppet package installation

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
JUJU_HIERA_PATH = '/var/lib/hiera/'
26
26
 
27
 
manifest = """{%- if fingerprint -%}
28
 
apt::key { 'key':
29
 
    key => '{{fingerprint}}',
30
 
{%- if pgp_block %}
31
 
    key_content => '{{pgp_block}}',
32
 
{%- endif %}
33
 
}
 
27
manifest = """{{%- if fingerprint -%}}
 
28
apt::key {{ 'key':
 
29
    key => '{{{{fingerprint}}}}',
 
30
{{%- if pgp_block %}}
 
31
    key_content => '{{{{pgp_block}}}}',
 
32
{{%- endif %}}
 
33
}}
34
34
 
35
 
{% endif -%}
36
 
class {'midonet::repository':}
37
 
class {'{{component}}':}
 
35
{{% endif -%}}
 
36
class {{'midonet::repository':}}
 
37
{actions}
38
38
 
39
39
Apt::Key <| name == 'key' |> -> Apt::Source <| |> ->
40
 
Class['{{component}}']
 
40
{ordering}
41
41
"""
42
42
 
43
43
 
44
 
class InstallCallback(base.ManagerCallback):
 
44
class PuppetCallback(base.ManagerCallback):
45
45
    def __call__(self, manager, service_name, event_name):
46
46
        service = manager.get_service(service_name)
47
 
        context = {}
 
47
        req_data = {}
48
48
        for ctx in service.get('required_data', []):
49
 
            context.update(ctx)
 
49
            req_data.update(ctx)
 
50
 
 
51
        ctxt = {
 
52
            'fingerprint': req_data.get(self._fingerprint_key),
 
53
            'pgp_block': req_data.get(self._content_key)
 
54
        }
 
55
        ctxt.update(self._ctxt)
50
56
 
51
57
        with tempfile.NamedTemporaryFile(mode='w+t') as tmp:
52
 
            content = jinja2.Environment().from_string(manifest).render(
53
 
                component=self._component,
54
 
                fingerprint=context.get(self._fingerprint_key),
55
 
                pgp_block=context.get(self._content_key))
 
58
            content = jinja2.Environment().from_string(self._manifest).render(
 
59
                **ctxt)
56
60
            tmp.write(content)
57
61
            tmp.flush()
58
62
            try:
62
66
                            content)
63
67
                raise
64
68
 
65
 
    def __init__(self, component):
66
 
        self._component = component
 
69
    def __init__(self, actions, ordering, ctxt):
67
70
        self._fingerprint_key = 'midonet_key_fingerprint'
68
71
        self._content_key = 'midonet_key_content'
 
72
        self._manifest = manifest.format(actions=actions, ordering=ordering)
 
73
        self._ctxt = ctxt
 
74
 
 
75
 
 
76
class ExecutPuppetClassCallback(PuppetCallback):
 
77
    execution_template = """class {'{{component}}':}"""
 
78
    ordering_template = """Class['{{component}}']"""
 
79
 
 
80
    def __init__(self, component):
 
81
        super(ExecutPuppetClassCallback, self).__init__(
 
82
            actions=self.execution_template,
 
83
            ordering=self.ordering_template,
 
84
            ctxt={'component': component})
 
85
 
 
86
 
 
87
class InstallPackagesCallback(PuppetCallback):
 
88
    action_template = """package { %s:
 
89
    ensure => present
 
90
}"""
 
91
 
 
92
    def __init__(self, packages=()):
 
93
        actions = []
 
94
        for package in packages:
 
95
            actions.append(self.action_template % package)
 
96
        super(InstallPackagesCallback, self).__init__(
 
97
            actions='\n'.join(actions),
 
98
            ordering="""Package<| |>""",
 
99
            ctxt={})
 
100
 
 
101
 
 
102
InstallCallback = ExecutPuppetClassCallback
69
103
 
70
104
 
71
105
def _apply(path, debug=False):
95
129
    subprocess.check_call(cmd)
96
130
 
97
131
 
98
 
def set_juju_hierarchy(attempts=10, extra_data_dirs=None):
 
132
def set_juju_hierarchy(attempts=10, extra_data_dirs=None,
 
133
                       hiera_path='/etc/puppet/hiera.yaml'):
99
134
    """Sets a puppet datadir lookup to find our customizations.
100
135
 
101
136
    Extra data dirs must be relative to /var/lib/hiera
103
138
    if attempts < 1:
104
139
        raise RuntimeError('Failed to set juju puppet hierarchy')
105
140
 
106
 
    hiera_path = '/etc/puppet/hiera.yaml'
107
141
    try:
108
142
        with open(hiera_path) as hiera:
109
143
            hiera_info = yaml.load(hiera)