configure: Silence check for broken strtok_r().
[sliver-openvswitch.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 cfg_mod="/usr/bin/ovs-cfg-mod"
30 vswitchd_cfg_filename="/etc/ovs-vswitchd.conf"
31 cacert_filename="/etc/ovs-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("mgmt.controller")
67     if controller == "":
68         return controller
69     if len(controller) < 4 or controller[0:4] != "ssl:":
70         return controller
71     else:
72         return controller[4:]
73
74 def removeControllerCfg():
75     vswitchCfgMod(["--del-match", "mgmt.controller=*",
76                    "--del-match", "ssl.bootstrap-ca-cert=*",
77                    "--del-match", "ssl.ca-cert=*",
78                    "--del-match", "ssl.private-key=*",
79                    "--del-match", "ssl.certificate=*"])
80                                        
81 def setControllerCfg(controller):
82     vswitchCfgMod(["--del-match", "mgmt.controller=*",
83                    "--del-match", "ssl.bootstrap-ca-cert=*",
84                    "--del-match", "ssl.ca-cert=*",
85                    "--del-match", "ssl.private-key=*",
86                    "--del-match", "ssl.certificate=*",
87                    "-a", "mgmt.controller=ssl:" + controller,
88                    "-a", "ssl.bootstrap-ca-cert=true",
89                    "-a", "ssl.ca-cert=/etc/ovs-vswitchd.cacert",
90                    "-a", "ssl.private-key=/etc/xensource/xapi-ssl.pem",
91                    "-a", "ssl.certificate=/etc/xensource/xapi-ssl.pem"])
92
93 def vswitchCfgQuery(key):
94     cmd = [cfg_mod, "--config-file=" + vswitchd_cfg_filename, "-q", key]
95     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
96     if len(output) == 0 or output[0] == None:
97         output = ""
98     else:
99         output = output[0].strip()
100     return output
101
102 def vswitchCfgMod(action_args):
103     cmd = [cfg_mod, "-vANY:console:emer",
104            "--config-file=" + vswitchd_cfg_filename] + action_args
105     exitcode = subprocess.call(cmd)
106     if exitcode != 0:
107         raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE",
108                                    [ str(exitcode) , str(action_args) ])
109     vswitchReload()
110     
111 def vswitchReload():
112     exitcode = subprocess.call(["/sbin/service", "vswitch", "reload"])
113     if exitcode != 0:
114         raise XenAPIPlugin.Failure("VSWITCH_CFG_RELOAD_FAILURE", [ str(exitcode) ])
115     
116
117 if __name__ == "__main__":
118     XenAPIPlugin.dispatch({"update": update})