#!/bin/bash set -e # Colors GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color log_info() { echo -e "${YELLOW}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check for sudo/root if [ "$EUID" -ne 0 ]; then log_error "Please run as root or with sudo" exit 1 fi # Determine paths SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" SYSTEMD_DIR="$PROJECT_ROOT/systemd" DEST_DIR="/etc/systemd/system" SERVICE_FILE="cert-renewer.service" TIMER_FILE="cert-renewer.timer" install_unit() { local unit_file=$1 local src_path="$SYSTEMD_DIR/$unit_file" local dest_path="$DEST_DIR/$unit_file" if [ ! -f "$src_path" ]; then log_error "Source file not found: $src_path" exit 1 fi log_info "Installing $unit_file..." # Remove existing link or file if it exists to ensure clean install if [ -L "$dest_path" ] || [ -f "$dest_path" ]; then log_info "Removing existing $dest_path" rm -f "$dest_path" fi # Create symlink ln -s "$src_path" "$dest_path" if [ -L "$dest_path" ]; then log_success "Linked $src_path to $dest_path" else log_error "Failed to link $unit_file" exit 1 fi } # Main execution log_info "Starting installation of systemd services..." install_unit "$SERVICE_FILE" install_unit "$TIMER_FILE" log_info "Reloading systemd daemon..." systemctl daemon-reload log_success "Systemd daemon reloaded" log_info "Enabling and starting $TIMER_FILE..." systemctl enable --now "$TIMER_FILE" log_success "$TIMER_FILE enabled and started" log_info "Checking status of $TIMER_FILE..." if systemctl is-active --quiet "$TIMER_FILE"; then systemctl status "$TIMER_FILE" --no-pager echo "" log_success "Installation complete!" else log_error "$TIMER_FILE is not active" systemctl status "$TIMER_FILE" --no-pager exit 1 fi