332 lines
9.8 KiB
Bash
Executable File
332 lines
9.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script untuk deploy CKB Laravel Application ke production dengan domain bengkel.digitaloasis.xyz
|
|
# Usage: ./docker-deploy-prod.sh [build|deploy|ssl|status]
|
|
|
|
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
|
|
DOMAIN="bengkel.digitaloasis.xyz"
|
|
EMAIL="admin@digitaloasis.xyz"
|
|
COMPOSE_FILE="docker-compose.prod.yml"
|
|
ENV_FILE=".env"
|
|
|
|
# Default action
|
|
ACTION="deploy"
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
build)
|
|
ACTION="build"
|
|
shift
|
|
;;
|
|
deploy)
|
|
ACTION="deploy"
|
|
shift
|
|
;;
|
|
ssl)
|
|
ACTION="ssl"
|
|
shift
|
|
;;
|
|
status)
|
|
ACTION="status"
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown option $1"
|
|
echo "Usage: $0 [build|deploy|ssl|status]"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Function to print colored output
|
|
print_status() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Function to check prerequisites
|
|
check_prerequisites() {
|
|
print_status "Checking prerequisites..."
|
|
|
|
# Check Docker
|
|
if ! docker info > /dev/null 2>&1; then
|
|
print_error "Docker is not running. Please start Docker first."
|
|
exit 1
|
|
fi
|
|
|
|
# Check Docker Compose
|
|
if ! docker-compose --version > /dev/null 2>&1; then
|
|
print_error "Docker Compose is not installed."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if .env file exists
|
|
if [[ ! -f $ENV_FILE ]]; then
|
|
print_warning "Environment file not found. Creating from production template..."
|
|
if [[ -f docker/env.example.production ]]; then
|
|
cp docker/env.example.production $ENV_FILE
|
|
print_warning "⚠️ IMPORTANT: Edit $ENV_FILE and change all CHANGE_THIS_* passwords before continuing!"
|
|
print_status "Production template copied. Please configure:"
|
|
echo " - DB_PASSWORD and DB_ROOT_PASSWORD"
|
|
echo " - REDIS_PASSWORD"
|
|
echo " - MAIL_* settings"
|
|
echo " - AWS_* settings (if using S3)"
|
|
exit 1
|
|
else
|
|
print_error "Production environment template not found: docker/env.example.production"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
print_success "Prerequisites check passed!"
|
|
}
|
|
|
|
# Function to setup production environment
|
|
setup_production_env() {
|
|
print_status "Setting up production environment variables..."
|
|
|
|
# Update .env for production
|
|
sed -i "s|APP_ENV=.*|APP_ENV=production|g" $ENV_FILE
|
|
sed -i "s|APP_DEBUG=.*|APP_DEBUG=false|g" $ENV_FILE
|
|
sed -i "s|APP_URL=.*|APP_URL=https://$DOMAIN|g" $ENV_FILE
|
|
|
|
# Check if database credentials are set
|
|
if grep -q "DB_PASSWORD=password" $ENV_FILE; then
|
|
print_warning "Please update database credentials in $ENV_FILE for production!"
|
|
print_warning "Current settings are for development only."
|
|
fi
|
|
|
|
print_success "Production environment configured!"
|
|
}
|
|
|
|
# Function to build containers
|
|
build_containers() {
|
|
print_status "Building production containers..."
|
|
|
|
# Pull latest images
|
|
docker-compose -f $COMPOSE_FILE pull
|
|
|
|
# Build application container
|
|
docker-compose -f $COMPOSE_FILE build --no-cache app
|
|
|
|
print_success "Containers built successfully!"
|
|
}
|
|
|
|
# Function to deploy application
|
|
deploy_application() {
|
|
print_status "Deploying CKB Laravel Application to production..."
|
|
|
|
# Stop existing containers
|
|
print_status "Stopping existing containers..."
|
|
docker-compose -f $COMPOSE_FILE down || true
|
|
|
|
# Start database and redis first
|
|
print_status "Starting database and Redis..."
|
|
docker-compose -f $COMPOSE_FILE up -d db redis
|
|
|
|
# Wait for database to be ready
|
|
print_status "Waiting for database to be ready..."
|
|
sleep 20
|
|
|
|
# Start application
|
|
print_status "Starting application..."
|
|
docker-compose -f $COMPOSE_FILE up -d app
|
|
|
|
# Wait for application to be ready
|
|
sleep 15
|
|
|
|
# Run Laravel setup commands
|
|
print_status "Running Laravel setup commands..."
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan key:generate --force || true
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan migrate --force
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan config:cache
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan route:cache
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan view:cache
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan storage:link || true
|
|
|
|
# Start nginx proxy
|
|
print_status "Starting nginx proxy..."
|
|
docker-compose -f $COMPOSE_FILE up -d nginx-proxy
|
|
|
|
print_success "Application deployed successfully!"
|
|
}
|
|
|
|
# Function to setup SSL
|
|
setup_ssl() {
|
|
print_status "Setting up SSL certificate..."
|
|
|
|
if [[ -f docker-ssl-setup.sh ]]; then
|
|
chmod +x docker-ssl-setup.sh
|
|
./docker-ssl-setup.sh
|
|
else
|
|
print_error "SSL setup script not found!"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to show deployment status
|
|
show_status() {
|
|
print_status "CKB Production Deployment Status"
|
|
echo "================================================"
|
|
|
|
# Show container status
|
|
print_status "Container Status:"
|
|
docker-compose -f $COMPOSE_FILE ps
|
|
echo ""
|
|
|
|
# Show application health
|
|
print_status "Application Health:"
|
|
if curl -s --max-time 10 http://localhost/health > /dev/null 2>&1; then
|
|
print_success "✅ Application is responding on HTTP"
|
|
else
|
|
print_warning "❌ Application not responding on HTTP"
|
|
fi
|
|
|
|
if curl -s --max-time 10 https://$DOMAIN/health > /dev/null 2>&1; then
|
|
print_success "✅ Application is responding on HTTPS"
|
|
else
|
|
print_warning "❌ Application not responding on HTTPS"
|
|
fi
|
|
|
|
# Show SSL certificate status
|
|
print_status "SSL Certificate Status:"
|
|
if openssl s_client -connect $DOMAIN:443 -servername $DOMAIN < /dev/null 2>/dev/null | openssl x509 -noout -dates 2>/dev/null; then
|
|
print_success "✅ SSL certificate is active"
|
|
else
|
|
print_warning "❌ SSL certificate not found or invalid"
|
|
fi
|
|
|
|
# Show disk usage
|
|
print_status "Docker Disk Usage:"
|
|
docker system df
|
|
echo ""
|
|
|
|
# Show logs summary
|
|
print_status "Recent Application Logs:"
|
|
docker-compose -f $COMPOSE_FILE logs --tail=10 app || true
|
|
|
|
echo ""
|
|
print_status "Access URLs:"
|
|
echo " 🌐 Application: https://$DOMAIN"
|
|
echo " 🔍 Health Check: https://$DOMAIN/health"
|
|
echo ""
|
|
|
|
print_status "Useful Commands:"
|
|
echo " - View logs: docker-compose -f $COMPOSE_FILE logs -f [service]"
|
|
echo " - Enter container: docker-compose -f $COMPOSE_FILE exec app bash"
|
|
echo " - Update SSL: ./docker-ssl-setup.sh"
|
|
echo " - Restart app: docker-compose -f $COMPOSE_FILE restart app"
|
|
}
|
|
|
|
# Function to backup before deployment
|
|
backup_before_deploy() {
|
|
print_status "Creating backup before deployment..."
|
|
|
|
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
|
|
mkdir -p $BACKUP_DIR
|
|
|
|
# Backup database
|
|
if docker-compose -f $COMPOSE_FILE ps db | grep -q "Up"; then
|
|
print_status "Backing up database..."
|
|
docker-compose -f $COMPOSE_FILE exec -T db mysqldump -u root -p"${DB_ROOT_PASSWORD:-rootpassword}" "${DB_DATABASE:-ckb_production}" > "$BACKUP_DIR/database.sql"
|
|
print_success "Database backed up to $BACKUP_DIR/database.sql"
|
|
fi
|
|
|
|
# Backup storage
|
|
if [[ -d storage ]]; then
|
|
print_status "Backing up storage directory..."
|
|
tar -czf "$BACKUP_DIR/storage.tar.gz" storage/
|
|
print_success "Storage backed up to $BACKUP_DIR/storage.tar.gz"
|
|
fi
|
|
|
|
# Backup environment
|
|
if [[ -f $ENV_FILE ]]; then
|
|
cp $ENV_FILE "$BACKUP_DIR/env.backup"
|
|
print_success "Environment backed up to $BACKUP_DIR/env.backup"
|
|
fi
|
|
|
|
print_success "Backup completed in $BACKUP_DIR"
|
|
}
|
|
|
|
# Function to optimize for production
|
|
optimize_production() {
|
|
print_status "Optimizing application for production..."
|
|
|
|
# Laravel optimizations
|
|
docker-compose -f $COMPOSE_FILE exec -T app composer install --optimize-autoloader --no-dev --no-interaction
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan config:cache
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan route:cache
|
|
docker-compose -f $COMPOSE_FILE exec -T app php artisan view:cache
|
|
|
|
# Clean up Docker
|
|
docker system prune -f
|
|
|
|
print_success "Production optimization completed!"
|
|
}
|
|
|
|
# Main execution
|
|
echo "================================================"
|
|
print_status "🚀 CKB Production Deployment Script"
|
|
print_status "Domain: $DOMAIN"
|
|
print_status "Action: $ACTION"
|
|
echo "================================================"
|
|
echo ""
|
|
|
|
# Check prerequisites
|
|
check_prerequisites
|
|
|
|
case $ACTION in
|
|
build)
|
|
print_status "Building containers only..."
|
|
build_containers
|
|
print_success "✅ Build completed!"
|
|
;;
|
|
|
|
deploy)
|
|
print_status "Full deployment process..."
|
|
backup_before_deploy
|
|
setup_production_env
|
|
build_containers
|
|
deploy_application
|
|
optimize_production
|
|
print_success "✅ Deployment completed!"
|
|
echo ""
|
|
print_status "Next steps:"
|
|
echo "1. Setup SSL certificate: ./docker-deploy-prod.sh ssl"
|
|
echo "2. Check status: ./docker-deploy-prod.sh status"
|
|
;;
|
|
|
|
ssl)
|
|
print_status "Setting up SSL certificate..."
|
|
setup_ssl
|
|
print_success "✅ SSL setup completed!"
|
|
;;
|
|
|
|
status)
|
|
show_status
|
|
;;
|
|
esac
|
|
|
|
echo ""
|
|
print_success "✅ Production deployment script completed!" |