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
Line 
1import threading
2import platform
3import time
4from enum import Enum
5import json
6import queue
7
8from src.HTTPParser import *
9
10if platform.system() == 'Linux':
11        from src.linux import ogOperations
12
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
23class restResponse():
24        def getResponse(response, jsonResp=None):
25                msg = ''
26                if response == ogResponses.BAD_REQUEST:
27                        msg = 'HTTP/1.0 400 Bad Request'
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
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'
45
46                return msg
47
48class ogThread():
49        # Executing cmd thread
50        def execcmd(httpparser):
51                return ogOperations.execCMD(httpparser)
52
53        # Powering off thread
54        def poweroff():
55                time.sleep(2)
56                ogOperations.poweroff()
57
58        # Rebooting thread
59        def reboot():
60                ogOperations.reboot()
61
62        # Process session
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))
71
72        # Process software
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))
90
91        # Process hardware
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))
105
106        # Process setup
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))
115
116        # Process image restore
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))
125
126class ogResponses(Enum):
127        BAD_REQUEST=0
128        IN_PROGRESS=1
129        OK=2
130        INTERNAL_ERR=3
131
132class ogRest():
133        def processOperation(self, httpparser, client):
134                op = httpparser.getRequestOP()
135                URI = httpparser.getURI()
136                if ("GET" in op):
137                        if "hardware" in URI:
138                                self.process_hardware(client)
139                        elif ("run/schedule" in URI):
140                                self.process_schedule(client)
141                        else:
142                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
143                elif ("POST" in op):
144                        if ("poweroff" in URI):
145                                self.process_poweroff(client)
146                        elif "probe" in URI:
147                                self.process_probe(client)
148                        elif ("reboot" in URI):
149                                self.process_reboot(client)
150                        elif ("shell/run" in URI):
151                                self.process_shellrun(client, httpparser)
152                        elif ("session" in URI):
153                                self.process_session(client, httpparser)
154                        elif ("software" in URI):
155                                self.process_software(client, httpparser)
156                        elif ("setup" in URI):
157                                self.process_setup(client, httpparser)
158                        elif ("image/restore" in URI):
159                                self.process_irestore(client, httpparser)
160                        else:
161                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
162                else:
163                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
164
165                return 0
166
167        def process_reboot(self, client):
168                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
169                client.disconnect()
170                threading.Thread(target=ogThread.reboot).start()
171
172        def process_poweroff(self, client):
173                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
174                client.disconnect()
175                threading.Thread(target=ogThread.poweroff).start()
176
177        def process_probe(self, client):
178                jsonResp = jsonResponse()
179                jsonResp.addElement('status', 'OPG')
180                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
181
182        def process_shellrun(self, client, httpparser):
183                if httpparser.getCMD() == None:
184                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
185                        return
186
187                try:
188                        shellout = ogThread.execcmd(httpparser)
189                except ValueError as err:
190                        print(err.args[0])
191                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
192                        return
193
194                if httpparser.getEcho():
195                        jsonResp = jsonResponse()
196                        jsonResp.addElement('out', shellout)
197                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
198                else:
199                        client.send(restResponse.getResponse(ogResponses.OK))
200
201        def process_session(self, client, httpparser):
202                threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start()
203
204        def process_software(self, client, httpparser):
205                path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
206                threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path,)).start()
207
208        def process_hardware(self, client):
209                path = '/tmp/Chrd-' + client.ip
210                threading.Thread(target=ogThread.prochardware, args=(client, path,)).start()
211
212        def process_schedule(self, client):
213                client.send(restResponse.getResponse(ogResponses.OK))
214
215        def process_setup(self, client, httpparser):
216                threading.Thread(target=ogThread.procsetup, args=(client, httpparser,)).start()
217
218        def process_irestore(self, client, httpparser):
219                threading.Thread(target=ogThread.procirestore, args=(client, httpparser,)).start()
Note: See TracBrowser for help on using the repository browser.