source: ogClient-Git/src/ogRest.py @ e96e187

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

Fix parseGetConf(...) configuration parsing

This patch also reformats the response to the /refresh command.

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