forked from maartendamen/HouseAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HouseAgent.py
131 lines (103 loc) · 5.2 KB
/
HouseAgent.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
131
from houseagent.core.coordinator import Coordinator
from houseagent.core.events import EventHandler
from houseagent.core.web import Web
from houseagent.core.database import Database
from twisted.internet import reactor
from houseagent.plugins.pluginapi import Logging
import sys
import os
import ConfigParser
if os.name == "nt":
import win32service
import win32serviceutil
import win32event
import win32evtlogutil
class MainWrapper():
'''
This is the main wrapper for HouseAgent, this class takes care of starting all important
core components for HouseAgent such as the event engine, network coordinator etc.
'''
def __init__(self):
from houseagent.utils.generic import get_configurationpath
self.config_path = get_configurationpath()
if os.path.exists(os.path.join(self.config_path, 'HouseAgent.conf')):
config = ConfigParser.RawConfigParser()
config.read(os.path.join(self.config_path, 'HouseAgent.conf'))
self.port = config.getint('webserver', 'port')
self.loglevel = config.get('general', 'loglevel')
# Get broker information (RabbitMQ)
self.broker_host = config.get("broker", "host")
self.broker_port = config.getint("broker", "port")
self.broker_user = config.get("broker", "username")
self.broker_pass = config.get("broker", "password")
self.broker_vhost = config.get("broker", "vhost")
else:
print "Configuration file not found! Make sure the configuration file is placed in the proper directory. For *nix: /etc/HouseAgent/, for Windows C:\Programdata\HouseAgent"
sys.exit()
def start(self):
self.log = Logging("Main")
self.log.set_level(self.loglevel)
self.log.debug("Starting HouseAgent database layer...")
database = Database(self.log)
self.log.debug("Starting HouseAgent coordinator...")
coordinator = Coordinator("houseagent", self.broker_host, self.broker_port, self.broker_user,
self.broker_pass, self.broker_vhost, database=database)
self.log.debug("Starting HouseAgent event handler...")
event_handler = EventHandler(coordinator, database)
self.log.debug("Starting HouseAgent web server...")
Web(self.port, coordinator, event_handler, database)
if os.name == 'nt':
reactor.run(installSignalHandlers=0)
else:
reactor.run()
return True
if os.name == "nt":
class HouseAgentService(win32serviceutil.ServiceFramework):
'''
This class is a Windows Service handler, it's common to run
long running tasks in the background on a Windows system, as such we
use Windows services for HouseAgent
'''
_svc_name_ = "hamain"
_svc_display_name_ = "HouseAgent - Main Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
self.isAlive=True
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
reactor.stop()
win32event.SetEvent(self.hWaitStop)
self.isAlive=False
def SvcDoRun(self):
import servicemanager
win32evtlogutil.ReportEvent(self._svc_name_,servicemanager.PYS_SERVICE_STARTED,0,
servicemanager.EVENTLOG_INFORMATION_TYPE,(self._svc_name_, ''))
self.timeout=1000 # In milliseconds (update every second)
main = MainWrapper()
# Fix working directory, Python Windows service bug
current_dir = os.path.dirname(sys.executable)
os.chdir(current_dir)
if main.start():
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
win32evtlogutil.ReportEvent(self._svc_name_,servicemanager.PYS_SERVICE_STOPPED,0,
servicemanager.EVENTLOG_INFORMATION_TYPE,(self._svc_name_, ''))
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
return
if __name__ == '__main__':
if os.name == "nt":
if len(sys.argv) == 1:
try:
import servicemanager, winerror
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(HouseAgentService)
servicemanager.Initialize('HouseAgentService', evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error, details:
if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(HouseAgentService)
else:
main = MainWrapper()
main.start()