Fix docker discovery with sudo fallback
This commit is contained in:
@@ -33,10 +33,7 @@ async def build_docker_map(cfg) -> Dict[str, str]:
|
||||
|
||||
# 1. autodiscovery
|
||||
if docker_cfg.get("autodiscovery"):
|
||||
rc, raw = await run_cmd(
|
||||
["sudo", "docker", "ps", "--format", "{{.Names}}"],
|
||||
timeout=20
|
||||
)
|
||||
rc, raw = await docker_cmd(["ps", "--format", "{{.Names}}"], timeout=20)
|
||||
if rc == 0:
|
||||
names = raw.splitlines()
|
||||
patterns = docker_cfg.get("match", [])
|
||||
@@ -61,10 +58,7 @@ async def discover_containers(cfg) -> Dict[str, str]:
|
||||
|
||||
# --- autodiscovery ---
|
||||
if docker_cfg.get("autodiscovery"):
|
||||
rc, raw = await run_cmd(
|
||||
["sudo", "docker", "ps", "--format", "{{.Names}}"],
|
||||
timeout=20
|
||||
)
|
||||
rc, raw = await docker_cmd(["ps", "--format", "{{.Names}}"], timeout=20)
|
||||
|
||||
if rc == 0:
|
||||
found = raw.splitlines()
|
||||
@@ -76,11 +70,10 @@ async def discover_containers(cfg) -> Dict[str, str]:
|
||||
# label-based discovery
|
||||
if label:
|
||||
key, val = label.split("=", 1)
|
||||
rc2, lbl = await run_cmd([
|
||||
"sudo", "docker", "inspect",
|
||||
"-f", f"{{{{ index .Config.Labels \"{key}\" }}}}",
|
||||
name
|
||||
])
|
||||
rc2, lbl = await docker_cmd(
|
||||
["inspect", "-f", f"{{{{ index .Config.Labels \"{key}\" }}}}", name],
|
||||
timeout=10
|
||||
)
|
||||
if rc2 == 0 and lbl.strip() == val:
|
||||
result[name] = name
|
||||
continue
|
||||
@@ -97,12 +90,19 @@ async def discover_containers(cfg) -> Dict[str, str]:
|
||||
return result
|
||||
|
||||
|
||||
async def docker_cmd(args: list[str], timeout: int = 20):
|
||||
rc, out = await run_cmd(["docker"] + args, timeout=timeout)
|
||||
if rc == 0:
|
||||
return rc, out
|
||||
return await run_cmd(["sudo", "docker"] + args, timeout=timeout)
|
||||
|
||||
|
||||
async def docker_watchdog(container_map, notify, bot, chat_id):
|
||||
last = {}
|
||||
while True:
|
||||
for alias, real in container_map.items():
|
||||
rc, state = await run_cmd(
|
||||
["docker", "inspect", "-f", "{{.State.Status}}", real],
|
||||
rc, state = await docker_cmd(
|
||||
["inspect", "-f", "{{.State.Status}}", real],
|
||||
timeout=10
|
||||
)
|
||||
if rc != 0:
|
||||
|
||||
Reference in New Issue
Block a user