-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAlarm.py
executable file
·79 lines (69 loc) · 2.38 KB
/
OpenAlarm.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
#! /usr/bin/python
from AlarmClock import AlarmClock
from Clock import Clock
from HTTPServer import Server
from daemon import runner
from handlers import AuthHandler, ActionHandler
import config
import os
import sys
class OpenAlarm(object):
def __init__(self, workingDir, dataDir):
# initialize daemon settings
self.stdin_path = '/dev/null'
#self.stdout_path = workingDir + os.sep + 'OpenAlarm.out'
#self.stderr_path = workingDir + os.sep + 'OpenAlarm.err'
self.stdout_path = '/dev/null'
self.stderr_path = '/dev/null'
self.pidfile_path = '/tmp/OpenAlarm.pid'
self.pidfile_timeout = 5
self.data_dir = dataDir + os.sep
self.source_dir = workingDir + os.sep
def run(self):
# try to read config files
[authConfig, alarmsConfig, httpdConfig] = \
config.readConfig(self.data_dir, self.source_dir)
# create the page handler
self.authHandler = AuthHandler(authConfig)
# create action handler
self.actionHandler = ActionHandler(alarmsConfig, self.authHandler)
# create the alarms
self.alarms = {'now': Clock()}
for (name, alarm) in self.actionHandler:
self.alarms[name] = AlarmClock(**alarm)
# create the server
Server._alarms = self.alarms
self.server = Server.startServer(**httpdConfig)
# run the server
self.server.serve_forever()
# main function to execute
def main():
# get directory paths
(source_dir, data_dir) = config.getDirectories()
# create the application
app = OpenAlarm(source_dir, data_dir)
# run the daemon
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
# function to test code
def test():
try:
(source_dir, data_dir) = config.getDirectories()
app = OpenAlarm(source_dir, data_dir)
app.run()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
if len(sys.argv) == 1:
print 'Usage: OpenAlarm run|start|stop|restart'
print ' '
print ' run: Start server in the foreground'
print ' start: Launch the server as a daemon'
print ' stop: Stop a running server daemon'
print ' restart: Restart a running server daemon'
print ' '
else:
if sys.argv[1] == 'run':
test()
else:
main()