-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumbleAutomaton.py
More file actions
executable file
·218 lines (196 loc) · 7.22 KB
/
HumbleAutomaton.py
File metadata and controls
executable file
·218 lines (196 loc) · 7.22 KB
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
#! /usr/bin/python
# python builtin
import logging
import os
import signal
import sys
import time
# add standard library to path
sys.path.append(os.path.abspath(os.path.dirname(__file__)) + os.sep + 'lib')
# custom imports
from daemon import runner
from WorkspaceManager import WorkspaceManager
import Log
import configuration
#import events
import Actions
import modules
import servers
# add workspace and custom library to path
sys.path.append(configuration.conf_directory + os.sep + 'lib')
sys.path.append(configuration.var_directory)
# debugging modules
try:
import pdb
import rlcompleter
DEBUG = True
except ImportError:
DEBUG = False
class app(object):
banner = """
__ __ __ __
/ / / /_ _ ____ ___ / /_ / /__
/ /_/ / / / / __ `__ \/ __ \/ / _ \\
/ __ / /_/ / / / / / / /_/ / / __/
/_/ /_/\__,_/_/ /_/ /_/_.___/_/\___/
___ __ __
/ | __ __/ /_____ ____ ___ ____ / /_____ ____
/ /| |/ / / / __/ __ \/ __ `__ \/ __ `/ __/ __ \/ __ \\
/ ___ / /_/ / /_/ /_/ / / / / / / /_/ / /_/ /_/ / / / /
/_/ |_\__,_/\__/\____/_/ /_/ /_/\__,_/\__/\____/_/ /_/
"""
def __init__(self):
# initialize daemon settings
self.running = True
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/null'
self.stderr_path = '/dev/null'
self.pidfile_path = '/tmp/HumbleAutomaton.pid'
self.pidfile_timeout = 5
self.servers = {'ftp': None, 'www': None}
self.booting = True
def run(self):
# create standard paths
log_path = configuration.var_directory + os.sep + 'automaton.log'
config_path = configuration.conf_directory + 'automaton.conf'
work_path = configuration.var_directory + os.sep + 'workspace'
# create log file
banner = self.banner.split('\n')
self.log = Log.logger(log_path, 1, 1)
self.log.info('Program Started')
for line in banner:
self.log.continuation(line)
# read config files
self.config = configuration.ConfigFile(fname=config_path, log=self.log)
self.log.setLevels(self.config['LogLevel'], self.config['StdoutLevel'])
# create ftp server
self.servers['ftp'] = servers.ftp.HumbleFTPServer(
self,
self.config['FTPHost'],
self.config['AdminUser'],
self.config['AdminPass'],
work_path, self.log)
self.servers['ftp'].serve_on_thread()
# create modules
self.modules = {}
for fullname, settings in self.config['Modules'].iteritems():
try:
names = fullname.split(':')
mod = getattr(modules, names[0])
self.modules[names[1]] = mod.install(log=self.log, **settings)
except AttributeError:
self.log.error('Could not install module (' +
names[0] + ') does not exist.')
except TypeError:
self.log.error('Could not install module (' +
names[0] + ') missing parameters.')
except:
self.log.error('Could not install module (' +
names[0] + ') unkown error.')
else:
self.log.info('Installed module ' +
names[0] + ' as ' + names[1])
# import workspace and setup event handler
self._ws = WorkspaceManager(self.log, work_path)
self.action_handler = \
Actions.actionHandler(self.log, self.config['LocString'])
workspace = self._ws.load()
self.action_handler.scanModule(workspace)
# wait until exit request
self.booting = False
self.log.info('Ready to Run')
while self.running:
try:
time.sleep(100)
except KeyboardInterrupt:
cmd = raw_input('Action (reboot/stop/debug/[none])? ')
if cmd == 'reboot':
self.reset()
elif cmd == 'stop':
self.stop()
elif cmd == 'debug':
self.debug()
else:
pass
self.stop()
def reset(self):
self.log.info('Rebooting Application')
self.booting = True
self.action_handler.clear()
workspace = self._ws.load()
self.action_handler.scanModule(workspace)
self.booting = False
def debug(self):
if DEBUG:
pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
self.log.info('Pausing...')
pdb.set_trace()
self.log.info('Resuming...')
else:
self.log.error('Debug feature requires that the pdb module ' +
'and rlcompleter module to be installed.')
def stop(self, *args, **kwargs):
self.log.info('Quitting Application')
# stop timer
self.action_handler.stop()
# quit modules
for device in self.modules:
self.modules[device].stop()
# close ftp server
self.servers['ftp'].stop()
# wait for threads to quit
for i in xrange(40):
threads_running = []
for mod in self.modules.itervalues():
threads_running.append(mod.isAlive())
threads_running.append(self.servers['ftp'].thread.isAlive())
threads_running.append(self.action_handler.isAlive())
if sum(threads_running) == 0:
break
time.sleep(0.25)
# verify all threads quit
for name, mod in self.modules.iteritems():
if mod.isAlive():
self.log.warning('Could not stop module, ' +
name + '. Forcing Stop.')
if self.servers['ftp'].thread.isAlive():
self.log.warning('FTP thread would not quit. Forcing stop.')
if self.action_handler.isAlive():
self.log.warning('Action Handler could not be quit. Forcing stop.')
# exit
sys.exit(0)
def kill(self, *args, **kwargs):
self.running = False
if __name__ == "__main__":
# set help documentation
help_string = """
Usage: HumbleAutomaton run|start|stop|restart|help
run: Start server in the foreground
start: Launch the server as a daemon
stop: Stop a running server daemon
restart: Restart a running server daemon
help: Display this message
"""
# create the application
global automaton
automaton = app()
signal.signal(signal.SIGTERM, app.kill)
logging.basicConfig(level=logging.CRITICAL)
# store application as module for easy importing
sys.modules['automaton'] = automaton
if len(sys.argv) == 1:
# no arguments supplied
print help_string
elif sys.argv[1] == 'help':
# help docs are requested
print help_string
elif sys.argv[1] == 'run':
# run the app in the foreground
try:
automaton.run()
except KeyboardInterrupt:
pass
else:
# run the app as a daemon
daemon_runner = runner.DaemonRunner(automaton)
daemon_runner.do_action()