Files
Fleetbase-Mirror-Repo/scripts/docker-install.sh
2025-05-26 15:20:48 +08:00

129 lines
4.5 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# scripts/docker-install.sh
# Fleetbase Docker installer (dev / prod aware)
# --------------------------------------------
set -euo pipefail
###############################################################################
# 1. Ask for host (default: localhost)
###############################################################################
read -rp "Enter host or IP address to bind to [localhost]: " HOST_INPUT
HOST=${HOST_INPUT:-localhost}
echo "➜ Using host: $HOST"
###############################################################################
# 2. Ask for environment (development | production)
###############################################################################
while true; do
read -rp "Choose environment (development / production) [development]: " ENV_INPUT
ENV_INPUT=$(echo "$ENV_INPUT" | tr '[:upper:]' '[:lower:]')
case "$ENV_INPUT" in
""|d|dev|development) ENVIRONMENT=development; break ;;
p|prod|production) ENVIRONMENT=production; break ;;
*) echo "Please type either 'development' or 'production'." ;;
esac
done
echo "➜ Environment: $ENVIRONMENT"
USE_HTTPS=false
APP_DEBUG=true
SC_SECURE=false
if [[ "$ENVIRONMENT" == "production" ]]; then
USE_HTTPS=true
APP_DEBUG=false
SC_SECURE=true
fi
###############################################################################
# 3. Determine project root no matter where script is called from
###############################################################################
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"
cd "$PROJECT_ROOT"
###############################################################################
# 4. Generate a fresh Laravel APP_KEY
###############################################################################
if ! command -v openssl >/dev/null 2>&1; then
echo "✖ openssl is required but not found. Install it and retry." >&2
exit 1
fi
APP_KEY="base64:$(openssl rand -base64 32 | tr -d '\n')"
echo "✔ Generated APP_KEY"
###############################################################################
# 5. Ensure dockercompose.override.yml is present & updated
###############################################################################
OVERRIDE_FILE="docker-compose.override.yml"
# url helpers
SCHEME_API=$([[ "$USE_HTTPS" == true ]] && echo "https" || echo "http")
SCHEME_CONSOLE=$([[ "$USE_HTTPS" == true ]] && echo "https" || echo "http")
update_override_with_yq() {
yq -i "
.services.application.environment.APP_KEY = \"$APP_KEY\" |
.services.application.environment.CONSOLE_HOST = \"$SCHEME_CONSOLE://$HOST:4200\" |
.services.application.environment.ENVIRONMENT = \"$ENVIRONMENT\" |
.services.application.environment.APP_DEBUG = \"$APP_DEBUG\"
" "$OVERRIDE_FILE"
echo "$OVERRIDE_FILE updated (yq)"
}
create_override() {
cat > "$OVERRIDE_FILE" <<YML
services:
application:
environment:
APP_KEY: "$APP_KEY"
CONSOLE_HOST: "$SCHEME_CONSOLE://$HOST:4200"
ENVIRONMENT: "$ENVIRONMENT"
APP_DEBUG: "$APP_DEBUG"
YML
echo "$OVERRIDE_FILE written"
}
if [[ -f "$OVERRIDE_FILE" ]]; then
if command -v yq >/dev/null 2>&1; then
update_override_with_yq
else
cp "$OVERRIDE_FILE" "${OVERRIDE_FILE}.bak.$(date +%Y%m%d%H%M%S)"
echo " Existing $OVERRIDE_FILE backed up (no yq found — recreating)"
create_override
fi
else
create_override
fi
###############################################################################
# 6. Write console/fleetbase.config.json atomically
###############################################################################
CONFIG_DIR="console"
CONFIG_PATH="$CONFIG_DIR/fleetbase.config.json"
mkdir -p "$CONFIG_DIR"
cat > "${CONFIG_PATH}.tmp" <<JSON
{
"API_HOST": "$SCHEME_API://$HOST:8000",
"SOCKETCLUSTER_HOST": "$HOST",
"SOCKETCLUSTER_PORT": "38000",
"SOCKETCLUSTER_SECURE": "$SC_SECURE"
}
JSON
mv -f "${CONFIG_PATH}.tmp" "$CONFIG_PATH"
echo "$CONFIG_PATH updated"
###############################################################################
# 7. Start stack & run deploy
###############################################################################
echo "⏳ Starting Fleetbase containers..."
docker compose up -d
echo "⏳ Running deploy script inside the application container..."
docker compose exec application bash -c "./deploy.sh"
docker compose up -d
echo
echo "🏁 Fleetbase is up!"
printf " API → %s://%s:8000\n" "$SCHEME_API" "$HOST"
printf " Console → %s://%s:4200\n\n" "$SCHEME_CONSOLE" "$HOST"