forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenvswitch.py
579 lines (436 loc) · 18.2 KB
/
openvswitch.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import logging, re, os, signal
try:
import autotest.common as common
except ImportError:
import common
from autotest.client import utils, os_dep
from autotest.client.shared import error
from versionable_class import VersionableClass
import utils_misc
class ServiceManagerInterface(VersionableClass):
def __new__(cls, *args, **kargs):
ServiceManagerInterface.master_class = ServiceManagerInterface
return super(ServiceManagerInterface, cls).__new__(cls, *args, **kargs)
@classmethod
def get_version(cls):
"""
Get version of ServiceManager.
@return: Version of ServiceManager.
"""
return open("/proc/1/comm", "r").read().strip()
def stop(self, service_name):
raise NotImplementedError("Method 'stop' must be"
" implemented in child class")
def start(self, service_name):
raise NotImplementedError("Method 'start' must be"
" implemented in child class")
def restart(self, service_name):
raise NotImplementedError("Method 'restart' must be"
" implemented in child class")
def status(self, service_name):
raise NotImplementedError("Method 'status' must be"
" implemented in child class")
class ServiceManagerSystemD(ServiceManagerInterface, VersionableClass):
@classmethod
def is_right_version(cls, version):
if version == "systemd":
return True
return False
def stop(self, service_name):
utils.run("systemctl stop %s.service" % (service_name))
def start(self, service_name):
utils.run("systemctl start %s.service" % (service_name))
def restart(self, service_name):
utils.run("systemctl restart %s.service" % (service_name))
def status(self, service_name):
utils.run("systemctl show %s.service" % (service_name))
class ServiceManagerSysvinit(ServiceManagerInterface, VersionableClass):
@classmethod
def is_right_version(cls, version):
if version == "init":
return True
return False
def stop(self, service_name):
utils.run("/etc/init.d/%s stop" % (service_name))
def start(self, service_name):
utils.run("/etc/init.d/%s start" % (service_name))
def restart(self, service_name):
utils.run("/etc/init.d/%s restart" % (service_name))
class ServiceManager(ServiceManagerInterface):
pass
class OpenVSwitchControl(object):
"""
Class select the best matches control class for installed version
of OpenVSwitch.
OpenVSwtich parameters are described in man ovs-vswitchd.conf.db
"""
def __new__(cls, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
"""
Makes initialization of OpenVSwitch.
@param tmpdir: Tmp directory for save openvswitch test files.
@param db_path: Path of OVS databimpoty ase.
@param db_socket: Path of OVS db socket.
@param db_pidfile: Path of OVS db ovsdb-server pid.
@param ovs_pidfile: Path of OVS ovs-vswitchd pid.
@param install_prefix: Path where is openvswitch installed.
"""
# if path is None set default path.
if not install_prefix:
install_prefix = "/"
if not db_path:
db_path = os.path.join(install_prefix,
"/etc/openvswitch/conf.db")
if not db_socket:
db_socket = os.path.join(install_prefix,
"/var/run/openvswitch/db.sock")
if not db_pidfile:
db_pidfile = os.path.join(install_prefix,
"/var/run/openvswitch/ovsdb-server.pid")
if not ovs_pidfile:
ovs_pidfile = os.path.join(install_prefix,
"/var/run/openvswitch/ovs-vswitchd.pid")
if not dbschema:
dbschema = os.path.join(install_prefix,
"/usr/share/openvswitch/vswitch.ovsschema")
OpenVSwitchControl.install_prefix = install_prefix
OpenVSwitchControl.db_path = db_path
OpenVSwitchControl.db_socket = db_socket
OpenVSwitchControl.db_pidfile = db_pidfile
OpenVSwitchControl.ovs_pidfile = ovs_pidfile
OpenVSwitchControl.dbschema = install_prefix, dbschema
os.environ["PATH"] = (os.path.join(install_prefix, "usr/bin:") +
os.environ["PATH"])
os.environ["PATH"] = (os.path.join(install_prefix, "usr/sbin:") +
os.environ["PATH"])
return super(OpenVSwitchControl, cls).__new__(cls)
@staticmethod
def convert_version_to_int(version):
"""
@param version: (int) Converted from version string 1.4.0 => int 140
"""
if (isinstance(version, int)):
return version
try:
int_ver = int(version.replace(".", ""))
except:
raise error.AutotestError("Wrong version format '%s'" % (version))
return int_ver
@classmethod
def get_version(cls):
"""
Get version of installed OpenVSwtich.
@return: Version of OpenVSwtich.
"""
version = None
try:
result = utils.run(os_dep.command("ovs-vswitchd"),
args=["--version"])
pattern = "ovs-vswitchd \(Open vSwitch\) (.+)"
version = re.search(pattern, result.stdout).group(1)
except error.CmdError:
logging.debug("OpenVSwitch is not available in system.")
return version
def status(self):
raise NotImplementedError()
def add_br(self, br_name):
raise NotImplementedError()
def del_br(self, br_name):
raise NotImplementedError()
def br_exist(self, br_name):
raise NotImplementedError()
def list_br(self):
raise NotImplementedError()
def add_port(self, br_name, port_name):
raise NotImplementedError()
def del_port(self, br_name, port_name):
raise NotImplementedError()
def add_port_tag(self, port_name, tag):
raise NotImplementedError()
def add_port_trunk(self, port_name, trunk):
raise NotImplementedError()
def set_vlanmode(self, port_name, vlan_mode):
raise NotImplementedError()
def check_port_in_br(self, br_name, port_name):
raise NotImplementedError()
class OpenVSwitchControlCli(OpenVSwitchControl, VersionableClass):
"""
Class select the best matches control class for installed version
of OpenVSwitch.
"""
def __new__(cls, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
OpenVSwitchControlCli.master_class = OpenVSwitchControlCli
return super(OpenVSwitchControlCli, cls).__new__(cls, db_path,
db_socket,
db_pidfile,
ovs_pidfile,
dbschema,
install_prefix)
class OpenVSwitchControlDB(OpenVSwitchControl, VersionableClass):
"""
Class select the best matches control class for installed version
of OpenVSwitch.
"""
def __new__(cls, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
OpenVSwitchControlDB.master_class = OpenVSwitchControlDB
return super(OpenVSwitchControlDB, cls).__new__(cls, db_path,
db_socket,
db_pidfile,
ovs_pidfile,
dbschema,
install_prefix)
class OpenVSwitchControlDB_140(OpenVSwitchControlDB, VersionableClass):
"""
Don't use this class directly. This class is automatically selected by
OpenVSwitchControl.
"""
@classmethod
def is_right_version(cls, version):
"""
Check condition for select control class.
@param version: version of OpenVSwtich
"""
if version is not None:
int_ver = cls.convert_version_to_int(version)
if int_ver <= 140:
return True
return False
#TODO: implement database manipulation methods.
class OpenVSwitchControlCli_140(OpenVSwitchControlCli, VersionableClass):
"""
Don't use this class directly. This class is automatically selected by
OpenVSwitchControl.
"""
@classmethod
def is_right_version(cls, version):
"""
Check condition for select control class.
@param version: version of OpenVSwtich
"""
if version is not None:
int_ver = cls.convert_version_to_int(version)
if int_ver <= 140:
return True
return False
def ovs_vsctl(self, parmas, ignore_status=False):
return utils.run(os_dep.command("ovs-vsctl"), timeout=10,
ignore_status=ignore_status, verbose=False,
args=["--db=unix:%s" % (self.db_socket)] + parmas)
def status(self):
return self.ovs_vsctl(["show"]).stdout
def add_br(self, br_name):
self.ovs_vsctl(["add-br", br_name])
def add_fake_br(self, br_name, parent, vlan):
self.ovs_vsctl(["add-br", br_name, parent, vlan])
def del_br(self, br_name):
try:
self.ovs_vsctl(["del-br", br_name])
except error.CmdError, e:
logging.debug(e.result_obj)
raise
def br_exist(self, br_name):
try:
self.ovs_vsctl(["br-exists", br_name])
except error.CmdError, e:
if e.result_obj.exit_status == 2:
return False
else:
raise
return True
def list_br(self):
return self.ovs_vsctl(["list-br"]).stdout.splitlines()
def add_port(self, br_name, port_name):
self.ovs_vsctl(["add-port", br_name, port_name])
def del_port(self, br_name, port_name):
self.ovs_vsctl(["del-port", br_name, port_name])
def add_port_tag(self, port_name, tag):
self.ovs_vsctl(["set", "Port", port_name, "tag=%s" % tag])
def add_port_trunk(self, port_name, trunk):
"""
@param trunk: list of vlans id.
"""
trunk = map(lambda x: str(x), trunk)
trunk = "[" + ",".join(trunk) + "]"
self.ovs_vsctl(["set", "Port", port_name, "trunk=%s" % trunk])
def set_vlanmode(self, port_name, vlan_mode):
self.ovs_vsctl(["set", "Port", port_name, "vlan-mode=%s" % vlan_mode])
def list_ports(self, br_name):
return self.ovs_vsctl(["list-ports", br_name]).stdout.splitlines()
def port_to_br(self, port_name):
"""
Return bridge which contain port.
@param port_name: Name of port.
@return: Bridge name or None if there is no bridge which contain port.
"""
bridge = None
try:
bridge = self.ovs_vsctl(["port-to-br", port_name]).stdout
except error.CmdError, e:
if e.result_obj.exit_status == 1:
pass
return bridge
class OpenVSwitchSystem(OpenVSwitchControlCli, OpenVSwitchControlDB):
"""
OpenVSwtich class.
"""
def __new__(cls, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
return super(OpenVSwitchSystem, cls).__new__(cls, db_path, db_socket,
db_pidfile, ovs_pidfile,
dbschema, install_prefix)
def __init__(self, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
"""
Makes initialization of OpenVSwitch.
@param db_path: Path of OVS database.
@param db_socket: Path of OVS db socket.
@param db_pidfile: Path of OVS db ovsdb-server pid.
@param ovs_pidfile: Path of OVS ovs-vswitchd pid.
@param install_prefix: Path where is openvswitch installed.
"""
super(OpenVSwitchSystem, self).__init__(self, db_path, db_socket,
db_pidfile, ovs_pidfile,
dbschema, install_prefix)
self.cleanup = False
self.pid_files_path = None
def is_installed(self):
"""
Check if OpenVSwitch is already installed in system on default places.
@return: Version of OpenVSwtich.
"""
if self.get_version():
return True
else:
return False
def check_db_daemon(self):
"""
Check if OVS daemon is started correctly.
"""
working = utils_misc.program_is_alive("ovsdb-server", self.pid_files_path)
if not working:
logging.error("OpenVSwitch database daemon with PID in file %s"
" not working.", self.db_pidfile)
return working
def check_switch_daemon(self):
"""
Check if OVS daemon is started correctly.
"""
working = utils_misc.program_is_alive("ovs-vswitchd", self.pid_files_path)
if not working:
logging.error("OpenVSwitch switch daemon with PID in file %s"
" not working.", self.ovs_pidfile)
return working
def check_db_file(self):
"""
Check if db_file exists.
"""
exists = os.path.exists(self.db_path)
if not exists:
logging.error("OpenVSwitch database file %s not exists.",
self.db_path)
return exists
def check_db_socket(self):
"""
Check if db socket exists.
"""
exists = os.path.exists(self.db_socket)
if not exists:
logging.error("OpenVSwitch database socket file %s not exists.",
self.db_socket)
return exists
def check(self):
return (self.check_db_daemon() and self.check_switch_daemon() and
self.check_db_file() and self.check_db_socket())
def init_system(self):
"""
Create new dbfile without any configuration.
"""
sm = ServiceManager()
try:
if utils.load_module("openvswitch"):
sm.restart("openvswitch")
except error.CmdError:
logging.error("Service OpenVSwitch is probably not"
" installed in system.")
raise
self.pid_files_path = "/var/run/openvswitch/"
def clean(self):
"""
Empty cleanup function
"""
pass
class OpenVSwitch(OpenVSwitchSystem):
"""
OpenVSwtich class.
"""
def __new__(cls, tmpdir, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
return super(OpenVSwitch, cls).__new__(cls, db_path, db_socket,
db_pidfile, ovs_pidfile,
dbschema, install_prefix)
def __init__(self, tmpdir, db_path=None, db_socket=None, db_pidfile=None,
ovs_pidfile=None, dbschema=None, install_prefix=None):
"""
Makes initialization of OpenVSwitch.
@param tmpdir: Tmp directory for save openvswitch test files.
@param db_path: Path of OVS database.
@param db_socket: Path of OVS db socket.
@param db_pidfile: Path of OVS db ovsdb-server pid.
@param ovs_pidfile: Path of OVS ovs-vswitchd pid.
@param install_prefix: Path where is openvswitch installed.
"""
super(OpenVSwitch, self).__init__(db_path, db_socket, db_pidfile,
ovs_pidfile, dbschema, install_prefix)
self.tmpdir = "/%s/openvswitch" % (tmpdir)
try:
os.mkdir(self.tmpdir)
except OSError, e:
if e.errno != 17:
raise
def init_db(self):
utils.run(os_dep.command("ovsdb-tool"), timeout=10,
args=["create", self.db_path, self.dbschema])
utils.run(os_dep.command("ovsdb-server"), timeout=10,
args=["--remote=punix:%s" % (self.db_socket),
"--remote=db:Open_vSwitch,manager_options",
"--pidfile=%s" % (self.db_pidfile),
"--detach"])
self.ovs_vsctl(["--no-wait", "init"])
def start_ovs_vswitchd(self):
utils.run(os_dep.command("ovs-vswitchd"), timeout=10,
args=["--detach",
"--pidfile=%s" % (self.ovs_pidfile),
"unix:%s" % (self.db_socket)])
def init_new(self):
"""
Create new dbfile without any configuration.
"""
self.db_path = os.path.join(self.tmpdir, "conf.db")
self.db_socket = os.path.join(self.tmpdir, "db.sock")
self.db_pidfile = utils_misc.get_pid_path("ovsdb-server")
self.ovs_pidfile = utils_misc.get_pid_path("ovs-vswitchd")
self.dbschema = "/usr/share/openvswitch/vswitch.ovsschema"
self.cleanup = True
sm = ServiceManager()
#Stop system openvswitch
try:
sm.stop("openvswitch")
except error.CmdError:
pass
utils.load_module("openvswitch")
self.clean()
if (os.path.exists(self.db_path)):
os.remove(self.db_path)
self.init_db()
self.start_ovs_vswitchd()
def clean(self):
logging.debug("Killall ovsdb-server")
utils.signal_program("ovsdb-server")
if (utils_misc.program_is_alive("ovsdb-server")):
utils.signal_program("ovsdb-server", signal.SIGKILL)
logging.debug("Killall ovs-vswitchd")
utils.signal_program("ovs-vswitchd")
if (utils_misc.program_is_alive("ovs-vswitchd")):
utils.signal_program("ovs-vswitchd", signal.SIGKILL)