source: ogClient-Git/src/ogRest.py @ 1ced3dd

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

Improve hardware command response behavior

This patch give us a better support in case of error or success execution.
In error cases, the new behavior is to send an Internal Error http message (500).
Otherwise, the server will receive a message with a json with this format:

{ "hardware" : "xyz" }

"xyz" is the output saved in a specific path during the execution of
InventarioHardware?.

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