Files
connpy/connpy/api.py

170 lines
4.6 KiB
Python
Raw Normal View History

2023-04-06 18:47:29 -03:00
from flask import Flask, request, jsonify
from connpy import configfile, node, nodes
from connpy.ai import ai as myai
2023-04-06 18:47:29 -03:00
from waitress import serve
import os
import signal
app = Flask(__name__)
conf = configfile()
2023-04-14 11:44:56 -03:00
PID_FILE1 = "/run/connpy.pid"
PID_FILE2 = "/tmp/connpy.pid"
2023-04-06 18:47:29 -03:00
@app.route("/")
2023-04-14 11:44:56 -03:00
def root():
return jsonify({
'message': 'Welcome to Connpy api',
'version': '1.0',
'documentation': 'https://fluzzi.github.io/connpy/'
})
@app.route("/list_nodes", methods=["POST"])
def list_nodes():
conf = app.custom_config
case = conf.config["case"]
try:
data = request.get_json()
filter = data["filter"]
if not case:
if isinstance(filter, list):
filter = [item.lower() for item in filter]
else:
filter = filter.lower()
output = conf._getallnodes(filter)
2023-04-14 11:44:56 -03:00
except:
output = conf._getallnodes()
return jsonify(output)
@app.route("/get_nodes", methods=["POST"])
def get_nodes():
conf = app.custom_config
case = conf.config["case"]
try:
data = request.get_json()
filter = data["filter"]
if not case:
if isinstance(filter, list):
filter = [item.lower() for item in filter]
else:
filter = filter.lower()
output = conf._getallnodesfull(filter)
except:
output = conf._getallnodesfull()
2023-04-14 11:44:56 -03:00
return jsonify(output)
2023-04-06 18:47:29 -03:00
@app.route("/ask_ai", methods=["POST"])
def ask_ai():
conf = app.custom_config
data = request.get_json()
input = data["input"]
if "dryrun" in data:
dryrun = data["dryrun"]
else:
dryrun = False
ai = myai(conf)
return ai.ask(input, dryrun)
2023-04-06 18:47:29 -03:00
@app.route("/run_commands", methods=["POST"])
def run_commands():
conf = app.custom_config
data = request.get_json()
2023-04-14 11:44:56 -03:00
case = conf.config["case"]
mynodes = {}
args = {}
try:
action = data["action"]
nodelist = data["nodes"]
args["commands"] = data["commands"]
if action == "test":
args["expected"] = data["expected"]
except KeyError as e:
error = "'{}' is mandatory".format(e.args[0])
return({"DataError": error})
if isinstance(nodelist, list):
mynodes = conf.getitems(nodelist)
2023-04-14 11:44:56 -03:00
else:
if not case:
nodelist = nodelist.lower()
if nodelist.startswith("@"):
mynodes = conf.getitem(nodelist)
else:
mynodes[nodelist] = conf.getitem(nodelist)
mynodes = nodes(mynodes, config=conf)
try:
args["vars"] = data["variables"]
except:
pass
try:
options = data["options"]
thisoptions = {k: v for k, v in options.items() if k in ["prompt", "parallel", "timeout"]}
args.update(thisoptions)
except:
options = None
if action == "run":
output = mynodes.run(**args)
elif action == "test":
output = mynodes.test(**args)
else:
error = "Wrong action '{}'".format(action)
return({"DataError": error})
2023-04-06 18:47:29 -03:00
return output
def stop_api():
# Read the process ID (pid) from the file
2023-04-14 11:44:56 -03:00
try:
with open(PID_FILE1, "r") as f:
2023-04-15 22:38:52 -03:00
pid = int(f.readline().strip())
port = int(f.readline().strip())
2023-04-14 11:44:56 -03:00
PID_FILE=PID_FILE1
except:
try:
with open(PID_FILE2, "r") as f:
2023-04-15 22:38:52 -03:00
pid = int(f.readline().strip())
port = int(f.readline().strip())
2023-04-14 11:44:56 -03:00
PID_FILE=PID_FILE2
except:
print("Connpy api server is not running.")
return
2023-04-06 18:47:29 -03:00
# Send a SIGTERM signal to the process
2023-04-18 18:00:43 -03:00
try:
os.kill(pid, signal.SIGTERM)
except:
pass
2023-04-06 18:47:29 -03:00
# Delete the PID file
os.remove(PID_FILE)
print(f"Server with process ID {pid} stopped.")
2023-04-15 22:38:52 -03:00
return port
def debug_api(port=8048):
app.custom_config = configfile()
app.run(debug=True, port=port)
2023-04-06 18:47:29 -03:00
2023-04-15 22:38:52 -03:00
def start_server(port=8048):
2023-04-14 11:44:56 -03:00
app.custom_config = configfile()
2023-04-15 22:38:52 -03:00
serve(app, host='0.0.0.0', port=port)
2023-04-06 18:47:29 -03:00
2023-04-15 22:38:52 -03:00
def start_api(port=8048):
2023-04-14 11:44:56 -03:00
if os.path.exists(PID_FILE1) or os.path.exists(PID_FILE2):
print("Connpy server is already running.")
return
2023-04-06 18:47:29 -03:00
pid = os.fork()
if pid == 0:
2023-04-15 22:38:52 -03:00
start_server(port)
2023-04-06 18:47:29 -03:00
else:
2023-04-14 11:44:56 -03:00
try:
with open(PID_FILE1, "w") as f:
2023-04-15 22:38:52 -03:00
f.write(str(pid) + "\n" + str(port))
2023-04-14 11:44:56 -03:00
except:
try:
with open(PID_FILE2, "w") as f:
2023-04-15 22:38:52 -03:00
f.write(str(pid) + "\n" + str(port))
2023-04-14 11:44:56 -03:00
except:
print("Cound't create PID file")
return
2023-04-15 22:38:52 -03:00
print(f'Server is running with process ID {pid} in port {port}')
2023-04-06 18:47:29 -03:00