#!/bin/bash # Resolve which deployzap/deployzap image tag to install. # # Usage: bash install.sh (installs :latest) # Usage: DEPLOYZAP_VERSION=0.9.0 bash install.sh (installs a specific release) # Usage: DEPLOYZAP_VERSION=canary bash install.sh (installs a locally built image, see TESTING.md) # Usage: bash install.sh update (pull + roll the running service) # # The `latest` tag on Docker Hub always points at the newest published release # (apps/deployzap/docker/push.sh pushes :latest alongside the version tag), so # there is nothing to look up remotely. detect_version() { local version="${DEPLOYZAP_VERSION:-latest}" # Release tags are v-prefixed (v0.9.0); Docker Hub tags are not (0.9.0). case "$version" in v[0-9]*) version="${version#v}" ;; esac if ! echo "$version" | grep -Eq '^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$'; then echo "Error: '$version' is not a valid Docker tag (DEPLOYZAP_VERSION)" >&2 exit 1 fi echo "$version" } # Make sure the image is available before anything destructive happens. # A locally built image (the canary flow in TESTING.md) is used as-is. ensure_image() { local image="$1" if docker image inspect "$image" >/dev/null 2>&1; then echo "Using image already present on this host: $image" return 0 fi echo "Pulling $image ..." if docker pull "$image"; then return 0 fi echo "" >&2 echo "Error: could not pull $image" >&2 echo "Either the tag does not exist, or this host is not logged in - deployzap/deployzap" >&2 echo "is a private repository, so run 'docker login' first. To install a locally built" >&2 echo "image instead, build it (see TESTING.md) and set DEPLOYZAP_VERSION=." >&2 exit 1 } # Function to detect if running in Proxmox LXC container is_proxmox_lxc() { # Check for LXC in environment if [ -n "$container" ] && [ "$container" = "lxc" ]; then return 0 # LXC container fi # Check for LXC in /proc/1/environ if grep -q "container=lxc" /proc/1/environ 2>/dev/null; then return 0 # LXC container fi return 1 # Not LXC } generate_random_password() { # Generate a secure random password using multiple methods with fallbacks local password="" # Try using openssl (most reliable, available on most systems) if command -v openssl >/dev/null 2>&1; then password=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32) # Fallback to /dev/urandom with tr (most Linux systems) elif [ -r /dev/urandom ]; then password=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32) # Last resort fallback using date and simple hashing else if command -v sha256sum >/dev/null 2>&1; then password=$(date +%s%N | sha256sum | base64 | head -c 32) elif command -v shasum >/dev/null 2>&1; then password=$(date +%s%N | shasum -a 256 | base64 | head -c 32) else # Very basic fallback - combines multiple sources of entropy password=$(echo "$(date +%s%N)-$(hostname)-$$-$RANDOM" | base64 | tr -d "=+/" | head -c 32) fi fi # Ensure we got a password of correct length if [ -z "$password" ] || [ ${#password} -lt 20 ]; then echo "Error: Failed to generate random password" >&2 exit 1 fi echo "$password" } install_deployzap() { # Detect version tag VERSION_TAG=$(detect_version) DOCKER_IMAGE="deployzap/deployzap:${VERSION_TAG}" echo "Installing DeployZap version: ${VERSION_TAG}" if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" >&2 exit 1 fi # check if is Mac OS if [ "$(uname)" = "Darwin" ]; then echo "This script must be run on Linux" >&2 exit 1 fi # check if is running inside a container if [ -f /.dockerenv ]; then echo "This script must be run on Linux" >&2 exit 1 fi # check if something is running on port 80 if ss -tulnp | grep ':80 ' >/dev/null; then echo "Error: something is already running on port 80" >&2 exit 1 fi # check if something is running on port 443 if ss -tulnp | grep ':443 ' >/dev/null; then echo "Error: something is already running on port 443" >&2 exit 1 fi # check if something is running on port 3000 if ss -tulnp | grep ':3000 ' >/dev/null; then echo "Error: something is already running on port 3000" >&2 echo "DeployZap requires port 3000 to be available. Please stop any service using this port." >&2 exit 1 fi command_exists() { command -v "$@" > /dev/null 2>&1 } if command_exists docker; then echo "Docker already installed" else curl -sSL https://get.docker.com | sh -s -- --version 28.5.0 fi # Resolve the image now - failing here costs nothing, whereas failing after # the swarm reset below leaves the host half-installed. ensure_image "$DOCKER_IMAGE" # Check if running in Proxmox LXC container and set endpoint mode endpoint_mode="" if is_proxmox_lxc; then echo "⚠️ WARNING: Detected Proxmox LXC container environment!" echo "Adding --endpoint-mode dnsrr to Docker services for LXC compatibility." echo "This may affect service discovery but is required for LXC containers." echo "" endpoint_mode="--endpoint-mode dnsrr" echo "Waiting for 5 seconds before continuing..." sleep 5 fi docker swarm leave --force 2>/dev/null get_ip() { local ip="" # Try IPv4 first # First attempt: ifconfig.io ip=$(curl -4s --connect-timeout 5 https://ifconfig.io 2>/dev/null) # Second attempt: icanhazip.com if [ -z "$ip" ]; then ip=$(curl -4s --connect-timeout 5 https://icanhazip.com 2>/dev/null) fi # Third attempt: ipecho.net if [ -z "$ip" ]; then ip=$(curl -4s --connect-timeout 5 https://ipecho.net/plain 2>/dev/null) fi # If no IPv4, try IPv6 if [ -z "$ip" ]; then # Try IPv6 with ifconfig.io ip=$(curl -6s --connect-timeout 5 https://ifconfig.io 2>/dev/null) # Try IPv6 with icanhazip.com if [ -z "$ip" ]; then ip=$(curl -6s --connect-timeout 5 https://icanhazip.com 2>/dev/null) fi # Try IPv6 with ipecho.net if [ -z "$ip" ]; then ip=$(curl -6s --connect-timeout 5 https://ipecho.net/plain 2>/dev/null) fi fi if [ -z "$ip" ]; then echo "Error: Could not determine server IP address automatically (neither IPv4 nor IPv6)." >&2 echo "Please set the ADVERTISE_ADDR environment variable manually." >&2 echo "Example: export ADVERTISE_ADDR=" >&2 exit 1 fi echo "$ip" } get_private_ip() { ip addr show | grep -E "inet (192\.168\.|10\.|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[0-1]\.)" | head -n1 | awk '{print $2}' | cut -d/ -f1 } advertise_addr="${ADVERTISE_ADDR:-$(get_private_ip)}" if [ -z "$advertise_addr" ]; then echo "ERROR: We couldn't find a private IP address." echo "Please set the ADVERTISE_ADDR environment variable manually." echo "Example: export ADVERTISE_ADDR=192.168.1.100" exit 1 fi echo "Using advertise address: $advertise_addr" # Allow custom Docker Swarm init arguments via DOCKER_SWARM_INIT_ARGS environment variable # Example: export DOCKER_SWARM_INIT_ARGS="--default-addr-pool 172.20.0.0/16 --default-addr-pool-mask-length 24" # This is useful to avoid CIDR overlapping with cloud provider VPCs (e.g., AWS) swarm_init_args="${DOCKER_SWARM_INIT_ARGS:-}" if [ -n "$swarm_init_args" ]; then echo "Using custom swarm init arguments: $swarm_init_args" docker swarm init --advertise-addr $advertise_addr $swarm_init_args else docker swarm init --advertise-addr $advertise_addr fi if [ $? -ne 0 ]; then echo "Error: Failed to initialize Docker Swarm" >&2 exit 1 fi echo "Swarm initialized" docker network rm -f deployzap-network 2>/dev/null docker network create --driver overlay --attachable deployzap-network echo "Network created" mkdir -p /etc/deployzap chmod 777 /etc/deployzap # Generate secure random password for Postgres POSTGRES_PASSWORD=$(generate_random_password) # Store password as Docker Secret (encrypted and secure) echo "$POSTGRES_PASSWORD" | docker secret create deployzap_postgres_password - 2>/dev/null || true # Generate secure auth secret for Better Auth AUTH_SECRET=$(openssl rand -hex 32) # Store auth secret as Docker Secret (encrypted and secure) echo "$AUTH_SECRET" | docker secret create deployzap_auth_secret - 2>/dev/null || true echo "Generated secure database credentials and auth secret (stored in Docker Secrets)" docker service create \ --name deployzap-postgres \ --constraint 'node.role==manager' \ --network deployzap-network \ --env POSTGRES_USER=deployzap \ --env POSTGRES_DB=deployzap \ --secret source=deployzap_postgres_password,target=/run/secrets/postgres_password \ --env POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password \ --mount type=volume,source=deployzap-postgres,target=/var/lib/postgresql/data \ $endpoint_mode \ postgres:16 docker service create \ --name deployzap-redis \ --constraint 'node.role==manager' \ --network deployzap-network \ --mount type=volume,source=deployzap-redis,target=/data \ $endpoint_mode \ redis:7 # Installation # Set RELEASE_TAG environment variable for canary/feature versions release_tag_env="" if [[ "$VERSION_TAG" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+ ]]; then # Specific version (0.1.0, 0.2.0, etc.) → latest release_tag_env="-e RELEASE_TAG=latest" elif [ "$VERSION_TAG" != "latest" ]; then # canary, feature/*, etc. → use the tag as-is release_tag_env="-e RELEASE_TAG=$VERSION_TAG" fi # Optional: custom template marketplace, e.g. # TEMPLATES_BASE_URL=https://templates.example.com ./install.sh templates_base_url_env="" if [ -n "${TEMPLATES_BASE_URL:-}" ]; then templates_base_url_env="-e TEMPLATES_BASE_URL=$TEMPLATES_BASE_URL" fi docker service create \ --name deployzap \ --replicas 1 \ --network deployzap-network \ --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ --mount type=bind,source=/etc/deployzap,target=/etc/deployzap \ --mount type=volume,source=deployzap,target=/root/.docker \ --secret source=deployzap_postgres_password,target=/run/secrets/postgres_password \ --secret source=deployzap_auth_secret,target=/run/secrets/deployzap_auth_secret \ --publish published=3000,target=3000,mode=host \ --update-parallelism 1 \ --update-order stop-first \ --constraint 'node.role == manager' \ --with-registry-auth \ $endpoint_mode \ $release_tag_env \ $templates_base_url_env \ -e ADVERTISE_ADDR=$advertise_addr \ -e POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password \ -e BETTER_AUTH_SECRET_FILE=/run/secrets/deployzap_auth_secret \ $DOCKER_IMAGE if ! docker service inspect deployzap >/dev/null 2>&1; then echo "Error: the deployzap service was not created - see the output above." >&2 exit 1 fi sleep 4 docker run -d \ --name deployzap-traefik \ --restart always \ -v /etc/deployzap/traefik/traefik.yml:/etc/traefik/traefik.yml \ -v /etc/deployzap/traefik/dynamic:/etc/deployzap/traefik/dynamic \ -v /var/run/docker.sock:/var/run/docker.sock:ro \ -p 80:80/tcp \ -p 443:443/tcp \ -p 443:443/udp \ traefik:v3.6.7 docker network connect deployzap-network deployzap-traefik # Optional: Use docker service create instead of docker run # docker service create \ # --name deployzap-traefik \ # --constraint 'node.role==manager' \ # --network deployzap-network \ # --mount type=bind,source=/etc/deployzap/traefik/traefik.yml,target=/etc/traefik/traefik.yml \ # --mount type=bind,source=/etc/deployzap/traefik/dynamic,target=/etc/deployzap/traefik/dynamic \ # --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ # --publish mode=host,published=443,target=443 \ # --publish mode=host,published=80,target=80 \ # --publish mode=host,published=443,target=443,protocol=udp \ # traefik:v3.6.7 GREEN="\033[0;32m" YELLOW="\033[1;33m" BLUE="\033[0;34m" NC="\033[0m" # No Color format_ip_for_url() { local ip="$1" if echo "$ip" | grep -q ':'; then # IPv6 echo "[${ip}]" else # IPv4 echo "${ip}" fi } public_ip="${ADVERTISE_ADDR:-$(get_ip)}" formatted_addr=$(format_ip_for_url "$public_ip") echo "" printf "${GREEN}Congratulations, DeployZap is installed!${NC}\n" printf "${BLUE}Wait 15 seconds for the server to start${NC}\n" printf "${YELLOW}Please go to http://${formatted_addr}:3000${NC}\n\n" } update_deployzap() { # Detect version tag VERSION_TAG=$(detect_version) DOCKER_IMAGE="deployzap/deployzap:${VERSION_TAG}" echo "Updating DeployZap to version: ${VERSION_TAG}" if ! docker service inspect deployzap >/dev/null 2>&1; then echo "Error: no deployzap service is running on this host - run 'bash install.sh' first." >&2 exit 1 fi # Pull the image (skipped for a locally built tag that is not on the registry) if ! docker pull $DOCKER_IMAGE && ! docker image inspect $DOCKER_IMAGE >/dev/null 2>&1; then echo "Error: could not pull $DOCKER_IMAGE - check the tag exists, or run 'docker login'." >&2 exit 1 fi # --force so the task is recreated even when the tag string is unchanged if ! docker service update --image $DOCKER_IMAGE --force --with-registry-auth deployzap; then echo "Error: failed to update the deployzap service" >&2 exit 1 fi echo "DeployZap has been updated to version: ${VERSION_TAG}" } # Main script execution if [ "$1" = "update" ]; then update_deployzap else install_deployzap fi