xenserver: Use ovs-vsctl for controller configuration in xsconsole
[cascardo/ovs.git] / xenserver / etc_xapi.d_plugins_vswitch-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 file that are managed in the xapi database
5 # when integrated with Citrix management tools.
6
7 # Copyright (C) 2009 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
29 vsctl="/usr/bin/ovs-vsctl"
30 cacert_filename="/etc/ovs-vswitchd.cacert"
31
32 # Delete the CA certificate, so that we go back to boot-strapping mode
33 def delete_cacert():
34     try:
35         os.remove(cacert_filename)
36     except OSError:
37         # Ignore error if file doesn't exist
38         pass
39
40 def update(session, args):
41     pools = session.xenapi.pool.get_all()
42     # We assume there is only ever one pool...
43     if len(pools) == 0:
44         raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
45     if len(pools) > 1:
46         raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
47     pool = session.xenapi.pool.get_record(pools[0])
48     try:
49         controller = pool["other_config"]["vSwitchController"]
50     except KeyError, e:
51         controller = ""
52     currentController = vswitchCurrentController()
53     if controller == "" and currentController != "":
54         delete_cacert()
55         removeControllerCfg()
56         return "Successfully removed controller config"
57     elif controller != currentController:
58         delete_cacert()
59         setControllerCfg(controller)
60         return "Successfully set controller to " + controller
61     else:
62         return "No change to configuration"
63         
64 def vswitchCurrentController():
65     controller = vswitchCfgQuery("get-controller")
66     if controller == "":
67         return controller
68     if len(controller) < 4 or controller[0:4] != "ssl:":
69         return controller
70     else:
71         return controller[4:]
72
73 def removeControllerCfg():
74     vswitchCfgMod(["--", "del-controller",
75                    "--", "del-ssl"])
76                                        
77 def setControllerCfg(controller):
78     vswitchCfgMod(["--", "del-controller",
79                    "--", "del-ssl",
80                    "--", "--bootstrap", "set-ssl",
81                    "/etc/xensource/xapi-ssl.pem",
82                    "/etc/xensource/xapi-ssl.pem",
83                    "/etc/ovs-vswitchd.cacert",
84                    "--", "set-controller", "ssl:" + controller])
85
86 def vswitchCfgQuery(action):
87     cmd = [vsctl, "-vANY:console:emer", action]
88     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
89     if len(output) == 0 or output[0] == None:
90         output = ""
91     else:
92         output = output[0].strip()
93     return output
94
95 def vswitchCfgMod(action_args):
96     cmd = [vsctl, "-vANY:console:emer"] + action_args
97     exitcode = subprocess.call(cmd)
98     if exitcode != 0:
99         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
100                                    [ str(exitcode) , str(action_args) ])
101     
102 if __name__ == "__main__":
103     XenAPIPlugin.dispatch({"update": update})