cosmo/deploy.sh

335 lines
7.9 KiB
Bash
Executable File

#!/bin/bash
# Cosmo Docker Deployment Script
# Usage: ./deploy.sh [--init|--start|--stop|--restart|--logs|--clean]
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
# Project root directory
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DATA_ROOT="/opt/cosmo/data"
# Log function
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
warn() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
# Check if Docker is installed
check_docker() {
if ! command -v docker &> /dev/null; then
error "Docker is not installed. Please install Docker first."
fi
if ! command -v docker-compose &> /dev/null; then
error "Docker Compose is not installed. Please install Docker Compose first."
fi
log "✓ Docker and Docker Compose are installed"
}
# Create data directories
create_directories() {
log "Creating data directories..."
sudo mkdir -p "$DATA_ROOT/postgres"
sudo mkdir -p "$DATA_ROOT/redis"
sudo mkdir -p "$DATA_ROOT/upload"
sudo mkdir -p "$DATA_ROOT/logs/backend"
sudo mkdir -p "$DATA_ROOT/backups"
# Set permissions
sudo chown -R $(whoami):$(whoami) "$DATA_ROOT"
sudo chmod -R 755 "$DATA_ROOT"
log "✓ Data directories created at $DATA_ROOT"
}
# Check environment file
check_env() {
if [ ! -f "$PROJECT_ROOT/.env.production" ]; then
error ".env.production file not found. Please create it first."
fi
log "✓ Environment file found"
}
# Initialize system
init_system() {
log "==================================="
log " Initializing Cosmo System"
log "==================================="
check_docker
create_directories
check_env
# Copy environment file
cp "$PROJECT_ROOT/.env.production" "$PROJECT_ROOT/.env"
log "Building Docker images..."
cd "$PROJECT_ROOT"
docker-compose build --no-cache
log "Starting database and Redis..."
docker-compose up -d postgres redis
log "Waiting for database to be ready..."
sleep 10
# Check if database is ready
for i in {1..30}; do
if docker-compose exec -T postgres pg_isready -U postgres &> /dev/null; then
log "✓ Database is ready"
break
fi
if [ $i -eq 30 ]; then
error "Database failed to start"
fi
sleep 2
done
log "✓ Database initialized with init_db.sql"
log "Note: Database tables and data are automatically loaded from init_db.sql"
log "Starting all services..."
docker-compose up -d
log "==================================="
log " Initialization Complete!"
log "==================================="
log ""
log "Services:"
log " - Frontend: http://localhost"
log " - Backend: http://localhost/api"
log " - API Docs: http://localhost/api/docs"
log ""
log "Data stored at: $DATA_ROOT"
log ""
log "Run './deploy.sh --logs' to view logs"
}
# Start services
start_services() {
log "Starting Cosmo services..."
cd "$PROJECT_ROOT"
docker-compose up -d
log "✓ Services started"
show_status
}
# Stop services
stop_services() {
log "Stopping Cosmo services..."
cd "$PROJECT_ROOT"
docker-compose stop
log "✓ Services stopped"
}
# Restart services
restart_services() {
log "Restarting Cosmo services..."
cd "$PROJECT_ROOT"
# Copy environment file to ensure latest config is used
if [ -f "$PROJECT_ROOT/.env.production" ]; then
cp "$PROJECT_ROOT/.env.production" "$PROJECT_ROOT/.env"
log "✓ Environment configuration updated"
fi
docker-compose restart
log "✓ Services restarted"
show_status
}
# Show logs
show_logs() {
cd "$PROJECT_ROOT"
docker-compose logs -f --tail=100
}
# Show status
show_status() {
log "Service Status:"
cd "$PROJECT_ROOT"
docker-compose ps
}
# Clean up (remove containers but keep data)
clean_containers() {
warn "This will remove all containers but keep your data"
read -p "Are you sure? (yes/no): " -r
if [[ $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
log "Stopping and removing containers..."
cd "$PROJECT_ROOT"
docker-compose down
log "✓ Containers removed. Data preserved at $DATA_ROOT"
else
log "Operation cancelled"
fi
}
# Full clean (remove containers and data)
full_clean() {
error_msg="This will PERMANENTLY DELETE all containers and data at $DATA_ROOT"
warn "$error_msg"
read -p "Are you ABSOLUTELY sure? Type 'DELETE' to confirm: " -r
if [[ $REPLY == "DELETE" ]]; then
log "Stopping and removing containers..."
cd "$PROJECT_ROOT"
docker-compose down -v
log "Removing data directories..."
sudo rm -rf "$DATA_ROOT"
log "✓ Complete cleanup finished"
else
log "Operation cancelled"
fi
}
# Backup data
backup_data() {
BACKUP_DIR="$DATA_ROOT/backups/backup_$(date +%Y%m%d_%H%M%S)"
log "Creating backup at $BACKUP_DIR..."
mkdir -p "$BACKUP_DIR"
# Backup database
log "Backing up database..."
docker-compose exec -T postgres pg_dump -U postgres cosmo_db > "$BACKUP_DIR/database.sql"
# Backup upload files
log "Backing up upload files..."
cp -r "$DATA_ROOT/upload" "$BACKUP_DIR/"
# Create archive
cd "$DATA_ROOT/backups"
tar -czf "backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$(basename $BACKUP_DIR)"
rm -rf "$BACKUP_DIR"
log "✓ Backup completed: $BACKUP_DIR.tar.gz"
}
# Update system
update_system() {
log "Updating Cosmo system..."
# Pull latest code
cd "$PROJECT_ROOT"
git pull
# Copy environment file to ensure latest config is used
if [ -f "$PROJECT_ROOT/.env.production" ]; then
cp "$PROJECT_ROOT/.env.production" "$PROJECT_ROOT/.env"
log "✓ Environment configuration updated"
fi
# Rebuild images
docker-compose build
# Restart services
docker-compose up -d
log "✓ System updated"
}
# Show help
show_help() {
cat << EOF
Cosmo Docker Deployment Script
Usage: ./deploy.sh [OPTION]
Options:
--init Initialize and start the system (first time setup)
--start Start all services
--stop Stop all services
--restart Restart all services
--logs Show and follow logs
--status Show service status
--backup Backup database and files
--update Update system (git pull + rebuild)
--clean Remove containers (keep data)
--full-clean Remove containers and ALL data (DANGEROUS!)
--help Show this help message
Data Location:
All data is stored at: $DATA_ROOT
- postgres/ Database files
- redis/ Redis persistence
- upload/ User uploaded files
- logs/ Application logs
- backups/ Backup archives
Examples:
./deploy.sh --init # First time setup
./deploy.sh --start # Start services
./deploy.sh --logs # View logs
./deploy.sh --backup # Create backup
EOF
}
# Main script
main() {
case "${1:-}" in
--init)
init_system
;;
--start)
start_services
;;
--stop)
stop_services
;;
--restart)
restart_services
;;
--logs)
show_logs
;;
--status)
show_status
;;
--backup)
backup_data
;;
--update)
update_system
;;
--clean)
clean_containers
;;
--full-clean)
full_clean
;;
--help|"")
show_help
;;
*)
error "Unknown option: $1"
show_help
;;
esac
}
# Run main function
main "$@"