summaryrefslogtreecommitdiffstats
path: root/src/ogRest.py
diff options
context:
space:
mode:
authorAlvaro Neira Ayuso <aneira@soleta.eu>2019-12-27 13:46:49 +0100
committerAlvaro Neira Ayuso <alvaroneay@gmail.com>2020-01-19 19:50:44 +0100
commite20daf639dd271268b172b42adc0d1b9d2103883 (patch)
treeaa9a27298d6a8b88d2a9d1364c5ab8afe00ea780 /src/ogRest.py
parentdfc97ffedb9a356299b593f76ce057f5953a77ab (diff)
Add shell run and output commands
Opengnsys needs a support to execute commands on the machine. This patch adds the support for executing two new commands "shell/run" and "shell/output". The first one, give us the support for executing a command in the machine and keep save in a queue the output. The second one, give us the support for sending the output from the command executed.
Diffstat (limited to 'src/ogRest.py')
-rw-r--r--src/ogRest.py54
1 files changed, 47 insertions, 7 deletions
diff --git a/src/ogRest.py b/src/ogRest.py
index 85e961a..ac02a4f 100644
--- a/src/ogRest.py
+++ b/src/ogRest.py
@@ -2,6 +2,8 @@ import threading
import platform
import time
from enum import Enum
+import json
+import queue
if platform.system() == 'Linux':
from src.linux import ogOperations
@@ -12,21 +14,44 @@ class ogResponses(Enum):
OK=2
class ogRest():
- def getResponse(self, response):
+ def __init__(self):
+ self.msgqueue = queue.Queue(1000)
+
+ def buildJsonResponse(self, idstr, content):
+ data = { idstr :content }
+ return json.dumps(data)
+
+ def getResponse(self, response, idstr=None, content=None):
+ msg = ''
if response == ogResponses.BAD_REQUEST:
- return 'HTTP/1.0 400 Bad request\r\n\r\n'
- if response == ogResponses.IN_PROGRESS:
- return 'HTTP/1.0 202 Accepted\r\n\r\n'
- if response == ogResponses.OK:
- return 'HTTP/1.0 200 OK\r\n\r\n'
+ msg = 'HTTP/1.0 400 Bad request'
+ elif response == ogResponses.IN_PROGRESS:
+ msg = 'HTTP/1.0 202 Accepted'
+ elif response == ogResponses.OK:
+ msg = 'HTTP/1.0 200 OK'
+ else:
+ return msg
- def processOperation(self, op, URI, client):
+ if not content == None:
+ jsonmsg = self.buildJsonResponse(idstr, content)
+ msg = msg + '\nContent-Type:application/json'
+ msg = msg + '\nContent-Length:' + str(len(jsonmsg))
+ msg = msg + '\n' + jsonmsg
+
+ msg = msg + '\r\n\r\n'
+ return msg
+
+ def processOperation(self, op, URI, cmd, client):
if ("poweroff" in URI):
self.process_poweroff(client)
elif ("reboot" in URI):
self.process_reboot(client)
elif ("probe" in URI):
self.process_probe(client)
+ elif ("shell/run" in URI):
+ self.process_shellrun(client, cmd)
+ elif ("shell/output" in URI):
+ self.process_shellout(client)
else:
client.send(self.getResponse(ogResponses.BAD_REQUEST))
@@ -53,3 +78,18 @@ class ogRest():
def process_probe(self, client):
client.send(self.getResponse(ogResponses.OK))
+
+ def process_shellrun(self, client, cmd):
+ if cmd == None:
+ client.send(self.getResponse(ogResponses.BAD_REQUEST))
+ return
+
+ self.msgqueue.put(ogOperations.execCMD(cmd))
+ client.send(self.getResponse(ogResponses.IN_PROGRESS))
+
+ def process_shellout(self, client):
+ if self.msgqueue.empty():
+ client.send(self.getResponse(ogResponses.IN_PROGRESS, 'out', ''))
+ else:
+ out = self.msgqueue.get()
+ client.send(self.getResponse(ogResponses.IN_PROGRESS, 'out', out))