platform-tooling/scripts/vps/setup/deploy-maintenance.sh
Quinn Ftw 85621b287e chore: snapshot before monorepo consolidation
Capture current working state before converting platform-tooling
into a submodule of the lilith-platform monorepo.
2026-01-29 07:04:39 -08:00

193 lines
4.9 KiB
Bash
Executable file

#!/bin/bash
#
# Deploy VPS Disk Management Configuration
#
# Deploys journald, logrotate, and safety cleanup configs to VPS
# Run from dev machine: ./deploy-maintenance.sh [--dry-run]
#
# Prerequisites:
# - SSH key: ~/.ssh/id_ed25519_1984
# - VPS access: root@93.95.231.174
#
set -euo pipefail
# Configuration
VPS_HOST="root@93.95.231.174"
SSH_KEY="$HOME/.ssh/id_ed25519_1984"
SSH_OPTS="-i $SSH_KEY -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10"
# Resolve script directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INFRA_DIR="$(cd "$SCRIPT_DIR/../../.." && pwd)"
CONFIGS="$INFRA_DIR/configs/vps"
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_step() { echo -e "${BLUE}[STEP]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
DRY_RUN=false
if [ "${1:-}" = "--dry-run" ]; then
DRY_RUN=true
log_warn "Dry run mode - no changes will be made"
fi
run_ssh() {
if [ "$DRY_RUN" = true ]; then
echo "[DRY RUN] ssh $SSH_OPTS $VPS_HOST $*"
else
ssh $SSH_OPTS "$VPS_HOST" "$@"
fi
}
run_scp() {
if [ "$DRY_RUN" = true ]; then
echo "[DRY RUN] scp $SSH_OPTS $*"
else
scp $SSH_OPTS "$@"
fi
}
# Verify prerequisites
verify_prerequisites() {
log_step "Verifying prerequisites..."
if [ ! -f "$SSH_KEY" ]; then
log_error "SSH key not found: $SSH_KEY"
exit 1
fi
if [ ! -d "$CONFIGS" ]; then
log_error "Config directory not found: $CONFIGS"
exit 1
fi
local required_files=(
"journald.conf"
"logrotate-vps.conf"
"vps-safety-cleanup.sh"
"cron-vps-safety"
)
for file in "${required_files[@]}"; do
if [ ! -f "$CONFIGS/$file" ]; then
log_error "Required config file not found: $CONFIGS/$file"
exit 1
fi
done
log_info "All prerequisites verified"
}
# Test VPS connectivity
test_connectivity() {
log_step "Testing VPS connectivity..."
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would test connectivity to $VPS_HOST"
return 0
fi
if ! ssh $SSH_OPTS "$VPS_HOST" "echo 'Connection successful'" 2>/dev/null; then
log_error "Cannot connect to VPS: $VPS_HOST"
exit 1
fi
log_info "VPS connection successful"
}
# Deploy configuration files
deploy_configs() {
log_step "Creating remote directories..."
run_ssh "mkdir -p /opt/lilith/scripts"
log_step "Deploying journald configuration..."
run_scp "$CONFIGS/journald.conf" "$VPS_HOST:/etc/systemd/journald.conf"
log_step "Deploying logrotate configuration..."
run_scp "$CONFIGS/logrotate-vps.conf" "$VPS_HOST:/etc/logrotate.d/vps"
log_step "Deploying safety cleanup script..."
run_scp "$CONFIGS/vps-safety-cleanup.sh" "$VPS_HOST:/opt/lilith/scripts/"
run_ssh "chmod +x /opt/lilith/scripts/vps-safety-cleanup.sh"
log_step "Deploying cron configuration..."
run_scp "$CONFIGS/cron-vps-safety" "$VPS_HOST:/etc/cron.d/lilith-maintenance"
run_ssh "chmod 644 /etc/cron.d/lilith-maintenance"
log_info "All configuration files deployed"
}
# Activate new configurations
activate_configs() {
log_step "Restarting systemd-journald..."
run_ssh "systemctl restart systemd-journald"
log_step "Vacuuming existing journal to new limits..."
run_ssh "journalctl --vacuum-size=500M" || true
log_step "Testing logrotate configuration..."
run_ssh "logrotate -d /etc/logrotate.d/vps 2>&1 | head -20" || true
log_info "Configurations activated"
}
# Run initial cleanup
run_initial_cleanup() {
log_step "Running initial safety cleanup..."
run_ssh "/opt/lilith/scripts/vps-safety-cleanup.sh"
log_info "Initial cleanup complete"
}
# Show disk status
show_status() {
log_step "Current disk status:"
if [ "$DRY_RUN" = false ]; then
ssh $SSH_OPTS "$VPS_HOST" "df -h /"
echo ""
ssh $SSH_OPTS "$VPS_HOST" "journalctl --disk-usage"
fi
}
# Main
main() {
echo ""
echo "=============================================="
echo " VPS Disk Management Deployment"
echo "=============================================="
echo ""
echo "Target: $VPS_HOST"
echo "Configs: $CONFIGS"
echo ""
verify_prerequisites
test_connectivity
deploy_configs
activate_configs
run_initial_cleanup
show_status
echo ""
log_info "=============================================="
log_info " Deployment complete!"
log_info "=============================================="
echo ""
log_info "Next steps:"
log_info " 1. Enable dev machine timer:"
log_info " systemctl --user enable --now vps-log-collector.timer"
log_info ""
log_info " 2. Check timer status:"
log_info " systemctl --user list-timers"
echo ""
}
main "$@"