forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etc_xapi.d_plugins_openvswitch-cfg-update
executable file
·268 lines (219 loc) · 9.3 KB
/
etc_xapi.d_plugins_openvswitch-cfg-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
#
# xapi plugin script to update the cache of configuration items in the
# ovs-vswitchd configuration that are managed in the xapi database when
# integrated with Citrix management tools.
# Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TBD: - error handling needs to be improved. Currently this can leave
# TBD: the system in a bad state if anything goes wrong.
import XenAPIPlugin
import os
import subprocess
import syslog
import re
vsctl = '/usr/bin/ovs-vsctl'
ofctl = '/usr/bin/ovs-ofctl'
cacert_filename = '/etc/openvswitch/vswitchd.cacert'
# Delete the CA certificate, so that we go back to boot-strapping mode
def delete_cacert():
try:
os.remove(cacert_filename)
except OSError:
# Ignore error if file doesn't exist
pass
def update(session, args):
# Refresh bridge network UUIDs in case this host joined or left a pool.
script = '/opt/xensource/libexec/interface-reconfigure'
try:
retval = subprocess.call([script, 'rewrite'])
if retval != 0:
syslog.syslog('%s exited with status %d' % (script, retval))
except OSError, e:
syslog.syslog('%s: failed to execute (%s)' % (script, e.strerror))
pools = session.xenapi.pool.get_all()
# We assume there is only ever one pool...
if len(pools) == 0:
raise XenAPIPlugin.Failure('NO_POOL_FOR_HOST', [])
if len(pools) > 1:
raise XenAPIPlugin.Failure('MORE_THAN_ONE_POOL_FOR_HOST', [])
new_controller = False
pool = session.xenapi.pool.get_record(pools[0])
controller = pool.get('vswitch_controller')
ret_str = ''
currentControllers = vswitchCurrentControllers()
if not controller and currentControllers:
delete_cacert()
try:
emergency_reset(session, None)
except:
pass
removeControllerCfg()
ret_str += 'Successfully removed controller config. '
# controller cannot be empty, otherwise, this will always be True.
elif controller and controller not in currentControllers:
delete_cacert()
try:
emergency_reset(session, None)
except:
pass
setControllerCfg(controller)
new_controller = True
ret_str += 'Successfully set controller to %s. ' % controller
try:
pool_fail_mode = pool['other_config']['vswitch-controller-fail-mode']
except KeyError, e:
pool_fail_mode = None
bton = {}
for rec in session.xenapi.network.get_all_records().values():
try:
bton[rec['bridge']] = rec
except KeyError:
pass
# If new controller, get management MAC addresses from XAPI now
# in case fail_mode set to secure which may affect XAPI access
mgmt_bridge = None
host_mgmt_mac = None
host_mgmt_device = None
pool_mgmt_macs = {}
if new_controller:
query = 'field "management"="true"'
recs = session.xenapi.PIF.get_all_records_where(query)
for rec in recs.itervalues():
pool_mgmt_macs[rec.get('MAC')] = rec.get('device')
dib_changed = False
fail_mode_changed = False
for bridge in vswitchCfgQuery(['list-br']).split():
network = bton[bridge]
bridge = vswitchCfgQuery(['br-to-parent', bridge])
xapi_dib = network['other_config'].get('vswitch-disable-in-band')
if not xapi_dib:
xapi_dib = ''
ovs_dib = vswitchCfgQuery(['--', '--if-exists', 'get', 'Bridge',
bridge,
'other_config:disable-in-band']).strip('"')
# Do nothing if setting is invalid, and warn the user.
if xapi_dib not in ['true', 'false', '']:
ret_str += '"' + xapi_dib + '"' + \
' is an invalid value for vswitch-disable-in-band on ' + \
bridge + ' '
# Change bridge disable-in-band option if XAPI and OVS states differ.
elif xapi_dib != ovs_dib:
# 'true' or 'false'
if xapi_dib:
vswitchCfgMod(['--', 'set', 'Bridge', bridge,
'other_config:disable-in-band=' + xapi_dib])
# '' or None
else:
vswitchCfgMod(['--', 'remove', 'Bridge', bridge,
'other_config', 'disable-in-band'])
dib_changed = True
# Change bridge fail_mode if XAPI state differs from OVS state.
bridge_fail_mode = vswitchCfgQuery(['get', 'Bridge',
bridge, 'fail_mode']).strip('[]"')
try:
other_config = bton[bridge]['other_config']
fail_mode = other_config['vswitch-controller-fail-mode']
except KeyError, e:
fail_mode = None
if fail_mode not in ['secure', 'standalone']:
fail_mode = pool_fail_mode
if fail_mode != 'secure':
fail_mode = 'standalone'
if bridge_fail_mode != fail_mode:
vswitchCfgMod(['--', 'set', 'Bridge', bridge,
'fail_mode=%s' % fail_mode])
fail_mode_changed = True
# Determine local mgmt MAC address if host being added to secure
# pool so we can add default flows to allow management traffic
if new_controller and fail_mode_changed and pool_fail_mode == 'secure':
oc = vswitchCfgQuery(['get', 'Bridge', bridge, 'other-config'])
m = re.match('.*hwaddr="([0-9a-fA-F:].*)".*', oc)
if m and m.group(1) in pool_mgmt_macs.keys():
mgmt_bridge = bridge
host_mgmt_mac = m.group(1)
host_mgmt_device = pool_mgmt_macs[host_mgmt_mac]
if (host_mgmt_mac is not None and mgmt_bridge is not None and
host_mgmt_device is not None):
tp = 'idle_timeout=0,priority=0'
port = vswitchCfgQuery(['get', 'interface', host_mgmt_device,
'ofport'])
addFlow(mgmt_bridge, '%s,in_port=%s,arp,nw_proto=1,actions=local' %
(tp, port))
addFlow(mgmt_bridge, '%s,in_port=local,arp,dl_src=%s,actions=%s' %
(tp, host_mgmt_mac, port))
addFlow(mgmt_bridge, '%s,in_port=%s,dl_dst=%s,actions=local' %
(tp, port, host_mgmt_mac))
addFlow(mgmt_bridge, '%s,in_port=local,dl_src=%s,actions=%s' %
(tp, host_mgmt_mac, port))
if dib_changed:
ret_str += 'Updated in-band management. '
if fail_mode_changed:
ret_str += 'Updated fail_mode. '
if ret_str != '':
return ret_str
else:
return 'No change to configuration'
def vswitchCurrentControllers():
controllers = vswitchCfgQuery(['get-manager'])
def parse_controller(controller):
if controller.startswith('ssl:'):
return controller.split(':')[1]
return controller.split(':')[0]
return [parse_controller(controller)
for controller in controllers.split('\n')
if controller]
def removeControllerCfg():
vswitchCfgMod(['--', 'del-manager',
'--', 'del-ssl'])
def setControllerCfg(controller):
# /etc/xensource/xapi-ssl.pem is mentioned twice below because it
# contains both the private key and the certificate.
vswitchCfgMod(['--', 'del-manager',
'--', 'del-ssl',
'--', '--bootstrap', 'set-ssl',
'/etc/xensource/xapi-ssl.pem',
'/etc/xensource/xapi-ssl.pem',
cacert_filename,
'--', 'set-manager', 'ssl:' + controller + ':6640'])
def vswitchCfgQuery(action_args):
cmd = [vsctl, '-vconsole:off'] + action_args
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
if len(output) == 0 or output[0] is None:
output = ''
else:
output = output[0].strip()
return output
def vswitchCfgMod(action_args):
cmd = [vsctl, '--timeout=5', '-vconsole:off'] + action_args
exitcode = subprocess.call(cmd)
if exitcode != 0:
raise XenAPIPlugin.Failure('VSWITCH_CONFIG_MOD_FAILURE',
[str(exitcode), str(action_args)])
def emergency_reset(session, args):
cmd = [vsctl, '--timeout=5', 'emer-reset']
exitcode = subprocess.call(cmd)
if exitcode != 0:
raise XenAPIPlugin.Failure('VSWITCH_EMER_RESET_FAILURE',
[str(exitcode)])
return 'Successfully reset configuration'
def addFlow(switch, flow):
cmd = [ofctl, 'add-flow', switch, flow]
exitcode = subprocess.call(cmd)
if exitcode != 0:
raise XenAPIPlugin.Failure('VSWITCH_ADD_FLOW_FAILURE',
[str(exitcode), str(switch), str(flow)])
if __name__ == '__main__':
XenAPIPlugin.dispatch({'update': update,
'emergency_reset': emergency_reset})