~hazmat/charms/precise/quantum-gateway/ssl-everywhere

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Adam Gandelman
  • Date: 2013-06-03 17:35:03 UTC
  • mfrom: (25.1.32 quantum-gateway)
  • Revision ID: adamg@canonical.com-20130603173503-qg646m67re20r3yn
Support for Grizzly. Support for HA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
import utils
 
3
import lib.utils as utils
 
4
import lib.cluster_utils as cluster
 
5
import lib.openstack_common as openstack
4
6
import sys
5
7
import quantum_utils as qutils
6
8
import os
21
23
        sys.exit(1)
22
24
 
23
25
 
 
26
@utils.inteli_restart(qutils.RESTART_MAP)
24
27
def config_changed():
 
28
    src = utils.config_get('openstack-origin')
 
29
    available = openstack.get_os_codename_install_source(src)
 
30
    installed = openstack.get_os_codename_package('quantum-common')
 
31
    if (available and
 
32
        openstack.get_os_version_codename(available) > \
 
33
        openstack.get_os_version_codename(installed)):
 
34
        qutils.do_openstack_upgrade()
 
35
 
25
36
    if PLUGIN in qutils.GATEWAY_PKGS.keys():
26
37
        render_quantum_conf()
 
38
        render_dhcp_agent_conf()
 
39
        render_l3_agent_conf()
 
40
        render_metadata_agent_conf()
 
41
        render_metadata_api_conf()
27
42
        render_plugin_conf()
28
 
        render_l3_agent_conf()
 
43
        render_ext_port_upstart()
 
44
        render_evacuate_unit()
29
45
        if PLUGIN == qutils.OVS:
30
46
            qutils.add_bridge(qutils.INT_BRIDGE)
31
47
            qutils.add_bridge(qutils.EXT_BRIDGE)
32
48
            ext_port = utils.config_get('ext-port')
33
49
            if ext_port:
34
50
                qutils.add_bridge_port(qutils.EXT_BRIDGE, ext_port)
35
 
        utils.restart(*qutils.GATEWAY_AGENTS[PLUGIN])
36
51
    else:
37
52
        utils.juju_log('ERROR',
38
53
                       'Please provide a valid plugin config')
44
59
    config_changed()
45
60
 
46
61
 
 
62
def render_ext_port_upstart():
 
63
    if utils.config_get('ext-port'):
 
64
        with open(qutils.EXT_PORT_CONF, "w") as conf:
 
65
            conf.write(utils.render_template(
 
66
                            os.path.basename(qutils.EXT_PORT_CONF),
 
67
                            {"ext_port": utils.config_get('ext-port')}
 
68
                            )
 
69
                       )
 
70
    else:
 
71
        if os.path.exists(qutils.EXT_PORT_CONF):
 
72
            os.remove(qutils.EXT_PORT_CONF)
 
73
 
 
74
 
47
75
def render_l3_agent_conf():
48
76
    context = get_keystone_conf()
49
77
    if (context and
56
84
                       )
57
85
 
58
86
 
 
87
def render_dhcp_agent_conf():
 
88
    if (os.path.exists(qutils.DHCP_AGENT_CONF)):
 
89
        with open(qutils.DHCP_AGENT_CONF, "w") as conf:
 
90
            conf.write(utils.render_template(
 
91
                            os.path.basename(qutils.DHCP_AGENT_CONF),
 
92
                            {}
 
93
                            )
 
94
                       )
 
95
 
 
96
 
 
97
def render_metadata_agent_conf():
 
98
    context = get_keystone_conf()
 
99
    if (context and
 
100
        os.path.exists(qutils.METADATA_AGENT_CONF)):
 
101
        context['local_ip'] = utils.get_host_ip()
 
102
        context['shared_secret'] = qutils.get_shared_secret()
 
103
        with open(qutils.METADATA_AGENT_CONF, "w") as conf:
 
104
            conf.write(utils.render_template(
 
105
                            os.path.basename(qutils.METADATA_AGENT_CONF),
 
106
                            context
 
107
                            )
 
108
                       )
 
109
 
 
110
 
59
111
def render_quantum_conf():
60
112
    context = get_rabbit_conf()
61
113
    if (context and
71
123
 
72
124
 
73
125
def render_plugin_conf():
74
 
    context = get_db_conf()
 
126
    context = get_quantum_db_conf()
75
127
    if (context and
76
128
        os.path.exists(qutils.PLUGIN_CONF[PLUGIN])):
77
129
        context['local_ip'] = utils.get_host_ip()
84
136
                       )
85
137
 
86
138
 
 
139
def render_metadata_api_conf():
 
140
    context = get_nova_db_conf()
 
141
    r_context = get_rabbit_conf()
 
142
    q_context = get_keystone_conf()
 
143
    if (context and r_context and q_context and
 
144
        os.path.exists(qutils.NOVA_CONF)):
 
145
        context.update(r_context)
 
146
        context.update(q_context)
 
147
        context['shared_secret'] = qutils.get_shared_secret()
 
148
        with open(qutils.NOVA_CONF, "w") as conf:
 
149
            conf.write(utils.render_template(
 
150
                            os.path.basename(qutils.NOVA_CONF),
 
151
                            context
 
152
                            )
 
153
                       )
 
154
 
 
155
 
 
156
def render_evacuate_unit():
 
157
    context = get_keystone_conf()
 
158
    if context:
 
159
        with open('/usr/local/bin/quantum-evacuate-unit', "w") as conf:
 
160
            conf.write(utils.render_template('evacuate_unit.py', context))
 
161
        os.chmod('/usr/local/bin/quantum-evacuate-unit', 0700)
 
162
 
 
163
 
87
164
def get_keystone_conf():
88
165
    for relid in utils.relation_ids('quantum-network-service'):
89
166
        for unit in utils.relation_list(relid):
98
175
                "service_password": utils.relation_get('service_password',
99
176
                                                       unit, relid),
100
177
                "service_tenant": utils.relation_get('service_tenant',
101
 
                                                     unit, relid)
 
178
                                                     unit, relid),
 
179
                "quantum_host": utils.relation_get('quantum_host',
 
180
                                                   unit, relid),
 
181
                "quantum_port": utils.relation_get('quantum_port',
 
182
                                                   unit, relid),
 
183
                "quantum_url": utils.relation_get('quantum_url',
 
184
                                                   unit, relid),
 
185
                "region": utils.relation_get('region',
 
186
                                             unit, relid)
102
187
                }
103
188
            if None not in conf.itervalues():
104
189
                return conf
106
191
 
107
192
 
108
193
def db_joined():
109
 
    utils.relation_set(username=qutils.DB_USER,
110
 
                       database=qutils.QUANTUM_DB,
111
 
                       hostname=utils.unit_get('private-address'))
112
 
 
113
 
 
 
194
    utils.relation_set(quantum_username=qutils.DB_USER,
 
195
                       quantum_database=qutils.QUANTUM_DB,
 
196
                       quantum_hostname=utils.unit_get('private-address'),
 
197
                       nova_username=qutils.NOVA_DB_USER,
 
198
                       nova_database=qutils.NOVA_DB,
 
199
                       nova_hostname=utils.unit_get('private-address'))
 
200
 
 
201
 
 
202
@utils.inteli_restart(qutils.RESTART_MAP)
114
203
def db_changed():
115
204
    render_plugin_conf()
116
 
    utils.restart(*qutils.GATEWAY_AGENTS[PLUGIN])
117
 
 
118
 
 
119
 
def get_db_conf():
 
205
    render_metadata_api_conf()
 
206
 
 
207
 
 
208
def get_quantum_db_conf():
120
209
    for relid in utils.relation_ids('shared-db'):
121
210
        for unit in utils.relation_list(relid):
122
211
            conf = {
123
 
                "host": utils.relation_get('private-address',
 
212
                "host": utils.relation_get('db_host',
124
213
                                           unit, relid),
125
214
                "user": qutils.DB_USER,
126
 
                "password": utils.relation_get('password',
 
215
                "password": utils.relation_get('quantum_password',
127
216
                                               unit, relid),
128
217
                "db": qutils.QUANTUM_DB
129
218
                }
132
221
    return None
133
222
 
134
223
 
 
224
def get_nova_db_conf():
 
225
    for relid in utils.relation_ids('shared-db'):
 
226
        for unit in utils.relation_list(relid):
 
227
            conf = {
 
228
                "host": utils.relation_get('db_host',
 
229
                                           unit, relid),
 
230
                "user": qutils.NOVA_DB_USER,
 
231
                "password": utils.relation_get('nova_password',
 
232
                                               unit, relid),
 
233
                "db": qutils.NOVA_DB
 
234
                }
 
235
            if None not in conf.itervalues():
 
236
                return conf
 
237
    return None
 
238
 
 
239
 
135
240
def amqp_joined():
136
241
    utils.relation_set(username=qutils.RABBIT_USER,
137
242
                       vhost=qutils.RABBIT_VHOST)
138
243
 
139
244
 
 
245
@utils.inteli_restart(qutils.RESTART_MAP)
140
246
def amqp_changed():
 
247
    render_dhcp_agent_conf()
141
248
    render_quantum_conf()
142
 
    utils.restart(*qutils.GATEWAY_AGENTS[PLUGIN])
 
249
    render_metadata_api_conf()
143
250
 
144
251
 
145
252
def get_rabbit_conf():
153
260
                "rabbit_password": utils.relation_get('password',
154
261
                                                      unit, relid)
155
262
                }
 
263
            clustered = utils.relation_get('clustered', unit, relid)
 
264
            if clustered:
 
265
                conf['rabbit_host'] = utils.relation_get('vip', unit, relid)
156
266
            if None not in conf.itervalues():
157
267
                return conf
158
268
    return None
159
269
 
160
270
 
 
271
@utils.inteli_restart(qutils.RESTART_MAP)
161
272
def nm_changed():
 
273
    render_dhcp_agent_conf()
162
274
    render_l3_agent_conf()
163
 
    utils.restart(*qutils.GATEWAY_AGENTS[PLUGIN])
164
 
 
 
275
    render_metadata_agent_conf()
 
276
    render_metadata_api_conf()
 
277
    render_evacuate_unit()
 
278
    store_ca_cert()
 
279
 
 
280
 
 
281
def store_ca_cert():
 
282
    ca_cert = get_ca_cert()
 
283
    if ca_cert:
 
284
        qutils.install_ca(ca_cert)
 
285
 
 
286
 
 
287
def get_ca_cert():
 
288
    for relid in utils.relation_ids('quantum-network-service'):
 
289
        for unit in utils.relation_list(relid):
 
290
            ca_cert = utils.relation_get('ca_cert', unit, relid)
 
291
            if ca_cert:
 
292
                return ca_cert
 
293
    return None
 
294
 
 
295
 
 
296
def cluster_departed():
 
297
    conf = get_keystone_conf()
 
298
    if conf and cluster.eligible_leader(None):
 
299
        qutils.reassign_agent_resources(conf)
165
300
 
166
301
utils.do_hooks({
167
302
    "install": install,
172
307
    "amqp-relation-joined": amqp_joined,
173
308
    "amqp-relation-changed": amqp_changed,
174
309
    "quantum-network-service-relation-changed": nm_changed,
 
310
    "cluster-relation-departed": cluster_departed
175
311
    })
176
312
 
177
313
sys.exit(0)