#!/bin/sh # Install Hatoba on a machine. hatoba.sh serves this file three times, each # with its own `role` below (scripts/install-scripts.sh writes the copies): # # curl -fsSL https://hatoba.sh/client | sh a machine that joins networks and deploys apps # curl -fsSL https://hatoba.sh/runner | sh a Linux machine that runs apps # curl -fsSL https://hatoba.sh | sh installs nothing, and says to pick one # # A hub has its own script, hatoba.sh/hub. A machine has one role, so each stops on a machine that already has another: a # client, a runner or a hub. Every role's package installs its command as `hatoba`. # # The client works out the operating system, the CPU and whether systemd runs the machine, then installs what fits: the # `hatoba` package on Linux with systemd, `hatoba-container` in a container without systemd, or the macOS package on a Mac. # It never touches Docker. The runner needs Linux with systemd: it installs Docker from Docker's own apt repository if # Docker is missing, then `hatoba-runner`, with its own service and the gVisor sandbox. Both only install; joining a network # is the next step, `sudo hatoba join `. # # On a new cloud server, Ubuntu's automatic updates hold apt's locks for the # first minutes; every apt step waits for them and says what it's waiting for. # Tool output goes to /var/log/hatoba/install.log. set -eu # client or runner; empty in the copy hatoba.sh serves at its root. role= downloads=${HATOBA_DOWNLOADS:-https://dl.hatoba.sh/mac} apt_repository=${HATOBA_APT:-https://apt.hatoba.sh} os_release=${HATOBA_OS_RELEASE:-/etc/os-release} install_log=/var/log/hatoba/install.log docker_guide=https://docs.docker.com/engine/install/ sudo_prompt='Installing Hatoba needs root. Password for %u: ' apt_locks=${HATOBA_APT_LOCKS:-/var/lib/dpkg/lock-frontend /var/lib/dpkg/lock /var/lib/apt/lists/lock /var/cache/apt/archives/lock} proc=${HATOBA_PROC:-/proc} hub_unit=${HATOBA_HUB_UNIT:-/usr/lib/systemd/system/hatoba-hub@.service} fail() { printf 'hatoba: %s\n' "$*" >&2 exit 1 } # HATOBA_DRY_RUN=1 prints each command that changes the system instead of # running it. Commands read nothing from stdin, which is this script when it's # piped from curl. run() { if [ -n "${HATOBA_DRY_RUN:-}" ]; then printf '+ %s\n' "$*"; else "$@" >"$log" 2>&1' \ sh "$install_log" "$@" fi } # Run a logged command; if it fails, stop with $failure. While $running names # a step, its line counts the time in place on a terminal. try() { if [ -n "${running:-}" ] && [ "$terminal" = yes ] && [ -z "${HATOBA_DRY_RUN:-}" ]; then logged "$@" & command_pid=$! while kill -0 "$command_pid" 2>/dev/null; do printf '\r\033[K … %-28s %s' "$running" "$(duration "$(($(date +%s) - running_since))")" sleep 1 done printf '\r\033[K' wait "$command_pid" || fail "$failure" else logged "$@" || fail "$failure" fi } # begin : the step whose line counts in place until `step` ends it. begin() { running=$1 running_since=$(date +%s) } step() { running= if [ -n "${2:-}" ]; then printf ' ✓ %-28s %s\n' "$1" "$2"; else printf ' ✓ %s\n' "$1"; fi } warn() { if [ -n "${2:-}" ]; then printf ' ! %-28s %s\n' "$1" "$2"; else printf ' ! %s\n' "$1"; fi } # A Next line, laid out as Hatoba's commands lay theirs out. next_step() { printf ' %-18s %s\n' "$1" "$2" } # How long a wait took: 40s, or 1m 52s. duration() { if [ "$1" -lt 60 ]; then printf '%ss' "$1"; else printf '%sm %ss' "$(($1 / 60))" "$(($1 % 60))"; fi } # The process holding one of apt's locks, as " ", or # nothing when they're all free. /proc/locks names a lock by its device and # inode, so this needs no root and no extra package. apt_lock_holder() { [ -r "$proc/locks" ] || return 0 for lock in $apt_locks; do file=$(stat -c '%D %i' "$lock" 2>/dev/null) || continue holder=$(awk -v file="$file" ' function hex(text, i, value) { value = 0 for (i = 1; i <= length(text); i++) value = value * 16 + index("0123456789abcdef", substr(tolower(text), i, 1)) - 1 return value } BEGIN { split(file, wanted, " "); device = hex(wanted[1]) } # A line with -> is a process waiting for the lock, not holding it. $2 != "->" && split($6, id, ":") == 3 && id[3] == wanted[2] { minor = hex(id[2]) if (hex(id[1]) * 256 + minor % 256 + (minor - minor % 256) * 4096 == device) { print $5; exit } }' "$proc/locks" 2>/dev/null) || holder= if [ -n "$holder" ]; then printf '%s %s\n' "$holder" "$lock" return 0 fi done } # Who holds the lock: Ubuntu's automatic updates, whichever of their # processes it is, or else the process itself, by name and ID. describe_holder() { ancestor=$1 depth=0 while [ "$ancestor" -gt 1 ] 2>/dev/null && [ "$depth" -lt 32 ]; do case "$(cat "$proc/$ancestor/comm" 2>/dev/null)" in unattended-upgr* | apt.systemd.dai*) holder_name="Ubuntu's automatic updates" holder_verb=are return 0 ;; esac # A process that ended in between has no parent to follow. ancestor=$(sed -n 's/^PPid:[[:space:]]*//p' "$proc/$ancestor/status" 2>/dev/null) || ancestor=0 depth=$((depth + 1)) done holder_name="$(cat "$proc/$1/comm" 2>/dev/null || printf 'a process') (process $1)" holder_verb=is } # Wait while another process holds apt's locks, without a time limit. One # line names the holder and, on a terminal, counts the time in place. Ubuntu's # updates take the locks in turns, so they must stay free for a few seconds. wait_for_apt() { found=$(apt_lock_holder) [ -n "$found" ] || return 0 [ "$terminal" != yes ] || trap 'printf "\n"; exit 130' INT waited=0 free=0 last= holders= while :; do if [ -n "$found" ]; then free=0 process=${found%% *} describe_holder "$process" if [ "$process" != "$last" ]; then last=$process logged printf '%s\n' "waiting for apt: ${found#* } is held by $(cat "$proc/$process/comm" 2>/dev/null) (process $process)" || true case "|$holders|" in *"|$holder_name|"*) ;; *) holders="${holders:+$holders|}$holder_name" ;; esac fi if [ "$terminal" = yes ]; then printf '\r\033[K … %-28s %s %s using it, %s' 'Waiting for apt' "$holder_name" "$holder_verb" "$(duration "$waited")" elif [ "$waited" = 0 ]; then printf ' … %-28s %s %s using it\n' 'Waiting for apt' "$holder_name" "$holder_verb" fi else free=$((free + 1)) [ "$free" -lt 3 ] || break fi sleep 1 waited=$((waited + 1)) found=$(apt_lock_holder) done [ "$terminal" != yes ] || printf '\r\033[K' trap - INT # The step that waited goes on counting after this one. resume=${running:-} step "Waited for apt" "$(duration "$waited"), for $(printf '%s' "$holders" | sed 's/|/ and /g')" running=$resume } # apt-get, once apt's locks are free. It also waits for them itself, in case # something takes them in between. apt_get() { wait_for_apt try apt-get -o DPkg::Lock::Timeout=-1 "$@" } # --help anywhere answers without installing anything. usage() { case "$role" in client) cat <<'EOF' Install Hatoba on this machine as a client, which joins networks, reaches their apps and deploys apps to runners: curl -fsSL https://hatoba.sh/client | sh It installs the package that fits: hatoba on Debian or Ubuntu with systemd, hatoba-container in a container without systemd, or the macOS package on a Mac with Apple silicon. Joining a network is the next step: sudo hatoba join . A machine that runs apps installs from hatoba.sh/runner instead; a machine has one role. EOF ;; runner) cat <<'EOF' Install Hatoba on this machine as a runner, which joins networks and runs the apps clients deploy to it: curl -fsSL https://hatoba.sh/runner | sh It needs Debian or Ubuntu on x86-64 or arm64 with systemd, installs Docker from Docker's own repository when it's missing, then hatoba-runner. Joining a network is the next step: sudo hatoba join . A machine has one role, so a client doesn't become a runner. EOF ;; *) cat <<'EOF' Install Hatoba for what this machine will be: curl -fsSL https://hatoba.sh/client | sh a client, which reaches and deploys apps curl -fsSL https://hatoba.sh/runner | sh a runner, which runs apps curl -fsSL https://hatoba.sh/hub | sh a hub server, which runs networks EOF ;; esac } # The path picks the role, and nothing else is needed. parse() { for argument in "$@"; do case "$argument" in -h | --help) usage exit 0 ;; esac done case "$role" in client | runner) ;; *) fail "hatoba.sh: add /client, /runner or /hub for what this machine will be" ;; esac for argument in "$@"; do case "$argument" in -*) fail "unknown option $argument" ;; hatoba1.*) fail "invites go to sudo hatoba join, after installing" ;; *) fail "unexpected argument $argument" ;; esac done } check_platform() { case "$os/$architecture" in Linux/x86_64 | Linux/amd64 | Linux/aarch64 | Linux/arm64) # Debian's name for it, which apt, the packages and Docker's repository use. case "$architecture" in x86_64 | amd64) linux_architecture=amd64 machine=x86-64 ;; *) linux_architecture=arm64 machine=arm64 ;; esac if [ "$systemd" = yes ]; then platform=linux else # A container without systemd as PID 1 gets the container package. [ "$role" = client ] || fail "hatoba.sh/runner: needs systemd; the container package doesn't run apps" platform=container fi ;; Darwin/arm64) [ "$role" = client ] || fail "hatoba.sh/runner: apps run on Linux, not on Macs" platform=mac ;; Darwin/*) fail "Intel Mac: not supported yet; Hatoba runs on Apple silicon" ;; Linux/*) fail "$architecture: not supported yet; Hatoba's Linux packages are for x86-64 and arm64" ;; *) fail "$os: not supported yet; Hatoba runs on Linux and macOS" ;; esac } # Whether a package is installed, even half-way. One removed but not purged # isn't. package_installed() { # shellcheck disable=SC2016 # dpkg-query's format, not a shell expansion package_status=$(dpkg-query -W -f='${Status}' "$1" 2>/dev/null) || return 1 case "$package_status" in '' | *' not-installed' | *' config-files') return 1 ;; esac } # Whether a client's or runner's package is on this machine with its networks: installed, even half-way, or removed but # not purged, which keeps them for a reinstall. Only a purge leaves them. member_package() { # shellcheck disable=SC2016 # dpkg-query's format, not a shell expansion package_status=$(dpkg-query -W -f='${Status}' "$1" 2>/dev/null) || return 1 case "$package_status" in '' | *' not-installed') return 1 ;; esac } # A machine has one role. join refuses on a hub server too; it looks for the hub package's systemd template. check_role() { if [ -e "$hub_unit" ] || package_installed hatoba-hub; then fail "hatoba.sh/$role: this server is a hub, and can't also be a $role" fi case "$role" in client) if member_package hatoba-runner; then fail "hatoba.sh/client: this machine is a runner, and can't also be a client; remove the runner with \`sudo apt purge hatoba-runner\`" fi ;; runner) for client_package in hatoba hatoba-container; do if member_package "$client_package"; then fail "hatoba.sh/runner: this machine is a client, and can't also be a runner; remove the client with \`sudo apt purge $client_package\`" fi done ;; esac } # Hatoba's Linux packages are Debian packages. A runner without Docker also # needs Docker's apt repository, which covers Debian and Ubuntu themselves. check_linux() { [ -r "$os_release" ] || fail "$os_release: not found; Hatoba's Linux packages are for Debian and Ubuntu" # shellcheck disable=SC1090 . "$os_release" distribution=${PRETTY_NAME:-${ID:-this system}} case " ${ID:-} ${ID_LIKE:-} " in *' debian '* | *' ubuntu '*) ;; *) fail "$distribution: not supported yet; Hatoba's Linux packages are for Debian and Ubuntu" ;; esac if [ "$role" = client ] || [ "$docker" = yes ]; then return 0 fi case "${ID:-}" in debian | ubuntu) ;; *) fail "$distribution: hatoba.sh/runner installs Docker only on Debian and Ubuntu; see $docker_guide" ;; esac codename=${UBUNTU_CODENAME:-${VERSION_CODENAME:-}} [ -n "$codename" ] || fail "$distribution: no release codename to pick Docker's apt repository; install Docker from $docker_guide" } # Ask for root once, before anything else, saying what it's for. It runs a command rather than `sudo -v`, which asks for # a password unless every sudo rule for the user needs none: on a cloud image the default user has a rule without one # and the sudo group's, which needs one, and often no password at all. setup_root() { [ "$(id -u)" != 0 ] || return 0 command -v sudo >/dev/null 2>&1 || fail "sudo: not installed; run this as root, or install sudo first" [ -z "${HATOBA_DRY_RUN:-}" ] || return 0 sudo -p "$sudo_prompt" true /dev/null | awk ' $1 == "Installed:" { installed = $2 } $1 == "Candidate:" { candidate = $2 } END { if (installed != "" && installed != "(none)" && candidate != "" && candidate != "(none)" && candidate != installed) print candidate }' } # The step for a package an earlier run installed. found_package() { waiting=$(waiting_version "$1") step "Found $1${2:+ $2}" "${waiting:+$waiting is available: sudo hatoba admin update}" } # The package's version, if it's installed and configured. installed_version() { # shellcheck disable=SC2016 # dpkg-query's format, not a shell expansion case "$(dpkg-query -W -f='${Status}' "$1" 2>/dev/null || true)" in *' ok installed') dpkg-query -W -f='${Version}' "$1" 2>/dev/null || true ;; *) return 1 ;; esac } add_repository() { begin "Adding Hatoba's apt repository" failure="adding Hatoba's apt repository failed; see $install_log" try curl -fsSL -o /usr/share/keyrings/hatoba.gpg "$apt_repository/hatoba.gpg" try sh -c "echo 'deb [signed-by=/usr/share/keyrings/hatoba.gpg] $apt_repository stable main' >/etc/apt/sources.list.d/hatoba.list" apt_get update -q step "Added Hatoba's apt repository" } install_package() { begin "Installing $1" failure="installing $1 failed; see $install_log" apt_get install -y "$1" # shellcheck disable=SC2016 # dpkg-query's format, not a shell expansion package_version=$(dpkg-query -W -f='${Version}' "$1" 2>/dev/null || true) step "Installed $1${package_version:+ $package_version}" } # Docker's own instructions for Debian and Ubuntu: its signing key, its apt # repository, then Docker Engine and its plugins. Never Ubuntu's docker.io. install_docker() { begin "Installing Docker" failure="installing Docker failed; see $install_log" apt_get update -q apt_get install -y ca-certificates curl try install -m 0755 -d /etc/apt/keyrings try curl -fsSL "https://download.docker.com/linux/$ID/gpg" -o /etc/apt/keyrings/docker.asc try chmod a+r /etc/apt/keyrings/docker.asc try sh -c "echo 'deb [arch=$linux_architecture signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/$ID $codename stable' > /etc/apt/sources.list.d/docker.list" apt_get update -q apt_get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker_version=$(docker --version 2>/dev/null | sed -n 's/^Docker version \([^,]*\),.*/\1/p') step "Installed Docker${docker_version:+ $docker_version}" } install_linux() { step "Checked this machine" "$(linux_system)" # Docker first, so hatoba-runner registers its sandbox with it. if [ "$role" = runner ] && [ "$docker" = no ]; then install_docker fi # An earlier run's package stays as it is. if package_version=$(installed_version "$package"); then found_package "$package" "$package_version" else add_repository install_package "$package" fi } install_container() { step "Checked this machine" "$(linux_system), without systemd" if package_version=$(installed_version hatoba-container); then found_package hatoba-container "$package_version" else add_repository install_package hatoba-container fi failure="starting Hatoba failed; see $install_log" try hatoba start warn "Started Hatoba" "after a container restart, run hatoba start" } # Whether version $1 is newer than version $2. newer_version() { awk -v a="$1" -v b="$2" 'BEGIN { n = split(a, x, "."); m = split(b, y, ".") for (i = 1; i <= (n > m ? n : m); i++) { if (x[i] + 0 != y[i] + 0) exit !(x[i] + 0 > y[i] + 0) } exit 1 }' } install_mac() { mac_version=$(sw_vers -productVersion 2>/dev/null || true) step "Checked this machine" "macOS${mac_version:+ $mac_version} on Apple silicon" # An earlier run's package stays as it is; the latest version is the first word of the version file. package_version=$(pkgutil --pkg-info dev.hatoba.mac 2>/dev/null | sed -n 's/^version: //p') if [ -n "$package_version" ]; then latest=$(curl -fsSL "$downloads/latest" 2>/dev/null | awk 'NR == 1 { print $1 }') || latest= waiting= if [ -n "$latest" ] && newer_version "$latest" "$package_version"; then waiting=$latest; fi step "Found hatoba $package_version" "${waiting:+$waiting is available: sudo hatoba admin update}" return 0 fi download=$(mktemp -d) trap 'rm -rf "$download"' EXIT failure="downloading hatoba.pkg failed; see $install_log" try curl -fsSL -o "$download/hatoba.pkg" "$downloads/hatoba.pkg" failure="installing hatoba.pkg failed; see $install_log" try installer -pkg "$download/hatoba.pkg" -target / package_version=$(pkgutil --pkg-info dev.hatoba.mac 2>/dev/null | sed -n 's/^version: //p') step "Installed hatoba${package_version:+ $package_version}" } # Whether this machine has joined a network. Right after an install the # service may still be starting, so give it a few seconds to answer. joined() { attempts=0 while :; do if status=$(query_root hatoba status 2>/dev/null); then case "$status" in *'on no network'*) return 1 ;; *) return 0 ;; esac fi attempts=$((attempts + 1)) if [ "$attempts" -ge 5 ] || [ -n "${HATOBA_DRY_RUN:-}" ]; then return 1 fi sleep 1 done } finish() { printf '\nHatoba is installed. Next:\n' if joined; then next_step "See the network" "hatoba status" elif [ "$(id -u)" = 0 ] && ! command -v sudo >/dev/null 2>&1; then next_step "Join a network" "hatoba join " else next_step "Join a network" "sudo hatoba join " fi } main() { # The role's Linux package; its command is hatoba either way. package=hatoba [ "$role" != runner ] || package=hatoba-runner parse "$@" os=${HATOBA_OS:-$(uname -s)} architecture=${HATOBA_ARCH:-$(uname -m)} systemd=${HATOBA_SYSTEMD:-$([ -d /run/systemd/system ] && echo yes || echo no)} docker=${HATOBA_DOCKER:-$(command -v docker >/dev/null 2>&1 && command -v dockerd >/dev/null 2>&1 && echo yes || echo no)} terminal=${HATOBA_TERMINAL:-$([ -t 1 ] && echo yes || echo no)} check_platform check_role [ "$platform" = mac ] || check_linux setup_root start_log header case "$platform" in linux) install_linux ;; container) install_container ;; mac) install_mac ;; esac finish } # Last, so that a download cut short runs nothing. main "$@"