templating - init integration into web ui
This commit is contained in:
parent
99c259bf18
commit
1d96620ae6
32
ui/Docker.py
Normal file
32
ui/Docker.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import docker
|
||||||
|
|
||||||
|
class Docker :
|
||||||
|
|
||||||
|
def __init__(self) :
|
||||||
|
self.__client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
|
||||||
|
|
||||||
|
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) :
|
||||||
|
return self.get_instance(id).kill(signal="SIGHUP")
|
||||||
|
|
||||||
|
def start_instance(self, id) :
|
||||||
|
return self.get_instance(id).start()
|
||||||
|
|
||||||
|
def stop_instance(self, id) :
|
||||||
|
return self.get_instance(id).stop()
|
||||||
|
|
||||||
|
def restart_instance(self, id) :
|
||||||
|
return self.get_instance(id).restart()
|
||||||
|
|
||||||
|
def remove_instance(self, id) :
|
||||||
|
return self.get_instance(id).remove(v=True, force=True)
|
||||||
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
from flask import Flask, render_template, current_app, request
|
from flask import Flask, render_template, current_app, request
|
||||||
|
|
||||||
import wrappers, utils
|
from Docker import Docker
|
||||||
|
import Docker, wrappers, utils
|
||||||
import os, json, re
|
import os, json, re
|
||||||
|
|
||||||
app = Flask(__name__, static_url_path="/", static_folder="static", template_folder="templates")
|
app = Flask(__name__, static_url_path="/", static_folder="static", template_folder="templates")
|
||||||
@ -10,8 +11,9 @@ ABSOLUTE_URI = ""
|
|||||||
if "ABSOLUTE_URI" in os.environ :
|
if "ABSOLUTE_URI" in os.environ :
|
||||||
ABSOLUTE_URI = os.environ["ABSOLUTE_URI"]
|
ABSOLUTE_URI = os.environ["ABSOLUTE_URI"]
|
||||||
app.config["ABSOLUTE_URI"] = ABSOLUTE_URI
|
app.config["ABSOLUTE_URI"] = ABSOLUTE_URI
|
||||||
with open("/opt/entrypoint/config.json", "r") as f :
|
with open("/opt/settings.json", "r") as f :
|
||||||
app.config["CONFIG"] = json.loads(f.read())
|
app.config["CONFIG"] = json.loads(f.read())
|
||||||
|
app.config["DOCKER"] = Docker()
|
||||||
app.jinja_env.globals.update(env_to_summary_class=utils.env_to_summary_class)
|
app.jinja_env.globals.update(env_to_summary_class=utils.env_to_summary_class)
|
||||||
app.jinja_env.globals.update(form_service_gen=utils.form_service_gen)
|
app.jinja_env.globals.update(form_service_gen=utils.form_service_gen)
|
||||||
app.jinja_env.globals.update(form_service_gen_multiple=utils.form_service_gen_multiple)
|
app.jinja_env.globals.update(form_service_gen_multiple=utils.form_service_gen_multiple)
|
||||||
@ -19,48 +21,48 @@ app.jinja_env.globals.update(form_service_gen_multiple_values=utils.form_service
|
|||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@app.route('/home')
|
@app.route('/home')
|
||||||
def home():
|
def home() :
|
||||||
check, client = wrappers.get_client()
|
try :
|
||||||
if not check :
|
instances_number = len(app.config["DOCKER"].get_instances())
|
||||||
return render_template("error.html", title="Error", error=client)
|
services_number = 0 # TODO
|
||||||
check, instances = wrappers.get_instances(client)
|
return render_template("home.html", title="Home", instances_number=instances_number, services_number=services_number)
|
||||||
if not check :
|
except Exception as e :
|
||||||
return render_template("error.html", title="Error", error=instances)
|
return render_template("error.html", title="Error", error=e)
|
||||||
check, services = wrappers.get_services()
|
|
||||||
if not check :
|
|
||||||
return render_template("error.html", title="Error", error=services)
|
|
||||||
return render_template("home.html", title="Home", instances_number=len(instances), services_number=len(services))
|
|
||||||
|
|
||||||
@app.route('/instances', methods=["GET", "POST"])
|
@app.route('/instances', methods=["GET", "POST"])
|
||||||
def instances():
|
def instances() :
|
||||||
|
try :
|
||||||
|
# Manage instances
|
||||||
|
operation = ""
|
||||||
|
if request.method == "POST" :
|
||||||
|
|
||||||
# Get the client
|
# Check operation
|
||||||
check, client = wrappers.get_client()
|
if not "operation" in request.form or not request.form["operation"] in ["reload", "start", "stop", "restart", "delete"] :
|
||||||
if not check :
|
return render_template("error.html", title="Error", error="Missing operation parameter on /instances.")
|
||||||
return render_template("error.html", title="Error", error=client)
|
|
||||||
|
|
||||||
# Manage instances
|
# Check that all fields are present
|
||||||
operation = ""
|
if not "INSTANCE_ID" in request.form :
|
||||||
if request.method == "POST" :
|
return render_template("error.html", title="Error", error="Missing INSTANCE_ID parameter.")
|
||||||
|
|
||||||
# Check operation
|
# Do the operation
|
||||||
if not "operation" in request.form or not request.form["operation"] in ["reload", "start", "stop", "restart", "delete"] :
|
if request.form["operation"] == "reload" :
|
||||||
return render_template("error.html", title="Error", error="Missing operation parameter on /instances.")
|
app.config["DOCKER"].reload(request_form["INSTANCE_ID"])
|
||||||
|
elif request.form["operation"] == "start" :
|
||||||
|
app.config["DOCKER"].start(request_form["INSTANCE_ID"])
|
||||||
|
elif request.form["operation"] == "stop" :
|
||||||
|
app.config["DOCKER"].stop(request_form["INSTANCE_ID"])
|
||||||
|
elif request.form["operation"] == "restart" :
|
||||||
|
app.config["DOCKER"].restart(request_form["INSTANCE_ID"])
|
||||||
|
elif request.form["operation"] == "delete" :
|
||||||
|
app.config["DOCKER"].remove(request_form["INSTANCE_ID"])
|
||||||
|
|
||||||
# Check that all fields are present
|
# Display instances
|
||||||
if not "INSTANCE_ID" in request.form :
|
instances = app.config["DOCKER"].get_instances()
|
||||||
return render_template("error.html", title="Error", error="Missing INSTANCE_ID parameter.")
|
return render_template("instances.html", title="Instances", instances=instances, operation="todo")
|
||||||
|
|
||||||
# Do the operation
|
except Exception as e :
|
||||||
check, operation = wrappers.operation_instance(client, request.form)
|
return render_template("error.html", title="Error", error=e)
|
||||||
if not check :
|
|
||||||
return render_template("error.html", title="Error", error=operation)
|
|
||||||
|
|
||||||
# Display 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)
|
|
||||||
|
|
||||||
@app.route('/services', methods=["GET", "POST"])
|
@app.route('/services', methods=["GET", "POST"])
|
||||||
def services():
|
def services():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user