-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (58 loc) 路 1.83 KB
/
Copy pathapp.py
File metadata and controls
71 lines (58 loc) 路 1.83 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
import yaml
import docker
from flask import Flask, render_template, request
app = Flask(__name__)
def get_docker_containers():
try:
client = docker.from_env()
containers = []
for c in client.containers.list():
ports = []
for container_port, bindings in (c.ports or {}).items():
if bindings:
for b in bindings:
host_port = b.get("HostPort")
if host_port:
ports.append(host_port)
else:
# exposed but not published
ports.append(container_port.split("/")[0])
containers.append(
{
"name": c.name,
"image": c.image.tags[0] if c.image.tags else c.image.short_id,
"status": c.status,
"ports": ports,
}
)
return containers, None
except Exception as e:
return [], str(e)
def get_services():
try:
with open("services.yaml") as f:
data = yaml.safe_load(f)
return data.get("services", [])
except FileNotFoundError:
return None
_LOCAL_HOST_RE = re.compile(
r"^(localhost|127\.\d+\.\d+\.\d+|10\.\d+\.\d+\.\d+|172\.(1[6-9]|2\d|3[01])\.\d+\.\d+|192\.168\.\d+\.\d+)(:\d+)?$"
)
def is_local_request():
host = request.host
return bool(_LOCAL_HOST_RE.match(host))
@app.route("/")
def index():
containers, docker_error = get_docker_containers()
services = get_services()
local = is_local_request()
return render_template(
"index.html",
containers=containers,
docker_error=docker_error,
services=services,
local=local,
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7777)