summaryrefslogtreecommitdiff
path: root/xenserver/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py
blob: 8946917d526eb43cc295df8bc83f2ec2416bc0d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Copyright (c) 2009,2010,2011,2012,2013 Nicira, Inc.
# Copyright (c) 2007-2011 Citrix Systems Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from XSConsoleLog import *

import os
import socket
import subprocess

vsctl="/usr/bin/ovs-vsctl"

if __name__ == "__main__":
    raise Exception("This script is a plugin for xsconsole and cannot run independently")

from XSConsoleStandard import *

class VSwitchService:
    service = {}

    def __init__(self, name, processname=None):
        self.name = name
        self.processname = processname
        if self.processname == None:
            self.processname = name

    def version(self):
        try:
            output = ShellPipe(["service", self.name, "version"]).Stdout()
        except StandardError as e:
            XSLogError("vswitch version retrieval error: " + str(e))
            return "<unknown>"
        for line in output:
            if self.processname in line:
                return line.split()[-1]
        return "<unknown>"

    def status(self):
        try:
            output = ShellPipe(["service", self.name, "status"]).Stdout()
        except StandardError as e:
            XSLogError("vswitch status retrieval error: " + str(e))
            return "<unknown>"
        if len(output) == 0:
            return "<unknown>"
        for line in output:
            if self.processname not in line:
                continue
            elif "running" in line:
                return "Running"
            elif "stop" in line:
                return "Stopped"
            else:
                return "<unknown>"
        return "<unknown>"

    def restart(self):
        try:
            ShellPipe(["service", self.name, "restart"]).Call()
        except StandardError as e:
            XSLogError("vswitch restart error: " + str(e))

    @classmethod
    def Inst(cls, name, processname=None):
        key = name
        if processname != None:
            key = key + "-" + processname
        if name not in cls.service:
            cls.service[key] = VSwitchService(name, processname)
        return cls.service[key]

class VSwitchConfig:

    @staticmethod
    def Get(action):
        try:
            arg = [vsctl, "-vconsole:off"] + action.split()
            output = ShellPipe(arg).Stdout()
        except StandardError as e:
            XSLogError("config retrieval error: " + str(e))
            return "<unknown>"

        if len(output) == 0:
            output = ""
        else:
            output = output[0].strip()
        return output


class VSwitchControllerDialogue(Dialogue):
    def __init__(self):
        Dialogue.__init__(self)
        data=Data.Inst()

        self.hostsInPool = 0
        self.hostsUpdated = 0
        self.xs_version = data.host.software_version.product_version('')
        pool = data.GetPoolForThisHost()
        if pool is not None:
            self.controller = pool.get("vswitch_controller", "")
        else:
            self.controller = ""

        choiceDefs = [
            ChoiceDef(Lang("Set pool-wide controller"),
                      lambda: self.getController()),
            ChoiceDef(Lang("Delete pool-wide controller"),
                      lambda: self.deleteController()),
            ChoiceDef(Lang("Resync server controller config"),
                      lambda: self.syncController()),
#             ChoiceDef(Lang("Restart ovs-vswitchd"),
#                       lambda: self.restartService("vswitch")),
            ]
        self.menu = Menu(self, None, Lang("Configure Open vSwitch"), choiceDefs)

        self.ChangeState("INITIAL")

    def BuildPane(self):
        pane = self.NewPane(DialoguePane(self.parent))
        pane.TitleSet(Lang("Configure Open vSwitch"))
        pane.AddBox()

    def ChangeState(self, inState):
        self.state = inState
        self.BuildPane()
        self.UpdateFields()

    def UpdateFields(self):
        self.Pane().ResetPosition()
        getattr(self, "UpdateFields" + self.state)() # Dispatch method named 'UpdateFields'+self.state

    def UpdateFieldsINITIAL(self):
        pane = self.Pane()
        pane.AddTitleField(Lang("Select an action"))
        pane.AddMenuField(self.menu)
        pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Cancel") } )

    def UpdateFieldsGETCONTROLLER(self):
        pane = self.Pane()
        pane.ResetFields()

        pane.AddTitleField(Lang("Enter IP address of controller"))
        pane.AddInputField(Lang("Address", 16), self.controller, "address")
        pane.AddKeyHelpField( { Lang("<Enter>") : Lang("OK"), Lang("<Esc>") : Lang("Exit") } )
        if pane.CurrentInput() is None:
            pane.InputIndexSet(0)

    def HandleKey(self, inKey):
        handled = False
        if hasattr(self, "HandleKey" + self.state):
            handled = getattr(self, "HandleKey" + self.state)(inKey)
        if not handled and inKey == 'KEY_ESCAPE':
            Layout.Inst().PopDialogue()
            handled = True
        return handled

    def HandleKeyINITIAL(self, inKey):
        return self.menu.HandleKey(inKey)

    def HandleKeyGETCONTROLLER(self, inKey):
        pane = self.Pane()
        if pane.CurrentInput() is None:
            pane.InputIndexSet(0)
        if inKey == 'KEY_ENTER':
            inputValues = pane.GetFieldValues()
            self.controller = inputValues['address']
            Layout.Inst().PopDialogue()

            # Make sure the controller is specified as a valid dotted quad
            try:
                socket.inet_aton(self.controller)
            except socket.error:
                Layout.Inst().PushDialogue(InfoDialogue(Lang("Please enter in dotted quad format")))
                return True

            Layout.Inst().TransientBanner(Lang("Setting controller..."))
            try:
                self.SetController(self.controller)
                Layout.Inst().PushDialogue(InfoDialogue(Lang("Setting controller successful")))
            except Exception as e:
                Layout.Inst().PushDialogue(InfoDialogue(Lang("Setting controller failed")))

            self.ChangeState("INITIAL")
            return True
        else:
            return pane.CurrentInput().HandleKey(inKey)

    def restartService(self, name):
        s = VSwitchService.Inst(name)
        s.restart()
        Layout.Inst().PopDialogue()

    def getController(self):
        self.ChangeState("GETCONTROLLER")
        self.Pane().InputIndexSet(0)

    def deleteController(self):
        self.controller = ""
        Layout.Inst().PopDialogue()
        Layout.Inst().TransientBanner(Lang("Deleting controller..."))
        try:
            self.SetController(None)
            Layout.Inst().PushDialogue(InfoDialogue(Lang("Controller deletion successful")))
        except Exception as e:
            Layout.Inst().PushDialogue(InfoDialogue(Lang("Controller deletion failed")))

    def syncController(self):
        Layout.Inst().PopDialogue()
        Layout.Inst().TransientBanner(Lang("Resyncing controller setting..."))
        try:
            Task.Sync(lambda s: self._updateThisServer(s))
            Layout.Inst().PushDialogue(InfoDialogue(Lang("Resyncing controller config successful")))
        except Exception as e:
            Layout.Inst().PushDialogue(InfoDialogue(Lang("Resyncing controller config failed")))

    def SetController(self, ip):
        self.hostsInPool = 0
        self.hostsUpdated = 0
        Task.Sync(lambda s: self._modifyPoolConfig(s, ip or ""))
        # Should be done asynchronously, maybe with an external script?
        Task.Sync(lambda s: self._updateActiveServers(s))

    def _modifyPoolConfig(self, session, value):
        """Modify pool configuration.

        If value == "" then delete configuration, otherwise set to value.
        """
        pools = session.xenapi.pool.get_all()
        # We assume there is only ever one pool...
        if len(pools) == 0:
            XSLogFatal(Lang("No pool found for host."))
            return
        if len(pools) > 1:
            XSLogFatal(Lang("More than one pool for host."))
            return
        session.xenapi.pool.set_vswitch_controller(value)
        Data.Inst().Update()

    def _updateActiveServers(self, session):
        hosts = session.xenapi.host.get_all()
        self.hostsUpdated = 0
        self.hostsInPool = len(hosts)
        self.UpdateFields()
        for host in hosts:
            Layout.Inst().TransientBanner("Updating host %d out of %d" 
                    % (self.hostsUpdated + 1, self.hostsInPool))
            session.xenapi.host.call_plugin(host, "openvswitch-cfg-update", "update", {})
            self.hostsUpdated = self.hostsUpdated + 1

    def _updateThisServer(self, session):
        data = Data.Inst()
        host = data.host.opaqueref()
        session.xenapi.host.call_plugin(host, "openvswitch-cfg-update", "update", {})


class XSFeatureVSwitch:

    @classmethod
    def StatusUpdateHandler(cls, inPane):
        data = Data.Inst()
        xs_version = data.host.software_version.product_version('')

        inPane.AddTitleField(Lang("Open vSwitch"))

        inPane.NewLine()

        inPane.AddStatusField(Lang("Version", 20),
                              VSwitchService.Inst("openvswitch", "ovs-vswitchd").version())

        inPane.NewLine()

        pool = data.GetPoolForThisHost()
        if pool is not None:
            dbController = pool.get("vswitch_controller", "")
        else:
            dbController = ""

        if dbController == "":
            dbController = Lang("<None>")
        inPane.AddStatusField(Lang("Controller (config)", 20), dbController)
        controller = VSwitchConfig.Get("get-manager")

        if controller == "":
            controller = Lang("<None>")
        elif controller[0:4] == "ssl:":
            controller = controller.split(':')[1]
        inPane.AddStatusField(Lang("Controller (in-use)", 20), controller)

        inPane.NewLine()
        inPane.AddStatusField(Lang("ovs-vswitchd status", 20),
                              VSwitchService.Inst("openvswitch", "ovs-vswitchd").status())
        inPane.AddStatusField(Lang("ovsdb-server status", 20),
                              VSwitchService.Inst("openvswitch", "ovsdb-server").status())

        inPane.AddKeyHelpField( {
            Lang("<Enter>") : Lang("Reconfigure"),
            Lang("<F5>") : Lang("Refresh")
        })

    @classmethod
    def ActivateHandler(cls):
        DialogueUtils.AuthenticatedOnly(lambda: Layout.Inst().PushDialogue(VSwitchControllerDialogue()))

    def Register(self):
        Importer.RegisterNamedPlugIn(
            self,
            'VSwitch', # Key of this plugin for replacement, etc.
            {
                'menuname' : 'MENU_NETWORK',
                'menupriority' : 800,
                'menutext' : Lang('Open vSwitch') ,
                'statusupdatehandler' : self.StatusUpdateHandler,
                'activatehandler' : self.ActivateHandler
            }
        )

# Register this plugin when module is imported, IFF vswitchd is running
if os.path.exists('/var/run/openvswitch/ovs-vswitchd.pid'):
    XSFeatureVSwitch().Register()