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 if controller == "do-not-update":
53 return "XAPI key set to do-not-update"
54 if controller.startswith("for-bridges|"):
55 l = controller.split("|")[1:]
57 xapiNet = session.xenapi.network
58 n, t = netctrl.split("=")
59 blist = xapiNet.get_by_name_label(n)
61 # If there is no bridge for the network, just keep
62 # going so we bring up as much as possible.
65 raise XenAPIPlugin.Failure("TOO_MANY_MATCHING_NETWORKS", [n,blist])
66 b = xapiNet.get_bridge(blist[0])
67 setBrControllerCfg(b, t)
68 return "Completed setting controllers on specific bridges"
69 currentController = vswitchCurrentController()
70 if controller == "" and currentController != "":
73 return "Successfully removed controller config"
74 elif controller != currentController:
76 setControllerCfg(controller)
77 return "Successfully set controller to " + controller
79 return "No change to configuration"
81 def vswitchCurrentController():
82 controller = vswitchCfgQuery("get-controller")
85 if len(controller) < 4 or controller[0:4] != "ssl:":
90 def removeControllerCfg():
91 vswitchCfgMod(["--", "del-controller",
94 def setBrControllerCfg(br, target):
95 # Terrible hack... When this is run at boot the required bridges
96 # may not be present. So, we fork a process for each bridge that
97 # needs to be set which sits around in the background and updates
98 # it when it becomes available, finally timing out after a long
99 # interval if it never becomes available.
101 # The right way to do this is to hook the bridge creation somehow
102 # but I don't believe this is possible in XenServer 5.5 without
103 # either listening to XAPI events or writing it in C code in
120 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
121 if maxfd == resource.RLIM_INFINITY:
123 for fd in range(0, maxfd):
128 os.open("/dev/null", os.O_RDWR)
132 syslog.openlog("vswitch-cfg-update", syslog.LOG_PID)
133 syslog.syslog(syslog.LOG_INFO,
134 "Started background process waiting on bridge %s" % (br,))
142 vswitchCfgMod(["--", "del-controller", br,
143 "--", "set-controller", br, target,
144 "--", "set-fail-mode", br, "secure"])
145 except XenAPIPlugin.Failure, e:
147 syslog.syslog(syslog.LOG_INFO,
148 "Attempt to set br %s controller failed" % (br,))
149 time.sleep(sleep_time)
151 syslog.syslog(syslog.LOG_INFO,
152 "Successfully set br %s controller to %s" % (br, repr(target)))
154 syslog.syslog(syslog.LOG_ERR,
155 "Giving up on setting br %s controller" % (br,))
157 def setControllerCfg(controller):
158 vswitchCfgMod(["--", "del-controller",
160 "--", "--bootstrap", "set-ssl",
161 "/etc/xensource/xapi-ssl.pem",
162 "/etc/xensource/xapi-ssl.pem",
163 "/etc/ovs-vswitchd.cacert",
164 "--", "set-controller", "ssl:" + controller])
166 def vswitchCfgQuery(action):
167 cmd = [vsctl, "-vANY:console:emer", action]
168 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
169 if len(output) == 0 or output[0] == None:
172 output = output[0].strip()
175 def vswitchCfgMod(action_args):
176 cmd = [vsctl, "-vANY:console:emer"] + action_args
177 exitcode = subprocess.call(cmd)
179 raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
180 [ str(exitcode) , str(action_args) ])
182 if __name__ == "__main__":
183 XenAPIPlugin.dispatch({"update": update})