Compare commits

..
2 Commits
4 changed files with 226 additions and 110 deletions

No files matched your search

+15 -11
View File
@@ -31,16 +31,17 @@ show_help() {
echo "Usage: appctl <command> [service] [options...]" echo "Usage: appctl <command> [service] [options...]"
echo "" echo ""
echo "Commands:" echo "Commands:"
echo " list, status [--fetch] [--core] List all applications with Git sync & container status" echo " list, status [--fetch] [--core] [--ssl] List all applications with Git sync & container status"
echo " info <service> Display comprehensive application & Git diagnostics" echo " ssl [service] Inspect SSL/TLS certificates across homelab domains"
echo " up <service> Start/build a service stack" echo " info <service> [--ssl] Display comprehensive application & Git diagnostics"
echo " down <service> Stop and remove a service stack" echo " up <service> Start/build a service stack"
echo " restart <service> Restart a service stack" echo " down <service> Stop and remove a service stack"
echo " update <service> Full stack update (git pull -> compose pull -> recreate)" echo " restart <service> Restart a service stack"
echo " pull <service> Pull latest images and recreate service" echo " update <service> Full stack update (git pull -> compose pull -> recreate)"
echo " logs <service> Stream service logs" echo " pull <service> Pull latest images and recreate service"
echo " config <service> Validate and view resolved compose configuration" echo " logs <service> Stream service logs"
echo " sync Synchronize Homepage dashboard services.yaml from app.yaml" echo " config <service> Validate and view resolved compose configuration"
echo " sync Synchronize Homepage dashboard services.yaml from app.yaml"
} }
# Setup application environment based on resolved metadata # Setup application environment based on resolved metadata
@@ -110,9 +111,12 @@ case "$COMMAND" in
list|status) list|status)
python3 "$ENGINE" list "${@:2}" python3 "$ENGINE" list "${@:2}"
;; ;;
ssl)
python3 "$ENGINE" ssl "${@:2}"
;;
info) info)
if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi if [ -z "$APP" ]; then echo "❌ Error: Service name required"; exit 1; fi
python3 "$ENGINE" info "$APP" python3 "$ENGINE" info "${@:2}"
;; ;;
sync) sync)
python3 "$ENGINE" sync "${@:2}" python3 "$ENGINE" sync "${@:2}"
+211 -11
View File
@@ -4,8 +4,11 @@ appctl_engine.py - Homelab Metadata Engine, Orchestration Parser & Git Sync Moni
""" """
import contextlib import contextlib
import datetime
import json import json
import os import os
import socket
import ssl
import subprocess import subprocess
import sys import sys
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
@@ -334,45 +337,216 @@ def get_core_services():
return core_services return core_services
def check_ssl_cert(domain, port=443, timeout=3.0):
"""Inspect SSL/TLS certificate for a domain using standard library ssl and socket."""
if not domain or domain == "internal" or domain.endswith(".local") or ":" in domain:
return {
"domain": domain,
"status": "⚪ Internal",
"issuer": "-",
"subject": "-",
"days_remaining": None,
"not_after": "-",
"formatted_expiry": "-",
"error": None,
}
try:
ctx = ssl.create_default_context()
with (
socket.create_connection((domain, port), timeout=timeout) as sock,
ctx.wrap_socket(sock, server_hostname=domain) as ssock,
):
cert = ssock.getpeercert()
not_after_str = cert.get("notAfter")
days_remaining = None
formatted_expiry = "-"
if not_after_str:
expiry_dt = datetime.datetime.strptime(not_after_str, "%b %d %H:%M:%S %Y %Z").replace(
tzinfo=datetime.timezone.utc
)
now_dt = datetime.datetime.now(datetime.timezone.utc)
days_remaining = (expiry_dt - now_dt).days
formatted_expiry = expiry_dt.strftime("%Y-%m-%d")
issuer_dict = dict(x[0] for x in cert.get("issuer", ()))
issuer = issuer_dict.get("organizationName") or issuer_dict.get("commonName") or "Unknown"
subject_dict = dict(x[0] for x in cert.get("subject", ()))
subject = subject_dict.get("commonName") or domain
if days_remaining is not None:
if days_remaining < 0:
status = f"🔴 Expired ({abs(days_remaining)}d ago)"
elif days_remaining <= 14:
status = f"🟡 {days_remaining}d remaining"
else:
status = f"🟢 {days_remaining}d remaining"
else:
status = "🟢 Valid"
return {
"domain": domain,
"status": status,
"issuer": issuer,
"subject": subject,
"days_remaining": days_remaining,
"not_after": not_after_str or "-",
"formatted_expiry": formatted_expiry,
"error": None,
}
except (ssl.SSLError, socket.gaierror, TimeoutError, ConnectionRefusedError, OSError, ValueError) as e:
return {
"domain": domain,
"status": "🔴 Failed",
"issuer": "-",
"subject": "-",
"days_remaining": None,
"not_after": "-",
"formatted_expiry": "-",
"error": str(e),
}
def check_all_ssl_certs(domains):
"""Check multiple domains concurrently with ThreadPoolExecutor."""
valid_domains = [d for d in set(domains) if d and d != "internal" and not d.endswith(".local") and ":" not in d]
results = {}
if not valid_domains:
return results
with ThreadPoolExecutor(max_workers=min(len(valid_domains), 8)) as executor:
future_to_domain = {executor.submit(check_ssl_cert, d): d for d in valid_domains}
for future, d in future_to_domain.items():
with contextlib.suppress(subprocess.SubprocessError, OSError):
results[d] = future.result()
return results
def cmd_list(args): def cmd_list(args):
"""Format and print application and core stack listings with Git sync status.""" """Format and print application and core stack listings with Git sync status and optional SSL cert check."""
show_core = any(a in ["--core", "-c", "--all", "-a"] for a in args) show_core = any(a in ["--core", "-c", "--all", "-a"] for a in args)
should_fetch = any(a in ["--fetch", "-f"] for a in args) should_fetch = any(a in ["--fetch", "-f"] for a in args)
show_ssl = any(a in ["--ssl", "-s"] for a in args)
apps = get_all_apps() apps = get_all_apps()
if should_fetch: if should_fetch:
print("📡 Fetching remote updates across all repositories...") print("📡 Fetching remote updates across all repositories...")
fetch_all_repositories(apps, include_core=show_core) fetch_all_repositories(apps, include_core=show_core)
print(f"{'SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'DIRECTORY'}") ssl_data = {}
print(f"{'-------':<16} {'------':<15} {'--------':<15} {'------':<28} {'---------'}") if show_ssl:
domains_to_check = [app["domain"] for app in apps]
if show_core:
core_services = get_core_services()
domains_to_check.extend([svc["domain"] for svc in core_services])
print("🔒 Inspecting SSL/TLS certificates...")
ssl_data = check_all_ssl_certs(domains_to_check)
if show_ssl:
print(f"{'SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'CERT STATUS':<22} {'ISSUER'}")
print(f"{'-------':<16} {'------':<15} {'--------':<15} {'------':<28} {'-----------':<22} {'------'}")
else:
print(f"{'SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'DIRECTORY'}")
print(f"{'-------':<16} {'------':<15} {'--------':<15} {'------':<28} {'---------'}")
for app in apps: for app in apps:
status = get_docker_status(app["dir_path"]) status = get_docker_status(app["dir_path"])
git_status = get_git_sync_status(app["dir_path"]) git_status = get_git_sync_status(app["dir_path"])
rel_dir = app["dir_path"].replace(os.path.expanduser("~"), "~") if show_ssl:
print(f"{app['name']:<16} {status:<15} {git_status:<15} {app['domain']:<28} {rel_dir}") cert_info = ssl_data.get(app["domain"], check_ssl_cert(app["domain"]))
print(f"{app['name']:<16} {status:<15} {git_status:<15} {app['domain']:<28} {cert_info['status']:<22} {cert_info['issuer']}")
else:
rel_dir = app["dir_path"].replace(os.path.expanduser("~"), "~")
print(f"{app['name']:<16} {status:<15} {git_status:<15} {app['domain']:<28} {rel_dir}")
if show_core: if show_core:
core_git = get_git_sync_status(CORE_DIR) core_git = get_git_sync_status(CORE_DIR)
print() print()
print(f"{'CORE SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'DIRECTORY'}") if show_ssl:
print(f"{'------------':<16} {'------':<15} {'--------':<15} {'------':<28} {'---------'}") print(f"{'CORE SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'CERT STATUS':<22} {'ISSUER'}")
print(f"{'------------':<16} {'------':<15} {'--------':<15} {'------':<28} {'-----------':<22} {'------'}")
else:
print(f"{'CORE SERVICE':<16} {'STATUS':<15} {'GIT SYNC':<15} {'DOMAIN':<28} {'DIRECTORY'}")
print(f"{'------------':<16} {'------':<15} {'--------':<15} {'------':<28} {'---------'}")
core_services = get_core_services() core_services = get_core_services()
rel_core = CORE_DIR.replace(os.path.expanduser("~"), "~") rel_core = CORE_DIR.replace(os.path.expanduser("~"), "~")
for i, svc in enumerate(core_services): for i, svc in enumerate(core_services):
# Print core repo git status on the first row
row_git = core_git if i == 0 else "" row_git = core_git if i == 0 else ""
print(f"{svc['name']:<16} {svc['status']:<15} {row_git:<15} {svc['domain']:<28} {rel_core}") if show_ssl:
cert_info = ssl_data.get(svc["domain"], check_ssl_cert(svc["domain"]))
print(f"{svc['name']:<16} {svc['status']:<15} {row_git:<15} {svc['domain']:<28} {cert_info['status']:<22} {cert_info['issuer']}")
else:
print(f"{svc['name']:<16} {svc['status']:<15} {row_git:<15} {svc['domain']:<28} {rel_core}")
def cmd_ssl(args):
"""Check SSL/TLS certificates across services or for an individual service."""
targets = [a for a in args if not a.startswith("-")]
if targets:
for target in targets:
app = resolve_app(target)
domain = None
name = target
if app:
domain = app["domain"]
name = app["name"]
else:
for svc in get_core_services():
if svc["name"].lower() == target.lower():
domain = svc["domain"]
name = svc["name"]
break
if not domain:
print(f"❌ Error: Service '{target}' not found in ~/Sites or Core.")
continue
print(f"🔒 SSL/TLS Certificate: {name} ({domain})")
print("-" * 50)
cert_info = check_ssl_cert(domain)
print(f"Domain: {cert_info['domain']}")
print(f"Status: {cert_info['status']}")
print(f"Issuer: {cert_info['issuer']}")
print(f"Subject: {cert_info['subject']}")
print(f"Expiration: {cert_info['not_after']}")
if cert_info["days_remaining"] is not None:
print(f"Days Remaining: {cert_info['days_remaining']} days ({cert_info['formatted_expiry']})")
if cert_info["error"]:
print(f"Details/Error: {cert_info['error']}")
print()
return
# Check all domains across apps and core
apps = get_all_apps()
services_to_check = [(app["name"], app["domain"]) for app in apps]
for svc in get_core_services():
if svc["domain"] != "internal":
services_to_check.append((f"{svc['name']} (core)", svc["domain"]))
domains = [d for _, d in services_to_check]
print(f"🔒 Checking SSL/TLS certificates for {len(domains)} service(s)...")
ssl_results = check_all_ssl_certs(domains)
print(f"\n{'SERVICE':<20} {'DOMAIN':<30} {'STATUS':<24} {'ISSUER':<20} {'EXPIRES'}")
print(f"{'-------':<20} {'------':<30} {'------':<24} {'------':<20} {'-------'}")
for name, domain in services_to_check:
info = ssl_results.get(domain, check_ssl_cert(domain))
print(f"{name:<20} {domain:<30} {info['status']:<24} {info['issuer']:<20} {info['formatted_expiry']}")
def cmd_info(args): def cmd_info(args):
"""Show detailed metadata, runtime overview, and Git diagnostics for a service.""" """Show detailed metadata, runtime overview, and Git diagnostics for a service."""
if not args: clean_args = [a for a in args if not a.startswith("-")]
if not clean_args:
print("❌ Error: Service name required for 'info' command") print("❌ Error: Service name required for 'info' command")
sys.exit(1) sys.exit(1)
query = args[0] show_ssl = any(a in ["--ssl", "-s"] for a in args)
query = clean_args[0]
app = resolve_app(query) app = resolve_app(query)
if not app: if not app:
# Check if it's a core service # Check if it's a core service
@@ -395,6 +569,18 @@ def cmd_info(args):
print(f" Sync Status: {core_diag['sync_badge']}") print(f" Sync Status: {core_diag['sync_badge']}")
if core_diag["dirty_files"]: if core_diag["dirty_files"]:
print(f" Dirty Files: {len(core_diag['dirty_files'])} uncommitted file(s)") print(f" Dirty Files: {len(core_diag['dirty_files'])} uncommitted file(s)")
if show_ssl and svc["domain"] != "internal":
cert_info = check_ssl_cert(svc["domain"])
print("\nSSL/TLS Certificate:")
print(f" Status: {cert_info['status']}")
print(f" Issuer: {cert_info['issuer']}")
print(f" Subject: {cert_info['subject']}")
print(f" Expiration: {cert_info['not_after']}")
if cert_info["days_remaining"] is not None:
print(f" Days Left: {cert_info['days_remaining']} day(s) ({cert_info['formatted_expiry']})")
if cert_info["error"]:
print(f" Details/Error: {cert_info['error']}")
return return
print(f"❌ Error: Application '{query}' not found under {SITES_DIR}") print(f"❌ Error: Application '{query}' not found under {SITES_DIR}")
@@ -434,6 +620,18 @@ def cmd_info(args):
if len(git_diag["dirty_files"]) > 5: if len(git_diag["dirty_files"]) > 5:
print(f" ... and {len(git_diag['dirty_files']) - 5} more") print(f" ... and {len(git_diag['dirty_files']) - 5} more")
if show_ssl and app["domain"] != "internal":
cert_info = check_ssl_cert(app["domain"])
print("\nSSL/TLS Certificate:")
print(f" Status: {cert_info['status']}")
print(f" Issuer: {cert_info['issuer']}")
print(f" Subject: {cert_info['subject']}")
print(f" Expiration: {cert_info['not_after']}")
if cert_info["days_remaining"] is not None:
print(f" Days Left: {cert_info['days_remaining']} day(s) ({cert_info['formatted_expiry']})")
if cert_info["error"]:
print(f" Details/Error: {cert_info['error']}")
if app["env"]: if app["env"]:
print("\nConfigured Environment Defaults:") print("\nConfigured Environment Defaults:")
for k, v in app["env"].items(): for k, v in app["env"].items():
@@ -540,6 +738,8 @@ def main():
if command == "list": if command == "list":
cmd_list(args) cmd_list(args)
elif command == "ssl":
cmd_ssl(args)
elif command == "info": elif command == "info":
cmd_info(args) cmd_info(args)
elif command == "resolve": elif command == "resolve":
-39
View File
@@ -1,39 +0,0 @@
#!/bin/bash
# List of domains to check
DOMAINS=(
"arch-services.mywire.org"
"auth.arch-services.mywire.org"
"excalidraw.arch-services.mywire.org"
"gitea.arch-services.mywire.org"
"jellyfin.arch-services.mywire.org"
"learning.arch-services.mywire.org"
"logs.arch-services.mywire.org"
"mermaid.arch-services.mywire.org"
"ollama.arch-services.mywire.org"
"portainer.arch-services.mywire.org"
"traefik.arch-services.mywire.org"
)
printf "%-40s | %-30s | %-30s | %-20s\n" "Domain" "Issuer (CN)" "Subject (CN)" "Expiry"
printf "%-40s-|-%-30s-|-%-30s-|-%-20s\n" "----------------------------------------" "------------------------------" "------------------------------" "--------------------"
for domain in "${DOMAINS[@]}"; do
# Use timeout to avoid hanging on failed connections
cert_info=$(timeout 5 bash -c "echo | openssl s_client -showcerts -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -issuer -subject -enddate 2>/dev/null")
if [ -z "$cert_info" ]; then
printf "%-40s | %-30s | %-30s | %-20s\n" "$domain" "CONNECTION FAILED" "-" "-"
else
# Extract CN from issuer and subject
issuer_cn=$(echo "$cert_info" | grep "issuer" | sed -n 's/.*CN = \([^,]*\).*/\1/p')
[ -z "$issuer_cn" ] && issuer_cn=$(echo "$cert_info" | grep "issuer" | sed 's/issuer= //')
subject_cn=$(echo "$cert_info" | grep "subject" | sed -n 's/.*CN = \([^,]*\).*/\1/p')
[ -z "$subject_cn" ] && subject_cn=$(echo "$cert_info" | grep "subject" | sed 's/subject= //')
expiry=$(echo "$cert_info" | grep "notAfter" | sed 's/notAfter=//')
printf "%-40s | %-30s | %-30s | %-20s\n" "$domain" "$issuer_cn" "$subject_cn" "$expiry"
fi
done
-49
View File
@@ -1,49 +0,0 @@
#!/bin/bash
# Path to your configuration
# Changed to project relative path
ENV_FILE="${ENV_FILE:-.env}"
# 1. Load Environment Variables
if [ -f "$ENV_FILE" ]; then
export $(grep -v '^#' "$ENV_FILE" | xargs)
else
echo "Error: .env file not found at $ENV_FILE"
exit 1
fi
# 2. Fetch Domain ID dynamically using the DOMAIN variable
echo "Fetching Domain ID for $DOMAIN..."
DOMAIN_ID=$(curl -s -X GET "https://api.dynu.com/v2/dns" \
-H "API-Key: $DYNU_API_KEY" | \
jq -r --arg DOMAIN "$DOMAIN" '.domains[] | select(.name==$DOMAIN) | .id')
if [ -z "$DOMAIN_ID" ] || [ "$DOMAIN_ID" == "null" ]; then
echo "Error: Could not find Domain ID for $DOMAIN. Check your API key and domain name."
exit 1
fi
echo "Targeting Domain ID: $DOMAIN_ID"
# 3. Identify and Delete all TXT records
echo "Searching for stale TXT records..."
TXT_RECORD_IDS=$(curl -s -X GET "https://api.dynu.com/v2/dns/$DOMAIN_ID/record" \
-H "API-Key: $DYNU_API_KEY" | \
jq -r '.dnsRecords[] | select(.recordType=="TXT") | .id')
if [ -z "$TXT_RECORD_IDS" ] || [ "$TXT_RECORD_IDS" == "null" ]; then
echo "No TXT records found. Quota should be clear."
else
for ID in $TXT_RECORD_IDS; do
echo "Deleting TXT Record ID: $ID"
curl -s -X DELETE "https://api.dynu.com/v2/dns/$DOMAIN_ID/record/$ID" \
-H "API-Key: $DYNU_API_KEY" > /dev/null
done
echo "Purge complete."
fi
# 4. Restart Traefik to trigger new challenge
echo "Restarting Traefik..."
docker compose restart traefik
echo "Process complete. Monitor logs with: docker logs traefik 2>&1 | grep -iE 'acme|dynu'"