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