|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# One-Click Deployment Script for Docker Compose Project |
| 4 | +# Usage: ./deploy.sh |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Colors |
| 9 | +GREEN='\033[0;32m' |
| 10 | +BLUE='\033[0;34m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +RED='\033[0;31m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +# Configuration |
| 16 | +REPO_DIR="/home/docker" |
| 17 | +DEFAULT_TARGET_BASE="/home/docker-compose" |
| 18 | +DEFAULT_ENV_FILE="$REPO_DIR/default.env" |
| 19 | + |
| 20 | +echo -e "${BLUE}=================================================${NC}" |
| 21 | +echo -e "${BLUE} Docker Compose One-Click Deployment ${NC}" |
| 22 | +echo -e "${BLUE}=================================================${NC}" |
| 23 | + |
| 24 | +# Check if running in REPO_DIR (roughly) |
| 25 | +if [ ! -f "default.env" ]; then |
| 26 | + echo -e "${YELLOW}Warning: default.env not found in current directory.${NC}" |
| 27 | + echo -e "Please run this script from the repository root (usually $REPO_DIR)." |
| 28 | + read -p "Continue anyway? (y/N) " confirm |
| 29 | + if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | +else |
| 33 | + REPO_DIR=$(pwd) |
| 34 | +fi |
| 35 | + |
| 36 | +# 1. Select Deployment Example |
| 37 | +echo -e "${GREEN}Select a deployment scenario:${NC}" |
| 38 | +OPTIONS=($(ls -d examples/*/ | grep -v "examples/docker" | xargs -n 1 basename)) |
| 39 | + |
| 40 | +if [ ${#OPTIONS[@]} -eq 0 ]; then |
| 41 | + echo -e "${RED}No examples found in examples/ directory.${NC}" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +PS3="Enter choice number: " |
| 46 | +select OPT in "${OPTIONS[@]}"; do |
| 47 | + if [ -n "$OPT" ]; then |
| 48 | + SELECTED_EXAMPLE="$OPT" |
| 49 | + break |
| 50 | + else |
| 51 | + echo "Invalid selection." |
| 52 | + fi |
| 53 | +done |
| 54 | + |
| 55 | +echo -e "${GREEN}Selected: $SELECTED_EXAMPLE${NC}" |
| 56 | + |
| 57 | +# 2. Prepare Target Directory |
| 58 | +read -p "Enter installation directory [${DEFAULT_TARGET_BASE}]: " TARGET_BASE |
| 59 | +TARGET_BASE=${TARGET_BASE:-$DEFAULT_TARGET_BASE} |
| 60 | +TARGET_DIR="$TARGET_BASE" |
| 61 | + |
| 62 | +echo -e "${BLUE}Installing to $TARGET_DIR...${NC}" |
| 63 | + |
| 64 | +if [ -d "$TARGET_DIR" ]; then |
| 65 | + echo -e "${YELLOW}Directory $TARGET_DIR already exists.${NC}" |
| 66 | + read -p "Overwrite contents? (y/N) " confirm |
| 67 | + if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then |
| 68 | + echo "Aborting." |
| 69 | + exit 0 |
| 70 | + fi |
| 71 | +fi |
| 72 | + |
| 73 | +mkdir -p "$TARGET_DIR" |
| 74 | + |
| 75 | +# 3. Copy Files |
| 76 | +echo "Copying files from examples/$SELECTED_EXAMPLE..." |
| 77 | +cp -r "examples/$SELECTED_EXAMPLE/"* "$TARGET_DIR/" |
| 78 | + |
| 79 | +# 4. Configure .env |
| 80 | +echo -e "${BLUE}Configuring .env file...${NC}" |
| 81 | +TARGET_ENV="$TARGET_DIR/.env" |
| 82 | +EXAMPLE_ENV="examples/$SELECTED_EXAMPLE/.env" |
| 83 | + |
| 84 | +# Start with default.env |
| 85 | +if [ -f "default.env" ]; then |
| 86 | + cp "default.env" "$TARGET_ENV" |
| 87 | +else |
| 88 | + echo -e "${RED}Error: default.env not found! Cannot proceed safely.${NC}" |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +# Merge example .env if it exists |
| 93 | +if [ -f "$EXAMPLE_ENV" ]; then |
| 94 | + echo "Applying overrides from $SELECTED_EXAMPLE/.env..." |
| 95 | + while IFS='=' read -r key value; do |
| 96 | + # Skip comments and empty lines |
| 97 | + [[ "$key" =~ ^#.*$ ]] && continue |
| 98 | + [[ -z "$key" ]] && continue |
| 99 | + |
| 100 | + # Trim whitespace |
| 101 | + key=$(echo "$key" | xargs) |
| 102 | + value=$(echo "$value" | xargs) |
| 103 | + |
| 104 | + # If value is quoted, remove quotes (simplified) |
| 105 | + value="${value%\'}" |
| 106 | + value="${value#\'}" |
| 107 | + value="${value%"}" |
| 108 | + value="${value#"}" |
| 109 | +
|
| 110 | + # Update key in TARGET_ENV |
| 111 | + if grep -q "^$key=" "$TARGET_ENV"; then |
| 112 | + # Escape special characters for sed |
| 113 | + ESCAPED_VAL=$(echo "$value" | sed 's/[\/&]/\\&/g') |
| 114 | + sed -i "s|^$key=.*|$key='$value'|" "$TARGET_ENV" |
| 115 | + fi |
| 116 | + done < "$EXAMPLE_ENV" |
| 117 | +fi |
| 118 | +
|
| 119 | +# 5. Interactive Configuration |
| 120 | +generate_password() { |
| 121 | + openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 16 |
| 122 | +} |
| 123 | +
|
| 124 | +echo -e "${GREEN}Checking required variables...${NC}" |
| 125 | +
|
| 126 | +# Variables to check |
| 127 | +VARS_TO_CHECK=("SERVER_IP_PUBLIC" "SERVER_HOSTNAME") |
| 128 | +PASS_VARS=("MYSQL_PASSWORD" "REDIS_PASSWORD" "RABBITMQ_PASSWORD" "MONGODB_PASSWORD" "MINIO_PASSWORD" "MAIL_PASSWORD") |
| 129 | +
|
| 130 | +# Helper to read env var from file |
| 131 | +get_env_val() { |
| 132 | + grep "^$1=" "$TARGET_ENV" | cut -d'=' -f2- | tr -d "'\"" |
| 133 | +} |
| 134 | +
|
| 135 | +# Update env var in file |
| 136 | +set_env_val() { |
| 137 | + local key="$1" |
| 138 | + local val="$2" |
| 139 | + local escaped_val=$(echo "$val" | sed 's/[\/&]/\\&/g') |
| 140 | + sed -i "s|^$key=.*|$key='$val'|" "$TARGET_ENV" |
| 141 | +} |
| 142 | +
|
| 143 | +# Prompt for IP and Hostname |
| 144 | +CURRENT_IP=$(get_env_val "SERVER_IP_PUBLIC") |
| 145 | +if [ -z "$CURRENT_IP" ]; then |
| 146 | + DETECTED_IP=$(curl -s ifconfig.me || echo "") |
| 147 | + read -p "Enter Server Public IP [${DETECTED_IP}]: " INPUT_IP |
| 148 | + INPUT_IP=${INPUT_IP:-$DETECTED_IP} |
| 149 | + set_env_val "SERVER_IP_PUBLIC" "$INPUT_IP" |
| 150 | +fi |
| 151 | +
|
| 152 | +CURRENT_HOST=$(get_env_val "SERVER_HOSTNAME") |
| 153 | +if [ -z "$CURRENT_HOST" ]; then |
| 154 | + read -p "Enter Server Hostname (Domain) [localhost]: " INPUT_HOST |
| 155 | + INPUT_HOST=${INPUT_HOST:-localhost} |
| 156 | + set_env_val "SERVER_HOSTNAME" "$INPUT_HOST" |
| 157 | +fi |
| 158 | +
|
| 159 | +# Handle Passwords |
| 160 | +echo -e "${BLUE}Password Configuration${NC}" |
| 161 | +read -p "Generate random passwords for empty/default fields? (Y/n) " GEN_PASS |
| 162 | +if [[ "$GEN_PASS" != "n" && "$GEN_PASS" != "N" ]]; then |
| 163 | + for var in "${PASS_VARS[@]}"; do |
| 164 | + VAL=$(get_env_val "$var") |
| 165 | + # Generate if empty or if it looks like the user hasn't changed it (logic can be improved) |
| 166 | + # Here we just generate if empty or user wants to overwrite all? |
| 167 | + # Let's just generate if empty for safety, or we can force generate. |
| 168 | + # But previous copy step might have copied passwords from example.env. |
| 169 | + |
| 170 | + if [ -z "$VAL" ]; then |
| 171 | + NEW_PASS=$(generate_password) |
| 172 | + echo "Generated password for $var" |
| 173 | + set_env_val "$var" "$NEW_PASS" |
| 174 | + else |
| 175 | + echo "$var is already set (starts with ${VAL:0:3}...). Keeping it." |
| 176 | + fi |
| 177 | + done |
| 178 | +fi |
| 179 | +
|
| 180 | +echo -e "${BLUE}Finalizing...${NC}" |
| 181 | +# 6. Check SSL |
| 182 | +SSL_VAL=$(get_env_val "SSL_CERTIFICATE") |
| 183 | +if [[ "$SSL_VAL" == *"placeholder"* ]]; then |
| 184 | + if [ -d "$TARGET_DIR/ssl" ]; then |
| 185 | + echo -e "${YELLOW}Notice: 'ssl' directory found in $TARGET_DIR.${NC}" |
| 186 | + echo -e "Current SSL_CERTIFICATE is set to placeholder." |
| 187 | + read -p "Do you want to switch to the included certificates? (y/N) " USE_SSL |
| 188 | + if [[ "$USE_SSL" == "y" || "$USE_SSL" == "Y" ]]; then |
| 189 | + # Assuming certificate names based on example |
| 190 | + # standard names in example seem to vary, usually certificate.crt/key? |
| 191 | + # Let's check specific example |
| 192 | + if [ -f "$TARGET_DIR/ssl/certificate.crt" ]; then |
| 193 | + set_env_val "SSL_CERTIFICATE" "$TARGET_DIR/ssl/certificate" |
| 194 | + echo "Updated SSL_CERTIFICATE to $TARGET_DIR/ssl/certificate" |
| 195 | + else |
| 196 | + echo "Could not find certificate.crt in $TARGET_DIR/ssl, skipping update." |
| 197 | + fi |
| 198 | + fi |
| 199 | + fi |
| 200 | +fi |
| 201 | +
|
| 202 | +echo -e "${GREEN}Deployment setup complete!${NC}" |
| 203 | +echo -e "Configuration file located at: ${YELLOW}$TARGET_ENV${NC}" |
| 204 | +echo -e "To start the services, run:" |
| 205 | +echo -e "${BLUE}cd $TARGET_DIR${NC}" |
| 206 | +echo -e "${BLUE}docker compose up -d${NC}" |
0 commit comments