UI - instances/services backend update (needs testing)
This commit is contained in:
parent
f9b9b9546f
commit
c65b78b1cc
|
@ -20,7 +20,10 @@ app.jinja_env.globals.update(form_service_gen_multiple_values=utils.form_service
|
|||
@app.route('/')
|
||||
@app.route('/home')
|
||||
def home():
|
||||
check, instances = wrappers.get_instances()
|
||||
check, client = wrappers.get_client()
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=client)
|
||||
check, instances = wrappers.get_instances(client)
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=instances)
|
||||
check, services = wrappers.get_services()
|
||||
|
@ -31,6 +34,11 @@ def home():
|
|||
@app.route('/instances', methods=["GET", "POST"])
|
||||
def instances():
|
||||
|
||||
# Get the client
|
||||
check, client = wrappers.get_client()
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=client)
|
||||
|
||||
# Manage instances
|
||||
operation = ""
|
||||
if request.method == "POST" :
|
||||
|
@ -44,12 +52,12 @@ def instances():
|
|||
return render_template("error.html", title="Error", error="Missing INSTANCE_ID parameter.")
|
||||
|
||||
# Do the operation
|
||||
check, operation = wrappers.operation_instance(request.form)
|
||||
check, operation = wrappers.operation_instance(client, request.form)
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=operation)
|
||||
|
||||
# Display instances
|
||||
check, instances = wrappers.get_instances()
|
||||
check, instances = wrappers.get_instances(client)
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=instances)
|
||||
return render_template("instances.html", title="Instances", instances=instances, operation=operation)
|
||||
|
@ -57,12 +65,15 @@ def instances():
|
|||
@app.route('/services', methods=["GET", "POST"])
|
||||
def services():
|
||||
|
||||
# Get the client
|
||||
check, client = wrappers.get_client()
|
||||
if not check :
|
||||
return render_template("error.html", title="Error", error=client)
|
||||
|
||||
# Manage services
|
||||
operation = ""
|
||||
if request.method == "POST" :
|
||||
|
||||
print(request.form, flush=True)
|
||||
|
||||
# Check operation
|
||||
if not "operation" in request.form or not request.form["operation"] in ["new", "edit", "delete"] :
|
||||
return render_template("error.html", title="Error", error="Missing operation parameter on /services.")
|
||||
|
@ -72,11 +83,22 @@ def services():
|
|||
if request.form["operation"] in ["new", "edit"] :
|
||||
for category in current_app.config["CONFIG"] :
|
||||
for param in current_app.config["CONFIG"][category]["params"] :
|
||||
if not param["env"] in request.form :
|
||||
return render_template("error.html", title="Error", error="Missing " + param["env"] + " parameter.")
|
||||
if not re.search(param["regex"], request.form[param["env"]]) :
|
||||
return render_template("error.html", title="Error", error="Parameter " + param["env"] + " doesn't match regex.")
|
||||
env[param["env"]] = request.form[param["env"]]
|
||||
if param["type"] == "multiple" :
|
||||
for param_user in request.form :
|
||||
if param_user.startswith(param["params"][0]["env"]) :
|
||||
suffix = param_user.replace(param["params"][0]["env"], "")
|
||||
for param_multiple in param["params"] :
|
||||
if not param_multiple["env"] + suffix in request.form :
|
||||
return render_template("error.html", title="Error", error="Missing " + param["env"] + " parameter.")
|
||||
if not re.search(param_multiple["regex"], request.form[param_multiple["env"] + suffix]) :
|
||||
return render_template("error.html", title="Error", error="Parameter " + param["env"] + " doesn't match regex.")
|
||||
env[param_multiple["env"] + suffix] = request.form[param_multiple["env"] + suffix]
|
||||
else :
|
||||
if not param["env"] in request.form :
|
||||
return render_template("error.html", title="Error", error="Missing " + param["env"] + " parameter.")
|
||||
if not re.search(param["regex"], request.form[param["env"]]) :
|
||||
return render_template("error.html", title="Error", error="Parameter " + param["env"] + " doesn't match regex.")
|
||||
env[param["env"]] = request.form[param["env"]]
|
||||
if request.form["operation"] == "edit" :
|
||||
if not "OLD_SERVER_NAME" in request.form :
|
||||
return render_template("error.html", title="Error", error="Missing OLD_SERVER_NAME parameter.")
|
||||
|
@ -89,7 +111,7 @@ def services():
|
|||
return render_template("error.html", title="Error", error="Parameter SERVER_NAME doesn't match regex.")
|
||||
|
||||
# Do the operation
|
||||
check, operation = wrappers.operation_service(request.form, env)
|
||||
check, operation = wrappers.operation_service(client, request.form, env)
|
||||
if not check :
|
||||
render_template("error.html", title="Error", error=operation)
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
{% if operation != "" %}
|
||||
<div class="row justify-content-center">
|
||||
<div class="col col-4 mb-3 text-center">
|
||||
<div class="col col-12 col-md-4 mb-3 text-center">
|
||||
<div class="alert alert-primary alert-dismissible fade show text-break" role="alert">
|
||||
{{ operation }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
|
|
128
ui/wrappers.py
128
ui/wrappers.py
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import utils, config
|
||||
import docker, os, stat, sys
|
||||
import docker, os, stat, sys, subprocess
|
||||
|
||||
def get_client() :
|
||||
endpoint = "/var/run/docker.sock"
|
||||
|
@ -13,18 +13,15 @@ def get_client() :
|
|||
return False, "Can't instantiate DockerClient : " + str(e)
|
||||
return True, client
|
||||
|
||||
def get_containers(label) :
|
||||
check, client = get_client()
|
||||
if not check :
|
||||
return check, client
|
||||
def get_containers(client, label) :
|
||||
try :
|
||||
containers = client.containers.list(all=True, filters={"label" : "bunkerized-nginx." + label})
|
||||
except docker.errors.APIError as e :
|
||||
return False, "Docker API error " + str(e)
|
||||
return True, containers
|
||||
|
||||
def get_instances() :
|
||||
return get_containers("UI")
|
||||
def get_instances(client) :
|
||||
return get_containers(client, "UI")
|
||||
|
||||
def get_services() :
|
||||
services = []
|
||||
|
@ -44,48 +41,129 @@ def get_services() :
|
|||
return False, str(e)
|
||||
return True, services
|
||||
|
||||
def new_service(env) :
|
||||
def reload_instances(client) :
|
||||
check, instances = get_instances(client)
|
||||
if not check :
|
||||
return check, instances
|
||||
i = 0
|
||||
for instance in intances :
|
||||
try :
|
||||
instance.kill(signal="SIGHUP")
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
i += 1
|
||||
return True, i
|
||||
|
||||
def new_service(client, env) :
|
||||
proc = subprocess.run(["/opt/entrypoint/site-config.sh", env["SERVER_NAME"]], env=env, capture_output=True)
|
||||
if proc.returncode != 0 :
|
||||
return False, "Error code " + str(proc.returncode) + " while generating config."
|
||||
check, nb = reload_instances(client)
|
||||
if not check :
|
||||
return check, nb
|
||||
return True, "Web service " + env["SERVER_NAME"] + " has been added."
|
||||
|
||||
def edit_service(old_server_name, env) :
|
||||
def edit_service(client, old_server_name, env) :
|
||||
check, delete = delete_service(client, old_server_name)
|
||||
if not check :
|
||||
return check, delete
|
||||
check, new = new_service(client, env)
|
||||
if not check :
|
||||
return check, new
|
||||
check, nb = reload_instances(client)
|
||||
if not check :
|
||||
return check, nb
|
||||
return True, "Web service " + old_server_name + " has been edited."
|
||||
|
||||
def delete_service(server_name) :
|
||||
def delete_service(client, server_name) :
|
||||
if not os.path.isdir("/etc/nginx/" + server_name) :
|
||||
return False, "Config doesn't exist."
|
||||
try :
|
||||
shutil.rmtree("/etc/nginx/" + server_name)
|
||||
except Exception as e :
|
||||
return False, str(e)
|
||||
check, nb = reload_instances(client)
|
||||
if not check :
|
||||
return check, nb
|
||||
return True, "Web service " + server_name + " has been deleted."
|
||||
|
||||
def operation_service(form, env) :
|
||||
def operation_service(client, form, env) :
|
||||
if form["operation"] == "new" :
|
||||
return new_service(env)
|
||||
return new_service(client, env)
|
||||
if form["operation"] == "edit" :
|
||||
return edit_service(form["OLD_SERVER_NAME"], env)
|
||||
return edit_service(client, form["OLD_SERVER_NAME"], env)
|
||||
if form["operation"] == "delete" :
|
||||
return delete_service(form["SERVER_NAME"])
|
||||
return delete_service(client, form["SERVER_NAME"])
|
||||
return False, "Wrong operation parameter."
|
||||
|
||||
def reload_instance(id) :
|
||||
def get_instance(client, id) :
|
||||
try :
|
||||
instance = client.containers.get(id)
|
||||
if not "bunkerized-nginx.UI" in instance.labels :
|
||||
raise docker.errors.NotFound()
|
||||
except Exception as e :
|
||||
return False, str(e)
|
||||
return True, instance
|
||||
|
||||
def reload_instance(client, id) :
|
||||
check, instance = get_instance(client, id)
|
||||
if not check :
|
||||
return check, instance
|
||||
try :
|
||||
instance.kill(signal="SIGHUP")
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
return True, "Instance " + id + " has been reloaded."
|
||||
|
||||
def start_instance(id) :
|
||||
def start_instance(client, id) :
|
||||
check, instance = get_instance(client, id)
|
||||
if not check :
|
||||
return check, instance
|
||||
try :
|
||||
instance.start()
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
return True, "Instance " + id + " has been started."
|
||||
|
||||
def stop_instance(id) :
|
||||
def stop_instance(client, id) :
|
||||
check, instance = get_instance(client, id)
|
||||
if not check :
|
||||
return check, instance
|
||||
try :
|
||||
instance.stop()
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
return True, "Instance " + id + " has been stopped."
|
||||
|
||||
def restart_instance(id) :
|
||||
def restart_instance(client, id) :
|
||||
check, instance = get_instance(client, id)
|
||||
if not check :
|
||||
return check, instance
|
||||
try :
|
||||
instance.restart()
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
return True, "Instance " + id + " has been restarted."
|
||||
|
||||
def delete_instance(id) :
|
||||
def delete_instance(client, id) :
|
||||
check, instance = get_instance(client, id)
|
||||
if not check :
|
||||
return check, instance
|
||||
try :
|
||||
instance.remove(v=True, force=True)
|
||||
except docker.errors.APIError as e :
|
||||
return False, str(e)
|
||||
return True, "Instance " + id + " has been deleted."
|
||||
|
||||
def operation_instance(form) :
|
||||
def operation_instance(client, form) :
|
||||
if form["operation"] == "reload" :
|
||||
return reload_instance(form["INSTANCE_ID"])
|
||||
return reload_instance(client, form["INSTANCE_ID"])
|
||||
if form["operation"] == "start" :
|
||||
return start_instance(form["INSTANCE_ID"])
|
||||
return start_instance(client, form["INSTANCE_ID"])
|
||||
if form["operation"] == "stop" :
|
||||
return stop_instance(form["INSTANCE_ID"])
|
||||
return stop_instance(client, form["INSTANCE_ID"])
|
||||
if form["operation"] == "restart" :
|
||||
return restart_instance(form["INSTANCE_ID"])
|
||||
return restart_instance(client, form["INSTANCE_ID"])
|
||||
if form["operation"] == "delete" :
|
||||
return delete_instance(form["INSTANCE_ID"])
|
||||
return delete_instance(client, form["INSTANCE_ID"])
|
||||
return False, "Wrong operation parameter."
|
||||
|
|
Loading…
Reference in New Issue