source: ogClient-Git/src/ogRest.py @ 230bdca

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

Execute cmd command using thread

This patch prepares the code for future stop command.

  • 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(client, httpparser):
51                if httpparser.getCMD() == None:
52                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
53                        return
54
55                try:
56                        shellout = ogOperations.execCMD(httpparser)
57                except ValueError as err:
58                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
59                        return
60
61                if httpparser.getEcho():
62                        jsonResp = jsonResponse()
63                        jsonResp.addElement('out', shellout)
64                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
65                else:
66                        client.send(restResponse.getResponse(ogResponses.OK))
67
68        # Powering off thread
69        def poweroff():
70                time.sleep(2)
71                ogOperations.poweroff()
72
73        # Rebooting thread
74        def reboot():
75                ogOperations.reboot()
76
77        # Process session
78        def procsession(client, httpparser):
79                try:
80                        ogOperations.procsession(httpparser)
81                except ValueError as err:
82                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
83                        return
84
85                client.send(restResponse.getResponse(ogResponses.OK))
86
87        # Process software
88        def procsoftware(client, httpparser, path):
89                try:
90                        ogOperations.procsoftware(httpparser, path)
91                except ValueError as err:
92                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
93                        return
94
95                jsonResp = jsonResponse()
96                jsonResp.addElement('disk', httpparser.getDisk())
97                jsonResp.addElement('partition', httpparser.getPartition())
98
99                f = open(path, "r")
100                lines = f.readlines()
101                f.close()
102                jsonResp.addElement('software', lines[0])
103
104                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
105
106        # Process hardware
107        def prochardware(client, path):
108                try:
109                        ogOperations.prochardware(path)
110                except ValueError as err:
111                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
112                        return
113
114                jsonResp = jsonResponse()
115                f = open(path, "r")
116                lines = f.readlines()
117                f.close()
118                jsonResp.addElement('hardware', lines[0])
119                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
120
121        # Process setup
122        def procsetup(client, httpparser):
123                jsonResp = jsonResponse()
124                jsonResp.addElement('disk', httpparser.getDisk())
125                jsonResp.addElement('cache', httpparser.getCache())
126                jsonResp.addElement('cache_size', httpparser.getCacheSize())
127                listconfig = ogOperations.procsetup(httpparser)
128                jsonResp.addElement('partition_setup', listconfig)
129                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
130
131        # Process image restore
132        def procirestore(httpparser):
133                try:
134                        ogOperations.procirestore(httpparser)
135                except ValueError as err:
136                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
137                        return
138
139                client.send(restResponse.getResponse(ogResponses.OK))
140
141class ogResponses(Enum):
142        BAD_REQUEST=0
143        IN_PROGRESS=1
144        OK=2
145        INTERNAL_ERR=3
146
147class ogRest():
148        def processOperation(self, httpparser, client):
149                op = httpparser.getRequestOP()
150                URI = httpparser.getURI()
151                if ("GET" in op):
152                        if "hardware" in URI:
153                                self.process_hardware(client)
154                        elif ("run/schedule" in URI):
155                                self.process_schedule(client)
156                        else:
157                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
158                elif ("POST" in op):
159                        if ("poweroff" in URI):
160                                self.process_poweroff(client)
161                        elif "probe" in URI:
162                                self.process_probe(client)
163                        elif ("reboot" in URI):
164                                self.process_reboot(client)
165                        elif ("shell/run" in URI):
166                                self.process_shellrun(client, httpparser)
167                        elif ("session" in URI):
168                                self.process_session(client, httpparser)
169                        elif ("software" in URI):
170                                self.process_software(client, httpparser)
171                        elif ("setup" in URI):
172                                self.process_setup(client, httpparser)
173                        elif ("image/restore" in URI):
174                                self.process_irestore(client, httpparser)
175                        else:
176                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
177                else:
178                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
179
180                return 0
181
182        def process_reboot(self, client):
183                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
184                client.disconnect()
185                threading.Thread(target=ogThread.reboot).start()
186
187        def process_poweroff(self, client):
188                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
189                client.disconnect()
190                threading.Thread(target=ogThread.poweroff).start()
191
192        def process_probe(self, client):
193                jsonResp = jsonResponse()
194                jsonResp.addElement('status', 'OPG')
195                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
196
197        def process_shellrun(self, client, httpparser):
198                threading.Thread(target=ogThread.execcmd, args=(client, httpparser,)).start()
199
200        def process_session(self, client, httpparser):
201                threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start()
202
203        def process_software(self, client, httpparser):
204                path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
205                threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path,)).start()
206
207        def process_hardware(self, client):
208                path = '/tmp/Chrd-' + client.ip
209                threading.Thread(target=ogThread.prochardware, args=(client, path,)).start()
210
211        def process_schedule(self, client):
212                client.send(restResponse.getResponse(ogResponses.OK))
213
214        def process_setup(self, client, httpparser):
215                threading.Thread(target=ogThread.procsetup, args=(client, httpparser,)).start()
216
217        def process_irestore(self, client, httpparser):
218                threading.Thread(target=ogThread.procirestore, args=(client, httpparser,)).start()
Note: See TracBrowser for help on using the repository browser.