42bd09a195772a06c3c524d46067397b0d5e961d
[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         removeControllerCfg()
62         return "Successfully removed controller config"
63     elif controller != currentController:
64         delete_cacert()
65         setControllerCfg(controller)
66         return "Successfully set controller to " + controller
67     else:
68         return "No change to configuration"
69
70 def vswitchCurrentController():
71     controller = vswitchCfgQuery(["get", "Open_vSwitch", 
72                                   ".", "managers"]).strip('[]"')
73     if controller == "":
74         return controller
75     if len(controller) < 4 or controller[0:4] != "ssl:":
76         return controller
77     else:
78         return controller.split(':')[1]
79
80 def removeControllerCfg():
81     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
82                    "--", "del-ssl"])
83
84 def setControllerCfg(controller):
85     # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
86     # contains both the private key and the certificate.
87     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
88                    "--", "del-ssl",
89                    "--", "--bootstrap", "set-ssl",
90                    "/etc/xensource/xapi-ssl.pem",
91                    "/etc/xensource/xapi-ssl.pem",
92                    cacert_filename,
93                    "--", "set", "Open_vSwitch", ".",
94                    'managers="ssl:' + controller + ':6632"'])
95
96 def vswitchCfgQuery(action_args):
97     cmd = [vsctl, "-vANY:console:emer"] + action_args
98     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
99     if len(output) == 0 or output[0] == None:
100         output = ""
101     else:
102         output = output[0].strip()
103     return output
104
105 def vswitchCfgMod(action_args):
106     cmd = [vsctl, "-vANY:console:emer"] + action_args
107     exitcode = subprocess.call(cmd)
108     if exitcode != 0:
109         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
110                                    [ str(exitcode) , str(action_args) ])
111
112 def emergency_reset(session, args):
113     # This function is just a place holder for testing until the real
114     # functionality is implemented.
115     syslog.syslog("openvswitch-cfg-update: emergency_reset called")
116     return "Need to implement emergency_reset"
117     
118 if __name__ == "__main__":
119     XenAPIPlugin.dispatch({"update": update,
120                            "emergency_reset": emergency_reset})