fetch_ml/scripts/setup-auto-cleanup.sh
Jeremie Fraeys c80e01b752 Add comprehensive self-cleaning system
- Add cleanup.sh script with dry-run, force, and all options
- Add auto-cleanup service setup for macOS (launchd) and Linux (systemd)
- Add cleanup-status.sh for monitoring Docker resources
- Add Makefile targets: self-cleanup, auto-cleanup
- Features colored output, confirmation prompts, and detailed logging
- Auto-cleanup runs daily to keep system clean
- Status monitoring shows resources and service state
2025-12-06 12:40:35 -05:00

90 lines
2.5 KiB
Bash
Executable file

#!/bin/bash
# Setup auto-cleanup service for fetch_ml
# This creates a systemd timer that runs cleanup daily
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_info "Setting up auto-cleanup service..."
# Check if running on macOS or Linux
if [[ "$OSTYPE" == "darwin"* ]]; then
log_info "Detected macOS - setting up launchd agent"
# Create launchd plist
cat > ~/Library/LaunchAgents/com.fetchml.cleanup.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.fetchml.cleanup</string>
<key>ProgramArguments</key>
<array>
<string>$PROJECT_DIR/scripts/cleanup.sh</string>
<string>--force</string>
</array>
<key>StartInterval</key>
<integer>86400</integer>
<key>RunAtLoad</key>
<false/>
<key>StandardOutPath</key>
<string>/tmp/fetchml-cleanup.log</string>
<key>StandardErrorPath</key>
<string>/tmp/fetchml-cleanup.error.log</string>
</dict>
</plist>
EOF
# Load the launchd agent
launchctl load ~/Library/LaunchAgents/com.fetchml.cleanup.plist
log_success "Auto-cleanup service installed for macOS"
log_info "Logs will be in /tmp/fetchml-cleanup.log"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
log_info "Detected Linux - setting up systemd timer"
# Copy service files
sudo cp "$SCRIPT_DIR/auto-cleanup.service" /etc/systemd/system/
sudo cp "$SCRIPT_DIR/auto-cleanup.timer" /etc/systemd/system/
# Reload systemd and enable timer
sudo systemctl daemon-reload
sudo systemctl enable auto-cleanup.timer
sudo systemctl start auto-cleanup.timer
log_success "Auto-cleanup service installed for Linux"
log_info "Check status with: systemctl status auto-cleanup.timer"
else
echo "Unsupported OS: $OSTYPE"
exit 1
fi
log_info "Auto-cleanup will run daily"
log_info "To uninstall:"
if [[ "$OSTYPE" == "darwin"* ]]; then
echo " launchctl unload ~/Library/LaunchAgents/com.fetchml.cleanup.plist"
echo " rm ~/Library/LaunchAgents/com.fetchml.cleanup.plist"
else
echo " sudo systemctl stop auto-cleanup.timer"
echo " sudo systemctl disable auto-cleanup.timer"
echo " sudo rm /etc/systemd/system/auto-cleanup.*"
fi