feat: adds borg, timers and systemd service support
This commit is contained in:
@@ -255,8 +255,44 @@ def check_service_running(service_name):
|
||||
return len(containers)
|
||||
|
||||
|
||||
def get_systemd_timer_status(timer_name):
|
||||
"""Check if a systemd timer is active and enabled, and get next run time"""
|
||||
# Check if timer is active (running/waiting)
|
||||
active_result = subprocess.run(
|
||||
["sudo", "systemctl", "is-active", timer_name],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Check if timer is enabled (will start on boot)
|
||||
enabled_result = subprocess.run(
|
||||
["sudo", "systemctl", "is-enabled", timer_name],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Get next run time
|
||||
list_result = subprocess.run(
|
||||
["sudo", "systemctl", "list-timers", timer_name, "--no-legend"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
is_active = active_result.returncode == 0
|
||||
is_enabled = enabled_result.returncode == 0
|
||||
|
||||
next_run = "unknown"
|
||||
if list_result.returncode == 0 and list_result.stdout.strip():
|
||||
parts = list_result.stdout.strip().split()
|
||||
if len(parts) >= 4:
|
||||
next_run = f"{parts[0]} {parts[1]} {parts[2]}"
|
||||
|
||||
return is_active, is_enabled, next_run
|
||||
|
||||
|
||||
def cmd_list(args):
|
||||
"""List available Docker services"""
|
||||
"""List available Docker services and systemd services"""
|
||||
# Docker services section
|
||||
if not os.path.exists(SERVICES_DIR):
|
||||
printfe("red", f"Error: Services directory not found at {SERVICES_DIR}")
|
||||
return 1
|
||||
@@ -270,21 +306,42 @@ def cmd_list(args):
|
||||
|
||||
if not services:
|
||||
printfe("yellow", "No Docker services found")
|
||||
return 0
|
||||
else:
|
||||
println("Available Docker services:", "blue")
|
||||
for service in sorted(services):
|
||||
container_count = check_service_running(service)
|
||||
is_running = container_count > 0
|
||||
|
||||
println("Available Docker services:", "blue")
|
||||
for service in sorted(services):
|
||||
container_count = check_service_running(service)
|
||||
is_running = container_count > 0
|
||||
if is_running:
|
||||
status = f"[RUNNING - {container_count} container{'s' if container_count > 1 else ''}]"
|
||||
color = "green"
|
||||
else:
|
||||
status = "[STOPPED]"
|
||||
color = "red"
|
||||
|
||||
if is_running:
|
||||
status = f"[RUNNING - {container_count} container{'s' if container_count > 1 else ''}]"
|
||||
printfe(color, f" - {service:<20} {status}")
|
||||
|
||||
# Systemd services section
|
||||
print()
|
||||
println("System services:", "blue")
|
||||
|
||||
systemd_timers = ["borg-backup.timer", "dynamic-dns.timer"]
|
||||
|
||||
for timer in systemd_timers:
|
||||
is_active, is_enabled, next_run = get_systemd_timer_status(timer)
|
||||
service_name = timer.replace('.timer', '')
|
||||
|
||||
if is_active and is_enabled:
|
||||
status = f"[TIMER ACTIVE - next: {next_run}]"
|
||||
color = "green"
|
||||
elif is_enabled:
|
||||
status = "[TIMER ENABLED - INACTIVE]"
|
||||
color = "yellow"
|
||||
else:
|
||||
status = "[STOPPED]"
|
||||
status = "[TIMER DISABLED]"
|
||||
color = "red"
|
||||
|
||||
printfe(color, f" - {service:<20} {status}")
|
||||
printfe(color, f" - {service_name:<20} {status}")
|
||||
|
||||
return 0
|
||||
|
||||
|
81
bin/actions/source.py
Executable file
81
bin/actions/source.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
# Add the helpers directory to the path
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'helpers'))
|
||||
from functions import printfe
|
||||
|
||||
def get_borg_passphrase():
|
||||
"""Get Borg passphrase from 1Password"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["op", "item", "get", "Borg Backup", "--vault=Dotfiles", "--fields=password", "--reveal"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
except subprocess.CalledProcessError:
|
||||
printfe("red", "Error: Failed to retrieve Borg passphrase from 1Password")
|
||||
return None
|
||||
|
||||
def main():
|
||||
"""Generate export commands for Borg environment variables"""
|
||||
args = sys.argv[1:] if len(sys.argv) > 1 else []
|
||||
|
||||
# Get passphrase from 1Password
|
||||
passphrase = get_borg_passphrase()
|
||||
if not passphrase:
|
||||
return 1
|
||||
|
||||
# Generate the export commands
|
||||
exports = [
|
||||
f'export BORG_REPO="/mnt/object_storage/borg-repo"',
|
||||
f'export BORG_PASSPHRASE="{passphrase}"',
|
||||
f'export BORG_CACHE_DIR="/home/menno/.config/borg/cache"',
|
||||
f'export BORG_CONFIG_DIR="/home/menno/.config/borg/config"',
|
||||
f'export BORG_SECURITY_DIR="/home/menno/.config/borg/security"',
|
||||
f'export BORG_KEYS_DIR="/home/menno/.config/borg/keys"'
|
||||
]
|
||||
|
||||
# Check if we're being eval'd (no arguments and stdout is a pipe)
|
||||
if not args and not os.isatty(sys.stdout.fileno()):
|
||||
# Just output the export commands for eval
|
||||
for export_cmd in exports:
|
||||
print(export_cmd)
|
||||
return 0
|
||||
|
||||
# Print instructions and examples
|
||||
printfe("cyan", "🔧 Borg Environment Setup")
|
||||
print()
|
||||
printfe("yellow", "Run the following command to setup your shell:")
|
||||
print()
|
||||
printfe("green", "eval $(dotf source)")
|
||||
print()
|
||||
printfe("red", "⚠️ Repository Permission Issue:")
|
||||
printfe("white", "The Borg repository was created by root, so you need sudo:")
|
||||
print()
|
||||
printfe("green", "sudo -E borg list")
|
||||
printfe("green", "sudo -E borg info")
|
||||
print()
|
||||
printfe("yellow", "Or copy and paste these exports:")
|
||||
print()
|
||||
|
||||
# Output the export commands
|
||||
for export_cmd in exports:
|
||||
print(export_cmd)
|
||||
|
||||
print()
|
||||
printfe("cyan", "📋 Borg commands (use with sudo -E):")
|
||||
printfe("white", " sudo -E borg list # List all backups")
|
||||
printfe("white", " sudo -E borg info # Repository info")
|
||||
printfe("white", " sudo -E borg list ::archive-name # List files in backup")
|
||||
printfe("white", " sudo -E borg mount . ~/borg-mount # Mount as filesystem")
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
99
bin/actions/timers.py
Executable file
99
bin/actions/timers.py
Executable file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# Add the helpers directory to the path
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'helpers'))
|
||||
from functions import printfe
|
||||
|
||||
def run_command(cmd, capture_output=True):
|
||||
"""Run a command and return the result"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=capture_output, text=True)
|
||||
return result
|
||||
except Exception as e:
|
||||
printfe("red", f"Error running command: {e}")
|
||||
return None
|
||||
|
||||
def show_timer_status(timer_name, system_level=True):
|
||||
"""Show concise status for a specific timer"""
|
||||
cmd_prefix = "sudo systemctl" if system_level else "systemctl --user"
|
||||
|
||||
# Get timer status
|
||||
status_cmd = f"{cmd_prefix} is-active {timer_name}"
|
||||
status_result = run_command(status_cmd)
|
||||
status = "active" if status_result and status_result.returncode == 0 else "inactive"
|
||||
|
||||
# Get next run time
|
||||
list_cmd = f"{cmd_prefix} list-timers {timer_name} --no-legend"
|
||||
list_result = run_command(list_cmd)
|
||||
next_run = "unknown"
|
||||
|
||||
if list_result and list_result.returncode == 0 and list_result.stdout.strip():
|
||||
parts = list_result.stdout.strip().split()
|
||||
if len(parts) >= 4:
|
||||
next_run = f"{parts[0]} {parts[1]} {parts[2]} ({parts[3]})"
|
||||
|
||||
# Get service name
|
||||
service_name = timer_name.replace('.timer', '.service')
|
||||
|
||||
# Format output
|
||||
status_color = "green" if status == "active" else "red"
|
||||
service_short = service_name.replace('.service', '')
|
||||
|
||||
printfe(status_color, f"● {service_short:<12} {status:<8} next: {next_run}")
|
||||
|
||||
def show_examples():
|
||||
"""Show example commands for checking services and logs"""
|
||||
printfe("cyan", "=== Useful Commands ===")
|
||||
print()
|
||||
|
||||
printfe("yellow", "Check service status:")
|
||||
print(" sudo systemctl status borg-backup.service")
|
||||
print(" sudo systemctl status dynamic-dns.service")
|
||||
print()
|
||||
|
||||
printfe("yellow", "View logs:")
|
||||
print(" sudo journalctl -u borg-backup.service -f")
|
||||
print(" sudo journalctl -u dynamic-dns.service -f")
|
||||
print(" tail -f /var/log/borg-backup.log")
|
||||
print()
|
||||
|
||||
printfe("yellow", "Manual trigger:")
|
||||
print(" sudo systemctl start borg-backup.service")
|
||||
print(" sudo systemctl start dynamic-dns.service")
|
||||
print()
|
||||
|
||||
printfe("yellow", "List all timers:")
|
||||
print(" sudo systemctl list-timers")
|
||||
print()
|
||||
|
||||
def main():
|
||||
"""Main timers action"""
|
||||
args = sys.argv[1:] if len(sys.argv) > 1 else []
|
||||
|
||||
printfe("cyan", "🕐 System Timers")
|
||||
print()
|
||||
|
||||
# Show timer statuses
|
||||
timers = [
|
||||
("borg-backup.timer", True),
|
||||
("dynamic-dns.timer", True)
|
||||
]
|
||||
|
||||
for timer_name, system_level in timers:
|
||||
if os.path.exists(f"/etc/systemd/system/{timer_name}"):
|
||||
show_timer_status(timer_name, system_level)
|
||||
else:
|
||||
printfe("yellow", f" {timer_name.replace('.timer', ''):<12} not found")
|
||||
|
||||
print()
|
||||
# Show helpful examples
|
||||
show_examples()
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
14
bin/dotf
14
bin/dotf
@@ -27,7 +27,7 @@ def run_script(script_path, args):
|
||||
if not os.path.isfile(script_path) or not os.access(script_path, os.X_OK):
|
||||
printfe("red", f"Error: Script not found or not executable: {script_path}")
|
||||
return 1
|
||||
|
||||
|
||||
result = subprocess.run([script_path] + args, env={**os.environ, "DOTFILES_PATH": DOTFILES_PATH})
|
||||
return result.returncode
|
||||
|
||||
@@ -59,6 +59,14 @@ def lint(args):
|
||||
"""Run the lint action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/lint.py", args)
|
||||
|
||||
def timers(args):
|
||||
"""Run the timers action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/timers.py", args)
|
||||
|
||||
def source(args):
|
||||
"""Run the source action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/source.py", args)
|
||||
|
||||
def ensure_git_hooks():
|
||||
"""Ensure git hooks are correctly set up"""
|
||||
hooks_dir = os.path.join(DOTFILES_ROOT, ".git/hooks")
|
||||
@@ -114,7 +122,9 @@ def main():
|
||||
"secrets": secrets,
|
||||
"auto-start": auto_start,
|
||||
"service": service,
|
||||
"lint": lint
|
||||
"lint": lint,
|
||||
"timers": timers,
|
||||
"source": source
|
||||
}
|
||||
|
||||
if command in commands:
|
||||
|
Reference in New Issue
Block a user