UI - basic read from docker API

This commit is contained in:
bunkerity 2020-12-11 15:17:18 +01:00
parent 002e3ed2ba
commit e6b5f460c9
No known key found for this signature in database
GPG Key ID: 654FFF51CEF7CC47
7 changed files with 104 additions and 3 deletions

View File

@ -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)

9
ui/templates/error.html Normal file
View File

@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
Something went wrong.
Some information : {{ error }}
{% endblock %}

View File

@ -2,6 +2,13 @@
{% block content %}
Lorem ipsum.
<div class="row">
<div class="col">
{{ instances_number }} bunkerized-nginx instances
</div>
<div class="col">
{{ services_number }} web services
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block content %}
<div class="row">
{% for instance in instances %}
<div class="col row-cols-1 row-cols-md-3">
ID = {{ instance["id"] }}<br />
Name = {{ instance["name"] }}<br />
Status = {{ instance["status"] }}
</div>
{% endfor %}
</div>
{% endblock %}

View File

@ -11,7 +11,10 @@
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
<a class="nav-link" href="/instances">Instances</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/services">Services</a>
</li>
</ul>
</div>

View File

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block content %}
<div class="row">
{% for service in services %}
<div class="col row-cols-1 row-cols-md-3">
ID = {{ instance["id"] }}<br />
Name = {{ instance["name"] }}<br />
Status = {{ instance["status"] }}
</div>
{% endfor %}
</div>
{% endblock %}

30
ui/wrappers.py Normal file
View File

@ -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")