source: ogClient-Git/src/ogRest.py @ 86eb703

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

(Clean-Up) Modify restResponse to get the response using constructor and get function

  • Property mode set to 100644
File size: 9.2 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
[2fa8aa4]19from src.HTTPParser import *
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():
64        # Executing cmd thread
[8fc251e]65        def execcmd(client, request, ogRest):
66                if request.getrun() == None:
[86eb703]67                        response = restResponse(ogResponses.BAD_REQUEST)
68                        client.send(response.get())
[230bdca]69                        return
70
71                try:
[8fc251e]72                        shellout = ogOperations.execCMD(request, ogRest)
[230bdca]73                except ValueError as err:
[86eb703]74                        response = restResponse(ogResponses.INTERNAL_ERR)
75                        client.send(response.get())
[230bdca]76                        return
77
[8fc251e]78                if request.getEcho():
[230bdca]79                        jsonResp = jsonResponse()
80                        jsonResp.addElement('out', shellout)
[86eb703]81                        response = restResponse(ogResponses.OK, jsonResp)
82                        client.send(response.get())
[230bdca]83                else:
[86eb703]84                        response = restResponse(ogResponses.OK)
85                        client.send(response.get())
[59a2823]86
87        # Powering off thread
[af90350]88        def poweroff():
[59a2823]89                time.sleep(2)
90                ogOperations.poweroff()
91
92        # Rebooting thread
[af90350]93        def reboot():
[59a2823]94                ogOperations.reboot()
95
[2fa8aa4]96        # Process session
[8fc251e]97        def procsession(client, request, ogRest):
[0f32b9c]98                try:
[8fc251e]99                        ogOperations.procsession(request, ogRest)
[0f32b9c]100                except ValueError as err:
[86eb703]101                        response = restResponse(ogResponses.INTERNAL_ERR)
102                        client.send(response.get())
[0f32b9c]103                        return
104
[86eb703]105                response = restResponse(ogResponses.OK)
106                client.send(response.get())
[2fa8aa4]107
[6d1e79b]108        # Process software
[8fc251e]109        def procsoftware(client, request, path, ogRest):
[683afa6]110                try:
[8fc251e]111                        ogOperations.procsoftware(request, path, ogRest)
[683afa6]112                except ValueError as err:
[86eb703]113                        response = restResponse(ogResponses.INTERNAL_ERR)
114                        client.send(response.get())
[683afa6]115                        return
116
117                jsonResp = jsonResponse()
[8fc251e]118                jsonResp.addElement('disk', request.getDisk())
119                jsonResp.addElement('partition', request.getPartition())
[683afa6]120
121                f = open(path, "r")
122                lines = f.readlines()
123                f.close()
124                jsonResp.addElement('software', lines[0])
125
[86eb703]126                response = restResponse(ogResponses.OK, jsonResp)
127                client.send(response.get())
[6d1e79b]128
[261a5ed]129        # Process hardware
[d5dca0f]130        def prochardware(client, path, ogRest):
[1ced3dd]131                try:
[d5dca0f]132                        ogOperations.prochardware(path, ogRest)
[1ced3dd]133                except ValueError as err:
[86eb703]134                        response = restResponse(ogResponses.INTERNAL_ERR)
135                        client.send(response.get())
[1ced3dd]136                        return
137
138                jsonResp = jsonResponse()
139                f = open(path, "r")
140                lines = f.readlines()
141                f.close()
142                jsonResp.addElement('hardware', lines[0])
[86eb703]143
144                response = restResponse(ogResponses.OK, jsonResp)
145                client.send(response.get())
[261a5ed]146
[efbe8a7]147        # Process setup
[8fc251e]148        def procsetup(client, request, ogRest):
[86eb703]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
[38b57c4]158                jsonResp = jsonResponse()
[8fc251e]159                jsonResp.addElement('disk', request.getDisk())
160                jsonResp.addElement('cache', request.getCache())
161                jsonResp.addElement('cache_size', request.getCacheSize())
[38b57c4]162                jsonResp.addElement('partition_setup', listconfig)
[86eb703]163
164                response = restResponse(ogResponses.OK, jsonResp)
165                client.send(response.get())
[efbe8a7]166
[cc11d8f]167        # Process image restore
[8fc251e]168        def procirestore(client, request, ogRest):
[a306b8b]169                try:
[8fc251e]170                        ogOperations.procirestore(request, ogRest)
[a306b8b]171                except ValueError as err:
[86eb703]172                        response = restResponse(ogResponses.INTERNAL_ERR)
173                        client.send(response.get())
[a306b8b]174                        return
175
[86eb703]176                response = restResponse(ogResponses.OK, jsonResp)
177                client.send(response.get())
[cc11d8f]178
[b2fd0b5]179        # Process image create
[8fc251e]180        def procicreate(client, path, request, ogRest):
[b2fd0b5]181                try:
[8fc251e]182                        ogOperations.procicreate(path, request, ogRest)
[b2fd0b5]183                except ValueError as err:
[86eb703]184                        response = restResponse(ogResponses.INTERNAL_ERR)
185                        client.send(response.get())
[b2fd0b5]186                        return
187
[5603a28]188                jsonResp = jsonResponse()
[8fc251e]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())
[b2fd0b5]195                f = open(path, "r")
196                lines = f.readlines()
197                f.close()
198                jsonResp.addElement('software', lines[0])
[86eb703]199
200                response = restResponse(ogResponses.OK, jsonResp)
201                client.send(response.get())
[b2fd0b5]202
[b5e182f]203        # Process refresh
204        def procrefresh(client, ogRest):
205                try:
206                        out = ogOperations.procrefresh(ogRest)
207                except ValueError as err:
[86eb703]208                        response = restResponse(ogResponses.INTERNAL_ERR)
209                        client.send(response.get())
[b5e182f]210                        return
211
212                jsonResp = jsonResponse()
213                jsonResp.addElement('disk', out[0])
214                jsonResp.addElement('partition_setup', out[1])
215
[86eb703]216                response = restResponse(ogResponses.OK, jsonResp)
217                client.send(response.get())
[b5e182f]218
[694bc49]219class ogResponses(Enum):
220        BAD_REQUEST=0
221        IN_PROGRESS=1
222        OK=2
[0f32b9c]223        INTERNAL_ERR=3
[d5dca0f]224        UNAUTHORIZED=4
[694bc49]225
[dfc97ff]226class ogRest():
[d5dca0f]227        def __init__(self):
228                self.proc = None
229                self.terminated = False
230
[8fc251e]231        def processOperation(self, request, client):
232                op = request.getRequestOP()
233                URI = request.getURI()
[d5dca0f]234
235                if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
[86eb703]236                        response = restResponse(ogResponses.UNAUTHORIZED)
237                        client.send(response.get())
[d5dca0f]238                        return
239
[6764fc4]240                if ("GET" in op):
[9c34a8e]241                        if "hardware" in URI:
[261a5ed]242                                self.process_hardware(client)
[9fd8f2d]243                        elif ("run/schedule" in URI):
244                                self.process_schedule(client)
[6764fc4]245                        else:
[86eb703]246                                response = restResponse(ogResponses.BAD_REQUEST)
247                                client.send(response.get())
[6764fc4]248                elif ("POST" in op):
249                        if ("poweroff" in URI):
250                                self.process_poweroff(client)
[9c34a8e]251                        elif "probe" in URI:
252                                self.process_probe(client)
[6764fc4]253                        elif ("reboot" in URI):
254                                self.process_reboot(client)
255                        elif ("shell/run" in URI):
[8fc251e]256                                self.process_shellrun(client, request)
[2fa8aa4]257                        elif ("session" in URI):
[8fc251e]258                                self.process_session(client, request)
[6d1e79b]259                        elif ("software" in URI):
[8fc251e]260                                self.process_software(client, request)
[efbe8a7]261                        elif ("setup" in URI):
[8fc251e]262                                self.process_setup(client, request)
[cc11d8f]263                        elif ("image/restore" in URI):
[8fc251e]264                                self.process_irestore(client, request)
[d5dca0f]265                        elif ("stop" in URI):
266                                self.process_stop(client)
[b2fd0b5]267                        elif ("image/create" in URI):
[8fc251e]268                                self.process_icreate(client, request)
[b5e182f]269                        elif ("refresh" in URI):
270                                self.process_refresh(client)
[6764fc4]271                        else:
[86eb703]272                                response = restResponse(ogResponses.BAD_REQUEST)
273                                client.send(response.get())
[dfc97ff]274                else:
[86eb703]275                        response = restResponse(ogResponses.BAD_REQUEST)
276                        client.send(response.get())
[dfc97ff]277
278                return 0
279
280        def process_reboot(self, client):
[86eb703]281                response = restResponse(ogResponses.IN_PROGRESS)
282                client.send(response.get())
283
[dfc97ff]284                client.disconnect()
[af90350]285                threading.Thread(target=ogThread.reboot).start()
[dfc97ff]286
287        def process_poweroff(self, client):
[86eb703]288                response = restResponse(ogResponses.IN_PROGRESS)
289                client.send(response.get())
290
[dfc97ff]291                client.disconnect()
[af90350]292                threading.Thread(target=ogThread.poweroff).start()
[dfc97ff]293
294        def process_probe(self, client):
[336b023]295                jsonResp = jsonResponse()
296                jsonResp.addElement('status', 'OPG')
[86eb703]297
298                response = restResponse(ogResponses.OK, jsonResp)
299                client.send(response.get())
[e20daf6]300
[8fc251e]301        def process_shellrun(self, client, request):
302                threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start()
[2fa8aa4]303
[8fc251e]304        def process_session(self, client, request):
305                threading.Thread(target=ogThread.procsession, args=(client, request, self,)).start()
[6d1e79b]306
[8fc251e]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()
[261a5ed]310
311        def process_hardware(self, client):
312                path = '/tmp/Chrd-' + client.ip
[d5dca0f]313                threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start()
[9fd8f2d]314
315        def process_schedule(self, client):
[86eb703]316                response = restResponse(ogResponses.OK)
317                client.send(response.get())
[efbe8a7]318
[8fc251e]319        def process_setup(self, client, request):
320                threading.Thread(target=ogThread.procsetup, args=(client, request, self,)).start()
[cc11d8f]321
[8fc251e]322        def process_irestore(self, client, request):
323                threading.Thread(target=ogThread.procirestore, args=(client, request, self,)).start()
[d5dca0f]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)
[b2fd0b5]334
[8fc251e]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()
[b5e182f]338
339        def process_refresh(self, client):
340                threading.Thread(target=ogThread.procrefresh, args=(client, self,)).start()
Note: See TracBrowser for help on using the repository browser.