142 lines
3.3 KiB
Bash
142 lines
3.3 KiB
Bash
#!/bin/bash
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to print colored messages
|
|
print_message() {
|
|
local color=$1
|
|
local message=$2
|
|
echo -e "${color}${message}${NC}"
|
|
}
|
|
|
|
# Function to check if command was successful
|
|
check_status() {
|
|
if [ $? -eq 0 ]; then
|
|
print_message "$GREEN" "✔ Success: $1"
|
|
else
|
|
print_message "$RED" "✘ Error: $1"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Default values
|
|
ENV="production"
|
|
SERVER_IP=""
|
|
FRONTEND_IP=""
|
|
DB_HOST="mysql"
|
|
|
|
# Help message
|
|
show_help() {
|
|
echo "Usage: ./deploy.sh [OPTIONS]"
|
|
echo "Deploy the router dashboard application"
|
|
echo
|
|
echo "Options:"
|
|
echo " -s, --server-ip Server IP address (required)"
|
|
echo " -f, --frontend-ip Frontend IP address (defaults to server IP if not provided)"
|
|
echo " -d, --db-host Database host (defaults to mysql)"
|
|
echo " -e, --environment Environment (development/staging/production, defaults to production)"
|
|
echo " -h, --help Show this help message"
|
|
echo
|
|
echo "Example:"
|
|
echo " ./deploy.sh -s 192.168.1.100 -f 192.168.1.101 -e production"
|
|
}
|
|
|
|
# Parse command line arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-s|--server-ip)
|
|
SERVER_IP="$2"
|
|
shift 2
|
|
;;
|
|
-f|--frontend-ip)
|
|
FRONTEND_IP="$2"
|
|
shift 2
|
|
;;
|
|
-d|--db-host)
|
|
DB_HOST="$2"
|
|
shift 2
|
|
;;
|
|
-e|--environment)
|
|
ENV="$2"
|
|
shift 2
|
|
;;
|
|
-h|--help)
|
|
show_help
|
|
exit 0
|
|
;;
|
|
*)
|
|
print_message "$RED" "Unknown option: $1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Validate required parameters
|
|
if [ -z "$SERVER_IP" ]; then
|
|
print_message "$RED" "Error: Server IP is required"
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# If frontend IP is not provided, use server IP
|
|
if [ -z "$FRONTEND_IP" ]; then
|
|
FRONTEND_IP=$SERVER_IP
|
|
print_message "$YELLOW" "Frontend IP not provided, using Server IP: $FRONTEND_IP"
|
|
fi
|
|
|
|
# Export environment variables
|
|
export SERVER_IP
|
|
export FRONTEND_IP
|
|
export DB_HOST
|
|
export NODE_ENV=$ENV
|
|
|
|
# Display deployment information
|
|
print_message "$GREEN" "\nDeployment Configuration:"
|
|
echo "Server IP: $SERVER_IP"
|
|
echo "Frontend IP: $FRONTEND_IP"
|
|
echo "Database Host: $DB_HOST"
|
|
echo "Environment: $ENV"
|
|
echo
|
|
|
|
# Confirm deployment
|
|
read -p "Do you want to continue with deployment? (y/n) " -n 1 -r
|
|
echo
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
print_message "$YELLOW" "Deployment cancelled"
|
|
exit 1
|
|
fi
|
|
|
|
# Start deployment
|
|
print_message "$GREEN" "\nStarting deployment..."
|
|
|
|
# Pull latest changes
|
|
print_message "$YELLOW" "Pulling latest changes..."
|
|
git pull
|
|
check_status "Git pull"
|
|
|
|
# Stop existing containers
|
|
print_message "$YELLOW" "Stopping existing containers..."
|
|
docker-compose down
|
|
check_status "Stopping containers"
|
|
|
|
# Build containers
|
|
print_message "$YELLOW" "Building containers..."
|
|
docker-compose build
|
|
check_status "Building containers"
|
|
|
|
# Start containers
|
|
print_message "$YELLOW" "Starting containers..."
|
|
docker-compose up -d
|
|
check_status "Starting containers"
|
|
|
|
# Check container status
|
|
print_message "$YELLOW" "Checking container status..."
|
|
docker-compose ps
|
|
check_status "Container status check"
|
|
|
|
print_message "$GREEN" "\nDeployment completed successfully!" |