#!/bin/bash # LabFusion Gitea Runners Management Script # Usage: ./scripts/manage-runners.sh [start|stop|restart|status|logs|clean] set -e COMPOSE_FILE="docker-compose.runners.yml" ENV_FILE=".env.runners" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Helper functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if .env.runners exists check_env_file() { if [ ! -f "$ENV_FILE" ]; then log_error "Environment file $ENV_FILE not found!" log_info "Please copy env.runners.example to $ENV_FILE and configure it:" log_info " cp env.runners.example $ENV_FILE" log_info " # Edit $ENV_FILE with your Gitea instance URL and runner token" exit 1 fi } # Start runners start_runners() { log_info "Starting LabFusion Gitea runners..." check_env_file docker-compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d log_success "Runners started successfully!" log_info "Runners:" log_info " - Heavy (Java/Python): labfusion-runner-heavy" log_info " - Light (Node.js/Frontend): labfusion-runner-light" log_info " - Docker (Integration): labfusion-runner-docker" log_info " - Security (Scans): labfusion-runner-security" # Wait a moment for health checks sleep 5 show_status } # Stop runners stop_runners() { log_info "Stopping LabFusion Gitea runners..." docker-compose -f "$COMPOSE_FILE" down log_success "Runners stopped successfully!" } # Restart runners restart_runners() { log_info "Restarting LabFusion Gitea runners..." stop_runners sleep 2 start_runners } # Show runner status show_status() { log_info "LabFusion Gitea Runners Status:" echo # Check if compose file exists if [ ! -f "$COMPOSE_FILE" ]; then log_error "Docker Compose file $COMPOSE_FILE not found!" exit 1 fi # Show container status docker-compose -f "$COMPOSE_FILE" ps echo log_info "Runner Health Status:" # Check each runner's health for runner in labfusion-runner-heavy labfusion-runner-light labfusion-runner-docker labfusion-runner-security; do if docker ps --format "table {{.Names}}\t{{.Status}}" | grep -q "$runner"; then status=$(docker inspect --format='{{.State.Health.Status}}' "$runner" 2>/dev/null || echo "unknown") case $status in "healthy") log_success "$runner: $status" ;; "unhealthy") log_error "$runner: $status" ;; "starting") log_warning "$runner: $status" ;; *) log_warning "$runner: $status" ;; esac else log_error "$runner: not running" fi done } # Show logs show_logs() { local runner=${2:-""} if [ -n "$runner" ]; then log_info "Showing logs for $runner..." docker-compose -f "$COMPOSE_FILE" logs -f "$runner" else log_info "Showing logs for all runners..." docker-compose -f "$COMPOSE_FILE" logs -f fi } # Clean up runners and data clean_runners() { log_warning "This will remove all runners and their data. Are you sure? (y/N)" read -r response if [[ "$response" =~ ^[Yy]$ ]]; then log_info "Cleaning up runners and data..." # Stop and remove containers docker-compose -f "$COMPOSE_FILE" down -v # Remove volumes docker volume rm $(docker volume ls -q | grep runner-data) 2>/dev/null || true docker volume rm shared-cache 2>/dev/null || true log_success "Cleanup completed!" else log_info "Cleanup cancelled." fi } # Show help show_help() { echo "LabFusion Gitea Runners Management Script" echo echo "Usage: $0 [COMMAND]" echo echo "Commands:" echo " start Start all runners" echo " stop Stop all runners" echo " restart Restart all runners" echo " status Show runner status and health" echo " logs Show logs for all runners" echo " logs [runner] Show logs for specific runner" echo " clean Remove all runners and data (destructive)" echo " help Show this help message" echo echo "Examples:" echo " $0 start" echo " $0 status" echo " $0 logs labfusion-runner-heavy" echo " $0 clean" } # Main script logic case "${1:-help}" in start) start_runners ;; stop) stop_runners ;; restart) restart_runners ;; status) show_status ;; logs) show_logs "$@" ;; clean) clean_runners ;; help|--help|-h) show_help ;; *) log_error "Unknown command: $1" echo show_help exit 1 ;; esac