source: ogClient-Git/src/ogRest.py @ 434bb27

Last change on this file since 434bb27 was 2e80653, checked in by Alvaro Neira Ayuso <aneira@…>, 5 years ago

(Clean-Up) Rename all the functions in ogRest to new ones more clears

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[05b1088]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
[dfc97ff]9import threading
10import platform
11import time
[694bc49]12from enum import Enum
[e20daf6]13import json
14import queue
[d5dca0f]15import sys
16import os
17import signal
[694bc49]18
[e39fe2f]19from src.restRequest import *
[2fa8aa4]20
[dfc97ff]21if platform.system() == 'Linux':
22        from src.linux import ogOperations
23
[7c26c55]24class 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
[0f32b9c]34class restResponse():
[86eb703]35        def __init__(self, response, jsonResp=None):
36                self.msg = ''
[0f32b9c]37                if response == ogResponses.BAD_REQUEST:
[86eb703]38                        self.msg = 'HTTP/1.0 400 Bad Request'
[0f32b9c]39                elif response == ogResponses.IN_PROGRESS:
[86eb703]40                        self.msg = 'HTTP/1.0 202 Accepted'
[0f32b9c]41                elif response == ogResponses.OK:
[86eb703]42                        self.msg = 'HTTP/1.0 200 OK'
[0f32b9c]43                elif response == ogResponses.INTERNAL_ERR:
[86eb703]44                        self.msg = 'HTTP/1.0 500 Internal Server Error'
[d5dca0f]45                elif response == ogResponses.UNAUTHORIZED:
[86eb703]46                        self.msg = 'HTTP/1.0 401 Unauthorized'
[0f32b9c]47                else:
[86eb703]48                        return self.msg
[0f32b9c]49
[86eb703]50                self.msg += '\r\n'
[3a44e48]51
52                if jsonResp:
[86eb703]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()
[3a44e48]56                else:
[86eb703]57                        self.msg += '\r\n'
[0f32b9c]58
[86eb703]59
60        def get(self):
61                return self.msg
[0f32b9c]62
[59a2823]63class ogThread():
[8fc251e]64        def execcmd(client, request, ogRest):
65                if request.getrun() == None:
[86eb703]66                        response = restResponse(ogResponses.BAD_REQUEST)
67                        client.send(response.get())
[230bdca]68                        return
69
70                try:
[8fc251e]71                        shellout = ogOperations.execCMD(request, ogRest)
[230bdca]72                except ValueError as err:
[86eb703]73                        response = restResponse(ogResponses.INTERNAL_ERR)
74                        client.send(response.get())
[230bdca]75                        return
76
[8fc251e]77                if request.getEcho():
[230bdca]78                        jsonResp = jsonResponse()
79                        jsonResp.addElement('out', shellout)
[86eb703]80                        response = restResponse(ogResponses.OK, jsonResp)
81                        client.send(response.get())
[230bdca]82                else:
[86eb703]83                        response = restResponse(ogResponses.OK)
84                        client.send(response.get())
[59a2823]85
[af90350]86        def poweroff():
[59a2823]87                time.sleep(2)
88                ogOperations.poweroff()
89
[af90350]90        def reboot():
[59a2823]91                ogOperations.reboot()
92
[2e80653]93        def session(client, request, ogRest):
[0f32b9c]94                try:
[2e80653]95                        ogOperations.session(request, ogRest)
[0f32b9c]96                except ValueError as err:
[86eb703]97                        response = restResponse(ogResponses.INTERNAL_ERR)
98                        client.send(response.get())
[0f32b9c]99                        return
100
[86eb703]101                response = restResponse(ogResponses.OK)
102                client.send(response.get())
[2fa8aa4]103
[2e80653]104        def software(client, request, path, ogRest):
[683afa6]105                try:
[2e80653]106                        ogOperations.software(request, path, ogRest)
[683afa6]107                except ValueError as err:
[86eb703]108                        response = restResponse(ogResponses.INTERNAL_ERR)
109                        client.send(response.get())
[683afa6]110                        return
111
112                jsonResp = jsonResponse()
[8fc251e]113                jsonResp.addElement('disk', request.getDisk())
114                jsonResp.addElement('partition', request.getPartition())
[683afa6]115
116                f = open(path, "r")
117                lines = f.readlines()
118                f.close()
119                jsonResp.addElement('software', lines[0])
120
[86eb703]121                response = restResponse(ogResponses.OK, jsonResp)
122                client.send(response.get())
[6d1e79b]123
[2e80653]124        def hardware(client, path, ogRest):
[1ced3dd]125                try:
[2e80653]126                        ogOperations.hardware(path, ogRest)
[1ced3dd]127                except ValueError as err:
[86eb703]128                        response = restResponse(ogResponses.INTERNAL_ERR)
129                        client.send(response.get())
[1ced3dd]130                        return
131
132                jsonResp = jsonResponse()
133                f = open(path, "r")
134                lines = f.readlines()
135                f.close()
136                jsonResp.addElement('hardware', lines[0])
[86eb703]137
138                response = restResponse(ogResponses.OK, jsonResp)
139                client.send(response.get())
[261a5ed]140
[2e80653]141        def setup(client, request, ogRest):
[86eb703]142                listconfig = []
143
144                try:
[2e80653]145                        listconfig = ogOperations.setup(request, ogRest)
[86eb703]146                except ValueError as err:
147                        response = restResponse(ogResponses.INTERNAL_ERR)
148                        client.send(response.get())
149                        return
150
[38b57c4]151                jsonResp = jsonResponse()
[8fc251e]152                jsonResp.addElement('disk', request.getDisk())
153                jsonResp.addElement('cache', request.getCache())
154                jsonResp.addElement('cache_size', request.getCacheSize())
[38b57c4]155                jsonResp.addElement('partition_setup', listconfig)
[86eb703]156
157                response = restResponse(ogResponses.OK, jsonResp)
158                client.send(response.get())
[efbe8a7]159
[2e80653]160        def image_restore(client, request, ogRest):
[a306b8b]161                try:
[2e80653]162                        ogOperations.image_restore(request, ogRest)
[a306b8b]163                except ValueError as err:
[86eb703]164                        response = restResponse(ogResponses.INTERNAL_ERR)
165                        client.send(response.get())
[a306b8b]166                        return
167
[86eb703]168                response = restResponse(ogResponses.OK, jsonResp)
169                client.send(response.get())
[cc11d8f]170
[2e80653]171        def image_create(client, path, request, ogRest):
[b2fd0b5]172                try:
[2e80653]173                        ogOperations.image_create(path, request, ogRest)
[b2fd0b5]174                except ValueError as err:
[86eb703]175                        response = restResponse(ogResponses.INTERNAL_ERR)
176                        client.send(response.get())
[b2fd0b5]177                        return
178
[5603a28]179                jsonResp = jsonResponse()
[8fc251e]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())
[b2fd0b5]186                f = open(path, "r")
187                lines = f.readlines()
188                f.close()
189                jsonResp.addElement('software', lines[0])
[86eb703]190
191                response = restResponse(ogResponses.OK, jsonResp)
192                client.send(response.get())
[b2fd0b5]193
[2e80653]194        def refresh(client, ogRest):
[b5e182f]195                try:
[2e80653]196                        out = ogOperations.refresh(ogRest)
[b5e182f]197                except ValueError as err:
[86eb703]198                        response = restResponse(ogResponses.INTERNAL_ERR)
199                        client.send(response.get())
[b5e182f]200                        return
201
202                jsonResp = jsonResponse()
203                jsonResp.addElement('disk', out[0])
204                jsonResp.addElement('partition_setup', out[1])
205
[86eb703]206                response = restResponse(ogResponses.OK, jsonResp)
207                client.send(response.get())
[b5e182f]208
[694bc49]209class ogResponses(Enum):
210        BAD_REQUEST=0
211        IN_PROGRESS=1
212        OK=2
[0f32b9c]213        INTERNAL_ERR=3
[d5dca0f]214        UNAUTHORIZED=4
[694bc49]215
[dfc97ff]216class ogRest():
[d5dca0f]217        def __init__(self):
218                self.proc = None
219                self.terminated = False
220
[8fc251e]221        def processOperation(self, request, client):
222                op = request.getRequestOP()
223                URI = request.getURI()
[d5dca0f]224
225                if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
[86eb703]226                        response = restResponse(ogResponses.UNAUTHORIZED)
227                        client.send(response.get())
[d5dca0f]228                        return
229
[6764fc4]230                if ("GET" in op):
[9c34a8e]231                        if "hardware" in URI:
[261a5ed]232                                self.process_hardware(client)
[9fd8f2d]233                        elif ("run/schedule" in URI):
234                                self.process_schedule(client)
[6764fc4]235                        else:
[86eb703]236                                response = restResponse(ogResponses.BAD_REQUEST)
237                                client.send(response.get())
[6764fc4]238                elif ("POST" in op):
239                        if ("poweroff" in URI):
240                                self.process_poweroff(client)
[9c34a8e]241                        elif "probe" in URI:
242                                self.process_probe(client)
[6764fc4]243                        elif ("reboot" in URI):
244                                self.process_reboot(client)
245                        elif ("shell/run" in URI):
[8fc251e]246                                self.process_shellrun(client, request)
[2fa8aa4]247                        elif ("session" in URI):
[8fc251e]248                                self.process_session(client, request)
[6d1e79b]249                        elif ("software" in URI):
[8fc251e]250                                self.process_software(client, request)
[efbe8a7]251                        elif ("setup" in URI):
[8fc251e]252                                self.process_setup(client, request)
[cc11d8f]253                        elif ("image/restore" in URI):
[2e80653]254                                self.process_imagerestore(client, request)
[d5dca0f]255                        elif ("stop" in URI):
256                                self.process_stop(client)
[b2fd0b5]257                        elif ("image/create" in URI):
[2e80653]258                                self.process_imagecreate(client, request)
[b5e182f]259                        elif ("refresh" in URI):
260                                self.process_refresh(client)
[6764fc4]261                        else:
[86eb703]262                                response = restResponse(ogResponses.BAD_REQUEST)
263                                client.send(response.get())
[dfc97ff]264                else:
[86eb703]265                        response = restResponse(ogResponses.BAD_REQUEST)
266                        client.send(response.get())
[dfc97ff]267
268                return 0
269
270        def process_reboot(self, client):
[86eb703]271                response = restResponse(ogResponses.IN_PROGRESS)
272                client.send(response.get())
273
[dfc97ff]274                client.disconnect()
[af90350]275                threading.Thread(target=ogThread.reboot).start()
[dfc97ff]276
277        def process_poweroff(self, client):
[86eb703]278                response = restResponse(ogResponses.IN_PROGRESS)
279                client.send(response.get())
280
[dfc97ff]281                client.disconnect()
[af90350]282                threading.Thread(target=ogThread.poweroff).start()
[dfc97ff]283
284        def process_probe(self, client):
[336b023]285                jsonResp = jsonResponse()
286                jsonResp.addElement('status', 'OPG')
[86eb703]287
288                response = restResponse(ogResponses.OK, jsonResp)
289                client.send(response.get())
[e20daf6]290
[8fc251e]291        def process_shellrun(self, client, request):
292                threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start()
[2fa8aa4]293
[8fc251e]294        def process_session(self, client, request):
[2e80653]295                threading.Thread(target=ogThread.session, args=(client, request, self,)).start()
[6d1e79b]296
[8fc251e]297        def process_software(self, client, request):
298                path = '/tmp/CSft-' + client.ip + '-' + request.getPartition()
[2e80653]299                threading.Thread(target=ogThread.software, args=(client, request, path, self,)).start()
[261a5ed]300
301        def process_hardware(self, client):
302                path = '/tmp/Chrd-' + client.ip
[2e80653]303                threading.Thread(target=ogThread.hardware, args=(client, path, self,)).start()
[9fd8f2d]304
305        def process_schedule(self, client):
[86eb703]306                response = restResponse(ogResponses.OK)
307                client.send(response.get())
[efbe8a7]308
[8fc251e]309        def process_setup(self, client, request):
[2e80653]310                threading.Thread(target=ogThread.setup, args=(client, request, self,)).start()
[cc11d8f]311
[2e80653]312        def process_imagerestore(self, client, request):
313                threading.Thread(target=ogThread.image_restore, args=(client, request, self,)).start()
[d5dca0f]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)
[b2fd0b5]324
[2e80653]325        def process_imagecreate(self, client, request):
[8fc251e]326                path = '/tmp/CSft-' + client.ip + '-' + request.getPartition()
[2e80653]327                threading.Thread(target=ogThread.image_create, args=(client, path, request, self,)).start()
[b5e182f]328
329        def process_refresh(self, client):
[2e80653]330                threading.Thread(target=ogThread.refresh, args=(client, self,)).start()
Note: See TracBrowser for help on using the repository browser.