added Shell, File and Sequence plugins
This commit is contained in:
parent
add7806166
commit
af43655dfd
|
@ -1,4 +1,5 @@
|
|||
import requests
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
class PyExecPlugins(type):
|
||||
|
@ -36,8 +37,35 @@ class Identity(PyExecPlugin):
|
|||
super().__init__(data)
|
||||
|
||||
def execute(self):
|
||||
self.data.pop("_result", None)
|
||||
return self.data
|
||||
|
||||
class Sequence(PyExecPlugin):
|
||||
name = "Sequence"
|
||||
|
||||
def __init__(self, data=None):
|
||||
super().__init__(data)
|
||||
|
||||
def handleSequence(self, sequence, currentresults):
|
||||
for t in sequence:
|
||||
operation = t.get("operation", "Nop")
|
||||
if operation == "Sequence":
|
||||
self.handleSequence(t.tasks, self.results)
|
||||
else:
|
||||
p = PyExecPlugin.get(operation)
|
||||
if p != None:
|
||||
t["_result"] = currentresults
|
||||
pi = p(t)
|
||||
ret = pi.execute()
|
||||
self.result.append(ret)
|
||||
|
||||
else: raise Exception("Operation {} not found.".format(operation))
|
||||
|
||||
def execute(self):
|
||||
self.result = []
|
||||
self.handleSequence(self.data.get("tasks",[]),self.result)
|
||||
return {"result": self.result}
|
||||
|
||||
class Http(PyExecPlugin):
|
||||
name = "Http"
|
||||
|
||||
|
@ -82,15 +110,17 @@ class Http(PyExecPlugin):
|
|||
outbody = self.response.json() if len(self.response.content) != 0 else None
|
||||
else:
|
||||
outbody = self.response.text
|
||||
print(outbody)
|
||||
#print(outbody)
|
||||
return {
|
||||
'status': status,
|
||||
'output': {
|
||||
"body" : outbody,
|
||||
"headers" : hdrs,
|
||||
"status" : self.response.status_code,
|
||||
"reason" : self.response.reason},
|
||||
'logs': ['one', 'two']}
|
||||
"reason" : self.response.reason
|
||||
},
|
||||
'logs': ['one', 'two']
|
||||
}
|
||||
|
||||
def execute(self):
|
||||
self.doRequest()
|
||||
|
@ -108,3 +138,49 @@ class Eval(PyExecPlugin):
|
|||
if code != None:
|
||||
ret = eval(code, { "data" : self.data})
|
||||
return { "result" : ret }
|
||||
|
||||
class Write(PyExecPlugin):
|
||||
name = "Write"
|
||||
|
||||
def __init__(self, data=None):
|
||||
super().__init__(data)
|
||||
|
||||
def execute(self):
|
||||
print("File opening at", self.data["path"])
|
||||
fo = open(self.data["path"], "w")
|
||||
print("File opened", fo)
|
||||
typ = self.data.get("type", "text")
|
||||
if typ == "json":
|
||||
fo.write(json.dumps(self.data.get("content","{}")))
|
||||
else:
|
||||
fo.write(self.data.get("content", ""))
|
||||
fo.close()
|
||||
return {}
|
||||
|
||||
class Shell(PyExecPlugin):
|
||||
name = "Shell"
|
||||
|
||||
def __init__(self, data=None):
|
||||
super().__init__(data)
|
||||
|
||||
def execute(self):
|
||||
output = {
|
||||
'results' : [],
|
||||
'status' : "COMPLETED"
|
||||
}
|
||||
for cmdentry in self.data.get("commands"):
|
||||
expect = cmdentry.get("expect", 0)
|
||||
cmd = cmdentry.get("line")
|
||||
#Use shell=True for very complex cmdlines that do have quotes and ther amenities
|
||||
withshell = cmdentry.get("withshell", self.data.get("withshell", False))
|
||||
if cmd == None:
|
||||
continue
|
||||
else:
|
||||
cmd = cmd.split() if not withshell else cmd
|
||||
print("Going to execute", withshell, cmd)
|
||||
completed = subprocess.run(cmd, shell=withshell, capture_output=True, text=True)
|
||||
output["results"].append({ 'returncode' : completed.returncode, 'stdout' : completed.stdout, 'stderr': completed.stderr})
|
||||
if completed.returncode != expect:
|
||||
output["status"] = "FAILED"
|
||||
break
|
||||
return output
|
||||
|
|
Loading…
Reference in New Issue