-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy patheasyovs
145 lines (123 loc) · 4.41 KB
/
easyovs
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
#!/usr/bin/python
__author__ = 'baohua'
import os
from oslo_config import cfg
import sys
import time
from easyovs import VERSION, config
from easyovs.bridge_ctrl import br_addflow, br_delflow, br_dump, br_list, \
br_show
from easyovs.cli import CLI
from easyovs.common import CMDS_ONE, CMDS_TWO, CMDS_OTHER
from easyovs.iptables import show_iptables_rules
from easyovs.log import info, debug, error, output, LEVELS, lg
from easyovs.neutron import show_port_info
from easyovs.util import cleanup, fmt_flow_str
# Fix setuptools' evil madness, and open up (more?) security holes
if 'PYTHONPATH' in os.environ:
sys.path = os.environ['PYTHONPATH'].split(':') + sys.path
class Platform(object):
"""
Build, setup, and run the platform.
"""
def __init__(self):
self.options = None
self.args = None # May be used someday for more CLI scripts
self.parse_args()
self.setup()
self.begin()
def parse_args(self):
config.init(sys.argv[1:])
def setup(self):
"""
Setup and validate environment.
"""
# set logging verbosity
if LEVELS[cfg.CONF.verbosity] > LEVELS['output']:
print ('*** WARNING: selected verbosity level (%s) will hide CLI '
'output!\n'
'Please restart easyOVS with -v [debug, info, output].'
% cfg.CONF.verbosity)
lg.set_log_level(cfg.CONF.verbosity)
info("Set log level to %s\n" % cfg.CONF.verbosity)
def begin(self):
if cfg.CONF.clean:
info("Cleaning environment\n")
cleanup()
exit()
output('EasyOVS %s, type help for information\n' % VERSION)
start = time.time()
cmd = cfg.CONF.cmd
info("cmd = %s\n" % cmd)
if len(cmd.split()) == 1 and cmd in CMDS_ONE:
if cmd == 'cli':
CLI()
elif cmd == 'list':
self.list()
elif len(cmd.split()) == 2:
if cmd.split()[1] in CMDS_TWO: # e.g., br0 show
arg, func = cmd.split()[0], cmd.split()[1]
getattr(self, func)(arg)
elif cmd.split()[0] in CMDS_TWO: # e.g., show br0
func, arg = cmd.split()[0], cmd.split()[1]
getattr(self, func)(arg)
else:
output('Wrong command format is given\n')
elif len(cmd.split()) >= 3:
if cmd.split()[1] in CMDS_OTHER: # e.g., br0 delflow 9
br, func, arg = cmd.split()[0], cmd.split()[1], ' '.join(
cmd.split()[2:])
getattr(self, func)(br, arg.replace(',', ' '))
elif cmd.split()[0] in CMDS_OTHER: # e.g., delflow br0 9
br, func, arg = cmd.split()[1], cmd.split()[0], ' '.join(
cmd.split()[2:])
getattr(self, func)(br, arg.replace(',', ' '))
elif cmd.split()[0] == 'ipt': # e.g., ipt 10.0.0.1, 10.0.0.2
func, arg = cmd.split()[0], ' '.join(cmd.split()[1:])
getattr(self, func)(arg)
else:
output('Wrong command format is given\n')
else:
output('Wrong command format is given\n')
elapsed = float(time.time() - start)
info('\n### Completed in %0.3f seconds ###\n' % elapsed)
@staticmethod
def dump(br):
br_dump(br)
@staticmethod
def ipt(ips):
show_iptables_rules(ips)
@staticmethod
def query(keyword):
show_port_info(keyword)
@staticmethod
def list():
br_list()
@staticmethod
def show(br):
br_show(br)
@staticmethod
def addflow(br, flow_str):
br_addflow(br, fmt_flow_str(flow_str))
@staticmethod
def delflow(br, flow_id):
br_delflow(br, flow_id)
if __name__ == "__main__":
try:
Platform()
except KeyboardInterrupt:
info("\n\nKeyboard Interrupt. Shutting down and cleaning up...\n\n")
cleanup()
except Exception:
# Print exception
type_, val_, trace_ = sys.exc_info()
errorMsg = ("-" * 80 + "\n" +
"Caught exception. Cleaning up...\n\n" +
"%s: %s\n" % (type_.__name__, val_) +
"-" * 80 + "\n")
error(errorMsg)
# Print stack trace to debug log
import traceback
stackTrace = traceback.format_exc()
debug(stackTrace + "\n")
cleanup()