From e6b5f460c9f59008b3b4cd92519a9259ae0fd4a0 Mon Sep 17 00:00:00 2001 From: bunkerity Date: Fri, 11 Dec 2020 15:17:18 +0100 Subject: [PATCH] UI - basic read from docker API --- ui/entrypoint.py | 24 +++++++++++++++++++++++- ui/templates/error.html | 9 +++++++++ ui/templates/home.html | 9 ++++++++- ui/templates/instances.html | 15 +++++++++++++++ ui/templates/menu.html | 5 ++++- ui/templates/services.html | 15 +++++++++++++++ ui/wrappers.py | 30 ++++++++++++++++++++++++++++++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 ui/templates/error.html create mode 100644 ui/templates/instances.html create mode 100644 ui/templates/services.html create mode 100644 ui/wrappers.py diff --git a/ui/entrypoint.py b/ui/entrypoint.py index 9c1eb65..aff7d6c 100644 --- a/ui/entrypoint.py +++ b/ui/entrypoint.py @@ -2,8 +2,30 @@ from flask import Flask, render_template +import wrappers + app = Flask(__name__, static_url_path="/", static_folder="static", template_folder="templates") @app.route('/') def home(): - return render_template("home.html", title="Home") + check, instances = wrappers.get_instances() + if not check : + return render_template("error.html", title="Error", error=instances) + 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') +def home(): + check, instances = wrappers.get_instances() + if not check : + return render_template("error.html", title="Error", error=instances) + return render_template("instances.html", title="Instances", instances=instances) + +@app.route('/services') +def home(): + check, services = wrappers.get_services() + if not check : + return render_template("error.html", title="Error", error=services) + return render_template("services.html", title="Services", services=services) diff --git a/ui/templates/error.html b/ui/templates/error.html new file mode 100644 index 0000000..bb63c8a --- /dev/null +++ b/ui/templates/error.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block content %} + +Something went wrong. + +Some information : {{ error }} + +{% endblock %} diff --git a/ui/templates/home.html b/ui/templates/home.html index a95052e..89fa7ed 100644 --- a/ui/templates/home.html +++ b/ui/templates/home.html @@ -2,6 +2,13 @@ {% block content %} -Lorem ipsum. +
+
+ {{ instances_number }} bunkerized-nginx instances +
+
+ {{ services_number }} web services +
+
{% endblock %} diff --git a/ui/templates/instances.html b/ui/templates/instances.html new file mode 100644 index 0000000..0049e20 --- /dev/null +++ b/ui/templates/instances.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} + +
+ {% for instance in instances %} +
+ ID = {{ instance["id"] }}
+ Name = {{ instance["name"] }}
+ Status = {{ instance["status"] }} +
+ {% endfor %} +
+ +{% endblock %} diff --git a/ui/templates/menu.html b/ui/templates/menu.html index 75dca83..bcbf30d 100644 --- a/ui/templates/menu.html +++ b/ui/templates/menu.html @@ -11,7 +11,10 @@ Home + diff --git a/ui/templates/services.html b/ui/templates/services.html new file mode 100644 index 0000000..87be787 --- /dev/null +++ b/ui/templates/services.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} + +
+ {% for service in services %} +
+ ID = {{ instance["id"] }}
+ Name = {{ instance["name"] }}
+ Status = {{ instance["status"] }} +
+ {% endfor %} +
+ +{% endblock %} diff --git a/ui/wrappers.py b/ui/wrappers.py new file mode 100644 index 0000000..1e40507 --- /dev/null +++ b/ui/wrappers.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import utils, config +import docker, os, stat, sys + +def get_client() : + endpoint = "/var/run/docker.sock" + if not os.path.exists(endpoint) or not stat.S_ISSOCK(os.stat(endpoint).st_mode) : + return False, "Can't connect to /var/run/docker.sock (is it mounted ?)" + try : + client = docker.DockerClient(base_url='unix:///var/run/docker.sock') + except Exception as e : + 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 + 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_services() : + return get_containers("SERVER_NAME")