3 # xapi plugin script to update the cache of configuration items in the
4 # ovs-vswitchd configuration that are managed in the xapi database when
5 # integrated with Citrix management tools.
7 # Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at:
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
21 # TBD: - error handling needs to be improved. Currently this can leave
22 # TBD: the system in a bad state if anything goes wrong.
30 vsctl="/usr/bin/ovs-vsctl"
31 cacert_filename="/etc/openvswitch/vswitchd.cacert"
33 # Delete the CA certificate, so that we go back to boot-strapping mode
36 os.remove(cacert_filename)
38 # Ignore error if file doesn't exist
41 def update(session, args):
42 # Refresh bridge network UUIDs in case this host joined or left a pool.
43 script = "/opt/xensource/libexec/interface-reconfigure"
45 retval = subprocess.call([script, "rewrite"])
47 syslog.syslog("%s exited with status %d" % (script, retval))
49 syslog.syslog("%s: failed to execute (%s)" % (script, e.strerror))
51 pools = session.xenapi.pool.get_all()
52 # We assume there is only ever one pool...
54 raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
56 raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
57 pool = session.xenapi.pool.get_record(pools[0])
60 controller = pool["vswitch_controller"]
62 # On systems older than XenServer 5.6.0, we needed to store
63 # the key in "other_config".
64 controller = pool["other_config"]["vSwitchController"]
68 currentController = vswitchCurrentController()
69 if controller == "" and currentController != "":
72 emergency_reset(session, None)
76 ret_str += "Successfully removed controller config. "
77 elif controller != currentController:
80 emergency_reset(session, None)
83 setControllerCfg(controller)
84 ret_str += "Successfully set controller to %s. " % controller
87 pool_fail_mode = pool["other_config"]["vswitch-controller-fail-mode"]
93 for n in session.xenapi.network.get_all():
94 rec = session.xenapi.network.get_record(n)
96 bton[rec['bridge']] = rec
101 fail_mode_changed = False
102 for bridge in vswitchCfgQuery(['list-br']).split():
103 network = bton[bridge]
104 bridge = vswitchCfgQuery(['br-to-parent', bridge])
106 xapi_dib = network['other_config'].get('vswitch-disable-in-band')
110 ovs_dib = vswitchCfgQuery(['get', 'Bridge', bridge,
111 'other_config:disable-in-band']).strip('"')
113 # Do nothing if setting is invalid, and warn the user.
114 if xapi_dib not in ['true', 'false', '']:
115 ret_str += '"' + xapi_dib + '"' + \
116 ' is an invalid value for vswitch-disable-in-band on ' + \
119 # Change bridge disable-in-band option if XAPI and OVS states differ.
120 elif xapi_dib != ovs_dib:
123 vswitchCfgMod(['--', 'set', 'Bridge', bridge,
124 'other_config:disable-in-band=' + xapi_dib])
127 vswitchCfgMod(['--', 'remove', 'Bridge', bridge,
128 'other_config', 'disable-in-band'])
131 # Change bridge fail_mode if XAPI state differs from OVS state.
132 bridge_fail_mode = vswitchCfgQuery(["get", "Bridge",
133 bridge, "fail_mode"]).strip('[]"')
136 fail_mode = bton[bridge]["other_config"]["vswitch-controller-fail-mode"]
140 if fail_mode not in ['secure', 'standalone']:
141 fail_mode = pool_fail_mode
143 if fail_mode != 'secure':
144 fail_mode = 'standalone'
146 if bridge_fail_mode != fail_mode:
147 vswitchCfgMod(['--', 'set', 'Bridge', bridge,
148 "fail_mode=%s" % fail_mode])
149 fail_mode_changed = True
152 ret_str += "Updated in-band management. "
153 if fail_mode_changed:
154 ret_str += "Updated fail_mode. "
159 return "No change to configuration"
161 def vswitchCurrentController():
162 controller = vswitchCfgQuery(["get-manager"])
165 if len(controller) < 4 or controller[0:4] != "ssl:":
168 return controller.split(':')[1]
170 def removeControllerCfg():
171 vswitchCfgMod(["--", "del-manager",
174 def setControllerCfg(controller):
175 # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
176 # contains both the private key and the certificate.
177 vswitchCfgMod(["--", "del-manager",
179 "--", "--bootstrap", "set-ssl",
180 "/etc/xensource/xapi-ssl.pem",
181 "/etc/xensource/xapi-ssl.pem",
183 "--", "set-manager", 'ssl:' + controller + ':6632'])
185 def vswitchCfgQuery(action_args):
186 cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args
187 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
188 if len(output) == 0 or output[0] == None:
191 output = output[0].strip()
194 def vswitchCfgMod(action_args):
195 cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args
196 exitcode = subprocess.call(cmd)
198 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
199 [ str(exitcode) , str(action_args) ])
201 def emergency_reset(session, args):
202 cmd = [vsctl, "--timeout=5", "emer-reset"]
203 exitcode = subprocess.call(cmd)
205 raise XenAPIPlugin.Failure("VSWITCH_EMER_RESET_FAILURE",
208 return "Successfully reset configuration"
210 if __name__ == "__main__":
211 XenAPIPlugin.dispatch({"update": update,
212 "emergency_reset": emergency_reset})