|
| 1 | +#!/bin/bash |
| 2 | +# ============================================================================= |
| 3 | +# Install Traefik as a systemd service |
| 4 | +# ============================================================================= |
| 5 | +# This script installs Traefik as a system service that starts automatically |
| 6 | +# on boot and runs continuously to route traffic to dev containers. |
| 7 | +# |
| 8 | +# Usage: sudo ./install-traefik-service.sh |
| 9 | +# ============================================================================= |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +# Colors |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +BLUE='\033[0;34m' |
| 18 | +NC='\033[0m' |
| 19 | + |
| 20 | +print_header() { |
| 21 | + echo -e "${BLUE}=====================================${NC}" |
| 22 | + echo -e "${BLUE}$1${NC}" |
| 23 | + echo -e "${BLUE}=====================================${NC}" |
| 24 | +} |
| 25 | + |
| 26 | +print_success() { |
| 27 | + echo -e "${GREEN}✓ $1${NC}" |
| 28 | +} |
| 29 | + |
| 30 | +print_warning() { |
| 31 | + echo -e "${YELLOW}⚠ $1${NC}" |
| 32 | +} |
| 33 | + |
| 34 | +print_error() { |
| 35 | + echo -e "${RED}✗ $1${NC}" |
| 36 | +} |
| 37 | + |
| 38 | +# Check if running as root |
| 39 | +if [ "$EUID" -ne 0 ]; then |
| 40 | + print_error "Please run as root: sudo $0" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 45 | +INSTALL_DIR="/opt/simpleaccounts-proxy" |
| 46 | + |
| 47 | +print_header "Installing Traefik Proxy Service" |
| 48 | + |
| 49 | +# Check prerequisites |
| 50 | +echo "Checking prerequisites..." |
| 51 | + |
| 52 | +if ! command -v docker &> /dev/null; then |
| 53 | + print_error "Docker is not installed. Please install Docker first." |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | +print_success "Docker is installed" |
| 57 | + |
| 58 | +if ! docker compose version &> /dev/null; then |
| 59 | + print_error "Docker Compose is not installed. Please install Docker Compose first." |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | +print_success "Docker Compose is installed" |
| 63 | + |
| 64 | +# Create installation directory |
| 65 | +echo "" |
| 66 | +echo "Setting up installation directory..." |
| 67 | +mkdir -p "$INSTALL_DIR" |
| 68 | +print_success "Created $INSTALL_DIR" |
| 69 | + |
| 70 | +# Copy docker-compose file |
| 71 | +cp "$SCRIPT_DIR/docker-compose.proxy.yml" "$INSTALL_DIR/" |
| 72 | +print_success "Copied docker-compose.proxy.yml" |
| 73 | + |
| 74 | +# Install systemd service |
| 75 | +echo "" |
| 76 | +echo "Installing systemd service..." |
| 77 | +cp "$SCRIPT_DIR/traefik.service" /etc/systemd/system/traefik-proxy.service |
| 78 | +print_success "Installed traefik-proxy.service" |
| 79 | + |
| 80 | +# Reload systemd |
| 81 | +systemctl daemon-reload |
| 82 | +print_success "Reloaded systemd daemon" |
| 83 | + |
| 84 | +# Enable service to start on boot |
| 85 | +systemctl enable traefik-proxy.service |
| 86 | +print_success "Enabled traefik-proxy service (starts on boot)" |
| 87 | + |
| 88 | +# Start the service |
| 89 | +echo "" |
| 90 | +echo "Starting Traefik..." |
| 91 | +systemctl start traefik-proxy.service |
| 92 | + |
| 93 | +# Wait for container to be ready |
| 94 | +sleep 3 |
| 95 | + |
| 96 | +# Check status |
| 97 | +if systemctl is-active --quiet traefik-proxy.service; then |
| 98 | + print_success "Traefik proxy is running!" |
| 99 | +else |
| 100 | + print_error "Failed to start Traefik. Check logs with: journalctl -u traefik-proxy.service" |
| 101 | + exit 1 |
| 102 | +fi |
| 103 | + |
| 104 | +# Detect server IP |
| 105 | +SERVER_IP=$(hostname -I 2>/dev/null | awk '{print $1}') |
| 106 | +if [ -z "$SERVER_IP" ]; then |
| 107 | + SERVER_IP=$(ip route get 1 2>/dev/null | awk '{print $7; exit}') |
| 108 | +fi |
| 109 | +NIP_IP=$(echo "$SERVER_IP" | tr '.' '-') |
| 110 | + |
| 111 | +# Print summary |
| 112 | +print_header "Installation Complete!" |
| 113 | +echo "" |
| 114 | +echo -e "Traefik is now running as a system service." |
| 115 | +echo "" |
| 116 | +echo -e "${YELLOW}Service Management:${NC}" |
| 117 | +echo -e " Status: ${GREEN}sudo systemctl status traefik-proxy${NC}" |
| 118 | +echo -e " Stop: ${GREEN}sudo systemctl stop traefik-proxy${NC}" |
| 119 | +echo -e " Start: ${GREEN}sudo systemctl start traefik-proxy${NC}" |
| 120 | +echo -e " Restart: ${GREEN}sudo systemctl restart traefik-proxy${NC}" |
| 121 | +echo -e " Logs: ${GREEN}journalctl -u traefik-proxy -f${NC}" |
| 122 | +echo "" |
| 123 | +echo -e "${YELLOW}Dashboard:${NC}" |
| 124 | +echo -e " http://proxy.${NIP_IP}.nip.io:8090" |
| 125 | +echo -e " http://localhost:8090" |
| 126 | +echo "" |
| 127 | +echo -e "${YELLOW}Next Steps:${NC}" |
| 128 | +echo -e " Each developer runs: ${GREEN}./setup-user.sh <username>${NC}" |
| 129 | +echo "" |
0 commit comments