UI - digging bugs from services, still some work to do

This commit is contained in:
bunkerity 2021-07-09 16:58:55 +02:00
parent 72a09eac6d
commit 3af1b397fa
No known key found for this signature in database
GPG Key ID: 3D80806F12602A7C
8 changed files with 49 additions and 70 deletions

View File

@ -656,7 +656,7 @@
"env": "SELF_SIGNED_SSL_COUNTRY",
"id": "self-signed-ssl-country",
"label": "Country of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Z]{2}$",
"type": "text"
},
{
@ -665,7 +665,7 @@
"env": "SELF_SIGNED_SSL_STATE",
"id": "self-signed-ssl-state",
"label": "State of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Za-z\\-]+$",
"type": "text"
},
{
@ -674,7 +674,7 @@
"env": "SELF_SIGNED_SSL_CITY",
"id": "self-signed-ssl-city",
"label": "City of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Za-z\\-]+$",
"type": "text"
},
{
@ -683,7 +683,7 @@
"env": "SELF_SIGNED_SSL_OU",
"id": "self-signed-ssl-ou",
"label": "Organizational Unit of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Za-z\\-]+$$",
"type": "text"
},
{
@ -692,7 +692,7 @@
"env": "SELF_SIGNED_SSL_ORG",
"id": "self-signed-ssl-org",
"label": "Organization name of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Za-z\\- ]+$$",
"type": "text"
},
{
@ -701,7 +701,7 @@
"env": "SELF_SIGNED_SSL_CN",
"id": "self-signed-ssl-cn",
"label": "Common Name of the self-signed certificate",
"regex": "^[:print:]+$",
"regex": "^[A-Za-z\\-\\.0-9]+$",
"type": "text"
}
]

View File

@ -1,37 +0,0 @@
import docker
class Docker :
def __init__(self, docker_host) :
self.__client = docker.DockerClient(base_url=docker_host)
def get_instances(self) :
return self.__client.containers.list(all=True, filters={"label" : "bunkerized-nginx.UI"})
def reload_instances(self) :
for instance in self.get_instances() :
instance.kill(signal="SIGHUP")
return True
def get_instance(self, id) :
return self.__client.containers.get(id)
def reload_instance(self, id) :
self.get_instance(id).kill(signal="SIGHUP")
return "Instance " + id + " has been reloaded."
def start_instance(self, id) :
self.get_instance(id).start()
return "Instance " + id + " has been started."
def stop_instance(self, id) :
self.get_instance(id).stop()
return "Instance " + id + " has been stopped."
def restart_instance(self, id) :
self.get_instance(id).restart()
return "Instance " + id + " has been restarted."
def delete_instance(self, id) :
self.get_instance(id).remove(v=True, force=True)
return "Instance " + id + " has been deleted."

View File

@ -1,6 +1,6 @@
FROM alpine
RUN apk add py3-pip bash
RUN apk add py3-pip bash build-base python3-dev libffi-dev
COPY ui/requirements.txt /tmp
RUN pip3 install -r /tmp/requirements.txt
@ -19,4 +19,4 @@ EXPOSE 5000
WORKDIR /opt/bunkerized-nginx/ui
USER nginx:nginx
ENTRYPOINT ["/usr/bin/gunicorn", "--bind", "0.0.0.0:5000", "-m", "007", "wsgi:app"]
ENTRYPOINT ["/usr/bin/gunicorn", "--bind", "0.0.0.0:5000", "-m", "007", "wsgi:app"]

View File

@ -6,9 +6,8 @@ from flask_wtf.csrf import CSRFProtect, CSRFError
from src.Instances import Instances
from src.User import User
from src.Config import Config
from Docker import Docker
from Config import Config
import utils
import os, json, re, copy, traceback
@ -73,7 +72,7 @@ def home() :
services_number = len(app.config["CONFIG"].get_services())
return render_template("home.html", title="Home", instances_number=instances_number, services_number=services_number)
except Exception as e :
return render_template("error.html", title="Error", error=e)
return render_template("error.html", title="Error", error=str(e) + "<br />" + traceback.format_exc().replace("\n", "<br />"))
@app.route('/instances', methods=["GET", "POST"])
@login_required
@ -106,7 +105,7 @@ def instances() :
return render_template("instances.html", title="Instances", instances=instances, operation=operation)
except Exception as e :
return render_template("error.html", title="Error", error=str(e))
return render_template("error.html", title="Error", error=str(e) + "\n" + traceback.format_exc())
@app.route('/services', methods=["GET", "POST"])
@ -123,6 +122,7 @@ def services():
# Check variables
variables = copy.deepcopy(request.form.to_dict())
del variables["csrf_token"]
if not "OLD_SERVER_NAME" in request.form and request.form["operation"] == "edit" :
raise Exception("Missing OLD_SERVER_NAME parameter.")
if request.form["operation"] in ["new", "edit"] :
@ -145,13 +145,14 @@ def services():
elif request.form["operation"] == "delete" :
operation = app.config["CONFIG"].delete_service(request.form["SERVER_NAME"])
# Reload containers
for instance in app.config["DOCKER"].get_instances() :
app.config["DOCKER"].reload_instance(instance.id)
# Reload instances
reload = app.config["INSTANCES"].reload_instances()
if not reload :
operation = "Reload failed for at least one instance..."
# Display services
services = app.config["CONFIG"].get_services()
return render_template("services.html", title="Services", services=services, operation=operation)
except Exception as e :
return render_template("error.html", title="Error", error=str(e) + traceback.format_exc())
return render_template("error.html", title="Error", error=str(e) + "\n" + traceback.format_exc())

View File

@ -1,4 +1,4 @@
import json, uuid, glob, copy, re, subprocess
import json, uuid, glob, copy, re, subprocess, os
class Config :
@ -7,6 +7,8 @@ class Config :
self.__settings = json.loads(f.read())
def __env_to_dict(self, filename) :
if not os.path.isfile(filename) :
return {}
with open(filename, "r") as f :
env = f.read()
data = {}
@ -25,6 +27,8 @@ class Config :
def __gen_conf(self, global_conf, services_conf) :
conf = copy.deepcopy(global_conf)
if not "SERVER_NAME" in conf :
conf["SERVER_NAME"] = ""
servers = conf["SERVER_NAME"].split(" ")
if conf["SERVER_NAME"] == "" :
servers = []
@ -39,9 +43,9 @@ class Config :
self.__dict_to_env(env_file, conf)
proc = subprocess.run(["/opt/bunkerized-nginx/gen/main.py", "--settings", "/opt/bunkerized-nginx/settings.json", "--templates", "/opt/bunkerized-nginx/confs", "--output", "/etc/nginx", "--variables", env_file], capture_output=True)
stderr = proc.stderr.decode("ascii")
#stdout = proc.stdout.decode("ascii")
stdout = proc.stdout.decode("ascii")
if stderr != "" or proc.returncode != 0 :
raise Exception("Error from generator (return code = " + str(proc.returncode) + ") : " + stderr)
raise Exception("Error from generator (return code = " + str(proc.returncode) + ") : " + stderr + "\n" + stdout)
def get_settings(self) :
return self.__settings
@ -54,6 +58,7 @@ class Config :
for filename in glob.iglob("/etc/nginx/**/site.env") :
env = self.__env_to_dict(filename)
services.append(env)
services.append(self.__env_to_dict("/etc/nginx/site.env"))
return services
def check_variables(self, variables) :

View File

@ -4,7 +4,9 @@
<div class="alert alert-danger text-center">
Something went wrong...<br /><br />
{{ error }}
{% autoescape false %}
{{ error | replace("\n", "<br/>") }}
{% endautoescape %}
</div>
{% endblock %}

View File

@ -15,6 +15,12 @@
<div class="row justify-content-center">
{% if instances|length == 0 %}
<div class="alert alert-primary text-center">
No instance to show...
</div>
{% endif %}
{% for instance in instances %}
{% set color = "dark" %}
{% if instance["status"] == "up" %}

View File

@ -2,24 +2,25 @@
{% block content %}
{% if operation != "" %}
<div class="row justify-content-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>
</div>
</div>
</div>
{% endif %}
<div class="row justify-content-center">
<div class="col col-12 mb-3 text-center">
<button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#modal-new"><i class="fas fa-plus"></i> New</button>
</div>
{% if operation != "" %}
<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>
</div>
{% endif %}
{% if services|length == 0 %}
<div class="alert alert-primary text-center">
No service to show...
</div>
{% endif %}
{% for service in services %}
{% set id_server_name = service["SERVER_NAME"].replace(".", "-") %}
@ -58,12 +59,13 @@
</div>
</div>
{% include "services-new.html" %}
{% include "services-edit.html" %}
{% include "services-delete.html" %}
{% endfor %}
{% include "services-new.html" %}
</div>
{% endblock %}