mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
commit
1e25e36ce6
195 files changed
+152301
No files matched your search
@@ -0,0 +1,15 @@
|
||||
FROM python:3.12-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt /app/requirements.txt
|
||||
RUN pip install --no-cache-dir -r /app/requirements.txt
|
||||
|
||||
COPY uptime-bot.py /app/uptime-bot.py
|
||||
COPY config.yml /app/config.yml
|
||||
COPY container-monitor-map.yaml /app/container-monitor-map.yaml
|
||||
|
||||
ENV UPTIME_BOT_CONFIG=/app/config.yml
|
||||
ENV UPTIME_BOT_MAP=/app/container-monitor-map.yaml
|
||||
|
||||
CMD ["python", "/app/uptime-bot.py"]
|
||||
@@ -0,0 +1,50 @@
|
||||
# Watchtower Export Bundle
|
||||
|
||||
This folder is a portable package with two Docker deployment modes:
|
||||
|
||||
- Standalone bot + separate watchtower container
|
||||
- Embedded bot inside a custom watchtower image
|
||||
|
||||
## Included Files
|
||||
|
||||
- Dockerfile
|
||||
- docker-compose.watchtower-uptime-sync.yml
|
||||
- docker-compose.watchtower-embedded.yml
|
||||
- uptime-bot.py
|
||||
- requirements.txt
|
||||
- config.yml
|
||||
- container-monitor-map.yaml
|
||||
- watchtower-embedded/Dockerfile.watchtower-with-uptime-bot
|
||||
- watchtower-embedded/start-watchtower-with-uptime-bot.sh
|
||||
|
||||
## Create Export Tarball
|
||||
|
||||
From uptime-updates directory:
|
||||
|
||||
```bash
|
||||
bash export-watchtower-bundle.sh
|
||||
```
|
||||
|
||||
This creates a timestamped tar.gz in this folder.
|
||||
|
||||
## Run Standalone Mode (Recommended)
|
||||
|
||||
In this folder:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.watchtower-uptime-sync.yml up -d --build
|
||||
```
|
||||
|
||||
## Run Embedded Watchtower Mode
|
||||
|
||||
In this folder:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.watchtower-embedded.yml up -d --build
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- The bot uses Docker host API endpoints listed in container-monitor-map.yaml.
|
||||
- Keep config.yml and container-monitor-map.yaml updated with your real values.
|
||||
- These Docker deployments do not require systemd.
|
||||
@@ -0,0 +1,9 @@
|
||||
uptime_kuma:
|
||||
url: "http://127.0.0.1:3001"
|
||||
api_key: "PUT_YOUR_API_KEY_HERE"
|
||||
|
||||
# Optional bot behavior settings
|
||||
bot:
|
||||
dry_run: false
|
||||
request_timeout_seconds: 10
|
||||
poll_interval_seconds: 60
|
||||
@@ -0,0 +1,43 @@
|
||||
# Container to Uptime Kuma monitor mapping
|
||||
#
|
||||
# Fill in one item per container you want tracked.
|
||||
# The bot can read this file to know:
|
||||
# 1) which Docker host to query
|
||||
# 2) which container on that host to watch
|
||||
# 3) which Uptime Kuma monitor to update when container ID changes
|
||||
#
|
||||
# Notes:
|
||||
# - uptime_monitor_id is the numeric ID in Uptime Kuma.
|
||||
# You can get it from the monitor URL, e.g. /dashboard/42 -> 42
|
||||
# - host_api should be the Docker API endpoint for that host.
|
||||
# Example: tcp://10.0.84.21:2375
|
||||
# - container_name should match docker container name used by that host.
|
||||
#
|
||||
# Optional fields:
|
||||
# - enabled: true/false (default true)
|
||||
# - expected_uptime_type: set to "docker" to protect against wrong monitor type updates
|
||||
|
||||
mappings:
|
||||
- name: "media-server jellyfin"
|
||||
enabled: true
|
||||
host_label: "media-server"
|
||||
host_api: "tcp://10.0.84.21:2375"
|
||||
container_name: "jellyfin"
|
||||
uptime_monitor_id: 21
|
||||
expected_uptime_type: "docker"
|
||||
|
||||
- name: "nas immich"
|
||||
enabled: true
|
||||
host_label: "nas"
|
||||
host_api: "tcp://10.0.84.22:2375"
|
||||
container_name: "immich-server"
|
||||
uptime_monitor_id: 34
|
||||
expected_uptime_type: "docker"
|
||||
|
||||
- name: "lab pihole"
|
||||
enabled: false
|
||||
host_label: "lab"
|
||||
host_api: "tcp://10.0.84.30:2375"
|
||||
container_name: "pihole"
|
||||
uptime_monitor_id: 56
|
||||
expected_uptime_type: "docker"
|
||||
@@ -0,0 +1,14 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
watchtower-with-uptime-bot:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: watchtower-embedded/Dockerfile.watchtower-with-uptime-bot
|
||||
container_name: watchtower-with-uptime-bot
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
- ./config.yml:/opt/uptime-bot/config.yml:ro
|
||||
- ./container-monitor-map.yaml:/opt/uptime-bot/container-monitor-map.yaml:ro
|
||||
command: --interval 300 --cleanup
|
||||
@@ -0,0 +1,23 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
watchtower:
|
||||
image: containrrr/watchtower:latest
|
||||
container_name: watchtower
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
command: --interval 300 --cleanup
|
||||
|
||||
uptime-sync-bot:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: uptime-sync-bot
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./config.yml:/app/config.yml:ro
|
||||
- ./container-monitor-map.yaml:/app/container-monitor-map.yaml:ro
|
||||
environment:
|
||||
- UPTIME_BOT_CONFIG=/app/config.yml
|
||||
- UPTIME_BOT_MAP=/app/container-monitor-map.yaml
|
||||
@@ -0,0 +1,2 @@
|
||||
uptime-kuma-api>=1.2.1
|
||||
PyYAML>=6.0
|
||||
@@ -0,0 +1,277 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Continuously sync Docker container IDs into Uptime Kuma docker monitors.
|
||||
|
||||
This bot reads:
|
||||
- config.yml
|
||||
- container-monitor-map.yaml
|
||||
|
||||
From the same directory as this script.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import yaml
|
||||
from uptime_kuma_api import UptimeKumaApi
|
||||
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
CONFIG_PATH = os.getenv("UPTIME_BOT_CONFIG", os.path.join(BASE_DIR, "config.yml"))
|
||||
MAP_PATH = os.getenv("UPTIME_BOT_MAP", os.path.join(BASE_DIR, "container-monitor-map.yaml"))
|
||||
|
||||
STOP = False
|
||||
|
||||
|
||||
def _signal_handler(signum: int, _frame: Any) -> None:
|
||||
global STOP
|
||||
STOP = True
|
||||
logging.info("Received signal %s, shutting down...", signum)
|
||||
|
||||
|
||||
def _load_yaml(path: str) -> Dict[str, Any]:
|
||||
with open(path, "r", encoding="utf-8") as handle:
|
||||
data = yaml.safe_load(handle) or {}
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError(f"YAML root in {path} must be a mapping")
|
||||
return data
|
||||
|
||||
|
||||
def _normalize_docker_host_url(raw_url: str) -> str:
|
||||
if raw_url.startswith("tcp://"):
|
||||
return "http://" + raw_url[len("tcp://") :]
|
||||
if raw_url.startswith("http://") or raw_url.startswith("https://"):
|
||||
return raw_url
|
||||
raise ValueError(f"Unsupported host_api scheme: {raw_url}")
|
||||
|
||||
|
||||
def _docker_get_container_id(host_api: str, container_name: str, timeout_seconds: int) -> Optional[str]:
|
||||
"""Resolve a container name on a Docker host to its full container ID.
|
||||
|
||||
Uses Docker remote API endpoint: /containers/{name}/json
|
||||
"""
|
||||
base_url = _normalize_docker_host_url(host_api).rstrip("/")
|
||||
encoded_name = urllib.parse.quote(container_name, safe="")
|
||||
url = f"{base_url}/containers/{encoded_name}/json"
|
||||
|
||||
request = urllib.request.Request(url=url, method="GET", headers={"Accept": "application/json"})
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout_seconds) as response:
|
||||
payload = json.loads(response.read().decode("utf-8"))
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code == 404:
|
||||
logging.warning("Container not found on host %s: %s", host_api, container_name)
|
||||
return None
|
||||
raise
|
||||
|
||||
container_id = payload.get("Id")
|
||||
if not container_id:
|
||||
raise RuntimeError(f"Docker API response missing Id for {container_name} on {host_api}")
|
||||
return str(container_id)
|
||||
|
||||
|
||||
def _api_login(url: str, api_key: str) -> UptimeKumaApi:
|
||||
api = UptimeKumaApi(url)
|
||||
# Uptime Kuma API key auth in wrapper is login_by_token.
|
||||
api.login_by_token(api_key)
|
||||
return api
|
||||
|
||||
|
||||
def _update_monitor_container_id(
|
||||
api: UptimeKumaApi,
|
||||
monitor_id: int,
|
||||
new_container_id: str,
|
||||
dry_run: bool,
|
||||
) -> bool:
|
||||
"""Attempt to update docker_container for a monitor.
|
||||
|
||||
Returns True if monitor was updated, False if unchanged or dry-run.
|
||||
"""
|
||||
monitor = api.get_monitor(monitor_id)
|
||||
if not isinstance(monitor, dict):
|
||||
raise RuntimeError(f"Unexpected monitor payload for id {monitor_id}: {type(monitor)}")
|
||||
|
||||
current_id = str(monitor.get("docker_container") or "")
|
||||
if current_id == new_container_id:
|
||||
return False
|
||||
|
||||
if dry_run:
|
||||
logging.info(
|
||||
"Dry-run: would update monitor %s docker_container from %s to %s",
|
||||
monitor_id,
|
||||
current_id,
|
||||
new_container_id,
|
||||
)
|
||||
return False
|
||||
|
||||
last_error: Optional[Exception] = None
|
||||
|
||||
# Try minimal edit call patterns first.
|
||||
for call in (
|
||||
lambda: api.edit_monitor(monitor_id, docker_container=new_container_id),
|
||||
lambda: api.edit_monitor(id=monitor_id, docker_container=new_container_id),
|
||||
):
|
||||
try:
|
||||
call()
|
||||
logging.info(
|
||||
"Updated monitor %s docker_container from %s to %s",
|
||||
monitor_id,
|
||||
current_id,
|
||||
new_container_id,
|
||||
)
|
||||
return True
|
||||
except Exception as exc: # noqa: BLE001
|
||||
last_error = exc
|
||||
|
||||
if last_error is None:
|
||||
raise RuntimeError(f"Failed to update monitor {monitor_id} for unknown reason")
|
||||
raise RuntimeError(f"Failed to update monitor {monitor_id}: {last_error}")
|
||||
|
||||
|
||||
def _run_once(config: Dict[str, Any], mapping: Dict[str, Any]) -> None:
|
||||
uptime_cfg = config.get("uptime_kuma") or {}
|
||||
bot_cfg = config.get("bot") or {}
|
||||
|
||||
uptime_url = str(uptime_cfg.get("url") or "").strip()
|
||||
api_key = str(uptime_cfg.get("api_key") or "").strip()
|
||||
dry_run = bool(bot_cfg.get("dry_run", False))
|
||||
timeout_seconds = int(bot_cfg.get("request_timeout_seconds", 10))
|
||||
|
||||
if not uptime_url:
|
||||
raise ValueError("config.yml missing uptime_kuma.url")
|
||||
if not api_key or api_key == "PUT_YOUR_API_KEY_HERE":
|
||||
raise ValueError("config.yml missing real uptime_kuma.api_key")
|
||||
|
||||
mappings = mapping.get("mappings") or []
|
||||
if not isinstance(mappings, list):
|
||||
raise ValueError("container-monitor-map.yaml: mappings must be a list")
|
||||
|
||||
if not mappings:
|
||||
logging.info("No mappings configured. Nothing to do.")
|
||||
return
|
||||
|
||||
api = _api_login(uptime_url, api_key)
|
||||
updated = 0
|
||||
|
||||
try:
|
||||
for item in mappings:
|
||||
if not isinstance(item, dict):
|
||||
logging.warning("Skipping invalid mapping item (not a mapping): %r", item)
|
||||
continue
|
||||
|
||||
if item.get("enabled", True) is False:
|
||||
continue
|
||||
|
||||
name = str(item.get("name") or "unnamed")
|
||||
host_api = str(item.get("host_api") or "").strip()
|
||||
container_name = str(item.get("container_name") or "").strip()
|
||||
expected_type = str(item.get("expected_uptime_type") or "").strip().lower()
|
||||
|
||||
monitor_id_raw = item.get("uptime_monitor_id")
|
||||
try:
|
||||
monitor_id = int(monitor_id_raw)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logging.error("[%s] Invalid uptime_monitor_id: %r (%s)", name, monitor_id_raw, exc)
|
||||
continue
|
||||
|
||||
if not host_api or not container_name:
|
||||
logging.error("[%s] Missing host_api or container_name", name)
|
||||
continue
|
||||
|
||||
try:
|
||||
container_id = _docker_get_container_id(host_api, container_name, timeout_seconds)
|
||||
if not container_id:
|
||||
continue
|
||||
|
||||
monitor = api.get_monitor(monitor_id)
|
||||
monitor_type = str((monitor or {}).get("type") or "").lower()
|
||||
if expected_type and monitor_type and monitor_type != expected_type:
|
||||
logging.error(
|
||||
"[%s] Monitor %s type mismatch. expected=%s actual=%s",
|
||||
name,
|
||||
monitor_id,
|
||||
expected_type,
|
||||
monitor_type,
|
||||
)
|
||||
continue
|
||||
|
||||
changed = _update_monitor_container_id(api, monitor_id, container_id, dry_run)
|
||||
if changed:
|
||||
updated += 1
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logging.exception("[%s] Failed to process mapping: %s", name, exc)
|
||||
finally:
|
||||
try:
|
||||
api.disconnect()
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
|
||||
logging.info("Cycle complete. monitors_updated=%s", updated)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
signal.signal(signal.SIGINT, _signal_handler)
|
||||
signal.signal(signal.SIGTERM, _signal_handler)
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
logging.error("Missing config file: %s", CONFIG_PATH)
|
||||
return 1
|
||||
if not os.path.exists(MAP_PATH):
|
||||
logging.error("Missing mapping file: %s", MAP_PATH)
|
||||
return 1
|
||||
|
||||
config = _load_yaml(CONFIG_PATH)
|
||||
mapping = _load_yaml(MAP_PATH)
|
||||
bot_cfg = config.get("bot") or {}
|
||||
interval_seconds = int(bot_cfg.get("poll_interval_seconds", 60))
|
||||
if interval_seconds < 5:
|
||||
logging.warning("poll_interval_seconds too low (%s), forcing to 5", interval_seconds)
|
||||
interval_seconds = 5
|
||||
|
||||
logging.info("Starting uptime bot. interval=%ss", interval_seconds)
|
||||
|
||||
while not STOP:
|
||||
try:
|
||||
_run_once(config, mapping)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logging.exception("Cycle failed: %s", exc)
|
||||
|
||||
if STOP:
|
||||
break
|
||||
|
||||
slept = 0
|
||||
while slept < interval_seconds and not STOP:
|
||||
time.sleep(1)
|
||||
slept += 1
|
||||
|
||||
# Reload config/map each cycle so file changes apply without restart.
|
||||
try:
|
||||
config = _load_yaml(CONFIG_PATH)
|
||||
mapping = _load_yaml(MAP_PATH)
|
||||
bot_cfg = config.get("bot") or {}
|
||||
new_interval = int(bot_cfg.get("poll_interval_seconds", interval_seconds))
|
||||
interval_seconds = max(5, new_interval)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logging.warning("Failed to reload config/map, using previous values: %s", exc)
|
||||
|
||||
logging.info("Uptime bot stopped")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Binary file not shown.
Binary file not shown.
+23
@@ -0,0 +1,23 @@
|
||||
FROM containrrr/watchtower:latest
|
||||
|
||||
USER root
|
||||
|
||||
RUN apk add --no-cache python3 py3-pip
|
||||
|
||||
WORKDIR /opt/uptime-bot
|
||||
|
||||
COPY requirements.txt /opt/uptime-bot/requirements.txt
|
||||
RUN pip3 install --no-cache-dir -r /opt/uptime-bot/requirements.txt
|
||||
|
||||
COPY uptime-bot.py /opt/uptime-bot/uptime-bot.py
|
||||
COPY config.yml /opt/uptime-bot/config.yml
|
||||
COPY container-monitor-map.yaml /opt/uptime-bot/container-monitor-map.yaml
|
||||
COPY watchtower-embedded/start-watchtower-with-uptime-bot.sh /usr/local/bin/start-watchtower-with-uptime-bot.sh
|
||||
|
||||
RUN chmod +x /usr/local/bin/start-watchtower-with-uptime-bot.sh
|
||||
|
||||
ENV UPTIME_BOT_CONFIG=/opt/uptime-bot/config.yml
|
||||
ENV UPTIME_BOT_MAP=/opt/uptime-bot/container-monitor-map.yaml
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/start-watchtower-with-uptime-bot.sh"]
|
||||
CMD ["--interval", "300", "--cleanup"]
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
BOT_SCRIPT="/opt/uptime-bot/uptime-bot.py"
|
||||
WATCHTOWER_BIN="/watchtower"
|
||||
|
||||
if [ ! -x "${WATCHTOWER_BIN}" ]; then
|
||||
echo "[ERROR] watchtower binary not found at ${WATCHTOWER_BIN}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "${BOT_SCRIPT}" ]; then
|
||||
echo "[ERROR] bot script not found at ${BOT_SCRIPT}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python3 "${BOT_SCRIPT}" &
|
||||
BOT_PID=$!
|
||||
|
||||
"${WATCHTOWER_BIN}" "$@" &
|
||||
WT_PID=$!
|
||||
|
||||
shutdown() {
|
||||
kill "${BOT_PID}" 2>/dev/null || true
|
||||
kill "${WT_PID}" 2>/dev/null || true
|
||||
wait "${BOT_PID}" 2>/dev/null || true
|
||||
wait "${WT_PID}" 2>/dev/null || true
|
||||
}
|
||||
|
||||
trap shutdown INT TERM
|
||||
|
||||
while :; do
|
||||
if ! kill -0 "${BOT_PID}" 2>/dev/null; then
|
||||
echo "[ERROR] uptime bot exited; stopping watchtower" >&2
|
||||
kill "${WT_PID}" 2>/dev/null || true
|
||||
wait "${WT_PID}" 2>/dev/null || true
|
||||
wait "${BOT_PID}" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! kill -0 "${WT_PID}" 2>/dev/null; then
|
||||
echo "[ERROR] watchtower exited; stopping uptime bot" >&2
|
||||
kill "${BOT_PID}" 2>/dev/null || true
|
||||
wait "${BOT_PID}" 2>/dev/null || true
|
||||
wait "${WT_PID}" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
done
|
||||
Reference in new issue
Block a user