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

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

Catch execution errors during restore image command

This patch allows us to send feedback to the server in case of error during
the execution of the command. In case of error, ogClient will send an
"Internal Error" http message.

  • 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                if not jsonResp == None:
38                        msg = msg + '\nContent-Type:application/json'
39                        msg = msg + '\nContent-Length:' + str(len(jsonResp.dumpMsg()))
40                        msg = msg + '\n' + jsonResp.dumpMsg()
41
42                msg = msg + '\r\n\r\n'
43                return msg
44
45class ogThread():
46        # Executing cmd thread
47        def execcmd(msgqueue, httpparser):
48                msgqueue.queue.clear()
49                msgqueue.put(ogOperations.execCMD(httpparser))
50
51        # Powering off thread
52        def poweroff():
53                time.sleep(2)
54                ogOperations.poweroff()
55
56        # Rebooting thread
57        def reboot():
58                ogOperations.reboot()
59
60        # Process session
61        def procsession(client, httpparser):
62                try:
63                        ogOperations.procsession(httpparser)
64                except ValueError as err:
65                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
66                        return
67
68                client.send(restResponse.getResponse(ogResponses.OK))
69
70        # Process software
71        def procsoftware(client, httpparser, path):
72                try:
73                        ogOperations.procsoftware(httpparser, path)
74                except ValueError as err:
75                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
76                        return
77
78                jsonResp = jsonResponse()
79                jsonResp.addElement('disk', httpparser.getDisk())
80                jsonResp.addElement('partition', httpparser.getPartition())
81
82                f = open(path, "r")
83                lines = f.readlines()
84                f.close()
85                jsonResp.addElement('software', lines[0])
86
87                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
88
89        # Process hardware
90        def prochardware(client, path):
91                try:
92                        ogOperations.prochardware(path)
93                except ValueError as err:
94                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
95                        return
96
97                jsonResp = jsonResponse()
98                f = open(path, "r")
99                lines = f.readlines()
100                f.close()
101                jsonResp.addElement('hardware', lines[0])
102                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
103
104        # Process setup
105        def procsetup(msgqueue, httpparser):
106                ogOperations.procsetup(httpparser)
107
108        # Process image restore
109        def procirestore(httpparser):
110                try:
111                        ogOperations.procirestore(httpparser)
112                except ValueError as err:
113                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
114                        return
115
116                client.send(restResponse.getResponse(ogResponses.OK))
117
118class ogResponses(Enum):
119        BAD_REQUEST=0
120        IN_PROGRESS=1
121        OK=2
122        INTERNAL_ERR=3
123
124class ogRest():
125        def __init__(self):
126                self.msgqueue = queue.Queue(1000)
127
128        def processOperation(self, httpparser, client):
129                op = httpparser.getRequestOP()
130                URI = httpparser.getURI()
131                if ("GET" in op):
132                        if ("probe" in URI):
133                                self.process_probe(client)
134                        elif ("shell/output" in URI):
135                                self.process_shellout(client)
136                        elif ("hardware" in URI):
137                                self.process_hardware(client)
138                        elif ("run/schedule" in URI):
139                                self.process_schedule(client)
140                        else:
141                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
142                elif ("POST" in op):
143                        if ("poweroff" in URI):
144                                self.process_poweroff(client)
145                        elif ("reboot" in URI):
146                                self.process_reboot(client)
147                        elif ("shell/run" in URI):
148                                self.process_shellrun(client, httpparser)
149                        elif ("session" in URI):
150                                self.process_session(client, httpparser)
151                        elif ("software" in URI):
152                                self.process_software(client, httpparser)
153                        elif ("setup" in URI):
154                                self.process_setup(client, httpparser)
155                        elif ("image/restore" in URI):
156                                self.process_irestore(client, httpparser)
157                        else:
158                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
159                else:
160                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
161
162                return 0
163
164        def process_reboot(self, client):
165                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
166                client.disconnect()
167                threading.Thread(target=ogThread.reboot).start()
168
169        def process_poweroff(self, client):
170                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
171                client.disconnect()
172                threading.Thread(target=ogThread.poweroff).start()
173
174        def process_probe(self, client):
175                client.send(restResponse.getResponse(ogResponses.OK))
176
177        def process_shellrun(self, client, httpparser):
178                if httpparser.getCMD() == None:
179                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
180                        return
181
182                try:
183                        ogThread.execcmd(self.msgqueue, httpparser)
184                except ValueError as err:
185                        print(err.args[0])
186                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
187                        return
188
189                client.send(restResponse.getResponse(ogResponses.OK))
190
191        def process_shellout(self, client):
192                jsonResp = jsonResponse()
193                if self.msgqueue.empty():
194                        jsonResp.addElement('out', '')
195                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
196                else:
197                        jsonResp.addElement('out', self.msgqueue.get())
198                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
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=(self.msgqueue, httpparser,)).start()
216                client.send(restResponse.getResponse(ogResponses.OK))
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.