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

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

Fix parseGetConf(...) configuration parsing

This patch also reformats the response to the /refresh command.

  • Property mode set to 100644
File size: 9.0 KB
Line 
1#
2# Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3#
4# This program is free software: you can redistribute it and/or modify it under
5# the terms of the GNU Affero General Public License as published by the
6# Free Software Foundation, version 3.
7#
8
9import threading
10import platform
11import time
12from enum import Enum
13import json
14import queue
15import sys
16import os
17import signal
18
19from src.restRequest import *
20
21if platform.system() == 'Linux':
22        from src.linux import ogOperations
23
24class jsonResponse():
25        def __init__(self, dictionary=None):
26                if dictionary:
27                        self.jsontree = dictionary
28                else:
29                        self.jsontree = {}
30
31        def addElement(self, key, value):
32                self.jsontree[key] = value
33
34        def dumpMsg(self):
35                return json.dumps(self.jsontree)
36
37class restResponse():
38        def __init__(self, response, jsonResp=None):
39                self.msg = ''
40                if response == ogResponses.BAD_REQUEST:
41                        self.msg = 'HTTP/1.0 400 Bad Request'
42                elif response == ogResponses.IN_PROGRESS:
43                        self.msg = 'HTTP/1.0 202 Accepted'
44                elif response == ogResponses.OK:
45                        self.msg = 'HTTP/1.0 200 OK'
46                elif response == ogResponses.INTERNAL_ERR:
47                        self.msg = 'HTTP/1.0 500 Internal Server Error'
48                elif response == ogResponses.UNAUTHORIZED:
49                        self.msg = 'HTTP/1.0 401 Unauthorized'
50                else:
51                        return self.msg
52
53                self.msg += '\r\n'
54
55                if jsonResp:
56                        self.msg += 'Content-Length: ' + str(len(jsonResp.dumpMsg()))
57                        self.msg += '\r\nContent-Type: application/json'
58                        self.msg += '\r\n\r\n' + jsonResp.dumpMsg()
59                else:
60                        self.msg += '\r\n'
61
62
63        def get(self):
64                return self.msg
65
66class ogThread():
67        def execcmd(client, request, ogRest):
68                if not request.getrun():
69                        response = restResponse(ogResponses.BAD_REQUEST)
70                        client.send(response.get())
71                        return
72
73                try:
74                        shellout = ogOperations.execCMD(request, ogRest)
75                except ValueError as err:
76                        response = restResponse(ogResponses.INTERNAL_ERR)
77                        client.send(response.get())
78                        return
79
80                if request.getEcho():
81                        jsonResp = jsonResponse()
82                        jsonResp.addElement('out', shellout)
83                        response = restResponse(ogResponses.OK, jsonResp)
84                        client.send(response.get())
85                else:
86                        response = restResponse(ogResponses.OK)
87                        client.send(response.get())
88
89        def poweroff():
90                time.sleep(2)
91                ogOperations.poweroff()
92
93        def reboot():
94                ogOperations.reboot()
95
96        def session(client, request, ogRest):
97                try:
98                        ogOperations.session(request, ogRest)
99                except ValueError as err:
100                        response = restResponse(ogResponses.INTERNAL_ERR)
101                        client.send(response.get())
102                        return
103
104                response = restResponse(ogResponses.OK)
105                client.send(response.get())
106
107        def software(client, request, path, ogRest):
108                try:
109                        ogOperations.software(request, path, ogRest)
110                except ValueError as err:
111                        response = restResponse(ogResponses.INTERNAL_ERR)
112                        client.send(response.get())
113                        return
114
115                jsonResp = jsonResponse()
116                jsonResp.addElement('disk', request.getDisk())
117                jsonResp.addElement('partition', request.getPartition())
118
119                f = open(path, "r")
120                output = f.read()
121                f.close()
122                jsonResp.addElement('software', output)
123
124                response = restResponse(ogResponses.OK, jsonResp)
125                client.send(response.get())
126
127        def hardware(client, path, ogRest):
128                try:
129                        ogOperations.hardware(path, ogRest)
130                except ValueError as err:
131                        response = restResponse(ogResponses.INTERNAL_ERR)
132                        client.send(response.get())
133                        return
134
135                jsonResp = jsonResponse()
136                f = open(path, "r")
137                text = f.read()
138                f.close()
139                jsonResp.addElement('hardware', text)
140
141                response = restResponse(ogResponses.OK, jsonResp)
142                client.send(response.get())
143
144        def setup(client, request, ogRest):
145                listconfig = []
146
147                try:
148                        listconfig = ogOperations.setup(request, ogRest)
149                except ValueError as err:
150                        response = restResponse(ogResponses.INTERNAL_ERR)
151                        client.send(response.get())
152                        return
153
154                jsonResp = jsonResponse()
155                jsonResp.addElement('disk', request.getDisk())
156                jsonResp.addElement('cache', request.getCache())
157                jsonResp.addElement('cache_size', request.getCacheSize())
158                jsonResp.addElement('partition_setup', listconfig)
159
160                response = restResponse(ogResponses.OK, jsonResp)
161                client.send(response.get())
162
163        def image_restore(client, request, ogRest):
164                try:
165                        ogOperations.image_restore(request, ogRest)
166                except ValueError as err:
167                        response = restResponse(ogResponses.INTERNAL_ERR)
168                        client.send(response.get())
169                        return
170
171                response = restResponse(ogResponses.OK, jsonResp)
172                client.send(response.get())
173
174        def image_create(client, path, request, ogRest):
175                try:
176                        ogOperations.image_create(path, request, ogRest)
177                except ValueError as err:
178                        response = restResponse(ogResponses.INTERNAL_ERR)
179                        client.send(response.get())
180                        return
181
182                jsonResp = jsonResponse()
183                jsonResp.addElement('disk', request.getDisk())
184                jsonResp.addElement('partition', request.getPartition())
185                jsonResp.addElement('code', request.getCode())
186                jsonResp.addElement('id', request.getId())
187                jsonResp.addElement('name', request.getName())
188                jsonResp.addElement('repository', request.getRepo())
189                f = open(path, "r")
190                lines = f.readlines()
191                f.close()
192                jsonResp.addElement('software', lines[0])
193
194                response = restResponse(ogResponses.OK, jsonResp)
195                client.send(response.get())
196
197        def refresh(client, ogRest):
198                try:
199                        out = ogOperations.refresh(ogRest)
200                except ValueError as err:
201                        response = restResponse(ogResponses.INTERNAL_ERR)
202                        client.send(response.get())
203                        return
204
205                jsonResp = jsonResponse(out)
206
207                response = restResponse(ogResponses.OK, jsonResp)
208                client.send(response.get())
209
210class ogResponses(Enum):
211        BAD_REQUEST=0
212        IN_PROGRESS=1
213        OK=2
214        INTERNAL_ERR=3
215        UNAUTHORIZED=4
216
217class ogRest():
218        def __init__(self):
219                self.proc = None
220                self.terminated = False
221
222        def processOperation(self, request, client):
223                op = request.getRequestOP()
224                URI = request.getURI()
225
226                if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
227                        response = restResponse(ogResponses.UNAUTHORIZED)
228                        client.send(response.get())
229                        return
230
231                if ("GET" in op):
232                        if "hardware" in URI:
233                                self.process_hardware(client)
234                        elif ("run/schedule" in URI):
235                                self.process_schedule(client)
236                        elif "refresh" in URI:
237                                self.process_refresh(client)
238                        else:
239                                response = restResponse(ogResponses.BAD_REQUEST)
240                                client.send(response.get())
241                elif ("POST" in op):
242                        if ("poweroff" in URI):
243                                self.process_poweroff(client)
244                        elif "probe" in URI:
245                                self.process_probe(client)
246                        elif ("reboot" in URI):
247                                self.process_reboot(client)
248                        elif ("shell/run" in URI):
249                                self.process_shellrun(client, request)
250                        elif ("session" in URI):
251                                self.process_session(client, request)
252                        elif ("software" in URI):
253                                self.process_software(client, request)
254                        elif ("setup" in URI):
255                                self.process_setup(client, request)
256                        elif ("image/restore" in URI):
257                                self.process_imagerestore(client, request)
258                        elif ("stop" in URI):
259                                self.process_stop(client)
260                        elif ("image/create" in URI):
261                                self.process_imagecreate(client, request)
262                        else:
263                                response = restResponse(ogResponses.BAD_REQUEST)
264                                client.send(response.get())
265                else:
266                        response = restResponse(ogResponses.BAD_REQUEST)
267                        client.send(response.get())
268
269                return 0
270
271        def process_reboot(self, client):
272                response = restResponse(ogResponses.IN_PROGRESS)
273                client.send(response.get())
274
275                client.disconnect()
276                threading.Thread(target=ogThread.reboot).start()
277
278        def process_poweroff(self, client):
279                response = restResponse(ogResponses.IN_PROGRESS)
280                client.send(response.get())
281
282                client.disconnect()
283                threading.Thread(target=ogThread.poweroff).start()
284
285        def process_probe(self, client):
286                jsonResp = jsonResponse()
287                jsonResp.addElement('status', 'OPG')
288
289                response = restResponse(ogResponses.OK, jsonResp)
290                client.send(response.get())
291
292        def process_shellrun(self, client, request):
293                threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start()
294
295        def process_session(self, client, request):
296                threading.Thread(target=ogThread.session, args=(client, request, self,)).start()
297
298        def process_software(self, client, request):
299                path = '/tmp/CSft-' + client.ip + '-' + str(request.getPartition())
300                threading.Thread(target=ogThread.software, args=(client, request, path, self,)).start()
301
302        def process_hardware(self, client):
303                path = '/tmp/Chrd-' + client.ip
304                threading.Thread(target=ogThread.hardware, args=(client, path, self,)).start()
305
306        def process_schedule(self, client):
307                response = restResponse(ogResponses.OK)
308                client.send(response.get())
309
310        def process_setup(self, client, request):
311                threading.Thread(target=ogThread.setup, args=(client, request, self,)).start()
312
313        def process_imagerestore(self, client, request):
314                threading.Thread(target=ogThread.image_restore, args=(client, request, self,)).start()
315
316        def process_stop(self, client):
317                client.disconnect()
318                if self.proc == None:
319                        return
320
321                if self.proc.poll() == None:
322                        os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM)
323                        self.terminated = True
324                        sys.exit(0)
325
326        def process_imagecreate(self, client, request):
327                path = '/tmp/CSft-' + client.ip + '-' + request.getPartition()
328                threading.Thread(target=ogThread.image_create, args=(client, path, request, self,)).start()
329
330        def process_refresh(self, client):
331                threading.Thread(target=ogThread.refresh, args=(client, self,)).start()
Note: See TracBrowser for help on using the repository browser.