1 | import threading |
---|
2 | import platform |
---|
3 | import time |
---|
4 | from enum import Enum |
---|
5 | import json |
---|
6 | import queue |
---|
7 | import sys |
---|
8 | import os |
---|
9 | import signal |
---|
10 | |
---|
11 | from src.HTTPParser import * |
---|
12 | |
---|
13 | if platform.system() == 'Linux': |
---|
14 | from src.linux import ogOperations |
---|
15 | |
---|
16 | class jsonResponse(): |
---|
17 | def __init__(self): |
---|
18 | self.jsontree = {} |
---|
19 | |
---|
20 | def addElement(self, key, value): |
---|
21 | self.jsontree[key] = value |
---|
22 | |
---|
23 | def dumpMsg(self): |
---|
24 | return json.dumps(self.jsontree) |
---|
25 | |
---|
26 | class restResponse(): |
---|
27 | def getResponse(response, jsonResp=None): |
---|
28 | msg = '' |
---|
29 | if response == ogResponses.BAD_REQUEST: |
---|
30 | msg = 'HTTP/1.0 400 Bad Request' |
---|
31 | elif response == ogResponses.IN_PROGRESS: |
---|
32 | msg = 'HTTP/1.0 202 Accepted' |
---|
33 | elif response == ogResponses.OK: |
---|
34 | msg = 'HTTP/1.0 200 OK' |
---|
35 | elif response == ogResponses.INTERNAL_ERR: |
---|
36 | msg = 'HTTP/1.0 500 Internal Server Error' |
---|
37 | elif response == ogResponses.UNAUTHORIZED: |
---|
38 | msg = 'HTTP/1.0 401 Unauthorized' |
---|
39 | else: |
---|
40 | return msg |
---|
41 | |
---|
42 | msg += '\r\n' |
---|
43 | |
---|
44 | if jsonResp: |
---|
45 | msg += 'Content-Length:' + str(len(jsonResp.dumpMsg())) |
---|
46 | msg += '\r\nContent-Type:application/json' |
---|
47 | msg += '\r\n\r\n' + jsonResp.dumpMsg() |
---|
48 | else: |
---|
49 | msg += '\r\n' |
---|
50 | |
---|
51 | return msg |
---|
52 | |
---|
53 | class ogThread(): |
---|
54 | # Executing cmd thread |
---|
55 | def execcmd(client, httpparser, ogRest): |
---|
56 | if httpparser.getCMD() == None: |
---|
57 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
58 | return |
---|
59 | |
---|
60 | try: |
---|
61 | shellout = ogOperations.execCMD(httpparser, ogRest) |
---|
62 | except ValueError as err: |
---|
63 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
64 | return |
---|
65 | |
---|
66 | if httpparser.getEcho(): |
---|
67 | jsonResp = jsonResponse() |
---|
68 | jsonResp.addElement('out', shellout) |
---|
69 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
70 | else: |
---|
71 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
72 | |
---|
73 | # Powering off thread |
---|
74 | def poweroff(): |
---|
75 | time.sleep(2) |
---|
76 | ogOperations.poweroff() |
---|
77 | |
---|
78 | # Rebooting thread |
---|
79 | def reboot(): |
---|
80 | ogOperations.reboot() |
---|
81 | |
---|
82 | # Process session |
---|
83 | def procsession(client, httpparser, ogRest): |
---|
84 | try: |
---|
85 | ogOperations.procsession(httpparser, ogRest) |
---|
86 | except ValueError as err: |
---|
87 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
88 | return |
---|
89 | |
---|
90 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
91 | |
---|
92 | # Process software |
---|
93 | def procsoftware(client, httpparser, path, ogRest): |
---|
94 | try: |
---|
95 | ogOperations.procsoftware(httpparser, path, ogRest) |
---|
96 | except ValueError as err: |
---|
97 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
98 | return |
---|
99 | |
---|
100 | jsonResp = jsonResponse() |
---|
101 | jsonResp.addElement('disk', httpparser.getDisk()) |
---|
102 | jsonResp.addElement('partition', httpparser.getPartition()) |
---|
103 | |
---|
104 | f = open(path, "r") |
---|
105 | lines = f.readlines() |
---|
106 | f.close() |
---|
107 | jsonResp.addElement('software', lines[0]) |
---|
108 | |
---|
109 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
110 | |
---|
111 | # Process hardware |
---|
112 | def prochardware(client, path, ogRest): |
---|
113 | try: |
---|
114 | ogOperations.prochardware(path, ogRest) |
---|
115 | except ValueError as err: |
---|
116 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
117 | return |
---|
118 | |
---|
119 | jsonResp = jsonResponse() |
---|
120 | f = open(path, "r") |
---|
121 | lines = f.readlines() |
---|
122 | f.close() |
---|
123 | jsonResp.addElement('hardware', lines[0]) |
---|
124 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
125 | |
---|
126 | # Process setup |
---|
127 | def procsetup(client, httpparser, ogRest): |
---|
128 | jsonResp = jsonResponse() |
---|
129 | jsonResp.addElement('disk', httpparser.getDisk()) |
---|
130 | jsonResp.addElement('cache', httpparser.getCache()) |
---|
131 | jsonResp.addElement('cache_size', httpparser.getCacheSize()) |
---|
132 | listconfig = ogOperations.procsetup(httpparser, ogRest) |
---|
133 | jsonResp.addElement('partition_setup', listconfig) |
---|
134 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
135 | |
---|
136 | # Process image restore |
---|
137 | def procirestore(httpparser, ogRest): |
---|
138 | try: |
---|
139 | ogOperations.procirestore(httpparser, ogRest) |
---|
140 | except ValueError as err: |
---|
141 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
142 | return |
---|
143 | |
---|
144 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
145 | |
---|
146 | class ogResponses(Enum): |
---|
147 | BAD_REQUEST=0 |
---|
148 | IN_PROGRESS=1 |
---|
149 | OK=2 |
---|
150 | INTERNAL_ERR=3 |
---|
151 | UNAUTHORIZED=4 |
---|
152 | |
---|
153 | class ogRest(): |
---|
154 | def __init__(self): |
---|
155 | self.proc = None |
---|
156 | self.terminated = False |
---|
157 | |
---|
158 | def processOperation(self, httpparser, client): |
---|
159 | op = httpparser.getRequestOP() |
---|
160 | URI = httpparser.getURI() |
---|
161 | |
---|
162 | if (not "stop" in URI and not self.proc == None and self.proc.poll() == None): |
---|
163 | client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED)) |
---|
164 | return |
---|
165 | |
---|
166 | if ("GET" in op): |
---|
167 | if "hardware" in URI: |
---|
168 | self.process_hardware(client) |
---|
169 | elif ("run/schedule" in URI): |
---|
170 | self.process_schedule(client) |
---|
171 | else: |
---|
172 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
173 | elif ("POST" in op): |
---|
174 | if ("poweroff" in URI): |
---|
175 | self.process_poweroff(client) |
---|
176 | elif "probe" in URI: |
---|
177 | self.process_probe(client) |
---|
178 | elif ("reboot" in URI): |
---|
179 | self.process_reboot(client) |
---|
180 | elif ("shell/run" in URI): |
---|
181 | self.process_shellrun(client, httpparser) |
---|
182 | elif ("session" in URI): |
---|
183 | self.process_session(client, httpparser) |
---|
184 | elif ("software" in URI): |
---|
185 | self.process_software(client, httpparser) |
---|
186 | elif ("setup" in URI): |
---|
187 | self.process_setup(client, httpparser) |
---|
188 | elif ("image/restore" in URI): |
---|
189 | self.process_irestore(client, httpparser) |
---|
190 | elif ("stop" in URI): |
---|
191 | self.process_stop(client) |
---|
192 | else: |
---|
193 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
194 | else: |
---|
195 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
196 | |
---|
197 | return 0 |
---|
198 | |
---|
199 | def process_reboot(self, client): |
---|
200 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
201 | client.disconnect() |
---|
202 | threading.Thread(target=ogThread.reboot).start() |
---|
203 | |
---|
204 | def process_poweroff(self, client): |
---|
205 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
206 | client.disconnect() |
---|
207 | threading.Thread(target=ogThread.poweroff).start() |
---|
208 | |
---|
209 | def process_probe(self, client): |
---|
210 | jsonResp = jsonResponse() |
---|
211 | jsonResp.addElement('status', 'OPG') |
---|
212 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
213 | |
---|
214 | def process_shellrun(self, client, httpparser): |
---|
215 | threading.Thread(target=ogThread.execcmd, args=(client, httpparser, self,)).start() |
---|
216 | |
---|
217 | def process_session(self, client, httpparser): |
---|
218 | threading.Thread(target=ogThread.procsession, args=(client, httpparser, self,)).start() |
---|
219 | |
---|
220 | def process_software(self, client, httpparser): |
---|
221 | path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition() |
---|
222 | threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path, self,)).start() |
---|
223 | |
---|
224 | def process_hardware(self, client): |
---|
225 | path = '/tmp/Chrd-' + client.ip |
---|
226 | threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start() |
---|
227 | |
---|
228 | def process_schedule(self, client): |
---|
229 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
230 | |
---|
231 | def process_setup(self, client, httpparser): |
---|
232 | threading.Thread(target=ogThread.procsetup, args=(client, httpparser, self,)).start() |
---|
233 | |
---|
234 | def process_irestore(self, client, httpparser): |
---|
235 | threading.Thread(target=ogThread.procirestore, args=(client, httpparser, self,)).start() |
---|
236 | |
---|
237 | def process_stop(self, client): |
---|
238 | client.disconnect() |
---|
239 | if self.proc == None: |
---|
240 | return |
---|
241 | |
---|
242 | if self.proc.poll() == None: |
---|
243 | os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM) |
---|
244 | self.terminated = True |
---|
245 | sys.exit(0) |
---|