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.
29 vsctl="/usr/bin/ovs-vsctl"
30 cacert_filename="/etc/openvswitch/vswitchd.cacert"
32 # Delete the CA certificate, so that we go back to boot-strapping mode
35 os.remove(cacert_filename)
37 # Ignore error if file doesn't exist
40 def update(session, args):
41 pools = session.xenapi.pool.get_all()
42 # We assume there is only ever one pool...
44 raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
46 raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
47 pool = session.xenapi.pool.get_record(pools[0])
49 controller = pool["other_config"]["vSwitchController"]
52 currentController = vswitchCurrentController()
53 if controller == "" and currentController != "":
56 return "Successfully removed controller config"
57 elif controller != currentController:
59 setControllerCfg(controller)
60 return "Successfully set controller to " + controller
62 return "No change to configuration"
64 def vswitchCurrentController():
65 controller = vswitchCfgQuery(["get", "Open_vSwitch",
66 ".", "managers"]).strip('[]"')
69 if len(controller) < 4 or controller[0:4] != "ssl:":
72 return controller.split(':')[1]
74 def removeControllerCfg():
75 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
78 def setControllerCfg(controller):
79 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
81 "--", "--bootstrap", "set-ssl",
82 "/etc/xensource/xapi-ssl.pem",
84 "--", "set", "Open_vSwitch", ".",
85 'managers="ssl:' + controller + ':6632"'])
87 def vswitchCfgQuery(action_args):
88 cmd = [vsctl, "-vANY:console:emer"] + action_args
89 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
90 if len(output) == 0 or output[0] == None:
93 output = output[0].strip()
96 def vswitchCfgMod(action_args):
97 cmd = [vsctl, "-vANY:console:emer"] + action_args
98 exitcode = subprocess.call(cmd)
100 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
101 [ str(exitcode) , str(action_args) ])
103 if __name__ == "__main__":
104 XenAPIPlugin.dispatch({"update": update})