paper-dynasty-discord/deploy.sh
Cal Corum 440f017c92 Add HTTP health check endpoint for container monitoring
Implements a comprehensive health check system using aiohttp to support
container orchestration and external monitoring systems.

Features:
- /health endpoint: Basic liveness check (is process running?)
- /ready endpoint: Readiness check (is bot connected to Discord?)
- /metrics endpoint: Detailed bot metrics (guilds, users, cogs, latency)

Changes:
- Add aiohttp to requirements.txt
- Create health_server.py module with HTTP server
- Update paperdynasty.py to run health server alongside bot
- Update docker-compose.yml with HTTP-based healthcheck
- Fix deploy.sh Docker image name

Benefits:
- Auto-restart on bot hangs/deadlocks
- Foundation for external monitoring (Prometheus, Grafana, etc.)
- Detailed diagnostics for troubleshooting
- Industry-standard health check pattern

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 14:44:53 -06:00

182 lines
4.8 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e # Exit on error
# Configuration
DOCKER_IMAGE="manticorum67/paper-dynasty-discordapp"
REMOTE_HOST="sba-bots"
REMOTE_PATH="/home/cal/container-data/paper-dynasty"
DOCKERFILE_PATH="."
AUTO_YES=false
# 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} $1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-y|--yes)
AUTO_YES=true
shift
;;
*)
VERSION=$1
shift
;;
esac
done
# Get version from argument or prompt
if [ -z "$VERSION" ]; then
echo ""
log_info "Recent version tags:"
git tag -l "v*" | sort -V | tail -5
echo ""
read -p "Enter new version (e.g., 1.3.0 for features, 1.2.1 for bugfix): " VERSION
fi
# Remove 'v' prefix if provided
VERSION=${VERSION#v}
# Validate version format
if ! [[ $VERSION =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
log_error "Version must be in format X.Y or X.Y.Z (e.g., 1.3 or 1.3.0)"
exit 1
fi
# Confirm deployment
echo ""
log_warning "About to deploy Paper Dynasty version ${VERSION}"
log_info "This will:"
echo " 1. Build Docker image with tags: latest and ${VERSION}"
echo " 2. Push both tags to Docker Hub (${DOCKER_IMAGE})"
echo " 3. Create git tag v${VERSION}"
echo " 4. Deploy to ${REMOTE_HOST}"
echo ""
if [ "$AUTO_YES" = false ]; then
read -p "Continue? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Deployment cancelled"
exit 0
fi
else
log_info "Auto-confirmed with --yes flag"
fi
# Check if git tag already exists
if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
log_error "Git tag v${VERSION} already exists!"
log_info "If you want to redeploy, delete the tag first: git tag -d v${VERSION} && git push origin :refs/tags/v${VERSION}"
exit 1
fi
# Check for uncommitted changes
if ! git diff-index --quiet HEAD --; then
log_warning "You have uncommitted changes!"
git status --short
echo ""
if [ "$AUTO_YES" = false ]; then
read -p "Continue anyway? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Deployment cancelled"
exit 0
fi
else
log_warning "Continuing with uncommitted changes (--yes flag)"
fi
fi
echo ""
log_info "🏗️ Building Docker image for version ${VERSION}..."
if docker build -t $DOCKER_IMAGE:latest -t $DOCKER_IMAGE:$VERSION $DOCKERFILE_PATH; then
log_success "Docker image built successfully"
else
log_error "Docker build failed"
exit 1
fi
echo ""
log_info "📦 Pushing images to Docker Hub..."
log_info "Pushing ${DOCKER_IMAGE}:latest..."
if docker push $DOCKER_IMAGE:latest; then
log_success "Pushed latest tag"
else
log_error "Failed to push latest tag"
exit 1
fi
log_info "Pushing ${DOCKER_IMAGE}:${VERSION}..."
if docker push $DOCKER_IMAGE:$VERSION; then
log_success "Pushed version ${VERSION} tag"
else
log_error "Failed to push version tag"
exit 1
fi
echo ""
log_info "🏷️ Creating git tag v${VERSION}..."
git tag -a "v${VERSION}" -m "Release v${VERSION}"
git push origin "v${VERSION}"
log_success "Git tag created and pushed"
echo ""
log_info "🚀 Deploying to ${REMOTE_HOST}..."
log_info "Running: cd ${REMOTE_PATH} && docker-compose pull"
if ssh $REMOTE_HOST "cd $REMOTE_PATH && docker-compose pull"; then
log_success "Successfully pulled latest image"
else
log_error "Failed to pull image on remote server"
exit 1
fi
log_info "Running: docker-compose down && docker-compose up -d"
if ssh $REMOTE_HOST "cd $REMOTE_PATH && docker-compose down && docker-compose up -d"; then
log_success "Container restarted successfully"
else
log_error "Failed to restart container"
log_warning "You may need to SSH in manually to fix: ssh ${REMOTE_HOST}"
exit 1
fi
# Optional: Clean up old images on remote
log_info "Cleaning up old Docker images on remote..."
ssh $REMOTE_HOST "docker image prune -f" >/dev/null 2>&1 || true
echo ""
log_success "🎉 Deployment complete!"
echo ""
log_info "Version v${VERSION} is now running on ${REMOTE_HOST}"
log_info "Check logs with: ssh ${REMOTE_HOST} 'cd ${REMOTE_PATH} && docker-compose logs -f paper-dynasty'"
log_info "Check status with: ssh ${REMOTE_HOST} 'cd ${REMOTE_PATH} && docker-compose ps'"
echo ""
log_info "If you need to rollback:"
echo " ssh ${REMOTE_HOST}"
echo " cd ${REMOTE_PATH}"
echo " # Find previous version: docker image ls | grep paper-dynasty"
echo " # Edit docker-compose.yml to use specific version tag"
echo " docker compose up -d"
echo ""