source: ogClient-Git/src/ogRest.py @ 683afa6

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

Improve software command response behavior

During our tests, we found some limitation during the execution of the software
command. We don't manage errors during the execution of this command. Moreover,
the server needs some information in case that everything is OK.

This patch modified the code for controlling the errors during the execution,
returning an "Internal Error" http message (500). Moreover, in case that
everything is OK, ogClient sends a message with this json body:

{ "disk" : "0", "partition" : "1", "software" : "xyz" }

"xyz" will be the output saved during the execution of InventarioSoftware? in
a specific path.

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