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 cfg_mod="/usr/bin/ovs-cfg-mod"
30 vswitchd_cfg_filename="/etc/ovs-vswitchd.conf"
31 cacert_filename="/etc/ovs-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])
50 controller = pool["other_config"]["vSwitchController"]
53 currentController = vswitchCurrentController()
54 if controller == "" and currentController != "":
57 return "Successfully removed controller config"
58 elif controller != currentController:
60 setControllerCfg(controller)
61 return "Successfully set controller to " + controller
63 return "No change to configuration"
65 def vswitchCurrentController():
66 controller = vswitchCfgQuery("mgmt.controller")
69 if len(controller) < 4 or controller[0:4] != "ssl:":
74 def removeControllerCfg():
75 vswitchCfgMod(["--del-match", "mgmt.controller=*",
76 "--del-match", "ssl.bootstrap-ca-cert=*",
77 "--del-match", "ssl.ca-cert=*",
78 "--del-match", "ssl.private-key=*",
79 "--del-match", "ssl.certificate=*"])
81 def setControllerCfg(controller):
82 vswitchCfgMod(["--del-match", "mgmt.controller=*",
83 "--del-match", "ssl.bootstrap-ca-cert=*",
84 "--del-match", "ssl.ca-cert=*",
85 "--del-match", "ssl.private-key=*",
86 "--del-match", "ssl.certificate=*",
87 "-a", "mgmt.controller=ssl:" + controller,
88 "-a", "ssl.bootstrap-ca-cert=true",
89 "-a", "ssl.ca-cert=/etc/ovs-vswitchd.cacert",
90 "-a", "ssl.private-key=/etc/xensource/xapi-ssl.pem",
91 "-a", "ssl.certificate=/etc/xensource/xapi-ssl.pem"])
93 def vswitchCfgQuery(key):
94 cmd = [cfg_mod, "--config-file=" + vswitchd_cfg_filename, "-q", key]
95 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
96 if len(output) == 0 or output[0] == None:
99 output = output[0].strip()
102 def vswitchCfgMod(action_args):
103 cmd = [cfg_mod, "-vANY:console:emer",
104 "--config-file=" + vswitchd_cfg_filename] + action_args
105 exitcode = subprocess.call(cmd)
107 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
108 [ str(exitcode) , str(action_args) ])
112 exitcode = subprocess.call(["/sbin/service", "vswitch", "reload"])
114 raise XenAPIPlugin.Failure("VSWITCH_CFG_RELOAD_FAILURE", [ str(exitcode) ])
117 if __name__ == "__main__":
118 XenAPIPlugin.dispatch({"update": update})