#!/bin/sh # Installs or upgrades Factorial in $HOME/factorial. It never starts the stack: it ends by # printing the command that does (`docker compose up -d --wait` in the folder): # curl -fsSL https://www.runfactorial.com/install.sh | sh # curl -fsSL https://www.runfactorial.com/install.sh | sh -s -- --version 0.1.3 # Installs made before this script are not supported: it knows only the stack it writes, whose # data is the `factorial-public` volume. # - The version: --version, else FACTORIAL_VERSION, else the one the folder's compose.yaml pins, # else the site's version.txt. When version.txt is newer than the pinned one, it asks before an # upgrade, on /dev/tty because stdin is the pipe from curl. With no terminal it stays on the # installed version. A version older than the pinned one is refused without --force: the # database is not migrated back. # - A new version is pulled before anything changes, so a failed pull leaves the stack running. # Then an upgrade takes the stack down (`docker compose down`, never -v: it removes the # containers and keeps the volume) and backs up the volume to backups/. A failed backup leaves # compose.yaml on the installed version. # - factorial-setup writes factorial.env and seccomp.json on the first run only. A rerun never # changes factorial.env. A `factorial-public` volume with no factorial.env in the folder stops # the script: its secrets open only with the file they were made with. # - compose.yaml is written from the site's template on a first run or a version change only # (the template's header names the tag rule). # - It never uses sudo, and never removes a volume or a folder. # - MIN_VERSION is the oldest version it installs, the first with the two-container stack: a # --version or a version.txt below it is refused. landing/build/version.ts names the same floor. # install.ps1 is the same flow for Windows; keep the two in step. # FACTORIAL_SITE replaces the site address, for tests. set -eu MIN_VERSION=0.1.3 main() { site=${FACTORIAL_SITE:-https://www.runfactorial.com} dir=$HOME/factorial requested=${FACTORIAL_VERSION:-} force= while [ $# -gt 0 ]; do case $1 in --version) [ $# -ge 2 ] || fail "--version needs a value, such as --version 0.1.3." requested=$2; shift 2 ;; --version=*) requested=${1#--version=}; shift ;; --force) force=1; shift ;; *) fail "Unknown option $1. The options are --version X.Y.Z and --force." ;; esac done [ -z "$requested" ] || is_version "$requested" \ || fail "$requested is not a version. Give one such as 0.1.3." [ -z "$requested" ] || supported "$requested" command -v docker >/dev/null 2>&1 \ || fail "Docker is not installed. Install Docker Desktop or Docker Engine, then run this again." docker compose version >/dev/null 2>&1 \ || fail "Docker Compose is not installed. Install the docker compose plugin, then run this again." docker info >/dev/null 2>&1 \ || fail "Docker is not running. Start Docker, then run this again." mkdir -p "$dir" cd "$dir" if docker volume inspect factorial-public >/dev/null 2>&1 && [ ! -f factorial.env ]; then fail "A Factorial data volume already exists. Put the factorial.env it was made with in $dir, or remove the volume with docker volume rm factorial-public to start over." fi template=$(mktemp) trap 'rm -f "$template"' EXIT newest=$(curl -fsSL "$site/version.txt") \ || fail "Cannot read $site/version.txt. Check the network, then run this again." newest=$(printf '%s' "$newest" | tr -d ' \r\n') is_version "$newest" || fail "$site/version.txt does not hold a version." supported "$newest" curl -fsSL -o "$template" "$site/docs/run-the-public-image/compose.yaml" \ || fail "Cannot read the compose.yaml template from $site. Check the network, then run this again." image=$(sed -n 's/^ *image: *\([^ ]*\):[^:]*$/\1/p' "$template" | head -n 1) [ -n "$image" ] || fail "The compose.yaml template names no image." installed= [ -f compose.yaml ] && installed=$(sed -n 's/^ *image: *[^ ]*:\([^:]*\)$/\1/p' compose.yaml | head -n 1) [ -z "$installed" ] || is_version "$installed" || installed= if [ -n "$installed" ] && newer "$MIN_VERSION" "$installed"; then fail "$dir holds an install made before $MIN_VERSION, which this script does not upgrade. Run docker compose down in $dir, move the folder away (its factorial.env opens the old factorial volume), then run this again." fi version=$requested if [ -n "$version" ] && [ -n "$installed" ] && newer "$installed" "$version" && [ -z "$force" ]; then fail "Factorial $installed is installed, and $version is older. The database is not migrated back, so going back means restoring a backup from $dir/backups made before the upgrade. To start $version on this data anyway, add --force." fi if [ -z "$version" ] && [ -n "$installed" ]; then version=$installed if newer "$newest" "$installed"; then if ask "Factorial $installed is installed and $newest is available. Upgrade now? A backup of the data is made first. [y/N] "; then version=$newest else say "Factorial $installed is installed and $newest is available. $installed stays. To upgrade, run this again from a terminal, or run: curl -fsSL $site/install.sh | sh -s -- --version $newest" fi fi fi [ -n "$version" ] || version=$newest first= [ -f factorial.env ] || first=1 if [ "$version" != "$installed" ]; then docker pull --platform linux/amd64 "$image:$version" \ || fail "Cannot pull $image:$version. Nothing changed. Check the network, then run this again." fi if [ -n "$first" ]; then say "Making the keys in $dir/factorial.env." docker run --rm --platform linux/amd64 -v "$dir:/out" --entrypoint factorial-setup "$image:$version" >/dev/null fi if [ -n "$installed" ] && [ "$version" != "$installed" ]; then backup=factorial-$installed-$(date -u +%Y%m%dT%H%M%SZ).tgz say "Stopping Factorial $installed, and saving a backup of the data to $dir/backups/$backup." docker compose down mkdir -p backups docker run --rm --platform linux/amd64 --entrypoint tar -v factorial-public:/data \ -v "$dir/backups:/backup" "$image:$installed" -czf "/backup/$backup" -C /data . \ || fail "The backup failed, so Factorial stays on $installed, stopped. Run docker compose up -d in $dir to start $installed again." fi if [ "$version" != "$installed" ]; then sed "s|^\( *image: *[^ ]*:\)[^:]*\$|\1$version|" "$template" > compose.yaml fi say "" say "Factorial $version is installed in $dir." if [ -n "$installed" ] && [ "$version" != "$installed" ]; then say "The stack is stopped until you start it." fi say "To start it, run: cd ~/factorial && docker compose up -d --wait" say "Then open http://localhost:8000/factorial/" if [ -n "$first" ]; then say "There, push Create an account. The first account becomes the owner." fi } say() { printf '%s\n' "$1"; } fail() { printf '%s\n' "$1" >&2 exit 1 } is_version() { printf '%s\n' "$1" | grep -Eq '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' } # True when $1 is a higher X.Y.Z than $2. newer() { old_ifs=$IFS IFS=. # shellcheck disable=SC2086 set -- $1 $2 IFS=$old_ifs [ "$1" -gt "$4" ] && return 0 [ "$1" -lt "$4" ] && return 1 [ "$2" -gt "$5" ] && return 0 [ "$2" -lt "$5" ] && return 1 [ "$3" -gt "$6" ] } supported() { if newer "$MIN_VERSION" "$1"; then fail "Factorial $1 predates this install script; use $MIN_VERSION or later." fi } # Opening /dev/tty fails with no controlling terminal; then the answer is no. ask() { (: /dev/null || return 1 printf '%s' "$1" >/dev/tty read -r answer