ovs-vsctl: Remove default timeout.
[sliver-openvswitch.git] / xenserver / etc_xapi.d_plugins_openvswitch-cfg-update
1 #!/usr/bin/env python
2 #
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.
6
7 # Copyright (C) 2009, 2010 Nicira Networks, Inc.
8 #
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:
12 #
13 #     http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20
21 # TBD: - error handling needs to be improved.  Currently this can leave
22 # TBD:   the system in a bad state if anything goes wrong.
23
24 import XenAPIPlugin
25 import XenAPI
26 import os
27 import subprocess
28 import syslog
29
30 vsctl="/usr/bin/ovs-vsctl"
31 cacert_filename="/etc/openvswitch/vswitchd.cacert"
32
33 # Delete the CA certificate, so that we go back to boot-strapping mode
34 def delete_cacert():
35     try:
36         os.remove(cacert_filename)
37     except OSError:
38         # Ignore error if file doesn't exist
39         pass
40
41 def update(session, args):
42     # Refresh bridge network UUIDs in case this host joined or left a pool.
43     script = "/opt/xensource/libexec/interface-reconfigure"
44     try:
45         retval = subprocess.call([script, "rewrite"])
46         if retval != 0:
47             syslog.syslog("%s exited with status %d" % (script, retval))
48     except OSError, e:
49         syslog.syslog("%s: failed to execute (%s)" % (script, e.strerror))
50
51     pools = session.xenapi.pool.get_all()
52     # We assume there is only ever one pool...
53     if len(pools) == 0:
54         raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
55     if len(pools) > 1:
56         raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
57     pool = session.xenapi.pool.get_record(pools[0])
58     try:
59         try:
60             controller = pool["vswitch_controller"]
61         except KeyError:
62             # On systems older than XenServer 5.6.0, we needed to store
63             # the key in "other_config".
64             controller = pool["other_config"]["vSwitchController"]
65     except KeyError, e:
66         controller = ""
67     currentController = vswitchCurrentController()
68     if controller == "" and currentController != "":
69         delete_cacert()
70         try:
71             emergency_reset(session, None)
72         except:
73             pass
74         removeControllerCfg()
75         return "Successfully removed controller config"
76     elif controller != currentController:
77         delete_cacert()
78         try:
79             emergency_reset(session, None)
80         except:
81             pass
82         setControllerCfg(controller)
83         return "Successfully set controller to " + controller
84     else:
85         return "No change to configuration"
86
87 def vswitchCurrentController():
88     controller = vswitchCfgQuery(["get", "Open_vSwitch", 
89                                   ".", "managers"]).strip('[]"')
90     if controller == "":
91         return controller
92     if len(controller) < 4 or controller[0:4] != "ssl:":
93         return controller
94     else:
95         return controller.split(':')[1]
96
97 def removeControllerCfg():
98     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
99                    "--", "del-ssl"])
100
101 def setControllerCfg(controller):
102     # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
103     # contains both the private key and the certificate.
104     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
105                    "--", "del-ssl",
106                    "--", "--bootstrap", "set-ssl",
107                    "/etc/xensource/xapi-ssl.pem",
108                    "/etc/xensource/xapi-ssl.pem",
109                    cacert_filename,
110                    "--", "set", "Open_vSwitch", ".",
111                    'managers="ssl:' + controller + ':6632"'])
112
113 def vswitchCfgQuery(action_args):
114     cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args
115     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
116     if len(output) == 0 or output[0] == None:
117         output = ""
118     else:
119         output = output[0].strip()
120     return output
121
122 def vswitchCfgMod(action_args):
123     cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args
124     exitcode = subprocess.call(cmd)
125     if exitcode != 0:
126         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
127                                    [ str(exitcode) , str(action_args) ])
128
129 def emergency_reset(session, args):
130     cmd = [vsctl, "--timeout=5", "emer-reset"]
131     exitcode = subprocess.call(cmd)
132     if exitcode != 0:
133         raise XenAPIPlugin.Failure("VSWITCH_EMER_RESET_FAILURE",
134                                    [ str(exitcode) ])
135
136     return "Successfully reset configuration"
137     
138 if __name__ == "__main__":
139     XenAPIPlugin.dispatch({"update": update,
140                            "emergency_reset": emergency_reset})