Files
mev-beta/scripts/deploy-production-docker.sh
Administrator 0b917b0ea3 feat(deployment): add production Docker deployment with auto-restart and on-boot startup
Add comprehensive production deployment infrastructure with Docker auto-restart
and systemd on-boot startup capabilities.

Changes:
- Add deploy-production-docker.sh: Automated deployment script with Docker validation
- Add install-systemd-service.sh: Systemd service installer for auto-start on boot
- Add scripts/mev-bot.service: Systemd service definition for MEV bot
- Update docker-compose.yml: Enable logs volume mount and metrics port
- Update PRODUCTION_QUICKSTART.md: Simplified deployment documentation

Features:
- Docker auto-restart on failure (restart: always policy)
- Systemd auto-start on system boot
- Persistent logs via volume mount
- Health checks and resource limits
- Comprehensive deployment validation
- Easy-to-use installation scripts

Usage:
  ./scripts/deploy-production-docker.sh        # Deploy with Docker
  sudo ./scripts/install-systemd-service.sh    # Enable auto-start on boot

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 03:55:48 +01:00

193 lines
6.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Production Docker Deployment Script for MEV Bot (Master Repo)
# This script deploys the MEV bot with Docker auto-restart and systemd on-boot startup
# Optimized for production use with the master repository
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Script information
echo -e "${PURPLE}🚀 MEV Bot Production Docker Deployment${NC}"
echo -e "${PURPLE}========================================${NC}"
echo ""
# Check if running from project root
if [ ! -f "go.mod" ]; then
echo -e "${RED}❌ Error: This script must be run from the project root directory${NC}"
exit 1
fi
# Check if Docker is available
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Error: Docker is not installed${NC}"
echo -e "${YELLOW}Please install Docker first: https://docs.docker.com/get-docker/${NC}"
exit 1
fi
# Check if Docker Compose is available
if ! docker compose version &> /dev/null; then
echo -e "${RED}❌ Error: Docker Compose is not available${NC}"
echo -e "${YELLOW}Please install Docker Compose: https://docs.docker.com/compose/install/${NC}"
exit 1
fi
echo -e "${GREEN}✅ Docker and Docker Compose are available${NC}"
# Check/Create .env file
echo -e "${BLUE}🔧 Checking environment configuration...${NC}"
if [ ! -f ".env" ]; then
if [ -f ".env.production" ]; then
echo -e "${YELLOW}⚠️ .env file not found, copying from .env.production${NC}"
cp .env.production .env
echo -e "${GREEN}✅ Created .env from .env.production${NC}"
elif [ -f ".env.example" ]; then
echo -e "${YELLOW}⚠️ .env file not found, copying from .env.example${NC}"
cp .env.example .env
echo -e "${YELLOW}⚠️ Please edit .env file with your configuration before running again${NC}"
echo -e "${YELLOW} Required: ARBITRUM_RPC_ENDPOINT, ARBITRUM_WS_ENDPOINT${NC}"
exit 1
else
echo -e "${RED}❌ Error: No .env or .env.example file found${NC}"
exit 1
fi
else
echo -e "${GREEN}✅ .env file exists${NC}"
fi
# Verify critical environment variables
echo -e "${BLUE}🔍 Verifying environment variables...${NC}"
source .env
MISSING_VARS=()
if [ -z "${ARBITRUM_RPC_ENDPOINT:-}" ]; then
MISSING_VARS+=("ARBITRUM_RPC_ENDPOINT")
fi
if [ -z "${ARBITRUM_WS_ENDPOINT:-}" ]; then
MISSING_VARS+=("ARBITRUM_WS_ENDPOINT")
fi
if [ ${#MISSING_VARS[@]} -ne 0 ]; then
echo -e "${RED}❌ Error: Missing required environment variables in .env:${NC}"
for var in "${MISSING_VARS[@]}"; do
echo -e "${RED} - $var${NC}"
done
exit 1
fi
echo -e "${GREEN}✅ Required environment variables are set${NC}"
# Create required directories
echo -e "${BLUE}📁 Creating required directories...${NC}"
mkdir -p logs config data
# Stop any existing containers
echo -e "${BLUE}⏹️ Stopping any existing containers...${NC}"
docker compose down 2>/dev/null || true
# Build the Docker image
echo -e "${BLUE}🔨 Building Docker image...${NC}"
docker compose build
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Error: Failed to build Docker image${NC}"
exit 1
fi
echo -e "${GREEN}✅ Docker image built successfully${NC}"
# Start the container
echo -e "${BLUE}🚀 Starting MEV Bot container...${NC}"
docker compose up -d
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Error: Failed to start container${NC}"
exit 1
fi
echo -e "${GREEN}✅ MEV Bot container started successfully${NC}"
# Wait for container to be healthy
echo -e "${BLUE}⏳ Waiting for container to be ready...${NC}"
sleep 5
# Check container status
CONTAINER_STATUS=$(docker compose ps --format json 2>/dev/null | grep -o '"State":"[^"]*"' | cut -d'"' -f4 || echo "unknown")
if [ "$CONTAINER_STATUS" = "running" ]; then
echo -e "${GREEN}✅ Container is running${NC}"
else
echo -e "${YELLOW}⚠️ Container status: $CONTAINER_STATUS${NC}"
fi
# Setup systemd service for auto-start on boot
echo ""
echo -e "${BLUE}🔧 Setting up systemd service for auto-start on boot...${NC}"
if [ "$EUID" -ne 0 ]; then
echo -e "${YELLOW}⚠️ Systemd setup requires root privileges${NC}"
echo -e "${YELLOW} Run the following commands to enable auto-start on boot:${NC}"
echo ""
echo -e "${CYAN} sudo cp scripts/mev-bot.service /etc/systemd/system/${NC}"
echo -e "${CYAN} sudo systemctl daemon-reload${NC}"
echo -e "${CYAN} sudo systemctl enable mev-bot.service${NC}"
echo -e "${CYAN} sudo systemctl start mev-bot.service${NC}"
echo ""
else
# Running as root, set up systemd service
cp scripts/mev-bot.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable mev-bot.service
systemctl start mev-bot.service
echo -e "${GREEN}✅ Systemd service installed and enabled${NC}"
fi
# Display deployment summary
echo ""
echo -e "${GREEN}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ 🎉 MEV Bot Production Deployment Complete! ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${BLUE}📊 Container Information:${NC}"
echo -e " Container: mev-bot-production"
echo -e " Status: $CONTAINER_STATUS"
echo -e " Restart Policy: Always (auto-restart on failure)"
echo ""
echo -e "${BLUE}📝 View Logs:${NC}"
echo -e " ${CYAN}docker compose logs -f mev-bot${NC}"
echo ""
echo -e "${BLUE}🔍 Container Status:${NC}"
echo -e " ${CYAN}docker compose ps${NC}"
echo ""
echo -e "${BLUE}🔄 Restart Container:${NC}"
echo -e " ${CYAN}docker compose restart mev-bot${NC}"
echo ""
echo -e "${BLUE}⏹️ Stop Container:${NC}"
echo -e " ${CYAN}docker compose down${NC}"
echo ""
echo -e "${BLUE}🔧 Systemd Commands (if installed):${NC}"
echo -e " ${CYAN}sudo systemctl status mev-bot${NC} # Check status"
echo -e " ${CYAN}sudo systemctl restart mev-bot${NC} # Restart service"
echo -e " ${CYAN}sudo systemctl stop mev-bot${NC} # Stop service"
echo -e " ${CYAN}journalctl -u mev-bot -f${NC} # View systemd logs"
echo ""
echo -e "${YELLOW}⚠️ Production Deployment Notes:${NC}"
echo -e " - Container will auto-restart on failure (restart: always)"
echo -e " - Install systemd service for auto-start on system boot"
echo -e " - Monitor logs regularly for any issues"
echo -e " - Keep your .env file secure and never commit it"
echo ""
echo -e "${CYAN}🚀 MEV Bot is now running and monitoring Arbitrum for opportunities${NC}"