1 | # |
---|
2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation, version 3. |
---|
7 | # |
---|
8 | |
---|
9 | import threading |
---|
10 | import platform |
---|
11 | import time |
---|
12 | from enum import Enum |
---|
13 | import json |
---|
14 | import queue |
---|
15 | import sys |
---|
16 | import os |
---|
17 | import signal |
---|
18 | |
---|
19 | from src.restRequest import * |
---|
20 | |
---|
21 | if platform.system() == 'Linux': |
---|
22 | from src.linux import ogOperations |
---|
23 | |
---|
24 | class jsonResponse(): |
---|
25 | def __init__(self): |
---|
26 | self.jsontree = {} |
---|
27 | |
---|
28 | def addElement(self, key, value): |
---|
29 | self.jsontree[key] = value |
---|
30 | |
---|
31 | def dumpMsg(self): |
---|
32 | return json.dumps(self.jsontree) |
---|
33 | |
---|
34 | class restResponse(): |
---|
35 | def __init__(self, response, jsonResp=None): |
---|
36 | self.msg = '' |
---|
37 | if response == ogResponses.BAD_REQUEST: |
---|
38 | self.msg = 'HTTP/1.0 400 Bad Request' |
---|
39 | elif response == ogResponses.IN_PROGRESS: |
---|
40 | self.msg = 'HTTP/1.0 202 Accepted' |
---|
41 | elif response == ogResponses.OK: |
---|
42 | self.msg = 'HTTP/1.0 200 OK' |
---|
43 | elif response == ogResponses.INTERNAL_ERR: |
---|
44 | self.msg = 'HTTP/1.0 500 Internal Server Error' |
---|
45 | elif response == ogResponses.UNAUTHORIZED: |
---|
46 | self.msg = 'HTTP/1.0 401 Unauthorized' |
---|
47 | else: |
---|
48 | return self.msg |
---|
49 | |
---|
50 | self.msg += '\r\n' |
---|
51 | |
---|
52 | if jsonResp: |
---|
53 | self.msg += 'Content-Length:' + str(len(jsonResp.dumpMsg())) |
---|
54 | self.msg += '\r\nContent-Type:application/json' |
---|
55 | self.msg += '\r\n\r\n' + jsonResp.dumpMsg() |
---|
56 | else: |
---|
57 | self.msg += '\r\n' |
---|
58 | |
---|
59 | |
---|
60 | def get(self): |
---|
61 | return self.msg |
---|
62 | |
---|
63 | class ogThread(): |
---|
64 | def execcmd(client, request, ogRest): |
---|
65 | if not request.getrun(): |
---|
66 | response = restResponse(ogResponses.BAD_REQUEST) |
---|
67 | client.send(response.get()) |
---|
68 | return |
---|
69 | |
---|
70 | try: |
---|
71 | shellout = ogOperations.execCMD(request, ogRest) |
---|
72 | except ValueError as err: |
---|
73 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
74 | client.send(response.get()) |
---|
75 | return |
---|
76 | |
---|
77 | if request.getEcho(): |
---|
78 | jsonResp = jsonResponse() |
---|
79 | jsonResp.addElement('out', shellout) |
---|
80 | response = restResponse(ogResponses.OK, jsonResp) |
---|
81 | client.send(response.get()) |
---|
82 | else: |
---|
83 | response = restResponse(ogResponses.OK) |
---|
84 | client.send(response.get()) |
---|
85 | |
---|
86 | def poweroff(): |
---|
87 | time.sleep(2) |
---|
88 | ogOperations.poweroff() |
---|
89 | |
---|
90 | def reboot(): |
---|
91 | ogOperations.reboot() |
---|
92 | |
---|
93 | def session(client, request, ogRest): |
---|
94 | try: |
---|
95 | ogOperations.session(request, ogRest) |
---|
96 | except ValueError as err: |
---|
97 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
98 | client.send(response.get()) |
---|
99 | return |
---|
100 | |
---|
101 | response = restResponse(ogResponses.OK) |
---|
102 | client.send(response.get()) |
---|
103 | |
---|
104 | def software(client, request, path, ogRest): |
---|
105 | try: |
---|
106 | ogOperations.software(request, path, ogRest) |
---|
107 | except ValueError as err: |
---|
108 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
109 | client.send(response.get()) |
---|
110 | return |
---|
111 | |
---|
112 | jsonResp = jsonResponse() |
---|
113 | jsonResp.addElement('disk', request.getDisk()) |
---|
114 | jsonResp.addElement('partition', request.getPartition()) |
---|
115 | |
---|
116 | f = open(path, "r") |
---|
117 | output = f.read() |
---|
118 | f.close() |
---|
119 | jsonResp.addElement('software', output) |
---|
120 | |
---|
121 | response = restResponse(ogResponses.OK, jsonResp) |
---|
122 | client.send(response.get()) |
---|
123 | |
---|
124 | def hardware(client, path, ogRest): |
---|
125 | try: |
---|
126 | ogOperations.hardware(path, ogRest) |
---|
127 | except ValueError as err: |
---|
128 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
129 | client.send(response.get()) |
---|
130 | return |
---|
131 | |
---|
132 | jsonResp = jsonResponse() |
---|
133 | f = open(path, "r") |
---|
134 | text = f.read() |
---|
135 | f.close() |
---|
136 | jsonResp.addElement('hardware', text) |
---|
137 | |
---|
138 | response = restResponse(ogResponses.OK, jsonResp) |
---|
139 | client.send(response.get()) |
---|
140 | |
---|
141 | def setup(client, request, ogRest): |
---|
142 | listconfig = [] |
---|
143 | |
---|
144 | try: |
---|
145 | listconfig = ogOperations.setup(request, ogRest) |
---|
146 | except ValueError as err: |
---|
147 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
148 | client.send(response.get()) |
---|
149 | return |
---|
150 | |
---|
151 | jsonResp = jsonResponse() |
---|
152 | jsonResp.addElement('disk', request.getDisk()) |
---|
153 | jsonResp.addElement('cache', request.getCache()) |
---|
154 | jsonResp.addElement('cache_size', request.getCacheSize()) |
---|
155 | jsonResp.addElement('partition_setup', listconfig) |
---|
156 | |
---|
157 | response = restResponse(ogResponses.OK, jsonResp) |
---|
158 | client.send(response.get()) |
---|
159 | |
---|
160 | def image_restore(client, request, ogRest): |
---|
161 | try: |
---|
162 | ogOperations.image_restore(request, ogRest) |
---|
163 | except ValueError as err: |
---|
164 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
165 | client.send(response.get()) |
---|
166 | return |
---|
167 | |
---|
168 | response = restResponse(ogResponses.OK, jsonResp) |
---|
169 | client.send(response.get()) |
---|
170 | |
---|
171 | def image_create(client, path, request, ogRest): |
---|
172 | try: |
---|
173 | ogOperations.image_create(path, request, ogRest) |
---|
174 | except ValueError as err: |
---|
175 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
176 | client.send(response.get()) |
---|
177 | return |
---|
178 | |
---|
179 | jsonResp = jsonResponse() |
---|
180 | jsonResp.addElement('disk', request.getDisk()) |
---|
181 | jsonResp.addElement('partition', request.getPartition()) |
---|
182 | jsonResp.addElement('code', request.getCode()) |
---|
183 | jsonResp.addElement('id', request.getId()) |
---|
184 | jsonResp.addElement('name', request.getName()) |
---|
185 | jsonResp.addElement('repository', request.getRepo()) |
---|
186 | f = open(path, "r") |
---|
187 | lines = f.readlines() |
---|
188 | f.close() |
---|
189 | jsonResp.addElement('software', lines[0]) |
---|
190 | |
---|
191 | response = restResponse(ogResponses.OK, jsonResp) |
---|
192 | client.send(response.get()) |
---|
193 | |
---|
194 | def refresh(client, ogRest): |
---|
195 | try: |
---|
196 | out = ogOperations.refresh(ogRest) |
---|
197 | except ValueError as err: |
---|
198 | response = restResponse(ogResponses.INTERNAL_ERR) |
---|
199 | client.send(response.get()) |
---|
200 | return |
---|
201 | |
---|
202 | jsonResp = jsonResponse() |
---|
203 | jsonResp.addElement('disk', out[0]) |
---|
204 | jsonResp.addElement('partition_setup', out[1]) |
---|
205 | |
---|
206 | response = restResponse(ogResponses.OK, jsonResp) |
---|
207 | client.send(response.get()) |
---|
208 | |
---|
209 | class ogResponses(Enum): |
---|
210 | BAD_REQUEST=0 |
---|
211 | IN_PROGRESS=1 |
---|
212 | OK=2 |
---|
213 | INTERNAL_ERR=3 |
---|
214 | UNAUTHORIZED=4 |
---|
215 | |
---|
216 | class ogRest(): |
---|
217 | def __init__(self): |
---|
218 | self.proc = None |
---|
219 | self.terminated = False |
---|
220 | |
---|
221 | def processOperation(self, request, client): |
---|
222 | op = request.getRequestOP() |
---|
223 | URI = request.getURI() |
---|
224 | |
---|
225 | if (not "stop" in URI and not self.proc == None and self.proc.poll() == None): |
---|
226 | response = restResponse(ogResponses.UNAUTHORIZED) |
---|
227 | client.send(response.get()) |
---|
228 | return |
---|
229 | |
---|
230 | if ("GET" in op): |
---|
231 | if "hardware" in URI: |
---|
232 | self.process_hardware(client) |
---|
233 | elif ("run/schedule" in URI): |
---|
234 | self.process_schedule(client) |
---|
235 | elif "refresh" in URI: |
---|
236 | self.process_refresh(client) |
---|
237 | else: |
---|
238 | response = restResponse(ogResponses.BAD_REQUEST) |
---|
239 | client.send(response.get()) |
---|
240 | elif ("POST" in op): |
---|
241 | if ("poweroff" in URI): |
---|
242 | self.process_poweroff(client) |
---|
243 | elif "probe" in URI: |
---|
244 | self.process_probe(client) |
---|
245 | elif ("reboot" in URI): |
---|
246 | self.process_reboot(client) |
---|
247 | elif ("shell/run" in URI): |
---|
248 | self.process_shellrun(client, request) |
---|
249 | elif ("session" in URI): |
---|
250 | self.process_session(client, request) |
---|
251 | elif ("software" in URI): |
---|
252 | self.process_software(client, request) |
---|
253 | elif ("setup" in URI): |
---|
254 | self.process_setup(client, request) |
---|
255 | elif ("image/restore" in URI): |
---|
256 | self.process_imagerestore(client, request) |
---|
257 | elif ("stop" in URI): |
---|
258 | self.process_stop(client) |
---|
259 | elif ("image/create" in URI): |
---|
260 | self.process_imagecreate(client, request) |
---|
261 | else: |
---|
262 | response = restResponse(ogResponses.BAD_REQUEST) |
---|
263 | client.send(response.get()) |
---|
264 | else: |
---|
265 | response = restResponse(ogResponses.BAD_REQUEST) |
---|
266 | client.send(response.get()) |
---|
267 | |
---|
268 | return 0 |
---|
269 | |
---|
270 | def process_reboot(self, client): |
---|
271 | response = restResponse(ogResponses.IN_PROGRESS) |
---|
272 | client.send(response.get()) |
---|
273 | |
---|
274 | client.disconnect() |
---|
275 | threading.Thread(target=ogThread.reboot).start() |
---|
276 | |
---|
277 | def process_poweroff(self, client): |
---|
278 | response = restResponse(ogResponses.IN_PROGRESS) |
---|
279 | client.send(response.get()) |
---|
280 | |
---|
281 | client.disconnect() |
---|
282 | threading.Thread(target=ogThread.poweroff).start() |
---|
283 | |
---|
284 | def process_probe(self, client): |
---|
285 | jsonResp = jsonResponse() |
---|
286 | jsonResp.addElement('status', 'OPG') |
---|
287 | |
---|
288 | response = restResponse(ogResponses.OK, jsonResp) |
---|
289 | client.send(response.get()) |
---|
290 | |
---|
291 | def process_shellrun(self, client, request): |
---|
292 | threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start() |
---|
293 | |
---|
294 | def process_session(self, client, request): |
---|
295 | threading.Thread(target=ogThread.session, args=(client, request, self,)).start() |
---|
296 | |
---|
297 | def process_software(self, client, request): |
---|
298 | path = '/tmp/CSft-' + client.ip + '-' + str(request.getPartition()) |
---|
299 | threading.Thread(target=ogThread.software, args=(client, request, path, self,)).start() |
---|
300 | |
---|
301 | def process_hardware(self, client): |
---|
302 | path = '/tmp/Chrd-' + client.ip |
---|
303 | threading.Thread(target=ogThread.hardware, args=(client, path, self,)).start() |
---|
304 | |
---|
305 | def process_schedule(self, client): |
---|
306 | response = restResponse(ogResponses.OK) |
---|
307 | client.send(response.get()) |
---|
308 | |
---|
309 | def process_setup(self, client, request): |
---|
310 | threading.Thread(target=ogThread.setup, args=(client, request, self,)).start() |
---|
311 | |
---|
312 | def process_imagerestore(self, client, request): |
---|
313 | threading.Thread(target=ogThread.image_restore, args=(client, request, self,)).start() |
---|
314 | |
---|
315 | def process_stop(self, client): |
---|
316 | client.disconnect() |
---|
317 | if self.proc == None: |
---|
318 | return |
---|
319 | |
---|
320 | if self.proc.poll() == None: |
---|
321 | os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM) |
---|
322 | self.terminated = True |
---|
323 | sys.exit(0) |
---|
324 | |
---|
325 | def process_imagecreate(self, client, request): |
---|
326 | path = '/tmp/CSft-' + client.ip + '-' + request.getPartition() |
---|
327 | threading.Thread(target=ogThread.image_create, args=(client, path, request, self,)).start() |
---|
328 | |
---|
329 | def process_refresh(self, client): |
---|
330 | threading.Thread(target=ogThread.refresh, args=(client, self,)).start() |
---|