Files
Lakehouse/scripts/deploy/export-inventory.py
T

39 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import json
import subprocess
SKIP_ENV_PREFIXES = (
"PATH=", "NODE_", "PYTHON_", "GPG_", "LANG=", "YARN=", "HOSTNAME=",
"NGINX_", "NJS_", "ACME_", "DYNPKG_", "PKG_", "CADVISOR_",
)
ids = subprocess.check_output(["docker", "ps", "-aq"], text=True).split()
if not ids or ids == [""]:
raise SystemExit("no containers")
raw = subprocess.check_output(["docker", "inspect", *ids])
data = json.loads(raw)
out = []
for c in data:
env = [
e for e in (c["Config"].get("Env") or [])
if not any(e.startswith(p) for p in SKIP_ENV_PREFIXES)
]
out.append({
"name": c["Name"].lstrip("/"),
"image": c["Config"]["Image"],
"ports": c["NetworkSettings"]["Ports"],
"restart": c["HostConfig"]["RestartPolicy"]["Name"],
"binds": c["HostConfig"].get("Binds") or [],
"volumes": [
{"name": m["Name"], "dest": m["Destination"]}
for m in c["Mounts"]
if m.get("Type") == "volume"
],
"env": env,
})
with open("/root/lakehouse/inventory/containers-atc-docker01.json", "w") as f:
json.dump(out, f, indent=2)
print("wrote", len(out), "containers")