source: ogClient-Git/src/ogRest.py @ 0f32b9c

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

Catch execution errors during init session command

This patch allows us to catch an exception when something wrong is happening
executing the init session command. In error cases, ogClient sends an internal
error http message to the server. Otherwise, an OK http message.

  • Property mode set to 100644
File size: 5.5 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(msgqueue, httpparser, path):
72                msgqueue.queue.clear()
73                msgqueue.put(ogOperations.procsoftware(httpparser, path))
74
75        # Process hardware
76        def prochardware(msgqueue, path):
77                msgqueue.queue.clear()
78                msgqueue.put(ogOperations.prochardware(path))
79
80        # Process setup
81        def procsetup(msgqueue, httpparser):
82                ogOperations.procsetup(httpparser)
83
84        # Process image restore
85        def procirestore(msgqueue, httpparser):
86                msgqueue.queue.clear()
87                msgqueue.put(ogOperations.procirestore(httpparser))
88
89class ogResponses(Enum):
90        BAD_REQUEST=0
91        IN_PROGRESS=1
92        OK=2
93        INTERNAL_ERR=3
94
95class ogRest():
96        def __init__(self):
97                self.msgqueue = queue.Queue(1000)
98
99        def processOperation(self, httpparser, client):
100                op = httpparser.getRequestOP()
101                URI = httpparser.getURI()
102                if ("GET" in op):
103                        if ("probe" in URI):
104                                self.process_probe(client)
105                        elif ("shell/output" in URI):
106                                self.process_shellout(client)
107                        elif ("hardware" in URI):
108                                self.process_hardware(client)
109                        elif ("run/schedule" in URI):
110                                self.process_schedule(client)
111                        else:
112                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
113                elif ("POST" in op):
114                        if ("poweroff" in URI):
115                                self.process_poweroff(client)
116                        elif ("reboot" in URI):
117                                self.process_reboot(client)
118                        elif ("shell/run" in URI):
119                                self.process_shellrun(client, httpparser)
120                        elif ("session" in URI):
121                                self.process_session(client, httpparser)
122                        elif ("software" in URI):
123                                self.process_software(client, httpparser)
124                        elif ("setup" in URI):
125                                self.process_setup(client, httpparser)
126                        elif ("image/restore" in URI):
127                                self.process_irestore(client, httpparser)
128                        else:
129                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
130                else:
131                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
132
133                return 0
134
135        def process_reboot(self, client):
136                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
137                client.disconnect()
138                threading.Thread(target=ogThread.reboot).start()
139
140        def process_poweroff(self, client):
141                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
142                client.disconnect()
143                threading.Thread(target=ogThread.poweroff).start()
144
145        def process_probe(self, client):
146                client.send(restResponse.getResponse(ogResponses.OK))
147
148        def process_shellrun(self, client, httpparser):
149                if httpparser.getCMD() == None:
150                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
151                        return
152
153                try:
154                        ogThread.execcmd(self.msgqueue, httpparser)
155                except ValueError as err:
156                        print(err.args[0])
157                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
158                        return
159
160                client.send(restResponse.getResponse(ogResponses.OK))
161
162        def process_shellout(self, client):
163                jsonResp = jsonResponse()
164                if self.msgqueue.empty():
165                        jsonResp.addElement('out', '')
166                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
167                else:
168                        jsonResp.addElement('out', self.msgqueue.get())
169                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
170
171        def process_session(self, client, httpparser):
172                threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start()
173
174        def process_software(self, client, httpparser):
175                path = '/tmp/CSft-' + client.ip + '-' + partition
176                threading.Thread(target=ogThread.procsoftware, args=(self.msgqueue, httpparser, path,)).start()
177                client.send(restResponse.getResponse(ogResponses.OK))
178
179        def process_hardware(self, client):
180                path = '/tmp/Chrd-' + client.ip
181                threading.Thread(target=ogThread.prochardware, args=(self.msgqueue, path,)).start()
182                client.send(restResponse.getResponse(ogResponses.OK))
183
184        def process_schedule(self, client):
185                client.send(restResponse.getResponse(ogResponses.OK))
186
187        def process_setup(self, client, httpparser):
188                threading.Thread(target=ogThread.procsetup, args=(self.msgqueue, httpparser,)).start()
189                client.send(restResponse.getResponse(ogResponses.OK))
190
191        def process_irestore(self, client, httpparser):
192                threading.Thread(target=ogThread.procirestore, args=(self.msgqueue, httpparser,)).start()
193                client.send(restResponse.getResponse(ogResponses.OK))
Note: See TracBrowser for help on using the repository browser.