| 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.HTTPParser 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 | # Executing cmd thread |
|---|
| 65 | def execcmd(client, request, ogRest): |
|---|
| 66 | if request.getrun() == None: |
|---|
| 67 | response = restResponse(ogResponses.BAD_REQUEST) |
|---|
| 68 | client.send(response.get()) |
|---|
| 69 | return |
|---|
| 70 | |
|---|
| 71 | try: |
|---|
| 72 | shellout = ogOperations.execCMD(request, ogRest) |
|---|
| 73 | except ValueError as err: |
|---|
| 74 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 75 | client.send(response.get()) |
|---|
| 76 | return |
|---|
| 77 | |
|---|
| 78 | if request.getEcho(): |
|---|
| 79 | jsonResp = jsonResponse() |
|---|
| 80 | jsonResp.addElement('out', shellout) |
|---|
| 81 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 82 | client.send(response.get()) |
|---|
| 83 | else: |
|---|
| 84 | response = restResponse(ogResponses.OK) |
|---|
| 85 | client.send(response.get()) |
|---|
| 86 | |
|---|
| 87 | # Powering off thread |
|---|
| 88 | def poweroff(): |
|---|
| 89 | time.sleep(2) |
|---|
| 90 | ogOperations.poweroff() |
|---|
| 91 | |
|---|
| 92 | # Rebooting thread |
|---|
| 93 | def reboot(): |
|---|
| 94 | ogOperations.reboot() |
|---|
| 95 | |
|---|
| 96 | # Process session |
|---|
| 97 | def procsession(client, request, ogRest): |
|---|
| 98 | try: |
|---|
| 99 | ogOperations.procsession(request, ogRest) |
|---|
| 100 | except ValueError as err: |
|---|
| 101 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 102 | client.send(response.get()) |
|---|
| 103 | return |
|---|
| 104 | |
|---|
| 105 | response = restResponse(ogResponses.OK) |
|---|
| 106 | client.send(response.get()) |
|---|
| 107 | |
|---|
| 108 | # Process software |
|---|
| 109 | def procsoftware(client, request, path, ogRest): |
|---|
| 110 | try: |
|---|
| 111 | ogOperations.procsoftware(request, path, ogRest) |
|---|
| 112 | except ValueError as err: |
|---|
| 113 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 114 | client.send(response.get()) |
|---|
| 115 | return |
|---|
| 116 | |
|---|
| 117 | jsonResp = jsonResponse() |
|---|
| 118 | jsonResp.addElement('disk', request.getDisk()) |
|---|
| 119 | jsonResp.addElement('partition', request.getPartition()) |
|---|
| 120 | |
|---|
| 121 | f = open(path, "r") |
|---|
| 122 | lines = f.readlines() |
|---|
| 123 | f.close() |
|---|
| 124 | jsonResp.addElement('software', lines[0]) |
|---|
| 125 | |
|---|
| 126 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 127 | client.send(response.get()) |
|---|
| 128 | |
|---|
| 129 | # Process hardware |
|---|
| 130 | def prochardware(client, path, ogRest): |
|---|
| 131 | try: |
|---|
| 132 | ogOperations.prochardware(path, ogRest) |
|---|
| 133 | except ValueError as err: |
|---|
| 134 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 135 | client.send(response.get()) |
|---|
| 136 | return |
|---|
| 137 | |
|---|
| 138 | jsonResp = jsonResponse() |
|---|
| 139 | f = open(path, "r") |
|---|
| 140 | lines = f.readlines() |
|---|
| 141 | f.close() |
|---|
| 142 | jsonResp.addElement('hardware', lines[0]) |
|---|
| 143 | |
|---|
| 144 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 145 | client.send(response.get()) |
|---|
| 146 | |
|---|
| 147 | # Process setup |
|---|
| 148 | def procsetup(client, request, ogRest): |
|---|
| 149 | listconfig = [] |
|---|
| 150 | |
|---|
| 151 | try: |
|---|
| 152 | listconfig = ogOperations.procsetup(request, ogRest) |
|---|
| 153 | except ValueError as err: |
|---|
| 154 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 155 | client.send(response.get()) |
|---|
| 156 | return |
|---|
| 157 | |
|---|
| 158 | jsonResp = jsonResponse() |
|---|
| 159 | jsonResp.addElement('disk', request.getDisk()) |
|---|
| 160 | jsonResp.addElement('cache', request.getCache()) |
|---|
| 161 | jsonResp.addElement('cache_size', request.getCacheSize()) |
|---|
| 162 | jsonResp.addElement('partition_setup', listconfig) |
|---|
| 163 | |
|---|
| 164 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 165 | client.send(response.get()) |
|---|
| 166 | |
|---|
| 167 | # Process image restore |
|---|
| 168 | def procirestore(client, request, ogRest): |
|---|
| 169 | try: |
|---|
| 170 | ogOperations.procirestore(request, ogRest) |
|---|
| 171 | except ValueError as err: |
|---|
| 172 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 173 | client.send(response.get()) |
|---|
| 174 | return |
|---|
| 175 | |
|---|
| 176 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 177 | client.send(response.get()) |
|---|
| 178 | |
|---|
| 179 | # Process image create |
|---|
| 180 | def procicreate(client, path, request, ogRest): |
|---|
| 181 | try: |
|---|
| 182 | ogOperations.procicreate(path, request, ogRest) |
|---|
| 183 | except ValueError as err: |
|---|
| 184 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 185 | client.send(response.get()) |
|---|
| 186 | return |
|---|
| 187 | |
|---|
| 188 | jsonResp = jsonResponse() |
|---|
| 189 | jsonResp.addElement('disk', request.getDisk()) |
|---|
| 190 | jsonResp.addElement('partition', request.getPartition()) |
|---|
| 191 | jsonResp.addElement('code', request.getCode()) |
|---|
| 192 | jsonResp.addElement('id', request.getId()) |
|---|
| 193 | jsonResp.addElement('name', request.getName()) |
|---|
| 194 | jsonResp.addElement('repository', request.getRepo()) |
|---|
| 195 | f = open(path, "r") |
|---|
| 196 | lines = f.readlines() |
|---|
| 197 | f.close() |
|---|
| 198 | jsonResp.addElement('software', lines[0]) |
|---|
| 199 | |
|---|
| 200 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 201 | client.send(response.get()) |
|---|
| 202 | |
|---|
| 203 | # Process refresh |
|---|
| 204 | def procrefresh(client, ogRest): |
|---|
| 205 | try: |
|---|
| 206 | out = ogOperations.procrefresh(ogRest) |
|---|
| 207 | except ValueError as err: |
|---|
| 208 | response = restResponse(ogResponses.INTERNAL_ERR) |
|---|
| 209 | client.send(response.get()) |
|---|
| 210 | return |
|---|
| 211 | |
|---|
| 212 | jsonResp = jsonResponse() |
|---|
| 213 | jsonResp.addElement('disk', out[0]) |
|---|
| 214 | jsonResp.addElement('partition_setup', out[1]) |
|---|
| 215 | |
|---|
| 216 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 217 | client.send(response.get()) |
|---|
| 218 | |
|---|
| 219 | class ogResponses(Enum): |
|---|
| 220 | BAD_REQUEST=0 |
|---|
| 221 | IN_PROGRESS=1 |
|---|
| 222 | OK=2 |
|---|
| 223 | INTERNAL_ERR=3 |
|---|
| 224 | UNAUTHORIZED=4 |
|---|
| 225 | |
|---|
| 226 | class ogRest(): |
|---|
| 227 | def __init__(self): |
|---|
| 228 | self.proc = None |
|---|
| 229 | self.terminated = False |
|---|
| 230 | |
|---|
| 231 | def processOperation(self, request, client): |
|---|
| 232 | op = request.getRequestOP() |
|---|
| 233 | URI = request.getURI() |
|---|
| 234 | |
|---|
| 235 | if (not "stop" in URI and not self.proc == None and self.proc.poll() == None): |
|---|
| 236 | response = restResponse(ogResponses.UNAUTHORIZED) |
|---|
| 237 | client.send(response.get()) |
|---|
| 238 | return |
|---|
| 239 | |
|---|
| 240 | if ("GET" in op): |
|---|
| 241 | if "hardware" in URI: |
|---|
| 242 | self.process_hardware(client) |
|---|
| 243 | elif ("run/schedule" in URI): |
|---|
| 244 | self.process_schedule(client) |
|---|
| 245 | else: |
|---|
| 246 | response = restResponse(ogResponses.BAD_REQUEST) |
|---|
| 247 | client.send(response.get()) |
|---|
| 248 | elif ("POST" in op): |
|---|
| 249 | if ("poweroff" in URI): |
|---|
| 250 | self.process_poweroff(client) |
|---|
| 251 | elif "probe" in URI: |
|---|
| 252 | self.process_probe(client) |
|---|
| 253 | elif ("reboot" in URI): |
|---|
| 254 | self.process_reboot(client) |
|---|
| 255 | elif ("shell/run" in URI): |
|---|
| 256 | self.process_shellrun(client, request) |
|---|
| 257 | elif ("session" in URI): |
|---|
| 258 | self.process_session(client, request) |
|---|
| 259 | elif ("software" in URI): |
|---|
| 260 | self.process_software(client, request) |
|---|
| 261 | elif ("setup" in URI): |
|---|
| 262 | self.process_setup(client, request) |
|---|
| 263 | elif ("image/restore" in URI): |
|---|
| 264 | self.process_irestore(client, request) |
|---|
| 265 | elif ("stop" in URI): |
|---|
| 266 | self.process_stop(client) |
|---|
| 267 | elif ("image/create" in URI): |
|---|
| 268 | self.process_icreate(client, request) |
|---|
| 269 | elif ("refresh" in URI): |
|---|
| 270 | self.process_refresh(client) |
|---|
| 271 | else: |
|---|
| 272 | response = restResponse(ogResponses.BAD_REQUEST) |
|---|
| 273 | client.send(response.get()) |
|---|
| 274 | else: |
|---|
| 275 | response = restResponse(ogResponses.BAD_REQUEST) |
|---|
| 276 | client.send(response.get()) |
|---|
| 277 | |
|---|
| 278 | return 0 |
|---|
| 279 | |
|---|
| 280 | def process_reboot(self, client): |
|---|
| 281 | response = restResponse(ogResponses.IN_PROGRESS) |
|---|
| 282 | client.send(response.get()) |
|---|
| 283 | |
|---|
| 284 | client.disconnect() |
|---|
| 285 | threading.Thread(target=ogThread.reboot).start() |
|---|
| 286 | |
|---|
| 287 | def process_poweroff(self, client): |
|---|
| 288 | response = restResponse(ogResponses.IN_PROGRESS) |
|---|
| 289 | client.send(response.get()) |
|---|
| 290 | |
|---|
| 291 | client.disconnect() |
|---|
| 292 | threading.Thread(target=ogThread.poweroff).start() |
|---|
| 293 | |
|---|
| 294 | def process_probe(self, client): |
|---|
| 295 | jsonResp = jsonResponse() |
|---|
| 296 | jsonResp.addElement('status', 'OPG') |
|---|
| 297 | |
|---|
| 298 | response = restResponse(ogResponses.OK, jsonResp) |
|---|
| 299 | client.send(response.get()) |
|---|
| 300 | |
|---|
| 301 | def process_shellrun(self, client, request): |
|---|
| 302 | threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start() |
|---|
| 303 | |
|---|
| 304 | def process_session(self, client, request): |
|---|
| 305 | threading.Thread(target=ogThread.procsession, args=(client, request, self,)).start() |
|---|
| 306 | |
|---|
| 307 | def process_software(self, client, request): |
|---|
| 308 | path = '/tmp/CSft-' + client.ip + '-' + request.getPartition() |
|---|
| 309 | threading.Thread(target=ogThread.procsoftware, args=(client, request, path, self,)).start() |
|---|
| 310 | |
|---|
| 311 | def process_hardware(self, client): |
|---|
| 312 | path = '/tmp/Chrd-' + client.ip |
|---|
| 313 | threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start() |
|---|
| 314 | |
|---|
| 315 | def process_schedule(self, client): |
|---|
| 316 | response = restResponse(ogResponses.OK) |
|---|
| 317 | client.send(response.get()) |
|---|
| 318 | |
|---|
| 319 | def process_setup(self, client, request): |
|---|
| 320 | threading.Thread(target=ogThread.procsetup, args=(client, request, self,)).start() |
|---|
| 321 | |
|---|
| 322 | def process_irestore(self, client, request): |
|---|
| 323 | threading.Thread(target=ogThread.procirestore, args=(client, request, self,)).start() |
|---|
| 324 | |
|---|
| 325 | def process_stop(self, client): |
|---|
| 326 | client.disconnect() |
|---|
| 327 | if self.proc == None: |
|---|
| 328 | return |
|---|
| 329 | |
|---|
| 330 | if self.proc.poll() == None: |
|---|
| 331 | os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM) |
|---|
| 332 | self.terminated = True |
|---|
| 333 | sys.exit(0) |
|---|
| 334 | |
|---|
| 335 | def process_icreate(self, client, request): |
|---|
| 336 | path = '/tmp/CSft-' + client.ip + '-' + request.getPartition() |
|---|
| 337 | threading.Thread(target=ogThread.procicreate, args=(client, path, request, self,)).start() |
|---|
| 338 | |
|---|
| 339 | def process_refresh(self, client): |
|---|
| 340 | threading.Thread(target=ogThread.procrefresh, args=(client, self,)).start() |
|---|