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

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

Include echo option for returning shell output

This patch adds a new echo option in /shell/run command. In case that the option
is set up to true, the server will receive in the response a json with the shell
output. Otherwise, the server will receive a response message without json
body.

A side effect of this change is that the command /shell/output/ disapears.

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