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-controller")
68 if len(controller) < 4 or controller[0:4] != "ssl:":
73 def removeControllerCfg():
74 vswitchCfgMod(["--", "del-controller",
77 def setControllerCfg(controller):
78 vswitchCfgMod(["--", "del-controller",
80 "--", "--bootstrap", "set-ssl",
81 "/etc/xensource/xapi-ssl.pem",
82 "/etc/xensource/xapi-ssl.pem",
83 "/etc/ovs-vswitchd.cacert",
84 "--", "set-controller", "ssl:" + controller])
86 def vswitchCfgQuery(action):
87 cmd = [vsctl, "-vANY:console:emer", action]
88 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
89 if len(output) == 0 or output[0] == None:
92 output = output[0].strip()
95 def vswitchCfgMod(action_args):
96 cmd = [vsctl, "-vANY:console:emer"] + action_args
97 exitcode = subprocess.call(cmd)
99 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
100 [ str(exitcode) , str(action_args) ])
102 if __name__ == "__main__":
103 XenAPIPlugin.dispatch({"update": update})