-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
44 lines (37 loc) · 1.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import docker
from flask import Flask, render_template, request, jsonify
from flask_basicauth import BasicAuth
import os, sys
app = Flask(__name__)
app.config["BASIC_AUTH_USERNAME"] = os.environ.get("BASIC_AUTH_USERNAME")
if app.config["BASIC_AUTH_USERNAME"] is None:
raise Exception("BASIC_AUTH_USERNAME not defined")
app.config["BASIC_AUTH_PASSWORD"] = os.environ.get("BASIC_AUTH_PASSWORD")
if app.config["BASIC_AUTH_PASSWORD"] is None:
raise Exception("BASIC_AUTH_PASSWORD not defined")
basic_auth = BasicAuth(app)
client = docker.from_env()
@app.route("/")
@basic_auth.required
def index():
containers = client.containers.list()
container_names = [container.name for container in containers]
if len(sys.argv) > 1:
container_names = filter(lambda x: x in sys.argv[1:], container_names)
return render_template("index.html", container_names=container_names)
@app.route("/logs")
@basic_auth.required
def logs():
try:
container_name = request.args.get("container_name")
filter_pattern = request.args.get("filter")
container = client.containers.get(container_name)
logs = container.logs(tail=500, stream=False, timestamps=True).decode("utf-8")
if filter_pattern:
logs = filter(lambda x: filter_pattern in x, logs.split("\n"))
logs = "\n".join(logs)
return logs
except (docker.errors.NotFound, docker.errors.NullResource):
return ""
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)