3 # xapi plugin script to update the cache of configuration items in the
4 # ovs-vswitchd configuration file that are managed in the xapi database
5 # when integrated with Citrix management tools.
7 # Copyright (C) 2009 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/ovs-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 . managers").strip('[]"')
68 if len(controller) < 4 or controller[0:4] != "ssl:":
71 return controller.split(':')[1]
73 def removeControllerCfg():
74 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
77 def setControllerCfg(controller):
78 vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
80 "--", "--bootstrap", "set-ssl",
81 "/etc/xensource/xapi-ssl.pem",
82 "/etc/xensource/xapi-ssl.pem",
83 "/etc/ovs-vswitchd.cacert",
84 "--", "set", "Open_vSwitch", ".",
85 'managers="ssl:' + controller + ':6632"'])
87 def vswitchCfgQuery(action):
88 cmd = [vsctl, "-vANY:console:emer", action]
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})