xenserver: Wire up emergency reset plug-in and call it on manager change
[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     pools = session.xenapi.pool.get_all()
43     # We assume there is only ever one pool...
44     if len(pools) == 0:
45         raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
46     if len(pools) > 1:
47         raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
48     pool = session.xenapi.pool.get_record(pools[0])
49     try:
50         try:
51             controller = pool["vswitch_controller"]
52         except KeyError:
53             # On systems older than XenServer 5.6.0, we needed to store
54             # the key in "other_config".
55             controller = pool["other_config"]["vSwitchController"]
56     except KeyError, e:
57         controller = ""
58     currentController = vswitchCurrentController()
59     if controller == "" and currentController != "":
60         delete_cacert()
61         try:
62             emergency_reset(session, None)
63         except:
64             pass
65         removeControllerCfg()
66         return "Successfully removed controller config"
67     elif controller != currentController:
68         delete_cacert()
69         try:
70             emergency_reset(session, None)
71         except:
72             pass
73         setControllerCfg(controller)
74         return "Successfully set controller to " + controller
75     else:
76         return "No change to configuration"
77
78 def vswitchCurrentController():
79     controller = vswitchCfgQuery(["get", "Open_vSwitch", 
80                                   ".", "managers"]).strip('[]"')
81     if controller == "":
82         return controller
83     if len(controller) < 4 or controller[0:4] != "ssl:":
84         return controller
85     else:
86         return controller.split(':')[1]
87
88 def removeControllerCfg():
89     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
90                    "--", "del-ssl"])
91
92 def setControllerCfg(controller):
93     # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
94     # contains both the private key and the certificate.
95     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
96                    "--", "del-ssl",
97                    "--", "--bootstrap", "set-ssl",
98                    "/etc/xensource/xapi-ssl.pem",
99                    "/etc/xensource/xapi-ssl.pem",
100                    cacert_filename,
101                    "--", "set", "Open_vSwitch", ".",
102                    'managers="ssl:' + controller + ':6632"'])
103
104 def vswitchCfgQuery(action_args):
105     cmd = [vsctl, "-vANY:console:emer"] + action_args
106     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
107     if len(output) == 0 or output[0] == None:
108         output = ""
109     else:
110         output = output[0].strip()
111     return output
112
113 def vswitchCfgMod(action_args):
114     cmd = [vsctl, "-vANY:console:emer"] + action_args
115     exitcode = subprocess.call(cmd)
116     if exitcode != 0:
117         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
118                                    [ str(exitcode) , str(action_args) ])
119
120 def emergency_reset(session, args):
121     cmd = [vsctl, "emer-reset"]
122     exitcode = subprocess.call(cmd)
123     if exitcode != 0:
124         raise XenAPIPlugin.Failure("VSWITCH_EMER_RESET_FAILURE",
125                                    [ str(exitcode) ])
126
127     return "Successfully reset configuration"
128     
129 if __name__ == "__main__":
130     XenAPIPlugin.dispatch({"update": update,
131                            "emergency_reset": emergency_reset})