Refactor Docker and configuration files for improved structure and functionality
- Updated `.dockerignore` to include additional development and temporary files, enhancing build efficiency. - Modified `.gitignore` to remove unnecessary entries and streamline ignored files. - Enhanced `docker-compose.yml` with health checks, resource limits, and improved environment variable handling for better service management. - Refactored `Dockerfile.bot` to utilize a multi-stage build for optimized image size and security. - Improved `Makefile` with new commands for deployment, migration, and backup, along with enhanced help documentation. - Updated `requirements.txt` to include new dependencies for environment variable management. - Refactored metrics handling in the bot to ensure proper initialization and collection.
This commit is contained in:
86
scripts/deploy.sh
Normal file
86
scripts/deploy.sh
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Configuration
|
||||
PROJECT_NAME="telegram-helper-bot"
|
||||
DOCKER_COMPOSE_FILE="docker-compose.yml"
|
||||
ENV_FILE=".env"
|
||||
|
||||
echo -e "${GREEN}🚀 Starting deployment of $PROJECT_NAME${NC}"
|
||||
|
||||
# Check if .env file exists
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
echo -e "${RED}❌ Error: $ENV_FILE file not found!${NC}"
|
||||
echo -e "${YELLOW}Please copy env.example to .env and configure your settings${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Load environment variables
|
||||
source "$ENV_FILE"
|
||||
|
||||
# Validate required environment variables
|
||||
required_vars=("BOT_TOKEN" "MAIN_PUBLIC" "GROUP_FOR_POSTS" "GROUP_FOR_MESSAGE" "GROUP_FOR_LOGS")
|
||||
for var in "${required_vars[@]}"; do
|
||||
if [ -z "${!var}" ]; then
|
||||
echo -e "${RED}❌ Error: Required environment variable $var is not set${NC}"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
echo -e "${GREEN}✅ Environment variables validated${NC}"
|
||||
|
||||
# Create necessary directories
|
||||
echo -e "${YELLOW}📁 Creating necessary directories...${NC}"
|
||||
mkdir -p database logs
|
||||
|
||||
# Set proper permissions
|
||||
echo -e "${YELLOW}🔐 Setting proper permissions...${NC}"
|
||||
chmod 600 "$ENV_FILE"
|
||||
chmod 755 database logs
|
||||
|
||||
# Stop existing containers
|
||||
echo -e "${YELLOW}🛑 Stopping existing containers...${NC}"
|
||||
docker-compose -f "$DOCKER_COMPOSE_FILE" down --remove-orphans || true
|
||||
|
||||
# Remove old images
|
||||
echo -e "${YELLOW}🧹 Cleaning up old images...${NC}"
|
||||
docker system prune -f
|
||||
|
||||
# Build and start services
|
||||
echo -e "${YELLOW}🔨 Building and starting services...${NC}"
|
||||
docker-compose -f "$DOCKER_COMPOSE_FILE" up -d --build
|
||||
|
||||
# Wait for services to be healthy
|
||||
echo -e "${YELLOW}⏳ Waiting for services to be healthy...${NC}"
|
||||
sleep 30
|
||||
|
||||
# Check service health
|
||||
echo -e "${YELLOW}🏥 Checking service health...${NC}"
|
||||
if docker-compose -f "$DOCKER_COMPOSE_FILE" ps | grep -q "unhealthy"; then
|
||||
echo -e "${RED}❌ Some services are unhealthy!${NC}"
|
||||
docker-compose -f "$DOCKER_COMPOSE_FILE" logs
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Show service status
|
||||
echo -e "${GREEN}📊 Service status:${NC}"
|
||||
docker-compose -f "$DOCKER_COMPOSE_FILE" ps
|
||||
|
||||
echo -e "${GREEN}✅ Deployment completed successfully!${NC}"
|
||||
echo -e "${GREEN}📊 Monitoring URLs:${NC}"
|
||||
echo -e " Prometheus: http://localhost:9090"
|
||||
echo -e " Grafana: http://localhost:3000"
|
||||
echo -e " Bot Metrics: http://localhost:8000/metrics"
|
||||
echo -e " Bot Health: http://localhost:8000/health"
|
||||
echo -e ""
|
||||
echo -e "${YELLOW}📝 Useful commands:${NC}"
|
||||
echo -e " View logs: docker-compose logs -f"
|
||||
echo -e " Restart: docker-compose restart"
|
||||
echo -e " Stop: docker-compose down"
|
||||
Reference in New Issue
Block a user