Add stop command

This patch includes a new support for stopping all the process running on
the ogClient.
more_events
Alvaro Neira Ayuso 2020-01-16 19:22:44 +01:00 committed by Alvaro Neira Ayuso
parent 230bdca0ea
commit d5dca0f756
2 changed files with 71 additions and 35 deletions

View File

@ -15,47 +15,51 @@ def reboot():
else: else:
subprocess.call(['/sbin/reboot']) subprocess.call(['/sbin/reboot'])
def execCMD(httpparser): def execCMD(httpparser, ogRest):
cmd = httpparser.getCMD() cmd = httpparser.getCMD()
cmds = cmd.split(" ") cmds = cmd.split(" ")
try: try:
result = subprocess.check_output(cmds) ogRest.proc = subprocess.Popen(cmds, stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
raise ValueError('Error: Incorrect command value') raise ValueError('Error: Incorrect command value')
return result.decode('utf-8') return output.decode('utf-8')
def procsession(httpparser): def procsession(httpparser, ogRest):
disk = httpparser.getDisk() disk = httpparser.getDisk()
partition = httpparser.getPartition() partition = httpparser.getPartition()
try: try:
result = subprocess.check_output([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], shell=True) ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/IniciarSesion', disk, partition], stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
raise ValueError('Error: Incorrect command value') raise ValueError('Error: Incorrect command value')
return result.decode('utf-8') return output.decode('utf-8')
def procsoftware(httpparser, path): def procsoftware(httpparser, path, ogRest):
disk = httpparser.getDisk() disk = httpparser.getDisk()
partition = httpparser.getPartition() partition = httpparser.getPartition()
try: try:
result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], shell=True) ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioSoftware', disk, partition, path], stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
raise ValueError('Error: Incorrect command value') raise ValueError('Error: Incorrect command value')
return result.decode('utf-8') return output.decode('utf-8')
def prochardware(path): def prochardware(path, ogRest):
try: try:
result = subprocess.check_output([OG_PATH + 'interfaceAdm/InventarioHardware', path], shell=True) ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/InventarioHardware', path], stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
raise ValueError('Error: Incorrect command value') raise ValueError('Error: Incorrect command value')
return result.decode('utf-8') return output.decode('utf-8')
def procsetup(httpparser): def procsetup(httpparser, ogRest):
disk = httpparser.getDisk() disk = httpparser.getDisk()
cache = httpparser.getCache() cache = httpparser.getCache()
cachesize = httpparser.getCacheSize() cachesize = httpparser.getCacheSize()
@ -66,8 +70,12 @@ def procsetup(httpparser):
i = 0 i = 0
json = {} json = {}
cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%' cfg = 'dis=' + disk + '*che=' + cache + '*tch=' + cachesize + '!par=' + part["partition"] + '*cpt='+part["code"] + '*sfi=' + part['filesystem'] + '*tam=' + part['size'] + '*ope=' + part['format'] + '%'
if ogRest.terminated:
break
try: try:
subprocess.check_output([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], shell=True) ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/Configurar', disk, cfg], stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
continue continue
@ -86,7 +94,7 @@ def procsetup(httpparser):
return listConfigs return listConfigs
def procirestore(httpparser): def procirestore(httpparser, ogRest):
disk = httpparser.getDisk() disk = httpparser.getDisk()
partition = httpparser.getPartition() partition = httpparser.getPartition()
name = httpparser.getName() name = httpparser.getName()
@ -96,8 +104,9 @@ def procirestore(httpparser):
cid = httpparser.getId() cid = httpparser.getId()
try: try:
result = subprocess.check_output([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], shell=True) ogRest.proc = subprocess.Popen([OG_PATH + 'interfaceAdm/RestaurarImagen', disk, partition, name, repo, ctype], stdout=subprocess.PIPE, shell=True)
(output, error) = ogRest.proc.communicate()
except: except:
raise ValueError('Error: Incorrect command value') raise ValueError('Error: Incorrect command value')
return result.decode('utf-8') return output.decode('utf-8')

View File

@ -4,6 +4,9 @@ import time
from enum import Enum from enum import Enum
import json import json
import queue import queue
import sys
import os
import signal
from src.HTTPParser import * from src.HTTPParser import *
@ -31,6 +34,8 @@ class restResponse():
msg = 'HTTP/1.0 200 OK' msg = 'HTTP/1.0 200 OK'
elif response == ogResponses.INTERNAL_ERR: elif response == ogResponses.INTERNAL_ERR:
msg = 'HTTP/1.0 500 Internal Server Error' msg = 'HTTP/1.0 500 Internal Server Error'
elif response == ogResponses.UNAUTHORIZED:
msg = 'HTTP/1.0 401 Unauthorized'
else: else:
return msg return msg
@ -47,13 +52,13 @@ class restResponse():
class ogThread(): class ogThread():
# Executing cmd thread # Executing cmd thread
def execcmd(client, httpparser): def execcmd(client, httpparser, ogRest):
if httpparser.getCMD() == None: if httpparser.getCMD() == None:
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
return return
try: try:
shellout = ogOperations.execCMD(httpparser) shellout = ogOperations.execCMD(httpparser, 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
@ -75,9 +80,9 @@ class ogThread():
ogOperations.reboot() ogOperations.reboot()
# Process session # Process session
def procsession(client, httpparser): def procsession(client, httpparser, ogRest):
try: try:
ogOperations.procsession(httpparser) ogOperations.procsession(httpparser, 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
@ -85,9 +90,9 @@ class ogThread():
client.send(restResponse.getResponse(ogResponses.OK)) client.send(restResponse.getResponse(ogResponses.OK))
# Process software # Process software
def procsoftware(client, httpparser, path): def procsoftware(client, httpparser, path, ogRest):
try: try:
ogOperations.procsoftware(httpparser, path) ogOperations.procsoftware(httpparser, 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
@ -104,9 +109,9 @@ class ogThread():
client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) client.send(restResponse.getResponse(ogResponses.OK, jsonResp))
# Process hardware # Process hardware
def prochardware(client, path): def prochardware(client, path, ogRest):
try: try:
ogOperations.prochardware(path) ogOperations.prochardware(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
@ -119,19 +124,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): def procsetup(client, httpparser, ogRest):
jsonResp = jsonResponse() jsonResp = jsonResponse()
jsonResp.addElement('disk', httpparser.getDisk()) jsonResp.addElement('disk', httpparser.getDisk())
jsonResp.addElement('cache', httpparser.getCache()) jsonResp.addElement('cache', httpparser.getCache())
jsonResp.addElement('cache_size', httpparser.getCacheSize()) jsonResp.addElement('cache_size', httpparser.getCacheSize())
listconfig = ogOperations.procsetup(httpparser) listconfig = ogOperations.procsetup(httpparser, 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(httpparser): def procirestore(httpparser, ogRest):
try: try:
ogOperations.procirestore(httpparser) ogOperations.procirestore(httpparser, 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
@ -143,11 +148,21 @@ class ogResponses(Enum):
IN_PROGRESS=1 IN_PROGRESS=1
OK=2 OK=2
INTERNAL_ERR=3 INTERNAL_ERR=3
UNAUTHORIZED=4
class ogRest(): class ogRest():
def __init__(self):
self.proc = None
self.terminated = False
def processOperation(self, httpparser, client): def processOperation(self, httpparser, client):
op = httpparser.getRequestOP() op = httpparser.getRequestOP()
URI = httpparser.getURI() URI = httpparser.getURI()
if (not "stop" in URI and not self.proc == None and self.proc.poll() == None):
client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED))
return
if ("GET" in op): if ("GET" in op):
if "hardware" in URI: if "hardware" in URI:
self.process_hardware(client) self.process_hardware(client)
@ -172,6 +187,8 @@ class ogRest():
self.process_setup(client, httpparser) self.process_setup(client, httpparser)
elif ("image/restore" in URI): elif ("image/restore" in URI):
self.process_irestore(client, httpparser) self.process_irestore(client, httpparser)
elif ("stop" in URI):
self.process_stop(client)
else: else:
client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) client.send(restResponse.getResponse(ogResponses.BAD_REQUEST))
else: else:
@ -195,24 +212,34 @@ class ogRest():
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, httpparser):
threading.Thread(target=ogThread.execcmd, args=(client, httpparser,)).start() threading.Thread(target=ogThread.execcmd, args=(client, httpparser, self,)).start()
def process_session(self, client, httpparser): def process_session(self, client, httpparser):
threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start() threading.Thread(target=ogThread.procsession, args=(client, httpparser, self,)).start()
def process_software(self, client, httpparser): def process_software(self, client, httpparser):
path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition() path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition()
threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path,)).start() threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path, self,)).start()
def process_hardware(self, client): def process_hardware(self, client):
path = '/tmp/Chrd-' + client.ip path = '/tmp/Chrd-' + client.ip
threading.Thread(target=ogThread.prochardware, args=(client, path,)).start() threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start()
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, httpparser):
threading.Thread(target=ogThread.procsetup, args=(client, httpparser,)).start() threading.Thread(target=ogThread.procsetup, args=(client, httpparser, self,)).start()
def process_irestore(self, client, httpparser): def process_irestore(self, client, httpparser):
threading.Thread(target=ogThread.procirestore, args=(client, httpparser,)).start() threading.Thread(target=ogThread.procirestore, args=(client, httpparser, self,)).start()
def process_stop(self, client):
client.disconnect()
if self.proc == None:
return
if self.proc.poll() == None:
os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM)
self.terminated = True
sys.exit(0)