openvswitch-cfg-update: Fix "set-ssl" command arguments.
[cascardo/ovs.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
29 vsctl="/usr/bin/ovs-vsctl"
30 cacert_filename="/etc/openvswitch/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", "Open_vSwitch", 
66                                   ".", "managers"]).strip('[]"')
67     if controller == "":
68         return controller
69     if len(controller) < 4 or controller[0:4] != "ssl:":
70         return controller
71     else:
72         return controller.split(':')[1]
73
74 def removeControllerCfg():
75     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
76                    "--", "del-ssl"])
77
78 def setControllerCfg(controller):
79     vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers",
80                    "--", "del-ssl",
81                    "--", "--bootstrap", "set-ssl",
82                    "/etc/xensource/xapi-ssl.pem",
83                    "/etc/xensource/xapi-ssl.pem",
84                    cacert_filename,
85                    "--", "set", "Open_vSwitch", ".",
86                    'managers="ssl:' + controller + ':6632"'])
87
88 def vswitchCfgQuery(action_args):
89     cmd = [vsctl, "-vANY:console:emer"] + action_args
90     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
91     if len(output) == 0 or output[0] == None:
92         output = ""
93     else:
94         output = output[0].strip()
95     return output
96
97 def vswitchCfgMod(action_args):
98     cmd = [vsctl, "-vANY:console:emer"] + action_args
99     exitcode = subprocess.call(cmd)
100     if exitcode != 0:
101         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
102                                    [ str(exitcode) , str(action_args) ])
103     
104 if __name__ == "__main__":
105     XenAPIPlugin.dispatch({"update": update})