-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathnmcontrol.py
executable file
·131 lines (111 loc) · 3.91 KB
/
nmcontrol.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
#!/usr/bin/python2
__version__ = '0.8'
import os
import sys
import inspect
import optparse
import ConfigParser
app = {}
def main():
# init app config
global app
app['conf'] = ConfigParser.SafeConfigParser()
app['path'] = {}
app['path']['app'] = os.path.dirname(os.path.realpath(__file__)) + os.sep
app['path']['conf'] = app['path']['app'] + os.sep + 'conf' + os.sep
# add import path
sys.path.append(app['path']['app'] + 'lib')
sys.path.append(app['path']['app'] + 'plugin')
sys.path.append(app['path']['app'] + 'service')
import common
common.app = app
import console
(cWidth, cHeight) = console.getTerminalSize()
fmt=optparse.IndentedHelpFormatter(indent_increment=4, max_help_position=40, width=cWidth-3, short_first=1 )
app['parser'] = optparse.OptionParser(formatter=fmt,description='nmcontrol %s' % __version__)
app['debug'] = False
# debug mode
for argv in sys.argv:
if argv in ['--debug=1','--main.debug=1']:
app['debug'] = True
# init modules
import re
import dircache
# init vars and main plugin
app['services'] = {}
app['plugins'] = {}
import pluginMain
app['plugins']['main'] = pluginMain.pluginMain('plugin')
# init service & plugins
for modType in ['service', 'plugin']:
modules = dircache.listdir(modType)
if modType == 'plugin': modules.remove('pluginMain.py')
for module in modules:
if re.match("^"+modType+".*.py$", module):
module = re.sub(r'\.py$', '', module)
modulename = re.sub(r'^'+modType, '', module).lower()
try:
importedModule = __import__(module)
importedClass = getattr(importedModule, module)
app[modType+'s'][importedClass.name] = importedClass(modType)
importedClass.app = app
except Exception as e:
print "Exception when loading "+modType, module, ":", e
# parse command line options
(options, app['args']) = app['parser'].parse_args()
if app['debug']: print "Cmdline args:", app['args']
if app['debug']: print "Cmdline options:", options
for option, value in vars(options).items():
if value is not None:
tmp = option.split('.')
if len(tmp) == 1:
app['plugins']['main'].conf[tmp[0]] = value
else:
module = tmp[0]
tmp.remove(module)
if module in app['plugins']:
app['plugins'][module].conf['.'.join(tmp)] = value
elif module in app['services']:
app['services'][module].conf['.'.join(tmp)] = value
###### Act as client : send rpc request ######
if len(app['args']) > 0 and app['args'][0] != 'start':
error, data = app['plugins']['rpc'].pSend(app['args'][:])
if error is True or data['error'] is True:
print "ERROR:", data
else:
if data['result']['reply'] in [None, True]:
print 'ok'
else:
print data['result']['reply']
if app['debug'] and data['result']['prints']: print "LOG:", data['result']['prints']
if app['args'][0] != 'restart':
return
# daemon mode
if int(app['plugins']['main'].conf['daemon']) == 1:
print "Entering background mode"
import daemonize
retCode = daemonize.createDaemon()
###### Act as server : start plugins ######
plugins_started = []
for plugin in app['plugins']:
if int(app['plugins'][plugin].conf['start']) == 1 and plugin not in ['rpc','main']:
# exit immediatly when main is stopped, unless in debug mode
app['plugins'][plugin].daemon=True
if app['plugins'][plugin].running is False:
app['plugins'][plugin].start()
plugins_started.append(app['plugins'][plugin].name)
print "Plugins started :", ', '.join(plugins_started)
#services_started = []
#for service in app['services']:
# if app['services'][service].running:
# services_started.append(app['services'][service].name)
#print "Services started :", ', '.join(services_started)
# stay there to catch CTRL + C and not exit when in daemon mode
try:
app['plugins']['main'].start2()
except (KeyboardInterrupt, SystemExit):
print '\n! Received keyboard interrupt, quitting threads.\n'
# stop main program
app['plugins']['main'].stop()
if __name__=='__main__':
main()