1 # Copyright (c) Citrix Systems 2008. All rights reserved.
2 # xsconsole is proprietary software.
4 # Xen, the Xen logo, XenCenter, XenMotion are trademarks or registered
5 # trademarks of Citrix Systems, Inc., in the United States and other
8 # Copyright (c) 2009 Nicira Networks.
10 from XSConsoleLog import *
16 vsctl="/usr/bin/ovs-vsctl"
18 if __name__ == "__main__":
19 raise Exception("This script is a plugin for xsconsole and cannot run independently")
21 from XSConsoleStandard import *
26 def __init__(self, name, processname=None):
28 self.processname = processname
29 if self.processname == None:
30 self.processname = name
34 output = ShellPipe(["service", self.name, "version"]).Stdout()
35 except StandardError, e:
36 XSLogError("vswitch version retrieval error: " + str(e))
39 if self.processname in line:
40 return line.split()[-1]
45 output = ShellPipe(["service", self.name, "status"]).Stdout()
46 except StandardError, e:
47 XSLogError("vswitch status retrieval error: " + str(e))
52 if self.processname not in line:
54 elif "running" in line:
64 ShellPipe(["service", self.name, "restart"]).Call()
65 except StandardError, e:
66 XSLogError("vswitch restart error: " + str(e))
69 def Inst(cls, name, processname=None):
71 if processname != None:
72 key = key + "-" + processname
73 if name not in cls.service:
74 cls.service[key] = VSwitchService(name, processname)
75 return cls.service[key]
82 output = ShellPipe([vsctl, "-vANY:console:emer", action]).Stdout()
83 except StandardError, e:
84 XSLogError("config retrieval error: " + str(e))
90 output = output[0].strip()
94 class VSwitchControllerDialogue(Dialogue):
96 Dialogue.__init__(self)
100 self.hostsUpdated = 0
101 pool = data.GetPoolForThisHost()
103 self.controller = pool.get("other_config", {}).get("vSwitchController", "")
108 ChoiceDef(Lang("Set pool-wide controller"),
109 lambda: self.getController()),
110 ChoiceDef(Lang("Delete pool-wide controller"),
111 lambda: self.deleteController()),
112 ChoiceDef(Lang("Resync server controller config"),
113 lambda: self.syncController()),
114 # ChoiceDef(Lang("Restart ovs-vswitchd"),
115 # lambda: self.restartService("vswitch")),
116 # ChoiceDef(Lang("Restart ovs-brcompatd"),
117 # lambda: self.restartService("vswitch-brcompatd"))
119 self.menu = Menu(self, None, Lang("Configure vSwitch"), choiceDefs)
121 self.ChangeState("INITIAL")
124 pane = self.NewPane(DialoguePane(self.parent))
125 pane.TitleSet(Lang("Configure vSwitch"))
128 def ChangeState(self, inState):
133 def UpdateFields(self):
134 self.Pane().ResetPosition()
135 getattr(self, "UpdateFields" + self.state)() # Dispatch method named 'UpdateFields'+self.state
137 def UpdateFieldsINITIAL(self):
139 pane.AddTitleField(Lang("Select an action"))
140 pane.AddMenuField(self.menu)
141 pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Cancel") } )
143 def UpdateFieldsGETCONTROLLER(self):
147 pane.AddTitleField(Lang("Enter IP address of controller"))
148 pane.AddInputField(Lang("Address", 16), self.controller, "address")
149 pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Exit") } )
150 if pane.CurrentInput() is None:
151 pane.InputIndexSet(0)
153 def HandleKey(self, inKey):
155 if hasattr(self, "HandleKey" + self.state):
156 handled = getattr(self, "HandleKey" + self.state)(inKey)
157 if not handled and inKey == 'KEY_ESCAPE':
158 Layout.Inst().PopDialogue()
162 def HandleKeyINITIAL(self, inKey):
163 return self.menu.HandleKey(inKey)
165 def HandleKeyGETCONTROLLER(self, inKey):
167 if pane.CurrentInput() is None:
168 pane.InputIndexSet(0)
169 if inKey == 'KEY_ENTER':
170 inputValues = pane.GetFieldValues()
171 self.controller = inputValues['address']
172 Layout.Inst().PopDialogue()
174 # Make sure the controller is specified as a valid dotted quad
176 socket.inet_aton(self.controller)
178 Layout.Inst().PushDialogue(InfoDialogue(Lang("Please enter in dotted quad format")))
181 Layout.Inst().TransientBanner(Lang("Setting controller..."))
183 self.SetController(self.controller)
184 Layout.Inst().PushDialogue(InfoDialogue(Lang("Setting controller successful")))
186 Layout.Inst().PushDialogue(InfoDialogue(Lang("Setting controller failed")))
188 self.ChangeState("INITIAL")
191 return pane.CurrentInput().HandleKey(inKey)
193 def restartService(self, name):
194 s = VSwitchService.Inst(name)
196 Layout.Inst().PopDialogue()
198 def getController(self):
199 self.ChangeState("GETCONTROLLER")
200 self.Pane().InputIndexSet(0)
202 def deleteController(self):
204 Layout.Inst().PopDialogue()
205 Layout.Inst().TransientBanner(Lang("Deleting controller..."))
207 self.SetController(None)
208 Layout.Inst().PushDialogue(InfoDialogue(Lang("Controller deletion successful")))
210 Layout.Inst().PushDialogue(InfoDialogue(Lang("Controller deletion failed")))
212 def syncController(self):
213 Layout.Inst().PopDialogue()
214 Layout.Inst().TransientBanner(Lang("Resyncing controller setting..."))
216 Task.Sync(lambda s: self._updateThisServer(s))
217 Layout.Inst().PushDialogue(InfoDialogue(Lang("Resyncing controller config successful")))
219 Layout.Inst().PushDialogue(InfoDialogue(Lang("Resyncing controller config failed")))
221 def SetController(self, ip):
223 self.hostsUpdated = 0
224 Task.Sync(lambda s: self._modifyPoolConfig(s, "vSwitchController", ip))
225 # Should be done asynchronously, maybe with an external script?
226 Task.Sync(lambda s: self._updateActiveServers(s))
228 def _modifyPoolConfig(self, session, key, value):
229 """Modify pool configuration.
231 If value == None then delete key, otherwise set key to value."""
232 pools = session.xenapi.pool.get_all()
233 # We assume there is only ever one pool...
235 log.error("No pool for host.")
236 raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", [])
238 log.error("More than one pool for host.")
239 raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", [])
240 session.xenapi.pool.remove_from_other_config(pools[0], key)
242 session.xenapi.pool.add_to_other_config(pools[0], key, value)
245 def _updateActiveServers(self, session):
246 hosts = session.xenapi.host.get_all()
247 self.hostsUpdated = 0
248 self.hostsInPool = len(hosts)
251 Layout.Inst().TransientBanner("Updating host %d out of %d"
252 % (self.hostsUpdated + 1, self.hostsInPool))
253 session.xenapi.host.call_plugin(host, "vswitch-cfg-update", "update", {})
254 self.hostsUpdated = self.hostsUpdated + 1
256 def _updateThisServer(self, session):
258 host = data.host.opaqueref()
259 session.xenapi.host.call_plugin(host, "vswitch-cfg-update", "update", {})
262 class XSFeatureVSwitch:
265 def StatusUpdateHandler(cls, inPane):
268 inPane.AddTitleField(Lang("vSwitch"))
272 inPane.AddStatusField(Lang("Version", 20),
273 VSwitchService.Inst("vswitch", "ovs-vswitchd").version())
277 pool = data.GetPoolForThisHost()
279 dbController = pool.get("other_config", {}).get("vSwitchController", "")
283 if dbController == "":
284 dbController = Lang("<None>")
285 inPane.AddStatusField(Lang("Controller (config)", 20), dbController)
286 controller = VSwitchConfig.Get("get-controller")
288 controller = Lang("<None>")
289 elif controller[0:4] == "ssl:":
290 controller = controller[4:]
291 inPane.AddStatusField(Lang("Controller (in-use)", 20), controller)
294 inPane.AddStatusField(Lang("ovs-vswitchd status", 20),
295 VSwitchService.Inst("vswitch", "ovs-vswitchd").status())
296 inPane.AddStatusField(Lang("ovs-brcompatd status", 20),
297 VSwitchService.Inst("vswitch", "ovs-brcompatd").status())
299 inPane.AddKeyHelpField( {
300 Lang("<Enter>") : Lang("Reconfigure"),
301 Lang("<F5>") : Lang("Refresh")
305 def ActivateHandler(cls):
306 DialogueUtils.AuthenticatedOnly(lambda: Layout.Inst().PushDialogue(VSwitchControllerDialogue()))
309 Importer.RegisterNamedPlugIn(
311 'VSwitch', # Key of this plugin for replacement, etc.
313 'menuname' : 'MENU_NETWORK',
314 'menupriority' : 800,
315 'menutext' : Lang('vSwitch') ,
316 'statusupdatehandler' : self.StatusUpdateHandler,
317 'activatehandler' : self.ActivateHandler
321 # Register this plugin when module is imported
322 XSFeatureVSwitch().Register()