mirror of https://git.48k.eu/ogclient
(Clean-Up) Rename HTTPParser to restRequest
parent
44a4662fef
commit
8fc251eff3
|
@ -10,7 +10,7 @@ import email
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
import json
|
import json
|
||||||
|
|
||||||
class HTTPParser:
|
class restRequest:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.requestLine = None
|
self.requestLine = None
|
||||||
self.headersAlone = None
|
self.headersAlone = None
|
||||||
|
|
|
@ -47,8 +47,8 @@ def reboot():
|
||||||
else:
|
else:
|
||||||
subprocess.call(['/sbin/reboot'])
|
subprocess.call(['/sbin/reboot'])
|
||||||
|
|
||||||
def execCMD(httpparser, ogRest):
|
def execCMD(request, ogRest):
|
||||||
cmd = httpparser.getrun()
|
cmd = request.getrun()
|
||||||
cmds = cmd.split(" ")
|
cmds = cmd.split(" ")
|
||||||
try:
|
try:
|
||||||
ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True)
|
ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True)
|
||||||
|
@ -58,9 +58,9 @@ def execCMD(httpparser, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def procsession(httpparser, ogRest):
|
def procsession(request, ogRest):
|
||||||
disk = httpparser.getDisk()
|
disk = request.getDisk()
|
||||||
partition = httpparser.getPartition()
|
partition = request.getPartition()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True)
|
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True)
|
||||||
|
@ -70,9 +70,9 @@ def procsession(httpparser, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def procsoftware(httpparser, path, ogRest):
|
def procsoftware(request, path, ogRest):
|
||||||
disk = httpparser.getDisk()
|
disk = request.getDisk()
|
||||||
partition = httpparser.getPartition()
|
partition = request.getPartition()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
|
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
|
||||||
|
@ -91,11 +91,11 @@ def prochardware(path, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def procsetup(httpparser, ogRest):
|
def procsetup(request, ogRest):
|
||||||
disk = httpparser.getDisk()
|
disk = request.getDisk()
|
||||||
cache = httpparser.getCache()
|
cache = request.getCache()
|
||||||
cachesize = httpparser.getCacheSize()
|
cachesize = request.getCacheSize()
|
||||||
partlist = httpparser.getPartitionSetup()
|
partlist = request.getPartitionSetup()
|
||||||
listConfigs = []
|
listConfigs = []
|
||||||
|
|
||||||
for part in partlist:
|
for part in partlist:
|
||||||
|
@ -112,14 +112,14 @@ def procsetup(httpparser, ogRest):
|
||||||
result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
|
result = subprocess.check_output([OG_PATH + 'interfaceAdm/getConfiguration'], shell=True)
|
||||||
return parseGetConf(result.decode('utf-8'))[1]
|
return parseGetConf(result.decode('utf-8'))[1]
|
||||||
|
|
||||||
def procirestore(httpparser, ogRest):
|
def procirestore(request, ogRest):
|
||||||
disk = httpparser.getDisk()
|
disk = request.getDisk()
|
||||||
partition = httpparser.getPartition()
|
partition = request.getPartition()
|
||||||
name = httpparser.getName()
|
name = request.getName()
|
||||||
repo = httpparser.getRepo()
|
repo = request.getRepo()
|
||||||
ctype = httpparser.getType()
|
ctype = request.getType()
|
||||||
profile = httpparser.getProfile()
|
profile = request.getProfile()
|
||||||
cid = httpparser.getId()
|
cid = request.getId()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
|
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
|
||||||
|
@ -129,11 +129,11 @@ def procirestore(httpparser, ogRest):
|
||||||
|
|
||||||
return output.decode('utf-8')
|
return output.decode('utf-8')
|
||||||
|
|
||||||
def procicreate(path, httpparser, ogRest):
|
def procicreate(path, request, ogRest):
|
||||||
disk = httpparser.getDisk()
|
disk = request.getDisk()
|
||||||
partition = httpparser.getPartition()
|
partition = request.getPartition()
|
||||||
name = httpparser.getName()
|
name = request.getName()
|
||||||
repo = httpparser.getRepo()
|
repo = request.getRepo()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
|
ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
|
||||||
|
|
|
@ -13,7 +13,7 @@ import time
|
||||||
import email
|
import email
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from src.HTTPParser import *
|
from src.restRequest import *
|
||||||
from src.ogRest import *
|
from src.ogRest import *
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ class ogClient:
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
self.data = self.data + data
|
self.data = self.data + data
|
||||||
httpparser = HTTPParser()
|
request = restRequest()
|
||||||
|
|
||||||
if not self.trailer:
|
if not self.trailer:
|
||||||
if self.data.find("\r\n") > 0:
|
if self.data.find("\r\n") > 0:
|
||||||
|
@ -97,8 +97,8 @@ class ogClient:
|
||||||
self.trailer = True
|
self.trailer = True
|
||||||
|
|
||||||
if self.trailer and len(self.data) >= self.content_len:
|
if self.trailer and len(self.data) >= self.content_len:
|
||||||
httpparser.parser(self.data)
|
request.parser(self.data)
|
||||||
self.ogrest.processOperation(httpparser, self)
|
self.ogrest.processOperation(request, self)
|
||||||
|
|
||||||
# Cleanup state information from request
|
# Cleanup state information from request
|
||||||
self.data = ""
|
self.data = ""
|
||||||
|
|
|
@ -60,18 +60,18 @@ class restResponse():
|
||||||
|
|
||||||
class ogThread():
|
class ogThread():
|
||||||
# Executing cmd thread
|
# Executing cmd thread
|
||||||
def execcmd(client, httpparser, ogRest):
|
def execcmd(client, request, ogRest):
|
||||||
if httpparser.getrun() == None:
|
if request.getrun() == None:
|
||||||
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
|
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shellout = ogOperations.execCMD(httpparser, ogRest)
|
shellout = ogOperations.execCMD(request, ogRest)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
|
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
|
||||||
return
|
return
|
||||||
|
|
||||||
if httpparser.getEcho():
|
if request.getEcho():
|
||||||
jsonResp = jsonResponse()
|
jsonResp = jsonResponse()
|
||||||
jsonResp.addElement('out', shellout)
|
jsonResp.addElement('out', shellout)
|
||||||
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
||||||
|
@ -88,9 +88,9 @@ class ogThread():
|
||||||
ogOperations.reboot()
|
ogOperations.reboot()
|
||||||
|
|
||||||
# Process session
|
# Process session
|
||||||
def procsession(client, httpparser, ogRest):
|
def procsession(client, request, ogRest):
|
||||||
try:
|
try:
|
||||||
ogOperations.procsession(httpparser, ogRest)
|
ogOperations.procsession(request, ogRest)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
||||||
return
|
return
|
||||||
|
@ -98,16 +98,16 @@ class ogThread():
|
||||||
client.send(restResponse.getResponse(ogResponses.OK))
|
client.send(restResponse.getResponse(ogResponses.OK))
|
||||||
|
|
||||||
# Process software
|
# Process software
|
||||||
def procsoftware(client, httpparser, path, ogRest):
|
def procsoftware(client, request, path, ogRest):
|
||||||
try:
|
try:
|
||||||
ogOperations.procsoftware(httpparser, path, ogRest)
|
ogOperations.procsoftware(request, path, ogRest)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
||||||
return
|
return
|
||||||
|
|
||||||
jsonResp = jsonResponse()
|
jsonResp = jsonResponse()
|
||||||
jsonResp.addElement('disk', httpparser.getDisk())
|
jsonResp.addElement('disk', request.getDisk())
|
||||||
jsonResp.addElement('partition', httpparser.getPartition())
|
jsonResp.addElement('partition', request.getPartition())
|
||||||
|
|
||||||
f = open(path, "r")
|
f = open(path, "r")
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
@ -132,19 +132,19 @@ class ogThread():
|
||||||
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
||||||
|
|
||||||
# Process setup
|
# Process setup
|
||||||
def procsetup(client, httpparser, ogRest):
|
def procsetup(client, request, ogRest):
|
||||||
jsonResp = jsonResponse()
|
jsonResp = jsonResponse()
|
||||||
jsonResp.addElement('disk', httpparser.getDisk())
|
jsonResp.addElement('disk', request.getDisk())
|
||||||
jsonResp.addElement('cache', httpparser.getCache())
|
jsonResp.addElement('cache', request.getCache())
|
||||||
jsonResp.addElement('cache_size', httpparser.getCacheSize())
|
jsonResp.addElement('cache_size', request.getCacheSize())
|
||||||
listconfig = ogOperations.procsetup(httpparser, ogRest)
|
listconfig = ogOperations.procsetup(request, ogRest)
|
||||||
jsonResp.addElement('partition_setup', listconfig)
|
jsonResp.addElement('partition_setup', listconfig)
|
||||||
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
||||||
|
|
||||||
# Process image restore
|
# Process image restore
|
||||||
def procirestore(client, httpparser, ogRest):
|
def procirestore(client, request, ogRest):
|
||||||
try:
|
try:
|
||||||
ogOperations.procirestore(httpparser, ogRest)
|
ogOperations.procirestore(request, ogRest)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
||||||
return
|
return
|
||||||
|
@ -152,20 +152,20 @@ class ogThread():
|
||||||
client.send(restResponse.getResponse(ogResponses.OK))
|
client.send(restResponse.getResponse(ogResponses.OK))
|
||||||
|
|
||||||
# Process image create
|
# Process image create
|
||||||
def procicreate(client, path, httpparser, ogRest):
|
def procicreate(client, path, request, ogRest):
|
||||||
try:
|
try:
|
||||||
ogOperations.procicreate(path, httpparser, ogRest)
|
ogOperations.procicreate(path, request, ogRest)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR))
|
||||||
return
|
return
|
||||||
|
|
||||||
jsonResp = jsonResponse()
|
jsonResp = jsonResponse()
|
||||||
jsonResp.addElement('disk', httpparser.getDisk())
|
jsonResp.addElement('disk', request.getDisk())
|
||||||
jsonResp.addElement('partition', httpparser.getPartition())
|
jsonResp.addElement('partition', request.getPartition())
|
||||||
jsonResp.addElement('code', httpparser.getCode())
|
jsonResp.addElement('code', request.getCode())
|
||||||
jsonResp.addElement('id', httpparser.getId())
|
jsonResp.addElement('id', request.getId())
|
||||||
jsonResp.addElement('name', httpparser.getName())
|
jsonResp.addElement('name', request.getName())
|
||||||
jsonResp.addElement('repository', httpparser.getRepo())
|
jsonResp.addElement('repository', request.getRepo())
|
||||||
f = open(path, "r")
|
f = open(path, "r")
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -198,9 +198,9 @@ class ogRest():
|
||||||
self.proc = None
|
self.proc = None
|
||||||
self.terminated = False
|
self.terminated = False
|
||||||
|
|
||||||
def processOperation(self, httpparser, client):
|
def processOperation(self, request, client):
|
||||||
op = httpparser.getRequestOP()
|
op = request.getRequestOP()
|
||||||
URI = httpparser.getURI()
|
URI = request.getURI()
|
||||||
|
|
||||||
if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
|
if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
|
||||||
client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED))
|
client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED))
|
||||||
|
@ -221,19 +221,19 @@ class ogRest():
|
||||||
elif ("reboot" in URI):
|
elif ("reboot" in URI):
|
||||||
self.process_reboot(client)
|
self.process_reboot(client)
|
||||||
elif ("shell/run" in URI):
|
elif ("shell/run" in URI):
|
||||||
self.process_shellrun(client, httpparser)
|
self.process_shellrun(client, request)
|
||||||
elif ("session" in URI):
|
elif ("session" in URI):
|
||||||
self.process_session(client, httpparser)
|
self.process_session(client, request)
|
||||||
elif ("software" in URI):
|
elif ("software" in URI):
|
||||||
self.process_software(client, httpparser)
|
self.process_software(client, request)
|
||||||
elif ("setup" in URI):
|
elif ("setup" in URI):
|
||||||
self.process_setup(client, httpparser)
|
self.process_setup(client, request)
|
||||||
elif ("image/restore" in URI):
|
elif ("image/restore" in URI):
|
||||||
self.process_irestore(client, httpparser)
|
self.process_irestore(client, request)
|
||||||
elif ("stop" in URI):
|
elif ("stop" in URI):
|
||||||
self.process_stop(client)
|
self.process_stop(client)
|
||||||
elif ("image/create" in URI):
|
elif ("image/create" in URI):
|
||||||
self.process_icreate(client, httpparser)
|
self.process_icreate(client, request)
|
||||||
elif ("refresh" in URI):
|
elif ("refresh" in URI):
|
||||||
self.process_refresh(client)
|
self.process_refresh(client)
|
||||||
else:
|
else:
|
||||||
|
@ -258,15 +258,15 @@ class ogRest():
|
||||||
jsonResp.addElement('status', 'OPG')
|
jsonResp.addElement('status', 'OPG')
|
||||||
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
|
||||||
|
|
||||||
def process_shellrun(self, client, httpparser):
|
def process_shellrun(self, client, request):
|
||||||
threading.Thread(target=ogThread.execcmd, args=(client, httpparser, self,)).start()
|
threading.Thread(target=ogThread.execcmd, args=(client, request, self,)).start()
|
||||||
|
|
||||||
def process_session(self, client, httpparser):
|
def process_session(self, client, request):
|
||||||
threading.Thread(target=ogThread.procsession, args=(client, httpparser, self,)).start()
|
threading.Thread(target=ogThread.procsession, args=(client, request, self,)).start()
|
||||||
|
|
||||||
def process_software(self, client, httpparser):
|
def process_software(self, client, request):
|
||||||
path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
|
path = '/tmp/CSft-' + client.ip + '-' + request.getPartition()
|
||||||
threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path, self,)).start()
|
threading.Thread(target=ogThread.procsoftware, args=(client, request, path, self,)).start()
|
||||||
|
|
||||||
def process_hardware(self, client):
|
def process_hardware(self, client):
|
||||||
path = '/tmp/Chrd-' + client.ip
|
path = '/tmp/Chrd-' + client.ip
|
||||||
|
@ -275,11 +275,11 @@ class ogRest():
|
||||||
def process_schedule(self, client):
|
def process_schedule(self, client):
|
||||||
client.send(restResponse.getResponse(ogResponses.OK))
|
client.send(restResponse.getResponse(ogResponses.OK))
|
||||||
|
|
||||||
def process_setup(self, client, httpparser):
|
def process_setup(self, client, request):
|
||||||
threading.Thread(target=ogThread.procsetup, args=(client, httpparser, self,)).start()
|
threading.Thread(target=ogThread.procsetup, args=(client, request, self,)).start()
|
||||||
|
|
||||||
def process_irestore(self, client, httpparser):
|
def process_irestore(self, client, request):
|
||||||
threading.Thread(target=ogThread.procirestore, args=(client, httpparser, self,)).start()
|
threading.Thread(target=ogThread.procirestore, args=(client, request, self,)).start()
|
||||||
|
|
||||||
def process_stop(self, client):
|
def process_stop(self, client):
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
@ -291,9 +291,9 @@ class ogRest():
|
||||||
self.terminated = True
|
self.terminated = True
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def process_icreate(self, client, httpparser):
|
def process_icreate(self, client, request):
|
||||||
path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
|
path = '/tmp/CSft-' + client.ip + '-' + request.getPartition()
|
||||||
threading.Thread(target=ogThread.procicreate, args=(client, path, httpparser, self,)).start()
|
threading.Thread(target=ogThread.procicreate, args=(client, path, request, self,)).start()
|
||||||
|
|
||||||
def process_refresh(self, client):
|
def process_refresh(self, client):
|
||||||
threading.Thread(target=ogThread.procrefresh, args=(client, self,)).start()
|
threading.Thread(target=ogThread.procrefresh, args=(client, self,)).start()
|
||||||
|
|
Loading…
Reference in New Issue