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 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 pools = session.xenapi.pool.get_all()
43 # We assume there is only ever one pool...
45 raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
47 raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
48 pool = session.xenapi.pool.get_record(pools[0])
51 controller = pool["vswitch_controller"]
53 # On systems older than XenServer 5.6.0, we needed to store
54 # the key in "other_config".
55 controller = pool["other_config"]["vSwitchController"]
58 currentController = vswitchCurrentController()
59 if controller == "" and currentController != "":
62 emergency_reset(session, None)
66 return "Successfully removed controller config"
67 elif controller != currentController:
70 emergency_reset(session, None)
73 setControllerCfg(controller)
74 return "Successfully set controller to " + controller
76 return "No change to configuration"
78 def vswitchCurrentController():
79 controller = vswitchCfgQuery(["get", "Open_vSwitch",
80 ".", "managers"]).strip('[]"')
83 if len(controller) < 4 or controller[0:4] != "ssl:":
86 return controller.split(':')[1]
88 def removeControllerCfg():
89 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
92 def setControllerCfg(controller):
93 # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
94 # contains both the private key and the certificate.
95 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
97 "--", "--bootstrap", "set-ssl",
98 "/etc/xensource/xapi-ssl.pem",
99 "/etc/xensource/xapi-ssl.pem",
101 "--", "set", "Open_vSwitch", ".",
102 'managers="ssl:' + controller + ':6632"'])
104 def vswitchCfgQuery(action_args):
105 cmd = [vsctl, "-vANY:console:emer"] + action_args
106 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
107 if len(output) == 0 or output[0] == None:
110 output = output[0].strip()
113 def vswitchCfgMod(action_args):
114 cmd = [vsctl, "-vANY:console:emer"] + action_args
115 exitcode = subprocess.call(cmd)
117 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
118 [ str(exitcode) , str(action_args) ])
120 def emergency_reset(session, args):
121 cmd = [vsctl, "emer-reset"]
122 exitcode = subprocess.call(cmd)
124 raise XenAPIPlugin.Failure("VSWITCH_EMER_RESET_FAILURE",
127 return "Successfully reset configuration"
129 if __name__ == "__main__":
130 XenAPIPlugin.dispatch({"update": update,
131 "emergency_reset": emergency_reset})