-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.py
306 lines (238 loc) · 12.3 KB
/
master.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import win32serviceutil
import win32service
import win32event
import servicemanager
import configparser
import os
import inspect
import logging
from multiprocessing import Process, Pipe, Queue
from logging.handlers import RotatingFileHandler
from db.sqlitemanager import SQLiteManager
from proc.node_listener_process import NodeListenerProcess
from proc.terminal_listener_process import TerminalListenerProcess
from proc.http_listener_process import HttpListenerProcess
import threading
import utils.taskrunner as taskrunner
import utils.script_manager as sm
import time
import utils.logging as logutils
def bootstrapper(wrapper_object, initialization_tuple):
instance = wrapper_object(initialization_tuple)
instance.start()
exit(0)
def pipe_recv_handler(master_process, parent_pipe):
master_process._logger.info("Pipe Recv Handler Spawned. Listening For Messages")
while True:
command = parent_pipe.recv()
master_process._logger.info("Received Command: " + str(command))
message_for = command["to"]
if message_for == "NODE":
master_process.sendMessageToNodeProcess(command)
elif message_for == "TERMINAL":
master_process.sendMessageToTerminalProcess(command)
elif message_for == "HTTP":
master_process.sendMessageToHttpProcess(command)
elif message_for == "MASTER":
answer = master_process.handle_master_requests(command)
# send the answer back wherever it came (most likely the http)
# send answer if it is not None
if answer is not None:
parent_pipe.send(answer)
else:
master_process._logger.warning("Could Not Determine What Message Is For. Can't Forward Appropriatly")
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "VesselService"
_svc_display_name_ = "Vessel Service Engine"
_config = configparser.ConfigParser()
_log_dir = None
_root_dir = None
_role = None
_script_dir = None
_node_process = None
_terminal_process = None
_http_process = None
terminal_parent_pipe = None
node_parent_pipe = None
http_parent_pipe = None
shutdown_occurring = False
shutdown_processing_complete = False
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self._config.read(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+ os.sep + 'conf' + os.sep + 'service.ini')
self._log_dir = self._config["LOGGING"]["log_dir"]
self._root_dir = self._config["DEFAULT"]["root_dir"]
self._script_dir = self._config["DEFAULT"].get("scripts_dir", self._root_dir + "/scripts")
logutils.initialize_all_logging_configuration(self._log_dir)
self._logger = logutils.master_logger
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.shutdown_occurring = True
self._logger.info("Service Is Stopping")
self._logger.info("Fetching All Nodes To Send Restart Requests")
# get list of all the nodes
sql_manager = SQLiteManager(self._config, self._logger)
all_nodes = sql_manager.getAllNodes()
self._logger.info("Nodes Fetched. Parsing")
self._logger.info("Parsing Complete")
self._logger.info("Nodes Fetched. Now Sending")
# send message to each node to disconnect, sleep and then start infinite reconnect attempts
for node in all_nodes:
action = dict()
action['command'] = "SYS"
action['from'] = "MASTER"
action['to'] = "NODE"
action['params'] = "RESTART"
action['rawdata'] = (str(node.guid),)
self.sendMessageToNodeProcess(action)
self._logger.info("Now Waiting For Nodes To Disconnect")
# wait for all the nodes to disconnect via checking the db
while len(sql_manager.getAllNodes()) > 0:
time.sleep(2)
pass
self._logger.info("Parse Complete. Closing Connection")
sql_manager.closeEverything()
# now it is safe to terminate
self.shutdown_processing_complete = True
self._logger.info("Shutdown Processing Completed")
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self._logger.info("Service Is Starting")
self.main()
def handle_master_requests(self, command):
# TODO: This is called if there is an error made by a request sent by master process. If master process
# TODO: makes calls using the pipes, it would be able to wait at the calling code point for the response
# TODO: this may be more ideal ??
if command['command'] == "ERROR":
self._logger.error("Error Command Received")
self._logger.error(command)
return None
if command['command'] == "EXEC" and command['params'] == "SCAN.SCRIPTS":
sqlite_manager = SQLiteManager(self._config, self._logger)
self.catalogue_local_scripts(sqlite_manager)
sqlite_manager.closeEverything()
old_from = command['from']
command['from'] = command['to']
command['to'] = old_from
command['param'] = "SUCCESS"
command['rawdata'] = ""
return command
if command['command'] == "EXEC" and command['params'] == "SCRIPTS.EXECUTE":
sqlite_manager = SQLiteManager(self._config, self._logger)
response = taskrunner.execute_script_on_node(sqlite_manager, command, self._logger)
sqlite_manager.closeEverything()
return response
if command['command'] == "GET" and command['params'] == "PING":
return taskrunner.get_ping_info(command, self._config)
def main(self):
self._logger.info("Service Is Initializing...")
# setup database
sqlite_manager = SQLiteManager(self._config, self._logger)
self._logger.info("Master Role Detected. Setting Up Service For Master Role")
# catalogue all the scripts in the system
self._logger.info("Catalogueing Engines On The System")
sm.catalogue_local_engines(sqlite_manager, self._logger)
self._logger.info("Catalogueing Scripts On The System")
sm.catalogue_local_scripts(sqlite_manager, self._script_dir, self._logger)
# create process for listening for terminal connections
try:
self._logger.info("Now Creating Pipe For Terminal Process")
terminal_parent_pipe, terminal_child_pipe = Pipe()
self.terminal_parent_pipe = terminal_parent_pipe
self._logger.info("Now Creating TerminalListenerProcess Class")
self._logger.info("Now Creating Process With Boostrapper")
self._terminal_process = Process(target=bootstrapper, args=(TerminalListenerProcess, (terminal_child_pipe,
self._config,
logutils.logging_queue)))
self._logger.info("Now Starting Process")
self._terminal_process.start()
self._logger.info("Termina Process Has Started Running")
except Exception as e:
self._logger.exception("An Exception Was Thrown Starting The Node Listener Process")
self._logger.error("Later - An Exception Was Thrown")
return
# create process for listening for node connections
# READ through parent_pipe, WRITE through child_pipe
try:
self._logger.info("Now Creating Pipe For Node Process")
node_parent_pipe, node_child_pipe = Pipe()
self.node_parent_pipe = node_parent_pipe
self._logger.info("Now Creating NodeListenerProcess Class")
#node_listener = NodeListenerProcess(to_parent_pipe, to_child_pipe, self._config)
self._logger.info("Now Creating Process With BootStrapper")
self._node_process = Process(target=bootstrapper, args=(NodeListenerProcess,(node_child_pipe, self._config,
logutils.logging_queue)))
self._logger.info("Now Starting Process")
self._node_process.start()
self._logger.info("Http Process Has Started Running")
except Exception as e:
self._logger.exception("An Exception Was Thrown Starting The Node Listener Process")
self._logger.error("Later - An Exception Was Thrown")
return
# create process for listening for http connections
try:
self._logger.info("Now Creating Pipe For Http Process")
http_parent_pipe, http_child_pipe = Pipe()
self.http_parent_pipe = http_parent_pipe
self._logger.info("Now Creating HttpListenerProcess Class")
self._logger.info("Now Creating Process With Bootstrapper")
self._http_process = Process(target=bootstrapper, args=(HttpListenerProcess, (http_child_pipe, self._config,
logutils.logging_queue)))
self._logger.info("Now Starting Http Process")
self._http_process.start()
self._logger.info("Http Process Has Started Running")
except Exception as e:
self._logger.exception("An Exception Was Thrown Starting The Http Listener Process")
self._logger.error("Later - An Exception Was Thrown")
return
# spawn threads to handle listening for commands from these three sources
self._logger.info("Launching Pipe Listening Thread For Terminal Process")
t_thread = threading.Thread(target=pipe_recv_handler,
args=(self, terminal_parent_pipe))
t_thread.daemon = True
t_thread.start()
self._logger.info("Launching Pipe Listening Thread For Node Process")
n_thread = threading.Thread(target=pipe_recv_handler,
args=(self, node_parent_pipe))
n_thread.daemon = True
n_thread.start()
self._logger.info("Launching Pipe Listening Thread For Http Process")
h_thread = threading.Thread(target=pipe_recv_handler,
args=(self, http_parent_pipe))
h_thread.daemon = True
h_thread.start()
# spawn logging thread
l_thread = logutils.start_logging_thread()
rc = None
while rc != win32event.WAIT_OBJECT_0:
self._logger.info("Service Is Now Running")
# hang for 1 minute or until service is stopped - whichever comes first
rc = win32event.WaitForSingleObject(self.hWaitStop, (1 * 60 * 1000))
self._logger.info("Service Shutdown Detected In Main Loop. Waiting For Shutdown Process To Complete")
# don't terminate processes until all of the shutdown procedure has completed
while not self.shutdown_processing_complete:
# loop until its done
pass
self._logger.info("Shutdown Process Completed. Terminating Other Processes")
# now temrinate processes
self._node_process.terminate()
self._terminal_process.terminate()
self._http_process.terminate()
self._logger.info("Main Loop Termination Completed. Terminating")
def sendMessageToNodeProcess(self, message):
self._logger.info("Sending Message To Node Process")
self.node_parent_pipe.send(message)
def sendMessageToHttpProcess(self, message):
self._logger.info("Sending Message To Http Process")
self.http_parent_pipe.send(message)
def sendMessageToTerminalProcess(self, message):
self._logger.info("Sending Message To Terminal Process")
self.terminal_parent_pipe(message)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)