-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy patheasyovs
89 lines (75 loc) · 2.54 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
#!/usr/bin/python
__author__ = 'baohua'
import os
from oslo_config import cfg
import sys
import time
from easyovs import config
from easyovs.cli import CLI
from easyovs.common import CMDS_ONE, CMDS_BR, CMDS_OTHER
from easyovs.log import info, debug, error, LEVELS, lg
from easyovs.util import cleanup
# 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 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()
start = time.time()
cmd = cfg.CONF.cmd
info("Run direct cmd = %s\n" % cmd)
cmd_split = cmd.split()
if len(cmd_split) == 1 and cmd == 'cli':
CLI()
elif cmd_split[0] in CMDS_ONE + CMDS_BR + CMDS_OTHER:
CLI(foreground=False).run(cmd, cfg.CONF.forced)
else:
error('Unknown command, cmd=%s\n' % cmd)
elapsed = float(time.time() - start)
info('\n### Completed in %0.3f seconds ###\n' % elapsed)
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()