#!/bin/bash # ============================================ # ASF Traceability Matrix Deployment Script # ============================================ # This script deploys the Traceability Matrix web app # using Docker Compose with Caddy reverse proxy # # Domain: Traceability.nabd-co.com # ============================================ set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration APP_NAME="traceability" APP_DIR="/opt/traceability" CADDY_DIR="/root/caddy" REPO_URL="" # Add your git repo URL if using git deployment echo -e "${BLUE}============================================${NC}" echo -e "${BLUE} ASF Traceability Matrix Deployment${NC}" echo -e "${BLUE}============================================${NC}" # Function to print status print_status() { echo -e "${GREEN}[✓]${NC} $1" } print_warning() { echo -e "${YELLOW}[!]${NC} $1" } print_error() { echo -e "${RED}[✗]${NC} $1" } # Check if running as root if [ "$EUID" -ne 0 ]; then print_error "Please run as root (sudo ./deploy.sh)" exit 1 fi # Step 1: Create application directory echo "" echo -e "${BLUE}Step 1: Setting up application directory...${NC}" mkdir -p $APP_DIR print_status "Created directory: $APP_DIR" # Step 2: Copy files to application directory (if running from repo) echo "" echo -e "${BLUE}Step 2: Copying application files...${NC}" if [ -f "docker-compose.yml" ]; then cp -r . $APP_DIR/ print_status "Copied application files to $APP_DIR" else print_warning "No local files found. Please ensure files are in $APP_DIR" fi cd $APP_DIR # Step 3: Ensure Caddy network exists echo "" echo -e "${BLUE}Step 3: Checking Docker network...${NC}" if ! docker network ls | grep -q "caddy_default"; then print_warning "Caddy network not found. Creating..." docker network create caddy_default print_status "Created caddy_default network" else print_status "Caddy network exists" fi # Step 4: Update Caddy configuration echo "" echo -e "${BLUE}Step 4: Updating Caddy configuration...${NC}" # Check if Traceability entry already exists in Caddyfile if grep -q "Traceability.nabd-co.com" "$CADDY_DIR/Caddyfile" 2>/dev/null; then print_status "Caddy configuration already exists" else # Append Traceability configuration to Caddyfile cat >> "$CADDY_DIR/Caddyfile" << 'EOF' # ------------------------- # Traceability Matrix Proxy # ------------------------- Traceability.nabd-co.com { reverse_proxy traceability_web:80 encode gzip } EOF print_status "Added Traceability configuration to Caddyfile" fi # Step 5: Build and start the application echo "" echo -e "${BLUE}Step 5: Building and starting application...${NC}" docker compose down --remove-orphans 2>/dev/null || true docker compose build --no-cache docker compose up -d print_status "Application started" # Step 6: Reload Caddy echo "" echo -e "${BLUE}Step 6: Reloading Caddy...${NC}" cd $CADDY_DIR docker compose exec -T caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null || { print_warning "Could not reload Caddy automatically. Restarting container..." docker compose restart caddy 2>/dev/null || docker restart caddy 2>/dev/null || true } print_status "Caddy reloaded" # Step 7: Health check echo "" echo -e "${BLUE}Step 7: Running health check...${NC}" sleep 5 if docker ps | grep -q "traceability_web"; then print_status "Container is running" else print_error "Container failed to start. Check logs with: docker logs traceability_web" exit 1 fi # Final output echo "" echo -e "${GREEN}============================================${NC}" echo -e "${GREEN} Deployment Complete!${NC}" echo -e "${GREEN}============================================${NC}" echo "" echo -e "Application URL: ${BLUE}https://Traceability.nabd-co.com${NC}" echo "" echo -e "Useful commands:" echo -e " View logs: ${YELLOW}docker logs -f traceability_web${NC}" echo -e " Restart: ${YELLOW}docker compose -f $APP_DIR/docker-compose.yml restart${NC}" echo -e " Stop: ${YELLOW}docker compose -f $APP_DIR/docker-compose.yml down${NC}" echo -e " Rebuild: ${YELLOW}docker compose -f $APP_DIR/docker-compose.yml up -d --build${NC}" echo ""