#!/bin/bash set -euo pipefail headline() { printf "\n== %s ==\n" "$1" printf '%*s\n' "${#1}" '' | tr ' ' '-' } info() { printf "[INFO] %s\n" "$1" } warn() { printf "[WARN] %s\n" "$1" } error_msg() { printf "[ERROR] %s\n" "$1" >&2 } prompt() { local message=$1 printf "? %s " "$message" } # ask reads an answer from the terminal, not from stdin: run as # `curl ... | bash`, stdin is the script itself. ask() { local answer read -r answer < /dev/tty printf '%s' "$answer" } headline "Pre-flight Checks" if [[ $(id -u) -ne 0 ]]; then error_msg "Please run as root" exit 1 fi if ! { : < /dev/tty; } 2>/dev/null; then error_msg 'The installer asks a few questions and needs a terminal to ask them in.' exit 1 fi if ! command -v docker >/dev/null 2>&1; then error_msg 'Docker is not installed' info 'You can install docker with: curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh' exit 1 fi if ! command -v curl >/dev/null 2>&1; then error_msg 'curl is required but not installed.' exit 1 fi info "All required binaries detected." headline "Directory Layout" # Certificates' private keys and database backups live here: only root, which # the containers run as, may read them. for dir in \ /etc/coswarm \ /etc/coswarm/certs \ /opt/coswarm \ /opt/coswarm/certs \ /opt/coswarm/backup do mkdir -p "$dir" chmod 700 "$dir" info "Ensured $dir exists, readable by root only" done headline "Network & Domain" if ! IP=$(curl -fsS4 icanhazip.com); then error_msg 'Unable to determine public IP address.' exit 1 fi info "Detected public IP: $IP" echo "Please enter the domain name of your server" echo "Don't add www. (ex: example.com)" while true; do prompt "Enter domain name:" domain=$(ask) # What a browser's address bar gives: a scheme, a path, www., capitals. domain=${domain,,} domain=${domain#http://} domain=${domain#https://} domain=${domain%%/*} domain=${domain#www.} domain=${domain%.} if [[ "$domain" =~ ^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$ ]]; then break fi warn "\"$domain\" is not a domain name (ex: example.com)." done info "Using domain: $domain" while true; do prompt "Enter coswarm subdomain:" subdomain=$(ask) subdomain=${subdomain,,} subdomain=${subdomain%".$domain"} if [[ "$subdomain" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$ ]]; then break fi warn "\"$subdomain\" is not a subdomain (ex: panel)." done info "Using subdomain: $subdomain" headline "Verification Method" warn 'We have two ways to verify your domain:' echo "1. http verification" echo "2. tls verification" warn 'If you choose tls verification and your domain behind cloudflare, switch DNS to DNS-only mode.' warn 'If you choose http verification behind cloudflare, no DNS change is required.' while true; do prompt "Use HTTP verification? (y/n):" http=$(ask) http=${http,,} if [[ "$http" == "y" || "$http" == "n" ]]; then break fi warn "Please enter y or n." done headline "Admin Contact" while true; do prompt "Enter admin email:" email=$(ask) echo if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then info "Email address $email is valid." break fi error_msg "Email address $email is invalid. Please try again." done headline "Deployment" info "Coswarm Installing ..." swarm_state=$(docker info --format '{{.Swarm.LocalNodeState}}' 2>/dev/null || echo 'inactive') if [[ "$swarm_state" != "active" ]]; then info "Initializing Docker Swarm..." # On a cloud provider's NAT (AWS, GCP, Azure, Oracle) the public address # is on none of the server's interfaces and swarm will not advertise it; # the address the server reaches the internet from is. if ! docker swarm init --advertise-addr "$IP" >/dev/null 2>&1; then local_ip=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "src") print $(i + 1)}' || true) if [[ -n "$local_ip" ]]; then warn "$IP is not an address of this server; advertising $local_ip instead." docker swarm init --advertise-addr "$local_ip" else docker swarm init fi fi elif [[ $(docker info --format '{{.Swarm.ControlAvailable}}') != "true" ]]; then error_msg "This server is a worker in a swarm. Run the installer on a manager." exit 1 else warn "Docker Swarm already initialized. Skipping init." fi if ! docker network ls --format '{{.Name}}' | grep -qx 'cloud'; then info "Creating encrypted overlay network 'cloud'..." docker network create --opt encrypted -d overlay --attachable cloud else warn "Docker network 'cloud' already exists." fi info "Pulling required container images..." for image in kintsdev/coswarm kintsdev/coswarm-proxy postgres:14.6; do docker pull "$image" done if docker service ls --format '{{.Name}}' | grep -qx 'coswarm'; then error_msg "Docker service 'coswarm' already exists. Remove it if you want a fresh install." exit 1 fi # On a manager: the panel runs the swarm through the docker socket, and # /opt/coswarm is on this server only. info "Creating coswarm service..." docker service create --name coswarm --network cloud \ --constraint node.role==manager \ --mount 'type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock' \ --mount 'type=bind,src=/opt/coswarm/,dst=/opt/coswarm/,readonly' \ -e CSW_DOMAIN="$domain" \ -e CSW_SUBDOMAIN="$subdomain" \ -e CSW_ADMIN_EMAIL="$email" \ -e HTTP_VERIFY="$http" \ -p 3000:3000 kintsdev/coswarm # The panel makes up the first admin's password and logs it once. info "Waiting for the panel to create the admin account..." password="" for _ in $(seq 1 60); do password=$(docker service logs --raw coswarm 2>/dev/null | awk '/^[[:space:]]*password: / {print $NF; exit}' || true) [[ -n "$password" ]] && break sleep 3 done headline "Success" info "Coswarm Installed Successfully" info "Access Coswarm at https://$subdomain.$domain" info "Point $subdomain.$domain at $IP in your DNS first, if it does not yet." if [[ -n "$password" ]]; then info "Sign in as $email with password: $password" info "This is shown once; you will be asked to change it at first sign-in." else warn "The panel has not shown the admin password yet. Find it with:" warn " docker service logs --raw coswarm 2>&1 | grep -A1 'email:'" warn "If this server had coswarm before, the admin account keeps its old password." fi warn "Keep ports 2377/tcp, 7946/tcp+udp and 4789/udp closed to the internet: swarm nodes talk on them, open them only between your nodes."