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

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

Fix echo check for process_shellrun in ogRest

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[dfc97ff]1import threading
2import platform
3import time
[694bc49]4from enum import Enum
[e20daf6]5import json
6import queue
[694bc49]7
[2fa8aa4]8from src.HTTPParser import *
9
[dfc97ff]10if platform.system() == 'Linux':
11        from src.linux import ogOperations
12
[7c26c55]13class jsonResponse():
14        def __init__(self):
15                self.jsontree = {}
16
17        def addElement(self, key, value):
18                self.jsontree[key] = value
19
20        def dumpMsg(self):
21                return json.dumps(self.jsontree)
22
[0f32b9c]23class restResponse():
24        def getResponse(response, jsonResp=None):
25                msg = ''
26                if response == ogResponses.BAD_REQUEST:
[3a44e48]27                        msg = 'HTTP/1.0 400 Bad Request'
[0f32b9c]28                elif response == ogResponses.IN_PROGRESS:
29                        msg = 'HTTP/1.0 202 Accepted'
30                elif response == ogResponses.OK:
31                        msg = 'HTTP/1.0 200 OK'
32                elif response == ogResponses.INTERNAL_ERR:
33                        msg = 'HTTP/1.0 500 Internal Server Error'
34                else:
35                        return msg
36
[3a44e48]37                msg += '\r\n'
38
39                if jsonResp:
40                        msg += 'Content-Length:' + str(len(jsonResp.dumpMsg()))
41                        msg += '\r\nContent-Type:application/json'
42                        msg += '\r\n\r\n' + jsonResp.dumpMsg()
43                else:
44                        msg += '\r\n'
[0f32b9c]45
46                return msg
47
[59a2823]48class ogThread():
49        # Executing cmd thread
[a9d81ad]50        def execcmd(httpparser):
51                return ogOperations.execCMD(httpparser)
[59a2823]52
53        # Powering off thread
[af90350]54        def poweroff():
[59a2823]55                time.sleep(2)
56                ogOperations.poweroff()
57
58        # Rebooting thread
[af90350]59        def reboot():
[59a2823]60                ogOperations.reboot()
61
[2fa8aa4]62        # Process session
[0f32b9c]63        def procsession(client, httpparser):
64                try:
65                        ogOperations.procsession(httpparser)
66                except ValueError as err:
67                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
68                        return
69
70                client.send(restResponse.getResponse(ogResponses.OK))
[2fa8aa4]71
[6d1e79b]72        # Process software
[683afa6]73        def procsoftware(client, httpparser, path):
74                try:
75                        ogOperations.procsoftware(httpparser, path)
76                except ValueError as err:
77                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
78                        return
79
80                jsonResp = jsonResponse()
81                jsonResp.addElement('disk', httpparser.getDisk())
82                jsonResp.addElement('partition', httpparser.getPartition())
83
84                f = open(path, "r")
85                lines = f.readlines()
86                f.close()
87                jsonResp.addElement('software', lines[0])
88
89                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[6d1e79b]90
[261a5ed]91        # Process hardware
[1ced3dd]92        def prochardware(client, path):
93                try:
94                        ogOperations.prochardware(path)
95                except ValueError as err:
96                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
97                        return
98
99                jsonResp = jsonResponse()
100                f = open(path, "r")
101                lines = f.readlines()
102                f.close()
103                jsonResp.addElement('hardware', lines[0])
104                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[261a5ed]105
[efbe8a7]106        # Process setup
[38b57c4]107        def procsetup(client, httpparser):
108                jsonResp = jsonResponse()
109                jsonResp.addElement('disk', httpparser.getDisk())
110                jsonResp.addElement('cache', httpparser.getCache())
111                jsonResp.addElement('cache_size', httpparser.getCacheSize())
112                listconfig = ogOperations.procsetup(httpparser)
113                jsonResp.addElement('partition_setup', listconfig)
114                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[efbe8a7]115
[cc11d8f]116        # Process image restore
[a306b8b]117        def procirestore(httpparser):
118                try:
119                        ogOperations.procirestore(httpparser)
120                except ValueError as err:
121                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
122                        return
123
124                client.send(restResponse.getResponse(ogResponses.OK))
[cc11d8f]125
[694bc49]126class ogResponses(Enum):
127        BAD_REQUEST=0
128        IN_PROGRESS=1
129        OK=2
[0f32b9c]130        INTERNAL_ERR=3
[694bc49]131
[dfc97ff]132class ogRest():
[2fa8aa4]133        def processOperation(self, httpparser, client):
134                op = httpparser.getRequestOP()
135                URI = httpparser.getURI()
[6764fc4]136                if ("GET" in op):
[9c34a8e]137                        if "hardware" in URI:
[261a5ed]138                                self.process_hardware(client)
[9fd8f2d]139                        elif ("run/schedule" in URI):
140                                self.process_schedule(client)
[6764fc4]141                        else:
[0f32b9c]142                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[6764fc4]143                elif ("POST" in op):
144                        if ("poweroff" in URI):
145                                self.process_poweroff(client)
[9c34a8e]146                        elif "probe" in URI:
147                                self.process_probe(client)
[6764fc4]148                        elif ("reboot" in URI):
149                                self.process_reboot(client)
150                        elif ("shell/run" in URI):
[2e342b5]151                                self.process_shellrun(client, httpparser)
[2fa8aa4]152                        elif ("session" in URI):
[2e342b5]153                                self.process_session(client, httpparser)
[6d1e79b]154                        elif ("software" in URI):
[2e342b5]155                                self.process_software(client, httpparser)
[efbe8a7]156                        elif ("setup" in URI):
[2e342b5]157                                self.process_setup(client, httpparser)
[cc11d8f]158                        elif ("image/restore" in URI):
[2e342b5]159                                self.process_irestore(client, httpparser)
[6764fc4]160                        else:
[0f32b9c]161                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[dfc97ff]162                else:
[0f32b9c]163                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[dfc97ff]164
165                return 0
166
167        def process_reboot(self, client):
[0f32b9c]168                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
[dfc97ff]169                client.disconnect()
[af90350]170                threading.Thread(target=ogThread.reboot).start()
[dfc97ff]171
172        def process_poweroff(self, client):
[0f32b9c]173                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
[dfc97ff]174                client.disconnect()
[af90350]175                threading.Thread(target=ogThread.poweroff).start()
[dfc97ff]176
177        def process_probe(self, client):
[336b023]178                jsonResp = jsonResponse()
179                jsonResp.addElement('status', 'OPG')
180                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[e20daf6]181
[2e342b5]182        def process_shellrun(self, client, httpparser):
183                if httpparser.getCMD() == None:
[0f32b9c]184                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[e20daf6]185                        return
186
[e6eba4b]187                try:
[a9d81ad]188                        shellout = ogThread.execcmd(httpparser)
[e6eba4b]189                except ValueError as err:
190                        print(err.args[0])
[0f32b9c]191                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[e6eba4b]192                        return
193
[b9c33f2]194                if httpparser.getEcho():
[a9d81ad]195                        jsonResp = jsonResponse()
196                        jsonResp.addElement('out', shellout)
[0f32b9c]197                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[e20daf6]198                else:
[a9d81ad]199                        client.send(restResponse.getResponse(ogResponses.OK))
[2fa8aa4]200
[2e342b5]201        def process_session(self, client, httpparser):
[0f32b9c]202                threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start()
[6d1e79b]203
[2e342b5]204        def process_software(self, client, httpparser):
[683afa6]205                path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
206                threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path,)).start()
[261a5ed]207
208        def process_hardware(self, client):
209                path = '/tmp/Chrd-' + client.ip
[1ced3dd]210                threading.Thread(target=ogThread.prochardware, args=(client, path,)).start()
[9fd8f2d]211
212        def process_schedule(self, client):
[0f32b9c]213                client.send(restResponse.getResponse(ogResponses.OK))
[efbe8a7]214
[2e342b5]215        def process_setup(self, client, httpparser):
[38b57c4]216                threading.Thread(target=ogThread.procsetup, args=(client, httpparser,)).start()
[cc11d8f]217
[2e342b5]218        def process_irestore(self, client, httpparser):
[a306b8b]219                threading.Thread(target=ogThread.procirestore, args=(client, httpparser,)).start()
Note: See TracBrowser for help on using the repository browser.