#!/bin/sh # FleetOps installer for Linux and macOS. # # curl -fsSL https://fleetops.run/install.sh | sh # curl -fsSL https://fleetops.run/install.sh | sh -s -- --help # # It checks the platform, Docker or Podman, the socket and a free port, then # runs FleetOps as a container (or, on Linux without Docker, as a verified # static binary under systemd) and waits until it answers. It never installs # Docker for you and asks for sudo only when it has to write outside $HOME. # # Source: https://github.com/pravinbanjade/fleetops-web/blob/main/public/install.sh # Everything runs from main at the end, so a download cut short runs nothing. set -eu REPO="pravinbanjade/fleetops" SITE="https://fleetops.run" NAME="${FLEETOPS_CONTAINER:-fleetops}" VOLUME="${FLEETOPS_VOLUME:-fleetops-data}" BUSYBOX="busybox:1.37" PORT=9000 PORT_SET=0 EDITION=ce EDITION_SET=0 METHOD=auto VERSION="" ACTION=install PURGE=0 SUDO="" TMP="" usage() { cat <%s %s\n' "$B" "$N" "$*"; } ok() { printf ' %s✓%s %s\n' "$G" "$N" "$*"; } warn() { printf ' %s!%s %s\n' "$Y" "$N" "$*" >&2; } hint() { printf ' %s%s%s\n' "$D" "$*" "$N" >&2; } die() { printf '%serror:%s %s\n' "$R" "$N" "$1" >&2 shift for line in "$@"; do hint "$line"; done exit 1 } have() { command -v "$1" >/dev/null 2>&1; } cleanup() { if [ -n "$TMP" ]; then rm -rf "$TMP"; fi; } trap cleanup EXIT INT TERM # --- arguments -------------------------------------------------------------- parse_args() { while [ $# -gt 0 ]; do case "$1" in --port) [ $# -ge 2 ] || die "--port needs a number" PORT=$2 PORT_SET=1 shift ;; --port=*) PORT=${1#*=} PORT_SET=1 ;; --edition) [ $# -ge 2 ] || die "--edition needs ce or ee" EDITION=$2 EDITION_SET=1 shift ;; --edition=*) EDITION=${1#*=} EDITION_SET=1 ;; --method) [ $# -ge 2 ] || die "--method needs docker or binary" METHOD=$2 shift ;; --method=*) METHOD=${1#*=} ;; --version) [ $# -ge 2 ] || die "--version needs a tag such as v1.4.1" VERSION=$2 shift ;; --version=*) VERSION=${1#*=} ;; --upgrade) ACTION=upgrade ;; --uninstall) ACTION=uninstall ;; --purge) PURGE=1 ;; -h | --help) usage exit 0 ;; *) die "unknown option: $1" "Run with --help to see the options." ;; esac shift done case "$PORT" in '' | *[!0-9]*) die "--port must be a number, not '$PORT'" ;; esac [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ] || die "--port must be between 1 and 65535" case "$EDITION" in ce | ee) ;; *) die "--edition must be ce or ee" ;; esac case "$METHOD" in auto | docker | binary) ;; *) die "--method must be docker or binary" ;; esac case "$VERSION" in '' | v[0-9]*.[0-9]*.[0-9]*) ;; *) die "--version must look like v1.4.1" ;; esac if [ "$PURGE" = 1 ] && [ "$ACTION" != uninstall ]; then die "--purge only goes with --uninstall"; fi } # --- platform --------------------------------------------------------------- detect_platform() { step "Checking this machine" OS=$(uname -s) case "$OS" in Linux | Darwin) ;; MINGW* | MSYS* | CYGWIN*) die "this shell runs on Windows; use PowerShell instead:" \ "irm $SITE/install.ps1 | iex" ;; *) die "FleetOps runs on Linux and macOS, not $OS" ;; esac case "$(uname -m)" in x86_64 | amd64) ARCH=amd64 ;; aarch64 | arm64) ARCH=arm64 ;; *) die "FleetOps is built for amd64 and arm64, not $(uname -m)" ;; esac have curl || die "curl is needed" "Install it with your package manager, then run this again." ok "$OS $ARCH" } latest_version() { [ -z "$VERSION" ] || return 0 url=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest") || die "could not reach GitHub to find the latest release" "Check the network, or pass --version v1.4.1." VERSION=${url##*/} case "$VERSION" in v[0-9]*) ;; *) die "could not read the latest release from $url" "Pass --version v1.4.1." ;; esac } # The image tag that follows patch releases of this minor version: v1.2.3 -> 1.2. minor_tag() { printf '%s\n' "$VERSION" | sed 's/^v//; s/^\([0-9]*\.[0-9]*\).*/\1/'; } # Releases before 1.0 shipped a separate Enterprise image and binary. From 1.0 # there is one build that runs as Community until a license key is installed. separate_ee() { [ "$EDITION" = ee ] && case "$VERSION" in v0.*) true ;; *) false ;; esac; } image() { if separate_ee; then printf 'fleets/fleetops-ee'; else printf 'fleets/fleetops'; fi } # Says what --edition ee, or an Enterprise-image install, means from 1.0 on. note_edition() { [ "$EDITION" = ee ] || return 0 separate_ee && return 0 ok "one image since 1.0: fleets/fleetops runs Enterprise once a license key is installed" if [ "${OLD_EE:-0}" = 1 ]; then hint "Moving off fleets/fleetops-ee, a deprecated alias of the same image. Your license and data stay."; fi } # Docker works when the CLI is there and the daemon answers. Podman's docker # shim answers too, but its socket needs the binary route. docker_state() { have docker || { DOCKER=missing return } if docker version 2>/dev/null | grep -qi podman; then DOCKER=podman return fi # Older clients print the daemon's error on stdout, so keep both streams. if err=$(docker info 2>&1); then DOCKER=ok; else DOCKER=down DOCKER_ERR=$(printf '%s' "$err" | tr '[:upper:]' '[:lower:]') fi } explain_docker() { case "$DOCKER" in missing) if [ "$OS" = Darwin ]; then die "Docker is not installed" \ "Install Docker Desktop, OrbStack or Colima (brew install colima docker && colima start)," \ "then run this again." fi die "Docker is not installed" \ "Install it (https://docs.docker.com/engine/install/), or run this with --method binary." ;; down) case "$DOCKER_ERR" in *"permission denied"*) die "this user cannot reach the Docker socket" \ "Add yourself to the docker group: sudo usermod -aG docker \$USER, then log in again." \ "Or run the installer as root: curl -fsSL $SITE/install.sh | sudo sh" ;; esac if [ "$OS" = Darwin ]; then die "Docker is installed but not running" "Start Docker Desktop, OrbStack or Colima (colima start), then run this again." fi die "Docker is installed but not running" "Start it: sudo systemctl start docker. Then run this again." ;; esac } choose_method() { docker_state if [ "$METHOD" = auto ]; then # Stay with how FleetOps was installed before. if [ "$OS" = Linux ] && binary_existing; then METHOD=binary; elif [ "$DOCKER" = ok ]; then METHOD=docker; elif [ "$OS" = Linux ] && [ "$DOCKER" != down ]; then METHOD=binary; else explain_docker; fi fi if [ "$METHOD" = docker ]; then [ "$DOCKER" = podman ] && die "the docker command here is Podman" "Run this with --method binary; FleetOps then talks to the Podman socket." [ "$DOCKER" = ok ] || explain_docker ok "Docker $(docker version --format '{{.Server.Version}}' 2>/dev/null || echo running)" else [ "$OS" = Linux ] || die "the binary is for Linux; on macOS FleetOps runs in Docker" "Start Docker Desktop, OrbStack or Colima, then run this again." ok "binary install" fi } # --- ports and health ------------------------------------------------------- # A port is free when nothing accepts a connection on it (curl exit 7). port_free() { rc=0 curl -s -o /dev/null --max-time 2 "http://127.0.0.1:$1/" || rc=$? [ "$rc" -eq 7 ] } pick_port() { if port_free "$PORT"; then ok "port $PORT is free" return fi [ "$PORT_SET" = 0 ] || die "port $PORT is already in use" "Pick another one with --port." for p in 9100 9200 9300 9400 9500 19000; do if port_free "$p"; then warn "port $PORT is in use; using $p instead" PORT=$p return fi done die "ports 9000 to 9500 are all in use" "Pick a free one with --port." } wait_healthy() { step "Waiting for FleetOps to answer" i=0 while [ $i -lt 60 ]; do if curl -fsS --max-time 2 "http://127.0.0.1:$PORT/api/v1/health" >/dev/null 2>&1; then ok "healthy on port $PORT" return 0 fi i=$((i + 1)) sleep 1 done return 1 } lan_ip() { if [ "$OS" = Darwin ]; then ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || true else hostname -I 2>/dev/null | awk '{print $1}' || true fi } done_message() { ip=$(lan_ip) printf '\n%sFleetOps is running.%s\n\n' "$G$B" "$N" printf ' Open http://localhost:%s\n' "$PORT" [ -z "$ip" ] || printf ' On your LAN http://%s:%s\n' "$ip" "$PORT" printf '\n Create the admin account now: setup locks 10 minutes after start.\n' printf ' Before other machines use it, turn on HTTPS: %s/install#s4\n' "$SITE" printf ' Upgrade later: curl -fsSL %s/install.sh | sh -s -- --upgrade\n' "$SITE" printf ' Remove: curl -fsSL %s/install.sh | sh -s -- --uninstall\n\n' "$SITE" printf ' SSO, image scanning and CVE alerts come with a paid plan: %s/pricing\n' "$SITE" printf ' If FleetOps helps, a star helps others find it: https://github.com/pravinbanjade/fleetops\n\n' } # --- docker ----------------------------------------------------------------- # The socket path as the Docker daemon sees it. Docker Desktop, OrbStack and # Colima run the daemon in a VM, where it is always /var/run/docker.sock; a # rootless daemon on Linux has its own path. daemon_socket() { SOCK=/var/run/docker.sock [ "$OS" = Linux ] || return 0 docker info --format '{{.OperatingSystem}}' 2>/dev/null | grep -q "Docker Desktop" && return 0 host=${DOCKER_HOST:-$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null || true)} case "$host" in unix://*) [ -S "${host#unix://}" ] && SOCK=${host#unix://} ;; esac return 0 } container_exists() { docker container inspect "$NAME" >/dev/null 2>&1; } container_running() { [ "$(docker container inspect -f '{{.State.Running}}' "$NAME" 2>/dev/null)" = true ]; } container_port() { docker port "$NAME" 9000/tcp 2>/dev/null | sed -n '1s/.*://p'; } docker_install() { if container_exists; then if [ "$ACTION" = install ]; then container_running || { step "Starting the existing $NAME container" docker start "$NAME" >/dev/null } PORT=$(container_port) PORT=${PORT:-9000} ok "FleetOps is already installed as the container $NAME" hint "To move to the newest release, run this again with --upgrade." wait_healthy || die "it is not answering" "See its log: docker logs $NAME" done_message return fi old=$(container_port) [ "$PORT_SET" = 1 ] || PORT=${old:-$PORT} # An upgrade keeps the installed edition unless --edition says otherwise. if [ "$EDITION_SET" = 0 ]; then case "$(docker container inspect -f '{{.Config.Image}}' "$NAME" 2>/dev/null)" in *fleetops-ee*) EDITION=ee OLD_EE=1 ;; *) EDITION=ce ;; esac fi elif [ "$ACTION" = upgrade ]; then warn "no $NAME container yet; installing instead" fi step "Preparing" note_edition daemon_socket tag=$(minor_tag) img="$(image):$tag" ok "image $img ($VERSION)" printf ' pulling…\n' docker pull -q "$img" >/dev/null || die "could not pull $img" "Check the network and that $VERSION exists." docker pull -q "$BUSYBOX" >/dev/null || die "could not pull $BUSYBOX" gid=$(docker run --rm -v "$SOCK:/s" "$BUSYBOX" stat -c %g /s 2>/dev/null) || die "could not read the group of $SOCK" "Is that the Docker socket? Set DOCKER_HOST if it lives elsewhere." ok "socket $SOCK, group $gid" # Settle the port before touching the old container, so a clash leaves it running. if [ -n "${old:-}" ] && [ "$PORT" = "$old" ]; then ok "keeping port $PORT"; else pick_port; fi if container_exists; then step "Replacing the $NAME container (data in the $VOLUME volume stays)" docker rm -f "$NAME" >/dev/null fi step "Starting FleetOps" docker run -d --name "$NAME" --restart unless-stopped \ -p "$PORT:9000" \ -v "$VOLUME:/data" \ -v "$SOCK:/var/run/docker.sock" \ --group-add "$gid" \ "$img" >/dev/null ok "container $NAME" wait_healthy || { docker logs --tail 20 "$NAME" >&2 || true die "FleetOps started but does not answer on port $PORT" "The log above usually says why. Full log: docker logs $NAME" } done_message } docker_uninstall() { if container_exists; then docker rm -f "$NAME" >/dev/null ok "removed the container $NAME" FOUND_DOCKER=1 fi if [ "$PURGE" = 1 ]; then if docker volume rm "$VOLUME" >/dev/null 2>&1; then ok "deleted the volume $VOLUME"; fi elif [ "$FOUND_DOCKER" = 1 ]; then hint "Your data is still in the volume $VOLUME. Delete it with --uninstall --purge." fi } # --- binary (Linux) --------------------------------------------------------- need_root() { if [ "$(id -u)" -eq 0 ]; then SUDO=""; return; fi have sudo || die "this step needs root and sudo is not installed" "Run the installer as root." SUDO=sudo [ -n "${SUDO_ASKED:-}" ] || printf ' sudo is needed to %s\n' "$1" SUDO_ASKED=1 } has_systemd() { have systemctl && [ -d /run/systemd/system ]; } binary_download() { note_edition file="fleetops-$VERSION-linux-$ARCH" if separate_ee; then file="fleetops-ee-$VERSION-linux-$ARCH"; fi base="https://github.com/$REPO/releases/download/$VERSION" TMP=$(mktemp -d) step "Downloading $file" curl -fsSL -o "$TMP/$file" "$base/$file" || die "could not download $base/$file" curl -fsSL -o "$TMP/$file.sha256" "$base/$file.sha256" || die "could not download the checksum" if have sha256sum; then (cd "$TMP" && sha256sum -c "$file.sha256" >/dev/null) || die "checksum mismatch; the download is damaged or altered" elif have shasum; then (cd "$TMP" && shasum -a 256 -c "$file.sha256" >/dev/null) || die "checksum mismatch; the download is damaged or altered" else die "sha256sum is needed to verify the download" fi ok "SHA-256 verified" BIN_SRC="$TMP/$file" } # Rootless Podman serves the Docker API on a per-user socket. podman_socket() { [ "$DOCKER" = podman ] || have podman || return 1 [ "$(id -u)" -ne 0 ] || return 1 if has_systemd; then systemctl --user enable --now podman.socket >/dev/null 2>&1 || true; fi sock="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/podman/podman.sock" [ -S "$sock" ] || return 1 SOCK=$sock } # An earlier binary install: its unit file, and the port it listens on. binary_existing() { for unit in /etc/systemd/system/fleetops.service "$HOME/.config/systemd/user/fleetops.service"; do if [ -f "$unit" ]; then EXISTING_PORT=$(sed -n 's/.*--bind [^ ]*:\([0-9]*\).*/\1/p' "$unit" | head -n 1) if [ "$EDITION_SET" = 0 ] && grep -q '^Description=FleetOps (ee)' "$unit"; then EDITION=ee OLD_EE=1; fi return 0 fi done return 1 } binary_install() { SOCK=/var/run/docker.sock USER_MODE=0 REUSE_PORT=0 if binary_existing; then if [ "$ACTION" = install ]; then PORT=${EXISTING_PORT:-$PORT} ok "FleetOps is already installed as a systemd service" hint "To move to the newest release, run this again with --upgrade." wait_healthy || die "it is not answering" "See: systemctl status fleetops (or systemctl --user status fleetops)" done_message return fi [ "$PORT_SET" = 1 ] || { PORT=${EXISTING_PORT:-$PORT} REUSE_PORT=1 } fi if podman_socket; then USER_MODE=1 ok "Podman socket $SOCK" elif [ -S /var/run/docker.sock ]; then ok "Docker socket $SOCK" else die "no Docker or Podman socket found" \ "Install Docker (https://docs.docker.com/engine/install/) or Podman, then run this again." fi if [ "$REUSE_PORT" = 1 ]; then ok "keeping port $PORT"; else pick_port; fi binary_download if [ "$USER_MODE" = 1 ]; then dest="$HOME/.local/bin/fleetops" data="$HOME/.local/share/fleetops" mkdir -p "$(dirname "$dest")" "$data" install -m 755 "$BIN_SRC" "$dest" else dest=/usr/local/bin/fleetops data=/var/lib/fleetops need_root "install /usr/local/bin/fleetops and a systemd service" $SUDO install -m 755 "$BIN_SRC" "$dest" fi ok "installed $dest ($("$dest" --version 2>/dev/null || echo "$VERSION"))" if has_systemd; then step "Setting up the systemd service" if [ "$USER_MODE" = 1 ]; then unit_dir="$HOME/.config/systemd/user" mkdir -p "$unit_dir" write_unit user >"$unit_dir/fleetops.service" systemctl --user daemon-reload systemctl --user enable fleetops >/dev/null 2>&1 systemctl --user restart fleetops have loginctl && loginctl enable-linger "$(id -un)" >/dev/null 2>&1 || true ok "user service fleetops (systemctl --user status fleetops)" else getent group docker >/dev/null 2>&1 || die "there is no docker group to give the service" \ "Run FleetOps with --method docker, or create the group that owns $SOCK." write_unit system | $SUDO tee /etc/systemd/system/fleetops.service >/dev/null $SUDO systemctl daemon-reload $SUDO systemctl enable fleetops >/dev/null 2>&1 $SUDO systemctl restart fleetops ok "service fleetops (systemctl status fleetops)" fi wait_healthy || { if [ "$USER_MODE" = 1 ]; then jflag=--user; else jflag=--system; fi journalctl "$jflag" -u fleetops -n 20 --no-pager >&2 2>/dev/null || true die "FleetOps started but does not answer on port $PORT" "See: journalctl $jflag -u fleetops" } else step "Starting FleetOps (no systemd here, so it will not start again after a reboot)" [ -w "$SOCK" ] || die "this user cannot use $SOCK" "Add yourself to the group that owns it, or run the installer as root." data="$HOME/.local/share/fleetops" mkdir -p "$data" log="$data/fleetops.log" DOCKER_HOST="unix://$SOCK" nohup "$dest" --data "$data" --bind "0.0.0.0:$PORT" >"$log" 2>&1 & wait_healthy || die "FleetOps does not answer on port $PORT" "See its log: $log" fi done_message } write_unit() { if [ "$1" = user ]; then cat </dev/null 2>&1 || true $SUDO rm -f /etc/systemd/system/fleetops.service /usr/local/bin/fleetops $SUDO systemctl daemon-reload found=1 ok "removed the fleetops service and /usr/local/bin/fleetops" fi # Data left by an earlier service install. if [ "$PURGE" = 1 ] && { [ -d /var/lib/fleetops ] || [ -d /var/lib/private/fleetops ]; }; then need_root "delete /var/lib/fleetops" $SUDO rm -rf /var/lib/fleetops /var/lib/private/fleetops ok "deleted /var/lib/fleetops" found=1 fi if [ -f "$HOME/.config/systemd/user/fleetops.service" ] || [ -f "$HOME/.local/bin/fleetops" ]; then systemctl --user disable --now fleetops >/dev/null 2>&1 || true rm -f "$HOME/.config/systemd/user/fleetops.service" "$HOME/.local/bin/fleetops" found=1 ok "removed the user service and ~/.local/bin/fleetops" if [ "$PURGE" = 1 ]; then rm -rf "$HOME/.local/share/fleetops" ok "deleted ~/.local/share/fleetops" fi fi [ "$found" = 1 ] || [ "$FOUND_DOCKER" = 1 ] || warn "no FleetOps install found" [ "$PURGE" = 1 ] || [ "$found" = 0 ] || hint "Your data is kept. Delete it with --uninstall --purge." } # --- main ------------------------------------------------------------------- main() { parse_args "$@" detect_platform if [ "$ACTION" = uninstall ]; then step "Removing FleetOps" FOUND_DOCKER=0 docker_state if [ "$DOCKER" = ok ] && [ "$METHOD" != binary ]; then docker_uninstall; fi if [ "$METHOD" != docker ]; then binary_uninstall; else [ "$FOUND_DOCKER" = 1 ] || warn "no FleetOps container found"; fi return fi choose_method latest_version if [ "$METHOD" = docker ]; then docker_install; else binary_install; fi } main "$@"