source: ogClient-Git/src/ogRest.py @ 44a4662

Last change on this file since 44a4662 was 8e54420, checked in by Alvaro Neira Ayuso <alvaroneay@…>, 5 years ago

(Clean-Up) Rename cmd function/variables to use "run" syntax

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[05b1088]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
[dfc97ff]9import threading
10import platform
11import time
[694bc49]12from enum import Enum
[e20daf6]13import json
14import queue
[d5dca0f]15import sys
16import os
17import signal
[694bc49]18
[2fa8aa4]19from src.HTTPParser import *
20
[dfc97ff]21if platform.system() == 'Linux':
22        from src.linux import ogOperations
23
[7c26c55]24class jsonResponse():
25        def __init__(self):
26                self.jsontree = {}
27
28        def addElement(self, key, value):
29                self.jsontree[key] = value
30
31        def dumpMsg(self):
32                return json.dumps(self.jsontree)
33
[0f32b9c]34class restResponse():
35        def getResponse(response, jsonResp=None):
36                msg = ''
37                if response == ogResponses.BAD_REQUEST:
[3a44e48]38                        msg = 'HTTP/1.0 400 Bad Request'
[0f32b9c]39                elif response == ogResponses.IN_PROGRESS:
40                        msg = 'HTTP/1.0 202 Accepted'
41                elif response == ogResponses.OK:
42                        msg = 'HTTP/1.0 200 OK'
43                elif response == ogResponses.INTERNAL_ERR:
44                        msg = 'HTTP/1.0 500 Internal Server Error'
[d5dca0f]45                elif response == ogResponses.UNAUTHORIZED:
46                        msg = 'HTTP/1.0 401 Unauthorized'
[0f32b9c]47                else:
48                        return msg
49
[3a44e48]50                msg += '\r\n'
51
52                if jsonResp:
53                        msg += 'Content-Length:' + str(len(jsonResp.dumpMsg()))
54                        msg += '\r\nContent-Type:application/json'
55                        msg += '\r\n\r\n' + jsonResp.dumpMsg()
56                else:
57                        msg += '\r\n'
[0f32b9c]58
59                return msg
60
[59a2823]61class ogThread():
62        # Executing cmd thread
[d5dca0f]63        def execcmd(client, httpparser, ogRest):
[8e54420]64                if httpparser.getrun() == None:
[230bdca]65                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
66                        return
67
68                try:
[d5dca0f]69                        shellout = ogOperations.execCMD(httpparser, ogRest)
[230bdca]70                except ValueError as err:
71                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
72                        return
73
74                if httpparser.getEcho():
75                        jsonResp = jsonResponse()
76                        jsonResp.addElement('out', shellout)
77                        client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
78                else:
79                        client.send(restResponse.getResponse(ogResponses.OK))
[59a2823]80
81        # Powering off thread
[af90350]82        def poweroff():
[59a2823]83                time.sleep(2)
84                ogOperations.poweroff()
85
86        # Rebooting thread
[af90350]87        def reboot():
[59a2823]88                ogOperations.reboot()
89
[2fa8aa4]90        # Process session
[d5dca0f]91        def procsession(client, httpparser, ogRest):
[0f32b9c]92                try:
[d5dca0f]93                        ogOperations.procsession(httpparser, ogRest)
[0f32b9c]94                except ValueError as err:
95                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
96                        return
97
98                client.send(restResponse.getResponse(ogResponses.OK))
[2fa8aa4]99
[6d1e79b]100        # Process software
[d5dca0f]101        def procsoftware(client, httpparser, path, ogRest):
[683afa6]102                try:
[d5dca0f]103                        ogOperations.procsoftware(httpparser, path, ogRest)
[683afa6]104                except ValueError as err:
105                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
106                        return
107
108                jsonResp = jsonResponse()
109                jsonResp.addElement('disk', httpparser.getDisk())
110                jsonResp.addElement('partition', httpparser.getPartition())
111
112                f = open(path, "r")
113                lines = f.readlines()
114                f.close()
115                jsonResp.addElement('software', lines[0])
116
117                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[6d1e79b]118
[261a5ed]119        # Process hardware
[d5dca0f]120        def prochardware(client, path, ogRest):
[1ced3dd]121                try:
[d5dca0f]122                        ogOperations.prochardware(path, ogRest)
[1ced3dd]123                except ValueError as err:
124                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
125                        return
126
127                jsonResp = jsonResponse()
128                f = open(path, "r")
129                lines = f.readlines()
130                f.close()
131                jsonResp.addElement('hardware', lines[0])
132                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[261a5ed]133
[efbe8a7]134        # Process setup
[d5dca0f]135        def procsetup(client, httpparser, ogRest):
[38b57c4]136                jsonResp = jsonResponse()
137                jsonResp.addElement('disk', httpparser.getDisk())
138                jsonResp.addElement('cache', httpparser.getCache())
139                jsonResp.addElement('cache_size', httpparser.getCacheSize())
[d5dca0f]140                listconfig = ogOperations.procsetup(httpparser, ogRest)
[38b57c4]141                jsonResp.addElement('partition_setup', listconfig)
142                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[efbe8a7]143
[cc11d8f]144        # Process image restore
[fdd4ba5]145        def procirestore(client, httpparser, ogRest):
[a306b8b]146                try:
[d5dca0f]147                        ogOperations.procirestore(httpparser, ogRest)
[a306b8b]148                except ValueError as err:
149                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
150                        return
151
152                client.send(restResponse.getResponse(ogResponses.OK))
[cc11d8f]153
[b2fd0b5]154        # Process image create
155        def procicreate(client, path, httpparser, ogRest):
156                try:
157                        ogOperations.procicreate(path, httpparser, ogRest)
158                except ValueError as err:
159                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
160                        return
161
[5603a28]162                jsonResp = jsonResponse()
163                jsonResp.addElement('disk', httpparser.getDisk())
164                jsonResp.addElement('partition', httpparser.getPartition())
165                jsonResp.addElement('code', httpparser.getCode())
166                jsonResp.addElement('id', httpparser.getId())
167                jsonResp.addElement('name', httpparser.getName())
168                jsonResp.addElement('repository', httpparser.getRepo())
[b2fd0b5]169                f = open(path, "r")
170                lines = f.readlines()
171                f.close()
172                jsonResp.addElement('software', lines[0])
173                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
174
[b5e182f]175        # Process refresh
176        def procrefresh(client, ogRest):
177                try:
178                        out = ogOperations.procrefresh(ogRest)
179                except ValueError as err:
180                        client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
181                        return
182
183                jsonResp = jsonResponse()
184                jsonResp.addElement('disk', out[0])
185                jsonResp.addElement('partition_setup', out[1])
186
187                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
188
[694bc49]189class ogResponses(Enum):
190        BAD_REQUEST=0
191        IN_PROGRESS=1
192        OK=2
[0f32b9c]193        INTERNAL_ERR=3
[d5dca0f]194        UNAUTHORIZED=4
[694bc49]195
[dfc97ff]196class ogRest():
[d5dca0f]197        def __init__(self):
198                self.proc = None
199                self.terminated = False
200
[2fa8aa4]201        def processOperation(self, httpparser, client):
202                op = httpparser.getRequestOP()
203                URI = httpparser.getURI()
[d5dca0f]204
205                if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
206                        client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED))
207                        return
208
[6764fc4]209                if ("GET" in op):
[9c34a8e]210                        if "hardware" in URI:
[261a5ed]211                                self.process_hardware(client)
[9fd8f2d]212                        elif ("run/schedule" in URI):
213                                self.process_schedule(client)
[6764fc4]214                        else:
[0f32b9c]215                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[6764fc4]216                elif ("POST" in op):
217                        if ("poweroff" in URI):
218                                self.process_poweroff(client)
[9c34a8e]219                        elif "probe" in URI:
220                                self.process_probe(client)
[6764fc4]221                        elif ("reboot" in URI):
222                                self.process_reboot(client)
223                        elif ("shell/run" in URI):
[2e342b5]224                                self.process_shellrun(client, httpparser)
[2fa8aa4]225                        elif ("session" in URI):
[2e342b5]226                                self.process_session(client, httpparser)
[6d1e79b]227                        elif ("software" in URI):
[2e342b5]228                                self.process_software(client, httpparser)
[efbe8a7]229                        elif ("setup" in URI):
[2e342b5]230                                self.process_setup(client, httpparser)
[cc11d8f]231                        elif ("image/restore" in URI):
[2e342b5]232                                self.process_irestore(client, httpparser)
[d5dca0f]233                        elif ("stop" in URI):
234                                self.process_stop(client)
[b2fd0b5]235                        elif ("image/create" in URI):
236                                self.process_icreate(client, httpparser)
[b5e182f]237                        elif ("refresh" in URI):
238                                self.process_refresh(client)
[6764fc4]239                        else:
[0f32b9c]240                                client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[dfc97ff]241                else:
[0f32b9c]242                        client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
[dfc97ff]243
244                return 0
245
246        def process_reboot(self, client):
[0f32b9c]247                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
[dfc97ff]248                client.disconnect()
[af90350]249                threading.Thread(target=ogThread.reboot).start()
[dfc97ff]250
251        def process_poweroff(self, client):
[0f32b9c]252                client.send(restResponse.getResponse(ogResponses.IN_PROGRESS))
[dfc97ff]253                client.disconnect()
[af90350]254                threading.Thread(target=ogThread.poweroff).start()
[dfc97ff]255
256        def process_probe(self, client):
[336b023]257                jsonResp = jsonResponse()
258                jsonResp.addElement('status', 'OPG')
259                client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
[e20daf6]260
[2e342b5]261        def process_shellrun(self, client, httpparser):
[d5dca0f]262                threading.Thread(target=ogThread.execcmd, args=(client, httpparser, self,)).start()
[2fa8aa4]263
[2e342b5]264        def process_session(self, client, httpparser):
[d5dca0f]265                threading.Thread(target=ogThread.procsession, args=(client, httpparser, self,)).start()
[6d1e79b]266
[2e342b5]267        def process_software(self, client, httpparser):
[683afa6]268                path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
[d5dca0f]269                threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path, self,)).start()
[261a5ed]270
271        def process_hardware(self, client):
272                path = '/tmp/Chrd-' + client.ip
[d5dca0f]273                threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start()
[9fd8f2d]274
275        def process_schedule(self, client):
[0f32b9c]276                client.send(restResponse.getResponse(ogResponses.OK))
[efbe8a7]277
[2e342b5]278        def process_setup(self, client, httpparser):
[d5dca0f]279                threading.Thread(target=ogThread.procsetup, args=(client, httpparser, self,)).start()
[cc11d8f]280
[2e342b5]281        def process_irestore(self, client, httpparser):
[d5dca0f]282                threading.Thread(target=ogThread.procirestore, args=(client, httpparser, self,)).start()
283
284        def process_stop(self, client):
285                client.disconnect()
286                if self.proc == None:
287                        return
288
289                if self.proc.poll() == None:
290                        os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM)
291                        self.terminated = True
292                        sys.exit(0)
[b2fd0b5]293
294        def process_icreate(self, client, httpparser):
295                path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
296                threading.Thread(target=ogThread.procicreate, args=(client, path, httpparser, self,)).start()
[b5e182f]297
298        def process_refresh(self, client):
299                threading.Thread(target=ogThread.procrefresh, args=(client, self,)).start()
Note: See TracBrowser for help on using the repository browser.