Files
CKB/docker-quick-setup.sh

254 lines
7.8 KiB
Bash
Executable File

#!/bin/bash
# Quick Setup Script untuk CKB Laravel Application dengan Auto Import Database
# Usage: ./docker-quick-setup.sh [dev|prod]
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
# Default values
ENVIRONMENT="dev"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
dev|development)
ENVIRONMENT="dev"
shift
;;
prod|production|staging)
ENVIRONMENT="prod"
shift
;;
*)
echo "Unknown option $1"
echo "Usage: $0 [dev|prod]"
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 if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
}
# Function to setup environment file
setup_env() {
if [[ ! -f .env ]]; then
if [[ $ENVIRONMENT == "dev" ]]; then
if [[ -f docker/env.example.local ]]; then
print_status "Setting up local development environment file..."
cp docker/env.example.local .env
print_success "Local environment file created: .env"
else
print_error "Local environment template not found: docker/env.example.local"
exit 1
fi
else
if [[ -f docker/env.example.production ]]; then
print_status "Setting up production environment file..."
cp docker/env.example.production .env
print_success "Production environment file created: .env"
print_warning "⚠️ IMPORTANT: Edit .env and change all CHANGE_THIS_* passwords!"
else
print_error "Production environment template not found: docker/env.example.production"
exit 1
fi
fi
else
print_status "Environment file already exists: .env"
fi
}
# Function to check if database file exists
check_db_file() {
if [[ ! -f ckb.sql ]]; then
print_error "Database backup file 'ckb.sql' not found!"
print_status "Please make sure you have the ckb.sql file in the project root."
exit 1
fi
print_status "Found database backup: ckb.sql ($(du -h ckb.sql | cut -f1))"
}
# Function to start containers
start_containers() {
print_status "Starting Docker containers for $ENVIRONMENT environment..."
if [[ $ENVIRONMENT == "dev" ]]; then
print_status "This will start development environment with:"
echo " - MySQL with auto-import from ckb.sql"
echo " - Redis for caching"
echo " - phpMyAdmin for database management"
echo " - MailHog for email testing"
echo ""
docker-compose up -d --build
print_success "Development containers started!"
echo ""
print_status "Services are starting up... Please wait..."
# Wait for MySQL to be ready
print_status "Waiting for MySQL to be ready..."
sleep 20
# Wait a bit more for MySQL to be fully ready
sleep 10
# Check if database was imported automatically
if docker-compose exec -T db mysql -u root -proot -e "USE ckb_db; SHOW TABLES;" > /dev/null 2>&1; then
table_count=$(docker-compose exec -T db mysql -u root -proot -e "USE ckb_db; SHOW TABLES;" 2>/dev/null | wc -l)
if [[ $table_count -gt 1 ]]; then
print_success "Database automatically imported from ckb.sql!"
else
print_warning "Database not imported automatically. Running manual import..."
./docker-import-db.sh dev
fi
else
print_warning "Database not accessible. Running manual import..."
sleep 15
./docker-import-db.sh dev
fi
else
print_status "Starting production environment..."
if [[ ! -f .env.production ]]; then
print_warning "Creating production environment file..."
cp docker/env.example .env.production
print_warning "Please edit .env.production with your production settings!"
fi
docker-compose -f docker-compose.prod.yml up -d --build
print_success "Production containers started!"
sleep 15
print_status "Importing database for production..."
./docker-import-db.sh prod
fi
}
# Function to generate application key
generate_app_key() {
print_status "Generating Laravel application key..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose exec app php artisan key:generate
else
docker-compose -f docker-compose.prod.yml exec app php artisan key:generate
fi
}
# Function to run Laravel setup commands
setup_laravel() {
print_status "Setting up Laravel application..."
if [[ $ENVIRONMENT == "dev" ]]; then
COMPOSE_CMD="docker-compose exec app"
else
COMPOSE_CMD="docker-compose -f docker-compose.prod.yml exec app"
fi
# Clear caches
$COMPOSE_CMD php artisan cache:clear || true
$COMPOSE_CMD php artisan config:clear || true
$COMPOSE_CMD php artisan view:clear || true
# Set up storage links
$COMPOSE_CMD php artisan storage:link || true
if [[ $ENVIRONMENT == "prod" ]]; then
print_status "Optimizing for production..."
$COMPOSE_CMD php artisan config:cache
$COMPOSE_CMD php artisan route:cache
$COMPOSE_CMD php artisan view:cache
fi
}
# Function to show access information
show_access_info() {
echo ""
print_success "🎉 CKB Laravel Application is now ready!"
echo ""
if [[ $ENVIRONMENT == "dev" ]]; then
print_status "Development Environment Access:"
echo " 🌐 Web Application: http://localhost:8000"
echo " 📊 phpMyAdmin: http://localhost:8080"
echo " - Server: db"
echo " - Username: root"
echo " - Password: root"
echo " - Database: ckb_db"
echo ""
echo " 📧 MailHog (Email Testing): http://localhost:8025"
echo " 🗄️ MySQL Direct: localhost:3306"
echo " 🔴 Redis: localhost:6379"
echo ""
print_status "Useful Commands:"
echo " - View logs: docker-compose logs -f"
echo " - Access container: docker-compose exec app bash"
echo " - Laravel commands: docker-compose exec app php artisan [command]"
echo " - Stop containers: docker-compose down"
else
print_status "Production Environment Access:"
echo " 🌐 Web Application: http://localhost (port 80)"
echo " 🗄️ Database: localhost:3306"
echo ""
print_status "Useful Commands:"
echo " - View logs: docker-compose -f docker-compose.prod.yml logs -f"
echo " - Access container: docker-compose -f docker-compose.prod.yml exec app bash"
echo " - Stop containers: docker-compose -f docker-compose.prod.yml down"
fi
echo ""
print_status "Database has been imported from ckb.sql successfully!"
}
# Main execution
echo "================================================"
print_status "🚀 CKB Laravel Application Quick Setup"
print_status "Environment: $ENVIRONMENT"
echo "================================================"
echo ""
# Check prerequisites
check_docker
check_db_file
# Setup process
setup_env
start_containers
generate_app_key
setup_laravel
# Show final information
show_access_info
echo ""
print_success "✅ Quick setup completed successfully!"