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 cfg_mod="/usr/bin/ovs-cfg-mod"
17 vswitchd_cfg_filename="/etc/ovs-vswitchd.conf"
19 if __name__ == "__main__":
20 raise Exception("This script is a plugin for xsconsole and cannot run independently")
22 from XSConsoleStandard import *
27 def __init__(self, name, processname=None):
29 self.processname = processname
30 if self.processname == None:
31 self.processname = name
35 output = ShellPipe(["service", self.name, "version"]).Stdout()
36 except StandardError, e:
37 XSLogError("vswitch version retrieval error: " + str(e))
40 if self.processname in line:
41 return line.split()[-1]
46 output = ShellPipe(["service", self.name, "status"]).Stdout()
47 except StandardError, e:
48 XSLogError("vswitch status retrieval error: " + str(e))
53 if self.processname not in line:
55 elif "running" in line:
65 ShellPipe(["service", self.name, "restart"]).Call()
66 except StandardError, e:
67 XSLogError("vswitch restart error: " + str(e))
70 def Inst(cls, name, processname=None):
72 if processname != None:
73 key = key + "-" + processname
74 if name not in cls.service:
75 cls.service[key] = VSwitchService(name, processname)
76 return cls.service[key]
83 output = ShellPipe([cfg_mod, "-vANY:console:emer", "-F",
84 vswitchd_cfg_filename, "-q", key]).Stdout()
85 except StandardError, e:
86 XSLogError("config retrieval error: " + str(e))
92 output = output[0].strip()
96 class VSwitchControllerDialogue(Dialogue):
98 Dialogue.__init__(self)
102 self.hostsUpdated = 0
103 pool = data.GetPoolForThisHost()
105 self.controller = pool.get("vswitch_controller", "")
110 ChoiceDef(Lang("Set pool-wide controller"),
111 lambda: self.getController()),
112 ChoiceDef(Lang("Delete pool-wide controller"),
113 lambda: self.deleteController()),
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 SetController(self, ip):
214 self.hostsUpdated = 0
215 Task.Sync(lambda s: self._modifyPoolConfig(s, ip or ""))
217 def _modifyPoolConfig(self, session, value):
218 """Modify pool configuration.
220 If value == None then delete configuration, otherwise set to value."""
221 pools = session.xenapi.pool.get_all()
222 # We assume there is only ever one pool...
224 XSLogFatal(Lang("No pool found for host."))
227 XSLogFatal(Lang("More than one pool for host."))
229 session.xenapi.pool.set_vswitch_controller(value)
232 class XSFeatureVSwitch:
235 def StatusUpdateHandler(cls, inPane):
238 inPane.AddTitleField(Lang("vSwitch"))
242 inPane.AddStatusField(Lang("Version", 20),
243 VSwitchService.Inst("vswitch", "ovs-vswitchd").version())
247 pool = data.GetPoolForThisHost()
249 dbController = pool.get("vswitch_controller", "")
253 if dbController == "":
254 dbController = Lang("<None>")
255 inPane.AddStatusField(Lang("Controller (config)", 20), dbController)
256 controller = VSwitchConfig.Get("mgmt.controller")
258 controller = Lang("<None>")
259 elif controller[0:4] == "ssl:":
260 controller = controller[4:]
261 inPane.AddStatusField(Lang("Controller (in-use)", 20), controller)
264 inPane.AddStatusField(Lang("ovs-vswitchd status", 20),
265 VSwitchService.Inst("vswitch", "ovs-vswitchd").status())
266 #inPane.AddStatusField(Lang("ovs-brcompatd status", 20),
267 # VSwitchService.Inst("vswitch", "ovs-brcompatd").status())
269 inPane.AddKeyHelpField( {
270 Lang("<Enter>") : Lang("Reconfigure"),
271 Lang("<F5>") : Lang("Refresh")
275 def ActivateHandler(cls):
276 DialogueUtils.AuthenticatedOnly(lambda: Layout.Inst().PushDialogue(VSwitchControllerDialogue()))
279 Importer.RegisterNamedPlugIn(
281 'VSwitch', # Key of this plugin for replacement, etc.
283 'menuname' : 'MENU_NETWORK',
284 'menupriority' : 800,
285 'menutext' : Lang('vSwitch') ,
286 'statusupdatehandler' : self.StatusUpdateHandler,
287 'activatehandler' : self.ActivateHandler
291 # Register this plugin when module is imported, IFF vswitchd is running
292 if os.path.exists('/var/run/ovs-vswitchd.pid'):
293 XSFeatureVSwitch().Register()