#!/bin/sh # Installs Kaeso with Docker Compose into a new folder and starts it: # # curl -fsSL https://kaeso.app/install.sh | sh # # Run it with --help for the options. Running it again in the same place keeps the settings and the master key. set -eu # Wrapped in a function so that a download cut off halfway runs nothing. main() { domain="${KAESO_DOMAIN:-}" public_url="${KAESO_PUBLIC_URL:-}" dir="${KAESO_INSTALL_DIR:-}" version="${KAESO_VERSION:-latest}" install_docker="${KAESO_INSTALL_DOCKER:-}" open_browser=yes [ "${KAESO_NO_OPEN:-}" != yes ] || open_browser=no updater=yes [ "${KAESO_NO_UPDATER:-}" != yes ] || updater=no update="${KAESO_UPDATE:-}" updated= while [ $# -gt 0 ]; do case "$1" in --domain) domain="${2:?--domain needs a value}"; shift 2 ;; --public-url) public_url="${2:?--public-url needs a value}"; shift 2 ;; --dir) dir="${2:?--dir needs a value}"; shift 2 ;; --version) version="${2:?--version needs a value}"; shift 2 ;; --install-docker) install_docker=yes; shift ;; --no-open) open_browser=no; shift ;; --no-updater) updater=no; shift ;; --update) update=yes; shift ;; -h | --help) usage; return 0 ;; *) fail "Unknown option $1. Run with --help to see the options." ;; esac done if [ -z "$dir" ]; then dir=kaeso ! is_install . || dir=. fi command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1 || fail "The installer needs curl or wget." docker_ready if [ -n "$domain" ]; then cleaned="$(clean_domain "$domain")" || fail "\"$domain\" is not a domain. Give one such as kaeso.example.com, or localhost." domain="$cleaned" fi if [ -n "$public_url" ]; then cleaned="$(clean_public_url "$public_url")" || fail "\"$public_url\" is not an address. Give one such as https://kaeso.example.com, with no path." public_url="$cleaned" fi umask 077 project="${COMPOSE_PROJECT_NAME:-kaeso}" created= [ -d "$dir" ] || created=yes mkdir -p "$dir" cd "$dir" here="$(pwd -P)" only_install "$project" "$here" || { status=$? cd - >/dev/null [ -z "$created" ] || rmdir "$here" 2>/dev/null || true exit "$status" } fresh= if [ -f .env ]; then current="$(sed -n 's/^KAESO_DOMAIN=//p' .env | head -n 1)" if [ -n "$domain" ] && [ "$domain" != "$current" ]; then note "This install keeps its domain $current. To change it, set KAESO_DOMAIN in .env and run docker compose up -d." fi domain="$current" if [ "$update" = yes ]; then update_compose else say "Kaeso is installed in $here; starting it" fi else fresh=yes [ -n "$domain" ] || domain="$(choose_domain)" check_dns "$domain" say "Installing Kaeso in $here" fi if [ ! -f compose.yaml ]; then if [ "$version" = latest ]; then url="https://github.com/logicplanes/kaeso/releases/latest/download/compose.yaml" else url="https://github.com/logicplanes/kaeso/releases/download/v${version#v}/compose.yaml" fi url="${KAESO_COMPOSE_URL:-$url}" download "$url" compose.yaml.part || { rm -f compose.yaml.part fail "Couldn't download $url. Check the version and the connection, and run the installer again." } mv compose.yaml.part compose.yaml fi if [ ! -f .env ]; then { echo "KAESO_DOMAIN=$domain" echo "POSTGRES_PASSWORD=$(secret)" echo "POSTGRES_SUPERUSER_PASSWORD=$(secret)" echo "CLICKHOUSE_PASSWORD=$(secret)" echo "VALKEY_PASSWORD=$(secret)" echo "KAESO_MAIL_TRANSPORT=log" } >.env.part mv .env.part .env fi public_address pin_subnet "$project" turn_on_updater if [ ! -f compose.override.yaml ] && [ -z "$(docker compose ps -q --status running caddy /dev/null)" ]; then free_ports fi images="$(docker compose config --images /dev/null 2>&1 /dev/null || fail "Docker can't see $here, so it can't start Kaeso from there. Install Kaeso in your home folder, or share this folder with Docker in its settings." if [ ! -s master-keys ]; then say "Creating the master key" docker run --rm "$gateway" master-key generate master-keys.part mv master-keys.part master-keys # The gateway reads the key as user 65532. Docker Desktop, Colima and OrbStack read the folder as you instead. if [ "$(uname -s)" = Darwin ] || docker info --format '{{.OperatingSystem}}' "$log" 2>&1; then explain_failure "$log" fi rm -f "$log" certificate_ready || explain_certificate setup="$(docker compose exec -T gateway kaeso setup-link &1 || true)" link="$(printf '%s\n' "$setup" | grep -m1 -oE 'https?://[^[:space:]]*/setup#token=[^[:space:]]+' || true)" if [ -n "$link" ]; then say "Kaeso is running. Open this link to create the first user:" echo printf '%s\n' "$setup" open_link "$link" else say "Kaeso is running at $(address). Sign in there." fi echo if [ "$domain" = localhost ]; then echo "Your browser asks you once to accept the certificate Caddy signed for localhost." over_ssh_hint fi if [ -n "$fresh" ]; then echo "Emails are only written to the gateway's log. To send them, add your SMTP server to .env:" echo "https://docs.kaeso.app/self-hosting/email" echo echo "Keep a copy of $here/master-keys apart from your backups: without it, stored provider keys can't be decrypted." echo echo "Next, add a provider key and create an API key in the console:" echo "https://docs.kaeso.app/get-started/quick-start#add-a-provider-key" fi } usage() { cat <<'EOF' Installs Kaeso with Docker Compose into a new folder and starts it. Usage: curl -fsSL https://kaeso.app/install.sh | sh -s -- [options] --domain DOMAIN the domain Kaeso answers on (default: localhost), or a port such as :80 for plain HTTP behind another proxy --public-url URL the address people open Kaeso at, when it isn't https:// and the domain, such as the address of the proxy in front of a port (default for a port: http://localhost and the port) --dir DIR the folder to install into (default: this folder when it holds an install, otherwise ./kaeso) --version VERSION the release to install (default: the newest stable release) --install-docker on Linux, install Docker Engine first if it's missing, without asking --no-open don't open the setup link in the browser --no-updater leave the updater off, so Kaeso updates only by hand --update in an install, update it to the newest stable release, or to the one --version names The environment variables KAESO_DOMAIN, KAESO_PUBLIC_URL, KAESO_INSTALL_DIR, KAESO_VERSION, KAESO_INSTALL_DOCKER=yes, KAESO_NO_OPEN=yes, KAESO_NO_UPDATER=yes and KAESO_UPDATE=yes set the same options. Running it again in the same folder keeps the settings and the master key, turns on the updater if it's off, and starts Kaeso if it's stopped. When Docker Desktop, OrbStack or Colima is installed but stopped, the installer starts it. EOF } DEFAULT_SUBNET=172.30.83.0/24 say() { printf '\n==> %s\n' "$1"; } note() { printf 'kaeso install: %s\n' "$1" >&2; } fail() { note "$1" exit 1 } can_ask() { (: /dev/null; } ask() { printf '%s' "$1" >/dev/tty read -r answer /dev/null 2>&1; then curl -fsSL --retry 3 -o "$2" "$1" else wget -q -O "$2" "$1" fi } secret() { od -An -N24 -tx1 /dev/urandom | tr -d ' \n'; } at_least() { major="${1%%.*}" rest="${1#*.}" minor="${rest%%.*}" [ "$major" -gt "$2" ] 2>/dev/null || { [ "$major" -eq "$2" ] 2>/dev/null && [ "$minor" -ge "$3" ] 2>/dev/null; } } docker_ready() { if ! command -v docker >/dev/null 2>&1; then [ "$(uname -s)" = Linux ] || fail "Docker isn't installed. Install Docker Desktop, OrbStack or Colima, start it and run the installer again: https://docs.docker.com/get-started/get-docker/" if [ "$install_docker" != yes ]; then can_ask || fail "Docker isn't installed. Install Docker Engine (https://docs.docker.com/engine/install/), or run the installer with --install-docker to install it first." ask "Docker isn't installed. Install Docker Engine now with Docker's script from get.docker.com? [Y/n] " case "$answer" in "" | y | Y | yes | Yes) ;; *) fail "Install Docker Engine (https://docs.docker.com/engine/install/), then run the installer again." ;; esac fi get_docker fi if ! info="$(docker info 2>&1 /dev/null 2>&1; then fail "Docker isn't running. Start it with sudo systemctl start docker and run the installer again." fi fail "Docker isn't running. Start it and run the installer again." fi ;; esac fi compose_version="$(docker compose version --short 2>/dev/null /dev/null && [ "$memory" -lt 3500000000 ]; then more="give Docker more" case "$(docker context show 2>/dev/null /dev/null 2>&1; then engine=orbstack elif command -v colima >/dev/null 2>&1; then engine=colima elif [ -d /Applications/Docker.app ] || [ -d "$HOME/Applications/Docker.app" ]; then engine=desktop else return 1 fi ;; esac case "$engine" in orbstack) command -v orb >/dev/null 2>&1 || return 1 say "Starting OrbStack" orb start /dev/null 2>&1 || return 1 ;; colima) command -v colima >/dev/null 2>&1 || return 1 say "Starting Colima; this takes a minute" colima start ${profile:+--profile "$profile"} /dev/null 2>&1 || return 1 ;; desktop) say "Starting Docker Desktop; this takes a minute" open -a Docker /dev/null 2>&1 || return 1 ;; esac ;; Linux) [ "$(id -u)" -eq 0 ] && command -v systemctl >/dev/null 2>&1 || return 1 say "Starting Docker" systemctl start docker /dev/null 2>&1 || return 1 ;; *) return 1 ;; esac tries=0 until info="$(docker info 2>&1 /dev/null 2>&1 || fail "Installing Docker needs root. Run the installer as root, or install Docker Engine first: https://docs.docker.com/engine/install/" sudo=sudo fi say "Installing Docker Engine with Docker's script from get.docker.com; this takes a minute" script="$(mktemp)" log="$(mktemp)" download https://get.docker.com "$script" || fail "Couldn't download Docker's script from https://get.docker.com." if ! $sudo sh "$script" "$log" 2>&1; then tail -n 20 "$log" >&2 rm -f "$script" "$log" fail "Docker's script couldn't install Docker on this system. Install Docker Engine by hand: https://docs.docker.com/engine/install/" fi rm -f "$script" "$log" if [ -n "$sudo" ]; then $sudo usermod -aG docker "$(id -un)" fail "Docker is installed, and $(id -un) is now in the docker group. Log out and in again, then run the installer again." fi } # One Kaeso per Docker host: its Compose project has a fixed name, so a second install would take over the first one. only_install() { elsewhere="$(docker ps -a --filter "label=com.docker.compose.project=$1" --format '{{.Label "com.docker.compose.project.working_dir"}}' /dev/null && pwd -P)" != "$2" ]; then if [ -d "$elsewhere" ]; then note "Kaeso is already installed on this machine, in $elsewhere. Run the installer there, or remove that install first: https://docs.kaeso.app/self-hosting/installation#remove-kaeso" else note "Kaeso is already installed on this machine, from $elsewhere, which $(id -un) can't open. Run the installer there as that folder's owner. Only if that folder is gone, remove the old install with docker compose -p $1 down -v, which deletes its data." fi return 1 fi if [ ! -f .env ] && docker volume ls -q --filter "label=com.docker.compose.project=$1" /dev/tty done } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 else openssl dgst -sha256 -r "$1" | cut -d' ' -f1 fi } # Semantic versioning: a release is newer than its own pre-releases, so 1.0.0 is newer than 1.0.0-rc.2. newer() { awk -v a="$1" -v b="$2" ' function cmp(x, y, xc, yc, xp, yp, xa, ya, nx, ny, i) { xc = x; sub(/-.*/, "", xc); yc = y; sub(/-.*/, "", yc) split(xc, xa, "."); split(yc, ya, ".") for (i = 1; i <= 3; i++) if (xa[i] + 0 != ya[i] + 0) return xa[i] + 0 > ya[i] + 0 ? 1 : -1 xp = index(x, "-") ? substr(x, index(x, "-") + 1) : "" yp = index(y, "-") ? substr(y, index(y, "-") + 1) : "" if (xp == yp) return 0 if (xp == "") return 1 if (yp == "") return -1 nx = split(xp, xa, "."); ny = split(yp, ya, ".") for (i = 1; i <= nx || i <= ny; i++) { if (xa[i] == ya[i]) continue if (i > nx) return -1 if (i > ny) return 1 if (xa[i] ~ /^[0-9]+$/ && ya[i] ~ /^[0-9]+$/) return xa[i] + 0 > ya[i] + 0 ? 1 : -1 return xa[i] > ya[i] ? 1 : -1 } return 0 } BEGIN { exit cmp(a, b) == 1 ? 0 : 1 }' } running_version() { sed -n 's#.*ghcr\.io/logicplanes/kaeso-gateway:\([0-9A-Za-z.+-]*\).*#\1#p' compose.yaml | head -n 1 } # The new compose.yaml only replaces the install's once its hash matches the release's manifest. It is written in place, # so the file keeps the owner and mode the updater relies on. update_compose() { releases="${KAESO_RELEASES_URL:-https://github.com/logicplanes/kaeso/releases}" if [ "$version" = latest ]; then from="$releases/latest/download" else from="$releases/download/v${version#v}" fi for file in release.json compose.yaml; do download "$from/$file" "$file.part" || { rm -f release.json.part compose.yaml.part fail "Couldn't download $from/$file. Check the version and the connection, and run the installer again." } done manifest="$(tr -d ' \t\r\n' compose.yaml rm -f compose.yaml.part updated="${running:-the previous release}" say "Updating Kaeso in $here from ${running:-its release} to $target" } # The new gateway checks the install's settings before anything stops: once it has started on a release # that migrates the databases, the previous release can't run on them. A gateway without the check # answers with a usage error (2) and goes on as before. check_settings() { [ -n "$updated" ] || return 0 status=0 checked="$(docker compose run --rm --no-deps -T gateway config check &1)" || status=$? case $status in 0 | 2) return 0 ;; esac cat compose.yaml.previous >compose.yaml if [ -n "${earlier:-}" ]; then printf '%s' "${earlier%x}" >compose.yaml.previous else rm -f compose.yaml.previous fi printf '%s\n' "$checked" | tail -n 5 >&2 fail "Kaeso $target refused this install's settings, so this install keeps running $updated. Correct them in .env, compose.override.yaml or the configuration file, then run the installer again; the release notes and https://docs.kaeso.app/self-hosting/upgrades say what changed." } is_install() { [ -f "$1/compose.yaml" ] && [ -f "$1/.env" ] && grep -q '^KAESO_DOMAIN=' "$1/.env" 2>/dev/null } # The updater is what makes Update in the console work. Its proxy speaks Docker API 1.44, which Docker Engine 25 brought. turn_on_updater() { [ "$updater" = yes ] || return 0 grep -q '^KAESO_UPDATER_TOKEN' .env && return 0 ! grep -qs 'KAESO_UPDATER_TOKEN_FILE' compose.override.yaml || return 0 engine="$(docker version --format '{{.Server.Version}}' /dev/null)" || engine= if ! at_least "$engine" 25 0; then note "The updater needs Docker Engine 25 or newer, and this is ${engine:-an older one}, so Kaeso updates by hand here: https://docs.kaeso.app/self-hosting/upgrades#update-by-hand" return 0 fi [ -n "$fresh" ] || say "Turning on the updater, so Update in the console installs new releases" { echo "KAESO_UPDATER_TOKEN=$(secret)$(secret)" echo "KAESO_DIR=$here" socket="$(rootless_socket)" && echo "DOCKER_SOCKET=$socket" } >>.env if grep -q '^COMPOSE_PROFILES=' .env; then awk '/^COMPOSE_PROFILES=/ && $0 !~ /[=,]updater(,|$)/ { $0 = $0 (/=$/ ? "" : ",") "updater" } { print }' .env >.env.part cat .env.part >.env rm -f .env.part else echo "COMPOSE_PROFILES=updater" >>.env fi } # Rootless Docker keeps its socket in the user's runtime folder instead of /var/run/docker.sock. rootless_socket() { docker info --format '{{.SecurityOptions}}' /dev/null | grep -q rootless || return 1 host="$(docker context inspect --format '{{.Endpoints.docker.Host}}' /dev/null)" || return 1 case "$host" in unix://*) printf '%s\n' "${host#unix://}" ;; *) return 1 ;; esac } # Caddy signs these itself instead of asking a public certificate authority. own_certificate() { case "$1" in localhost | *.localhost | *.local | *.internal | *.home.arpa | :*) return 0 ;; *[!0-9.]*) return 1 ;; *) return 0 ;; esac } resolves() { if command -v getent >/dev/null 2>&1; then getent hosts "$1" >/dev/null 2>&1 elif command -v dscacheutil >/dev/null 2>&1; then dscacheutil -q host -a name "$1" 2>/dev/null | grep -q address elif command -v host >/dev/null 2>&1; then host "$1" >/dev/null 2>&1 else return 0 fi } check_dns() { own_certificate "$1" && return 0 resolves "$1" && return 0 if can_ask; then ask "$1 has no DNS record yet, so Caddy can't get its certificate. Install anyway? [y/N] " case "$answer" in y | Y | yes | Yes) return 0 ;; *) fail "Point an A or AAAA record for $1 at this server, wait until it resolves, and run the installer again. To try Kaeso first, use --domain localhost." ;; esac fi note "$1 has no DNS record yet, so Caddy can't get its certificate. Point an A or AAAA record at this server; Caddy keeps trying until it resolves." } # Asks Caddy on this machine, so a router that can't loop back to its own public address doesn't matter. certificate_ready() { own_certificate "$domain" && return 0 [ -z "$(sed -n 's/^KAESO_PUBLIC_URL=//p' .env | head -n 1)" ] || return 0 command -v curl >/dev/null 2>&1 || return 0 tries=0 until curl -fsS -o /dev/null --max-time 5 --resolve "$domain:443:127.0.0.1" "https://$domain/healthz" /dev/null; do tries=$((tries + 1)) [ "$tries" -lt 30 ] || return 1 sleep 2 done } explain_certificate() { note "Caddy hasn't got a certificate for $domain yet, so browsers can't open Kaeso there." answer="$(docker compose logs --no-log-prefix --tail 200 caddy /dev/null | grep 'could not get certificate' | sed -n 's/.*"error":"\([^"]*\)".*/\1/p' | tail -n 1)" [ -z "$answer" ] || note "The certificate authority answered: $answer" note "Check that the DNS record of $domain points at this server and that ports 80 and 443 are open to the internet. Caddy keeps trying; docker compose logs caddy in $here shows its progress." } open_link() { [ "$open_browser" = yes ] && [ -t 1 ] && [ -z "${SSH_CONNECTION:-}" ] || return 0 if [ "$(uname -s)" = Darwin ]; then opener=open elif { [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; } && command -v xdg-open >/dev/null 2>&1; then opener=xdg-open else return 0 fi if "$opener" "$1" /dev/null 2>&1; then echo echo "It's open in your browser." fi } address() { url="$(sed -n 's/^KAESO_PUBLIC_URL=//p' .env | head -n 1)" if [ -n "$url" ]; then echo "$url" else case "$domain" in :*) echo "http://localhost$domain" ;; *) echo "https://$domain" ;; esac fi } port_taken() { command -v curl >/dev/null 2>&1 || return 1 curl -s -o /dev/null --connect-timeout 2 "http://127.0.0.1:$1/" compose.override.yaml </dev/null 2>&1 || true fail "Port ${port:-80 or 443} on this machine is taken by another program, so Caddy can't serve Kaeso. Stop that program, or let it forward to Kaeso (https://docs.kaeso.app/self-hosting/domains-and-tls#use-your-own-reverse-proxy), and run the installer again." fi if grep -q 'Pool overlaps' "$1"; then rm -f "$1" fail "Another Docker network uses the range of Kaeso's edge network. Set EDGE_SUBNET in .env to a free range, such as 10.83.31.0/24, and run the installer again." fi tail -n 5 "$1" >&2 rm -f "$1" for service in $(docker compose ps -a --format '{{.Service}}|{{.State}}|{{.Health}}|{{.ExitCode}}' &2 docker compose logs --no-log-prefix --tail 15 "$service" &2 || true done [ -z "$updated" ] || note "The compose.yaml of $updated is in compose.yaml.previous. Going back to it needs the database backup from before the update: https://docs.kaeso.app/self-hosting/upgrades#downgrade" fail "Kaeso didn't start. Fix the cause above and run the installer again; docker compose logs in $here shows more." } main "$@"