Your cloud, on your machine
One command on a Linux machine, a few questions, and it is running. Then open it in a browser, and put it on your computers and your phone.
-
1
Run one command on a Linux machine
$ curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/install-server.sh | sudo shWhat this command runs
install-server.shIt runs as root on the machine that will be your cloud, so read it first if you like. 1458 lines, from deploy/install-server.sh on GitHub.
#!/bin/sh # Install (or re-install) the Cloudmorrow server on this machine. # # One command, six questions — what your cloud is called, who its first # account (the administrator) is, where on this machine it goes (Enter # takes the usual places), which of its addresses it answers on, which # database it keeps its records in (the built-in one, unless you run a # PostgreSQL), and which of the standard quills it has — and it is running: # # curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/install-server.sh | sudo sh # # Or from a checkout: `sudo sh deploy/install-server.sh`. Every answer can be # given as a flag instead, for a script or a re-run that should ask nothing: # # sudo sh install-server.sh --name "The Larsens" --user alice --quills all # # The cloud is reached at this machine's address, and on a home network at # <name>.local. Reaching it from outside is your own setup: a domain and a # reverse proxy of your own (--public-url), or a VPN such as Tailscale. # On the server itself, `cm` opens the terminal app and signs in to the # server beside it, whichever addresses it was told to answer on. # # Run it again on a machine that already has Cloudmorrow and it checks the # server first: running and answering, it asks whether to update it (Enter # says yes; without a terminal it changes nothing); otherwise it updates # and reinstalls it, and checks again. --update does that without asking. # It never overwrites an existing # /etc/cloudmorrow/server.toml, everyone's files, or the database, and it never # asks a question it already has the answer to. For routine "I pushed a # change" updates, use `cloudmorrow update server` from any machine, or the # `cloudmorrow-update` command this script installs. set -eu DEFAULT_REPO="https://github.com/Cloudmorrow/cloudmorrow.git" REPO="" BRANCH="main" DEFAULT_PREFIX="/opt/cloudmorrow" DEFAULT_CONFIG_DIR="/etc/cloudmorrow" DEFAULT_DATA_DIR="/var/lib/cloudmorrow" PREFIX="" CONFIG_DIR="" DATA_DIR="" FILES_DIR="" SHARES_DIR="" CLOUD_NAME="" PUBLIC_URL="" TRUSTED_PROXIES="" HOST="0.0.0.0" PORT="8787" SERVICE_USER="cloudmorrow" SERVICE_NAME="cloudmorrow" ADMIN_USER="${SUDO_USER:-}" ACCOUNT="" ACCOUNT_PASSWORD="${CLOUDMORROW_ADMIN_PASSWORD:-}" SSH_KEY="" NO_SSH_KEY="" QUILLS="" DRY_RUN="" UPDATE="" HOST_GIVEN="" DATABASE="" usage() { sed -n '2,20p' "$0" cat <<EOF Options: --name TEXT what your cloud is called (asked if not given) --public-url URL an address of your own, behind a reverse proxy of your own --trusted-proxy IP that proxy, when it is another machine: its address (or CIDR; several comma-separated), so the server believes what it says about https --user NAME the first account, an administrator (asked if not given; its password is asked for, or read from \$CLOUDMORROW_ADMIN_PASSWORD) --quills LIST standard quills to have, comma-separated, or 'all' (asked if not given, with Files, Calendar, Notes and Tasks ticked; without a terminal those four; 'cloudmorrow-server quill standard' lists them) --repo URL git URL to clone (default: this checkout's origin, else $DEFAULT_REPO) --branch NAME branch to deploy (default: $BRANCH) --prefix DIR the checkout and the virtualenv (default: $DEFAULT_PREFIX) --config-dir DIR server.toml and the sealing key (default: $DEFAULT_CONFIG_DIR) --data-dir DIR everyone's files, the database, keys and quills (default: $DEFAULT_DATA_DIR) (where things go is asked when none of the three is given; a re-run finds them where they are) --files-dir DIR everyone's files — a folder per person, and the Shares folder — when they are not to be under the data directory (default: <data>/files) --shares-dir DIR the Shares folder, when it is not to be with the files (default: <files>/Shares) --host ADDR address(es) to answer on, comma-separated (asked if not given; default: $HOST, every address) --port N bind port (default: $PORT) --database WHAT where the records are kept: builtin (a file in the data directory), container (PostgreSQL in a container beside the server, with docker or podman), or the URL of a PostgreSQL server you already have (asked if not given) --service-user NAME system user to run as (default: $SERVICE_USER) --admin USER unix user allowed to update and restart (default: \$SUDO_USER) --ssh-key PATH deploy key for a private repo (default: generate one) --no-ssh-key the repo needs no key (it is public, or https) --update update and reinstall an existing server even when it is running well --dry-run print what would happen, ask nothing, change nothing EOF } while [ $# -gt 0 ]; do case "$1" in --name) CLOUD_NAME="$2"; shift 2 ;; --public-url) PUBLIC_URL="$2"; shift 2 ;; --trusted-proxy) TRUSTED_PROXIES="$2"; shift 2 ;; --user) ACCOUNT="$2"; shift 2 ;; --quills) QUILLS="$2"; shift 2 ;; --repo) REPO="$2"; shift 2 ;; --branch) BRANCH="$2"; shift 2 ;; --prefix) PREFIX="$2"; shift 2 ;; --config-dir) CONFIG_DIR="$2"; shift 2 ;; --files-dir | --notes-dir) FILES_DIR="$2"; shift 2 ;; --shares-dir) SHARES_DIR="$2"; shift 2 ;; --data-dir) DATA_DIR="$2"; shift 2 ;; --host) HOST="$2"; HOST_GIVEN="1"; shift 2 ;; --port) PORT="$2"; shift 2 ;; --database) DATABASE="$2"; shift 2 ;; --service-user) SERVICE_USER="$2"; shift 2 ;; --service-name) SERVICE_NAME="$2"; shift 2 ;; --admin) ADMIN_USER="$2"; shift 2 ;; --ssh-key) SSH_KEY="$2"; shift 2 ;; --no-ssh-key) NO_SSH_KEY="1"; shift ;; --update) UPDATE="1"; shift ;; --dry-run) DRY_RUN="1"; shift ;; -h | --help) usage; exit 0 ;; *) echo "unknown option: $1" >&2; usage >&2; exit 2 ;; esac done say() { printf '\033[36m::\033[0m %s\n' "$*"; } warn() { printf '\033[33mnote:\033[0m %s\n' "$*"; } die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; } # The finished page's colours: the brand's sky for headings, its muted grey # for the words beside them. None when nobody is looking, or NO_COLOR says so. if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then HEAD='\033[1;38;2;90;166;224m' SOFT='\033[38;2;153;161;179m' GOOD='\033[1;38;2;92;232;155m' OFF='\033[0m' else HEAD='' SOFT='' GOOD='' OFF='' fi heading() { printf "\n ${HEAD}%s${OFF}\n" "$*"; } # line TEXT: one line under a heading. item LABEL TEXT: the same, after a # short grey label; labels are a word, so they line up at any width. line() { printf ' %s\n' "$1"; } item() { printf " ${SOFT}%-9s${OFF} %s\n" "$1" "$2"; } term_width() { width="$(stty size </dev/tty 2>/dev/null | awk '{print $2}')" echo "${width:-${COLUMNS:-80}}" } # --- the wordmark (scripts/installer_banner.py draws this) --------------- wordmark() { if [ "$1" -ge 67 ]; then printf ' \033[1;38;2;18;86;145m▄▀▀▀▄ \033[1;38;2;21;93;154m█ \033[1;38;2;24;101;163m▄▀▀▀▄ \033[1;38;2;27;108;172m█ █ \033[1;38;2;33;116;181m█▀▀▄ \033[1;38;2;42;124;188m█▄ ▄█ \033[1;38;2;52;133;195m▄▀▀▀▄ \033[1;38;2;61;141;202m█▀▀▀▄ \033[1;38;2;71;149;210m█▀▀▀▄ \033[1;38;2;80;158;217m▄▀▀▀▄ \033[1;38;2;90;166;224m█ █\033[0m\n' printf ' \033[1;38;2;18;86;145m█ \033[1;38;2;21;93;154m█ \033[1;38;2;24;101;163m█ █ \033[1;38;2;27;108;172m█ █ \033[1;38;2;33;116;181m█ █ \033[1;38;2;42;124;188m█ █ █ \033[1;38;2;52;133;195m█ █ \033[1;38;2;61;141;202m█▄▄▄▀ \033[1;38;2;71;149;210m█▄▄▄▀ \033[1;38;2;80;158;217m█ █ \033[1;38;2;90;166;224m█ ▄ █\033[0m\n' printf ' \033[1;38;2;18;86;145m█ ▄ \033[1;38;2;21;93;154m█ \033[1;38;2;24;101;163m█ █ \033[1;38;2;27;108;172m█ █ \033[1;38;2;33;116;181m█ ▄▀ \033[1;38;2;42;124;188m█ █ \033[1;38;2;52;133;195m█ █ \033[1;38;2;61;141;202m█ ▀▄ \033[1;38;2;71;149;210m█ ▀▄ \033[1;38;2;80;158;217m█ █ \033[1;38;2;90;166;224m█▄▀▄█\033[0m\n' printf ' \033[1;38;2;18;86;145m ▀▀▀ \033[1;38;2;21;93;154m▀▀▀▀▀ \033[1;38;2;24;101;163m ▀▀▀ \033[1;38;2;27;108;172m ▀▀▀ \033[1;38;2;33;116;181m▀▀▀ \033[1;38;2;42;124;188m▀ ▀ \033[1;38;2;52;133;195m ▀▀▀ \033[1;38;2;61;141;202m▀ ▀ \033[1;38;2;71;149;210m▀ ▀ \033[1;38;2;80;158;217m ▀▀▀ \033[1;38;2;90;166;224m▀ ▀\033[0m\n' else printf ' \033[1;38;2;18;86;145mC \033[1;38;2;21;93;154mL \033[1;38;2;24;101;163mO \033[1;38;2;27;108;172mU \033[1;38;2;33;116;181mD \033[1;38;2;42;124;188mM \033[1;38;2;52;133;195mO \033[1;38;2;61;141;202mR \033[1;38;2;71;149;210mR \033[1;38;2;80;158;217mO \033[1;38;2;90;166;224mW\033[0m\n' fi printf ' \033[38;2;153;161;179mown your data, choose your apps\033[0m\n\n' } # --- end of the wordmark ------------------------------------------------- if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then printf '\n' wordmark "$(term_width)" fi run() { if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould run:\033[0m %s\n' "$*" else "$@" fi } write_file() { # write_file <path> <mode>, contents on stdin if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould write:\033[0m %s (mode %s)\n' "$1" "$2" cat >/dev/null else cat >"$1" chmod "$2" "$1" fi } # --- where things are, or go ---------------------------------------------- # A flag says. Else, on a machine that has the server, the unit says where # the venv and the config are and the config says where the data and the # files are, so a re-run needs no flags to find a custom layout. On a fresh # machine the usual places are offered on the terminal (ask_places, below), # to change or to take with Enter. Everything people keep — a folder per # person, and the Shares folder — is under the data directory, in files/, # unless a flag or the config says otherwise. UNIT="/etc/systemd/system/$SERVICE_NAME.service" unit_value() { sed -n "s/^$1=//p" "$UNIT" 2>/dev/null | head -n 1; } ASK_PLACES="" if [ -z "$PREFIX$CONFIG_DIR$DATA_DIR$FILES_DIR$SHARES_DIR" ] && [ ! -f "$UNIT" ] && [ ! -f "$DEFAULT_CONFIG_DIR/server.toml" ]; then ASK_PLACES="1" fi if [ -z "$PREFIX" ]; then # ExecStart=/opt/cloudmorrow/venv/bin/cloudmorrow-server serve exec_start="$(unit_value ExecStart | cut -d' ' -f1)" case "$exec_start" in */venv/bin/cloudmorrow-server) PREFIX="${exec_start%/venv/bin/cloudmorrow-server}" ;; *) PREFIX="$DEFAULT_PREFIX" ;; esac fi if [ -z "$CONFIG_DIR" ]; then # Environment=CLOUDMORROW_SERVER_CONFIG=/etc/cloudmorrow/server.toml config_now="$(unit_value Environment=CLOUDMORROW_SERVER_CONFIG)" case "$config_now" in /*/server.toml) CONFIG_DIR="$(dirname "$config_now")" ;; *) CONFIG_DIR="$DEFAULT_CONFIG_DIR" ;; esac fi CONFIG="$CONFIG_DIR/server.toml" config_value() { sed -n "s/^$1 = \"\{0,1\}\([^\"]*\)\"\{0,1\}\$/\1/p" "$CONFIG" 2>/dev/null | head -n 1; } [ -n "$DATA_DIR" ] || DATA_DIR="$(config_value data_dir)" [ -n "$DATA_DIR" ] || DATA_DIR="$DEFAULT_DATA_DIR" # The config's key for the files is notes_dir, from when notes were all it # held; the flag and the installer's words say files. FILES_GIVEN="$FILES_DIR" [ -n "$FILES_GIVEN" ] || FILES_GIVEN="$(config_value notes_dir)" [ -n "$SHARES_DIR" ] || SHARES_DIR="$(config_value shares_dir)" # places: what follows from the answers; called again once they are asked. places() { SRC="$PREFIX/src" VENV="$PREFIX/venv" CONFIG="$CONFIG_DIR/server.toml" KEY_FILE="$CONFIG_DIR/cloudmorrow.key" FILES_DIR="${FILES_GIVEN:-$DATA_DIR/files}" } places # shares_outside: the Shares folder when it is a directory of its own — # outside the files, the data and the code — to make, and to let the # service write to (ReadWritePaths in the unit). Moved by hand later, a # run with --update rewrites the unit to match. shares_outside() { case "$SHARES_DIR" in "" | "$FILES_DIR" | "$FILES_DIR"/* | "$DATA_DIR" | "$DATA_DIR"/* | "$PREFIX" | "$PREFIX"/*) ;; *) printf '%s' "$SHARES_DIR" ;; esac } # place_flags: the flags that name every place that is not the usual one, # for a message that says how to run this again. place_flags() { [ "$PREFIX" = "$DEFAULT_PREFIX" ] || printf ' --prefix %s' "$PREFIX" [ "$CONFIG_DIR" = "$DEFAULT_CONFIG_DIR" ] || printf ' --config-dir %s' "$CONFIG_DIR" [ "$DATA_DIR" = "$DEFAULT_DATA_DIR" ] || printf ' --data-dir %s' "$DATA_DIR" [ "$FILES_DIR" = "$DATA_DIR/files" ] || printf ' --files-dir %s' "$FILES_DIR" [ -z "$SHARES_DIR" ] || printf ' --shares-dir %s' "$SHARES_DIR" } [ -n "$DRY_RUN" ] || [ "$(id -u)" = "0" ] || die "run this with sudo" # --- what it needs ---------------------------------------------------------- # Checked before anything is asked or changed, so a missing package shows up # in the first second, not after the user and the checkout are made. This # runs as root, so it installs what is missing with the machine's own # package manager; where it cannot, it says the exact command and stops. PKG="" for candidate in apt-get dnf yum zypper pacman apk; do if command -v "$candidate" >/dev/null 2>&1; then PKG="$candidate" break fi done pkg_command() { case "$PKG" in apt-get) echo "apt-get install -y $*" ;; dnf | yum) echo "$PKG install -y $*" ;; zypper) echo "zypper --non-interactive install $*" ;; pacman) echo "pacman -S --noconfirm --needed $*" ;; apk) echo "apk add $*" ;; *) echo "" ;; esac } # The package that brings a command, by package manager. pkg_for() { case "$1:$PKG" in git:* | sudo:*) echo "$1" ;; python:pacman) echo python ;; python:zypper) echo python311 ;; python:*) echo python3 ;; useradd:apt-get) echo passwd ;; useradd:dnf | useradd:yum) echo shadow-utils ;; useradd:*) echo shadow ;; ssh:apt-get) echo openssh-client ;; ssh:dnf | ssh:yum | ssh:zypper) echo openssh-clients ;; ssh:*) echo openssh ;; esac } # install_packages "what for" pkg...: installs them, or explains and stops. install_packages() { what="$1" shift command="$(pkg_command "$@")" if [ -z "$command" ]; then die "this machine needs $what, and no package manager here is one this script knows. Install it and run this again." fi if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould run:\033[0m %s (for %s)\n' "$command" "$what" return 0 fi say "installing $what: $command" if [ "$PKG" = "apt-get" ]; then DEBIAN_FRONTEND=noninteractive apt-get -qq update >/dev/null 2>&1 || true fi # The command is ours, and split on purpose. # shellcheck disable=SC2086 DEBIAN_FRONTEND=noninteractive $command >/dev/null || die "that did not work. Install it yourself with: sudo $command" } find_python() { PYTHON="" for candidate in python3.14 python3.13 python3.12 python3.11 python3; do if command -v "$candidate" >/dev/null 2>&1 && "$candidate" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)' 2>/dev/null; then PYTHON="$candidate" return 0 fi done return 1 } MISSING="" NEEDED="" need() { # need <command> <what>: notes the package when the command is missing. if ! command -v "$1" >/dev/null 2>&1; then MISSING="$MISSING $(pkg_for "$2")" NEEDED="$NEEDED${NEEDED:+, }$1" fi } need git git need sudo sudo need useradd useradd find_python || { MISSING="$MISSING $(pkg_for python)"; NEEDED="$NEEDED${NEEDED:+, }python3"; } if [ -n "$MISSING" ]; then # shellcheck disable=SC2086 install_packages "$NEEDED" $MISSING fi if ! find_python && [ -z "$DRY_RUN" ]; then newest="$(python3 -V 2>/dev/null || echo "no Python at all")" die "Python 3.11 or newer is required, and this machine has $newest. Install a newer one (your distribution's python3.11 or later) and run this again." fi # Debian and Ubuntu ship venv's pip bootstrap in a package of its own # (python3.12-venv), so "import venv" works and making one does not, or # makes one without pip. Only making one, and asking its pip, says for sure. if [ -n "$PYTHON" ]; then VENV_TEST="$(mktemp -d)" if ! "$PYTHON" -m venv "$VENV_TEST/venv" >/dev/null 2>&1 || ! "$VENV_TEST/venv/bin/python" -m pip --version >/dev/null 2>&1; then version="$("$PYTHON" -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")')" case "$PKG" in apt-get) install_packages "Python's venv module and pip" "python$version-venv" ;; *) die "$PYTHON cannot make a virtualenv with pip in it here. Install the venv module and pip for Python $version, then run this again." ;; esac if [ -z "$DRY_RUN" ]; then rm -rf "$VENV_TEST/venv" { "$PYTHON" -m venv "$VENV_TEST/venv" >/dev/null 2>&1 && "$VENV_TEST/venv/bin/python" -m pip --version >/dev/null 2>&1; } || die "$PYTHON still cannot make a virtualenv with pip in it. Install python$version-venv and run this again." fi fi rm -rf "$VENV_TEST" fi # The default repo is wherever this checkout came from, so running the script # straight out of a clone does the obvious thing; a copy on its own, or one # that arrived through curl, installs the public code. # Only a script that is really on disk has a checkout around it; through a # pipe, $0 is the shell, and whatever directory this runs from is not ours # to read a remote off. SCRIPT_DIR="" if [ -f "$0" ]; then SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" 2>/dev/null && pwd || true)" fi if [ -z "$REPO" ] && [ -n "$SCRIPT_DIR" ] && [ -d "$SCRIPT_DIR/../.git" ]; then REPO="$(git -C "$SCRIPT_DIR/.." remote get-url origin 2>/dev/null || true)" fi [ -n "$REPO" ] || REPO="$DEFAULT_REPO" # A repository over ssh needs a deploy key, and the tools to make one. case "$REPO" in git@* | ssh://*) if [ -z "$NO_SSH_KEY" ] && ! command -v ssh-keygen >/dev/null 2>&1; then install_packages "ssh-keygen" "$(pkg_for ssh)" fi ;; esac # --- already here? ------------------------------------------------------------ # A server that is installed, running and answering is left as it is. One # that is not gets the whole install again over it: the checkout reset to # the branch, the packages reinstalled, the unit rewritten, the service # restarted. That mends almost everything short of lost data. # listening_on "HOST, HOST": sets HEALTH_HOST, how this machine reaches the # server (loopback when it listens there or everywhere, else the first # address), and LAN_HOST, how the network does (the machine's own IP when # it listens everywhere, else the first address that is not loopback). listening_on() { loop="" first="" lan="" for h in $(printf '%s' "$1" | tr ',' ' '); do case "$h" in 0.0.0.0 | ::) loop="127.0.0.1"; lan="${lan:-$(hostname -I 2>/dev/null | awk '{print $1}')}" ;; 127.* | localhost) loop="${loop:-$h}" ;; ::1) loop="${loop:-[::1]}" ;; *:*) first="${first:-[$h]}"; lan="${lan:-[$h]}" ;; *) first="${first:-$h}"; lan="${lan:-$h}" ;; esac done HEALTH_HOST="${loop:-${first:-127.0.0.1}}" LAN_HOST="${lan:-$HEALTH_HOST}" } HEALTH_PORT="$PORT" if [ -f "$CONFIG" ]; then listening_on "$(config_value host)" HEALTH_PORT="$(config_value port)" else listening_on "$HOST" fi HEALTH_URL="http://$HEALTH_HOST:${HEALTH_PORT:-8787}/api/health" HAS_SYSTEMD="" if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then HAS_SYSTEMD="1" fi # health: prints what is wrong and fails, or prints nothing when all is well. health() { if [ -n "$HAS_SYSTEMD" ]; then if ! systemctl is-active --quiet "$SERVICE_NAME"; then echo "the $SERVICE_NAME service is not running" return 1 fi if [ -f /etc/systemd/system/cloudmorrow-agent.service ] && ! systemctl is-active --quiet cloudmorrow-agent; then echo "the server's agent (cloudmorrow-agent) is not running" return 1 fi fi if [ ! -x "$VENV/bin/python" ]; then echo "$VENV is missing" return 1 fi "$VENV/bin/python" - "$HEALTH_URL" <<'PY' import json, sys, urllib.request try: with urllib.request.urlopen(sys.argv[1], timeout=5) as r: status = json.load(r).get("status") except Exception as exc: print(f"it does not answer on {sys.argv[1]} ({exc})") sys.exit(1) if status != "ok": print(f"{sys.argv[1]} says {status!r}") sys.exit(1) PY } # wait_healthy: a freshly restarted server takes a few seconds to answer. wait_healthy() { tries=0 while ! problem="$(health)"; do tries=$((tries + 1)) if [ "$tries" -ge 20 ]; then printf '%s' "$problem" return 1 fi sleep 1 done } # Questions are asked on the terminal, not stdin: the script itself may be # what is on stdin when it arrives through `curl | sh`. Nothing is asked at # all without a terminal or with --dry-run, so a script that gives every # answer as a flag runs straight through. TTY="" if [ -z "$DRY_RUN" ] && ( : </dev/tty ) 2>/dev/null; then TTY="1" fi if [ -z "$UPDATE" ] && [ -z "$DRY_RUN" ] && [ -f "$CONFIG" ] && [ -x "$VENV/bin/cloudmorrow-server" ]; then if problem="$(health)"; then NAME_NOW="$(config_value name)" URL_NOW="$(config_value public_url)" LOCAL_NOW="${HEALTH_URL%/api/health}" printf "\n ${GOOD}✓${OFF} %s is already here, running and answering.\n" "${NAME_NOW:-Cloudmorrow}" heading "Open it" line "${URL_NOW:-$LOCAL_NOW}/app" printf '\n' # Somebody at a terminal ran the installer again on purpose, and the # usual purpose is an update: the newest code, the packages, the unit, # the commands, all of it, with the config, files and database left as # they are. Enter says yes. Without a terminal nothing is asked, and a # server that is well is left as it is — --update says otherwise. if [ -n "$TTY" ]; then printf '\033[1m%s\033[0m [Y/n]: ' "Update it? (settings, files and accounts are kept)" >/dev/tty read -r answer </dev/tty || answer="n" case "$answer" in "" | [Yy] | [Yy][Ee][Ss]) printf '\n' >/dev/tty say "updating and reinstalling it, then checking again" ;; *) printf '\n Left as it is.\n\n' exit 0 ;; esac else heading "Nothing to do. To update it anyway" item "here" "cloudmorrow-update" item "anywhere" "cloudmorrow update server" item "all again" "... | sudo sh -s -- --update" printf '\n' exit 0 fi else warn "Cloudmorrow is installed here, but $problem" say "updating and reinstalling it, then checking again" fi fi # --- the questions --------------------------------------------------------- # Nothing is asked twice — an existing config keeps its name and address, an # existing account is left alone. ask() { # ask VAR "question" "default": sets VAR unless it already has a value. eval "current=\${$1}" [ -z "$current" ] || return 0 [ -n "$TTY" ] || return 0 if [ -n "$3" ]; then printf '\033[1m%s\033[0m [%s]: ' "$2" "$3" >/dev/tty else printf '\033[1m%s\033[0m: ' "$2" >/dev/tty fi read -r answer </dev/tty || answer="" [ -n "$answer" ] || answer="$3" eval "$1=\$answer" } read_secret() { # read_secret VAR "prompt": like ask, with the echo off. printf '\033[1m%s\033[0m: ' "$2" >/dev/tty stty -echo </dev/tty 2>/dev/null || true read -r answer </dev/tty || answer="" stty echo </dev/tty 2>/dev/null || true printf '\n' >/dev/tty eval "$1=\$answer" } ask_password() { # The server wants eight characters or more and a match, so the check is # made here, while it is still cheap to answer again. while :; do read_secret first "Password for $ACCOUNT" if [ "${#first}" -lt 8 ]; then printf 'At least eight characters, please.\n' >/dev/tty continue fi read_secret second "Repeat the password" if [ "$first" = "$second" ]; then ACCOUNT_PASSWORD="$first" return 0 fi printf 'They differ; try again.\n' >/dev/tty done } if [ -f "$CONFIG" ]; then # A re-run: the config is kept as it is, so its name and address are the # answers, whatever the terminal might say. [ -n "$CLOUD_NAME" ] || CLOUD_NAME="$(sed -n 's/^name = "\(.*\)"$/\1/p' "$CONFIG" | head -n 1)" [ -n "$PUBLIC_URL" ] || PUBLIC_URL="$(sed -n 's/^public_url = "\(.*\)"$/\1/p' "$CONFIG" | head -n 1)" fi # Somebody on this server already means the first account exists: not asked. EXISTING_USERS="0" if [ -f "$CONFIG" ] && [ -x "$VENV/bin/cloudmorrow-server" ]; then EXISTING_USERS="$(CLOUDMORROW_SERVER_CONFIG="$CONFIG" "$VENV/bin/cloudmorrow-server" user list --count 2>/dev/null || echo 0)" fi if [ -n "$TTY" ]; then printf ' A few questions, and your cloud is running.\n\n' >/dev/tty fi ask CLOUD_NAME "What is your cloud called?" "Cloudmorrow" if [ "$EXISTING_USERS" = "0" ]; then ask ACCOUNT "Username for the first account (the administrator)" "${SUDO_USER:-admin}" if [ -n "$ACCOUNT" ]; then # The same rule the server applies, so it is not learnt at the end. ACCOUNT="$(printf '%s' "$ACCOUNT" | tr 'A-Z' 'a-z')" printf '%s' "$ACCOUNT" | grep -Eq '^[a-z_][a-z0-9_-]{0,31}$' || die "a username is 1-32 lowercase letters, digits, '_' or '-', starting with a letter or '_'" if [ -z "$ACCOUNT_PASSWORD" ] && [ -n "$TTY" ]; then ask_password fi fi fi if [ -n "$TTY" ]; then printf '\n' >/dev/tty fi # --- where it goes --------------------------------------------------------- # The usual places, each on a row to change or to leave: Enter takes them # all. The form (cloudmorrow.form) is in the checkout when this runs from # one; through curl | sh nothing is installed yet, so the three files it is # made of are fetched from the repository on their own. Failing both, each # place is asked on a line. PLACES_PYTHON="" PLACES_DIR="" places_python() { if [ -n "$SCRIPT_DIR" ] && [ -f "$SCRIPT_DIR/../src/cloudmorrow/form.py" ]; then PLACES_PYTHON="env PYTHONPATH=$SCRIPT_DIR/../src $PYTHON" return 0 fi if [ -x "$VENV/bin/python" ] && "$VENV/bin/python" -c "import cloudmorrow.form" 2>/dev/null; then PLACES_PYTHON="$VENV/bin/python" return 0 fi case "$REPO" in https://github.com/*) raw="https://raw.githubusercontent.com/${REPO#https://github.com/}" ;; [email protected]:*) raw="https://raw.githubusercontent.com/${REPO#[email protected]:}" ;; *) return 1 ;; esac raw="${raw%.git}/$BRANCH/src/cloudmorrow" if command -v curl >/dev/null 2>&1; then fetch="curl -fsSL -o" elif command -v wget >/dev/null 2>&1; then fetch="wget -qO" else return 1 fi PLACES_DIR="$(mktemp -d)" mkdir -p "$PLACES_DIR/cloudmorrow" : >"$PLACES_DIR/cloudmorrow/__init__.py" for file in form.py checklist.py palette.py; do $fetch "$PLACES_DIR/cloudmorrow/$file" "$raw/$file" 2>/dev/null || return 1 done PLACES_PYTHON="env PYTHONPATH=$PLACES_DIR $PYTHON" } ask_places() { status=0 answer="$($PLACES_PYTHON -m cloudmorrow.form \ --title "Where should it go? Enter takes the usual places. Everything people keep — a folder per person, and the Shares folder — goes under Data, in files/." \ --field prefix "Code" "$DEFAULT_PREFIX" "the checkout and the virtualenv" \ --field config "Settings" "$DEFAULT_CONFIG_DIR" "server.toml and the sealing key" \ --field data "Data" "$DEFAULT_DATA_DIR" "files/, the database, keys and quills" \ --pattern '/\S*' --problem "an absolute path, please")" || status=$? case "$status" in 0) PREFIX="$(printf '%s\n' "$answer" | sed -n 's/^prefix=//p')" CONFIG_DIR="$(printf '%s\n' "$answer" | sed -n 's/^config=//p')" DATA_DIR="$(printf '%s\n' "$answer" | sed -n 's/^data=//p')" ;; 130) die "stopped. Run this again to carry on where it left off." ;; *) return 1 ;; esac } ask_places_on_lines() { printf ' Where should it go? Enter takes the usual place. Everything people keep\n' >/dev/tty printf ' — a folder per person, and the Shares folder — goes under Data, in files/.\n' >/dev/tty PREFIX="" CONFIG_DIR="" DATA_DIR="" ask PREFIX "Code, the checkout and the virtualenv" "$DEFAULT_PREFIX" ask CONFIG_DIR "Settings, server.toml and the sealing key" "$DEFAULT_CONFIG_DIR" ask DATA_DIR "Data, everyone's files, the database, keys and quills" "$DEFAULT_DATA_DIR" for place in "$PREFIX" "$CONFIG_DIR" "$DATA_DIR"; do case "$place" in /*) ;; *) die "$place is not an absolute path" ;; esac done printf '\n' >/dev/tty } if [ -n "$ASK_PLACES" ]; then if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould ask:\033[0m where things go (the usual places unless changed)\n' elif [ -n "$TTY" ]; then if places_python && ask_places; then : else ask_places_on_lines fi [ -z "$PLACES_DIR" ] || rm -rf "$PLACES_DIR" places fi fi CLOUD_NAME="${CLOUD_NAME:-Cloudmorrow}" PUBLIC_URL="$(printf '%s' "$PUBLIC_URL" | sed 's|/*$||')" # The name it announces on the home network: "The Larsens" -> the-larsens.local. LABEL="$(printf '%s' "$CLOUD_NAME" | tr 'A-Z' 'a-z' | sed -e 's/[^a-z0-9-]\{1,\}/-/g' -e 's/-\{2,\}/-/g' -e 's/^-*//' -e 's/-*$//' | cut -c1-63)" [ -n "$LABEL" ] || LABEL="cloudmorrow" case "$PUBLIC_URL" in http://*) warn "$PUBLIC_URL is plain http: every client will have to allow that" warn "(cloudmorrow config set allow_insecure_http true). Put TLS in front when you can." ;; esac if [ -n "$DRY_RUN" ]; then [ -n "$ACCOUNT" ] || [ "$EXISTING_USERS" != "0" ] || warn "would ask for the first account's username and password" fi say "python: $($PYTHON -V 2>&1)" say "cloud: $CLOUD_NAME${PUBLIC_URL:+ at $PUBLIC_URL}" # --- service user ---------------------------------------------------------- if id "$SERVICE_USER" >/dev/null 2>&1; then say "service user $SERVICE_USER already exists" else say "creating system user $SERVICE_USER" run useradd --system --home-dir "$DATA_DIR" --create-home \ --shell /usr/sbin/nologin "$SERVICE_USER" fi # --- directories ----------------------------------------------------------- SHARES_OUTSIDE="$(shares_outside)" say "directories: $PREFIX, $CONFIG_DIR, $DATA_DIR, $FILES_DIR${SHARES_OUTSIDE:+, $SHARES_OUTSIDE}" run mkdir -p "$PREFIX" "$DATA_DIR" "$FILES_DIR" "$CONFIG_DIR" ${SHARES_OUTSIDE:+"$SHARES_OUTSIDE"} run chown -R "$SERVICE_USER:$SERVICE_USER" "$PREFIX" "$DATA_DIR" "$FILES_DIR" ${SHARES_OUTSIDE:+"$SHARES_OUTSIDE"} # --- deploy key, for a private repo over ssh ------------------------------- # GitHub deploy keys are the least-privilege way to let one machine pull one # private repo: read-only, revocable, and no account token on disk. SSH_DIR="$DATA_DIR/.ssh" case "$REPO" in git@* | ssh://*) NEEDS_KEY="1" ;; *) NEEDS_KEY="" ;; esac if [ -n "$NO_SSH_KEY" ]; then NEEDS_KEY="" fi if [ -n "$NEEDS_KEY" ]; then [ -n "$SSH_KEY" ] || SSH_KEY="$SSH_DIR/id_ed25519" run mkdir -p "$SSH_DIR" run chown "$SERVICE_USER:$SERVICE_USER" "$SSH_DIR" run chmod 700 "$SSH_DIR" # Trust the forge's host key, or every non-interactive git call hangs. REPO_HOST="$(printf '%s' "$REPO" | sed -e 's|^ssh://||' -e 's|^[^@]*@||' -e 's|[:/].*$||')" if [ -n "$REPO_HOST" ] && [ ! -f "$SSH_DIR/known_hosts" ]; then say "recording the host key for $REPO_HOST" if [ -z "$DRY_RUN" ]; then ssh-keyscan -t rsa,ecdsa,ed25519 "$REPO_HOST" >"$SSH_DIR/known_hosts" 2>/dev/null chown "$SERVICE_USER:$SERVICE_USER" "$SSH_DIR/known_hosts" chmod 644 "$SSH_DIR/known_hosts" else printf ' \033[2mwould run:\033[0m ssh-keyscan %s > %s\n' "$REPO_HOST" "$SSH_DIR/known_hosts" fi fi GENERATED="" if [ ! -f "$SSH_KEY" ]; then say "generating a deploy key at $SSH_KEY" run sudo -u "$SERVICE_USER" ssh-keygen -q -t ed25519 -N "" \ -C "cloudmorrow@$(hostname)" -f "$SSH_KEY" GENERATED="1" else say "using the existing key at $SSH_KEY" fi GIT_SSH="ssh -i $SSH_KEY -o IdentitiesOnly=yes -o UserKnownHostsFile=$SSH_DIR/known_hosts" export GIT_SSH_COMMAND="$GIT_SSH" if [ -n "$GENERATED" ] && [ -z "$DRY_RUN" ]; then PLACE_FLAGS="$(place_flags)" cat <<EOF This machine has no access to $REPO yet. Add its new public key to the repository as a read-only deploy key (GitHub: Settings -> Deploy keys -> Add deploy key), then run this script again — it will pick up where it left off. ${PLACE_FLAGS:+ Give it the same places:$PLACE_FLAGS } EOF printf ' ' cat "$SSH_KEY.pub" printf '\n' exit 0 fi fi # --- checkout -------------------------------------------------------------- # sudo scrubs the environment, so the ssh command is passed through explicitly # and then stored in the repo itself, where every later git call finds it — # including `cloudmorrow-server update`, whoever ends up running it. as_service() { if [ -n "${GIT_SSH:-}" ]; then run sudo -u "$SERVICE_USER" -H env GIT_SSH_COMMAND="$GIT_SSH" "$@" else run sudo -u "$SERVICE_USER" -H "$@" fi } if [ -d "$SRC/.git" ]; then say "updating existing checkout at $SRC" as_service git -C "$SRC" fetch --quiet origin "$BRANCH" as_service git -C "$SRC" checkout --quiet "$BRANCH" # reset, not merge: this checkout is a mirror of the branch, so it follows # the branch wherever it went — including back, or onto a rewritten history. as_service git -C "$SRC" reset --hard --quiet "origin/$BRANCH" else say "cloning $REPO ($BRANCH) into $SRC" as_service git clone --quiet --branch "$BRANCH" "$REPO" "$SRC" fi if [ -n "${GIT_SSH:-}" ]; then run sudo -u "$SERVICE_USER" -H git -C "$SRC" config core.sshCommand "$GIT_SSH" fi # --- virtualenv ------------------------------------------------------------ # A venv without pip is what a run that stopped halfway leaves behind (the # venv module could make the directory but not bootstrap pip). It holds # nothing but installed packages, so it is made again. if [ -x "$VENV/bin/python" ] && ! "$VENV/bin/python" -m pip --version >/dev/null 2>&1; then say "the virtualenv at $VENV has no pip; making it again" run rm -rf "$VENV" fi if [ ! -x "$VENV/bin/python" ]; then say "creating the virtualenv at $VENV" run sudo -u "$SERVICE_USER" "$PYTHON" -m venv "$VENV" fi # Three extras: the server runs an agent of its own, so it needs what an # agent needs as well as what the API needs; and the terminal app is here # too, for `cm` at the box. say "installing cloudmorrow[server,agent,tui] (editable, so updates are a git pull)" run sudo -u "$SERVICE_USER" "$VENV/bin/python" -m pip install --quiet --upgrade pip run sudo -u "$SERVICE_USER" "$VENV/bin/python" -m pip install --quiet --editable "$SRC[server,agent,tui]" run ln -sf "$VENV/bin/cloudmorrow-server" /usr/local/bin/cloudmorrow-server # The terminal app, on the server as on any other machine: `cm` (or # `cloudmorrow`) signs in with a name and a password and has Administration. # It needs no address: with no config of their own, a client on this box # reads where the server listens off server.toml (cloudmorrow/localserver.py) # and reaches it there, whichever addresses were chosen above. A `cm` on # this machine that is not ours is left where it is. link_command() { if [ -e "/usr/local/bin/$1" ] || [ -L "/usr/local/bin/$1" ]; then case "$(readlink "/usr/local/bin/$1" 2>/dev/null)" in "$VENV/bin/$1") ;; *) warn "/usr/local/bin/$1 is not ours, so it stays; the terminal app here is $VENV/bin/$1"; return 0 ;; esac fi run ln -sf "$VENV/bin/$1" "/usr/local/bin/$1" } say "linking cm and cloudmorrow, the terminal app, into /usr/local/bin" link_command cm link_command cloudmorrow # --- where it answers ----------------------------------------------------- # Asked once the software is in, because the question is a list of boxes to # tick that the checkout brings (cloudmorrow.checklist). A config that # exists already has its answer, and so does --host. # interfaces: "name=address" for each IPv4 address on this machine but # loopback, which the list offers on a line of its own. interfaces() { if command -v ip >/dev/null 2>&1; then ip -o -4 addr show 2>/dev/null | awk '$2 != "lo" { sub("/.*", "", $4); print $2 "=" $4 }' else for address in $(hostname -I 2>/dev/null); do case "$address" in *:*) ;; *) echo "=$address" ;; esac done fi } ask_addresses() { set -- --title "Which addresses should it answer on?" --need-one \ --item 0.0.0.0 "Every address" "all networks, now and later" --on 0.0.0.0 --alone 0.0.0.0 \ --item 127.0.0.1 "This machine only" "for a proxy on this box" for pair in $(interfaces); do set -- "$@" --item "${pair#*=}" "${pair#*=}" "${pair%%=*}" done set -- "$@" --other "Other" --other-hint "type an address or a host name" \ --other-pattern '[A-Za-z0-9._:-]+' status=0 chosen="$("$VENV/bin/python" -m cloudmorrow.checklist "$@")" || status=$? case "$status" in 0) HOST="$(printf '%s\n' "$chosen" | paste -sd, - | sed 's/,/, /g')" ;; 130) die "stopped. Run this again to carry on where it left off." ;; *) warn "could not ask, so it answers on every address (--host chooses)" ;; esac } if [ -z "$HOST_GIVEN" ] && [ ! -f "$CONFIG" ]; then if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould ask:\033[0m which addresses to answer on\n' elif [ -n "$TTY" ]; then ask_addresses fi fi if [ ! -f "$CONFIG" ]; then listening_on "$HOST" HEALTH_URL="http://$HEALTH_HOST:$PORT/api/health" fi # --- the database ---------------------------------------------------------- # Asked once the software is in, like the addresses; a config that exists # has its answer. Built in is a file in the data directory, which is right # for most. A container runs PostgreSQL beside the server with docker or # podman, its password made once and kept beside the sealing key, where # only the service user reads it, and handed to the driver as a passfile so # server.toml holds no secret. A URL is a PostgreSQL server of your own, # reached once before anything is written. Chosen here and not changed # after: the records do not move between databases. PG_CONTAINER="cloudmorrow-postgres" PG_IMAGE="postgres:16" PG_PORT="5432" PGPASS="$CONFIG_DIR/postgres.pgpass" DUMP_DIR="$DATA_DIR/postgres" DATABASE_URL="" CONTAINER_ENGINE="" CONTAINER_UNIT="" hide_password() { printf '%s' "$1" | sed 's|://[^@/]*@|://…@|'; } ask_database() { set -- --title "Which database should it keep its records in?" --need-one \ --item builtin "Built in" "a file in $DATA_DIR; right for most" --on builtin --alone builtin \ --item container "PostgreSQL in a container" "beside the server, with docker or podman" --alone container \ --other "A PostgreSQL server you have" --other-hint "postgresql://user:password@host:5432/cloudmorrow" \ --other-pattern 'postgres(ql)?://[^ ]+' status=0 chosen="$("$VENV/bin/python" -m cloudmorrow.checklist "$@")" || status=$? case "$status" in 0) DATABASE="$(printf '%s\n' "$chosen" | head -n 1)" ;; 130) die "stopped. Run this again to carry on where it left off." ;; *) warn "could not ask, so the records go in the built-in database (--database chooses)" ;; esac } container_engine() { for candidate in docker podman; do if command -v "$candidate" >/dev/null 2>&1; then CONTAINER_ENGINE="$candidate" return 0 fi done return 1 } database_container() { if ! container_engine; then [ -n "$DRY_RUN" ] || die "a PostgreSQL container needs docker or podman on this machine: install one, or choose --database builtin" warn "this machine has neither docker nor podman, which the container needs" CONTAINER_ENGINE="docker" fi [ "$CONTAINER_ENGINE" != "docker" ] || CONTAINER_UNIT="docker.service" if [ -f "$PGPASS" ]; then say "keeping the database password at $PGPASS" PG_PORT="$(cut -d: -f2 "$PGPASS" | head -n 1)" password="$(cut -d: -f5 "$PGPASS" | head -n 1)" else # A port nothing else answers on: a PostgreSQL of the machine's own # would be on 5432 already. while ss -ltn 2>/dev/null | grep -q ":$PG_PORT "; do PG_PORT=$((PG_PORT + 1)) done say "writing the database password to $PGPASS" if [ -n "$DRY_RUN" ]; then password="(generated)" else password="$(head -c 24 /dev/urandom | base64 | tr -d '/+=\n')" (umask 077 && printf '127.0.0.1:%s:cloudmorrow:cloudmorrow:%s\n' "$PG_PORT" "$password" >"$PGPASS") fi run chown "$SERVICE_USER:$SERVICE_USER" "$PGPASS" run chmod 600 "$PGPASS" fi if "$CONTAINER_ENGINE" container inspect "$PG_CONTAINER" >/dev/null 2>&1; then say "starting the container $PG_CONTAINER, which is there already" run "$CONTAINER_ENGINE" start "$PG_CONTAINER" else say "running PostgreSQL as the container $PG_CONTAINER ($PG_IMAGE), on 127.0.0.1:$PG_PORT" run "$CONTAINER_ENGINE" run -d --name "$PG_CONTAINER" --restart unless-stopped \ -p "127.0.0.1:$PG_PORT:5432" \ -e POSTGRES_USER=cloudmorrow -e "POSTGRES_PASSWORD=$password" -e POSTGRES_DB=cloudmorrow \ -v "$PG_CONTAINER:/var/lib/postgresql/data" "$PG_IMAGE" fi password="" if [ -z "$DRY_RUN" ]; then tries=0 until "$CONTAINER_ENGINE" exec "$PG_CONTAINER" pg_isready -U cloudmorrow -q 2>/dev/null; do tries=$((tries + 1)) [ "$tries" -lt 60 ] || die "the database container did not answer within a minute: $CONTAINER_ENGINE logs $PG_CONTAINER" sleep 1 done fi DATABASE_URL="postgresql://[email protected]:$PG_PORT/cloudmorrow?passfile=$PGPASS" } check_database() { if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould check:\033[0m the database at %s\n' "$(hide_password "$1")" return 0 fi say "checking the database at $(hide_password "$1")" sudo -u "$SERVICE_USER" -H "$VENV/bin/python" -c 'import sys, psycopg psycopg.connect(sys.argv[1], connect_timeout=10).close()' "$1" || die "cannot reach that database: check the URL, the password, and who may connect to the server (pg_hba.conf)" } if [ -f "$CONFIG" ]; then DATABASE_URL="$(config_value database_url)" case "$DATABASE_URL" in *"passfile=$PGPASS"*) DATABASE="container" ;; "") DATABASE="builtin" ;; *) DATABASE="$DATABASE_URL" ;; esac elif [ -z "$DATABASE" ]; then if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould ask:\033[0m which database to keep its records in\n' elif [ -n "$TTY" ]; then ask_database fi [ -n "$DATABASE" ] || DATABASE="builtin" fi case "$DATABASE" in builtin) ;; container | postgresql://* | postgres://*) # The driver, on top of what is installed already. say "installing the PostgreSQL driver (cloudmorrow[postgres])" run sudo -u "$SERVICE_USER" "$VENV/bin/python" -m pip install --quiet --editable "$SRC[server,agent,tui,postgres]" ;; *) die "--database is builtin, container, or a postgresql:// URL, not \"$DATABASE\"" ;; esac case "$DATABASE" in container) database_container ;; postgresql://* | postgres://*) DATABASE_URL="$DATABASE" [ -f "$CONFIG" ] || check_database "$DATABASE_URL" ;; esac # --- configuration --------------------------------------------------------- if [ -f "$CONFIG" ]; then say "keeping the existing $CONFIG" else say "writing $CONFIG" if [ -n "$SHARES_DIR" ]; then SHARES_LINE="shares_dir = \"$SHARES_DIR\"" else SHARES_LINE="# shares_dir = \"/srv/shares\"" fi TRUSTED_PROXIES_LINE="$(printf '%s' "$TRUSTED_PROXIES" | tr ',' '\n' | sed -e 's/^ *//' -e 's/ *$//' -e '/^$/d' -e 's/.*/"&"/' | paste -sd, - | sed 's/,/, /g')" if [ -n "$DATABASE_URL" ]; then DATABASE_LINE="database_url = \"$DATABASE_URL\"" else DATABASE_LINE="# database_url = \"postgresql://[email protected]:5432/cloudmorrow?passfile=$PGPASS\"" fi write_file "$CONFIG" 0644 <<EOF # Written by install-server.sh. Safe to edit; the installer never rewrites it. [server] # What this cloud is called: on the sign-in screen, the install page and # the phone's home screen. name = "$CLOUD_NAME" # Everyone's files: a folder per person, and the Shares folder. (The key # is called notes_dir from when notes were all that was kept in it.) notes_dir = "$FILES_DIR" # The keys, the quills, and the database when it is the built-in one. data_dir = "$DATA_DIR" per_user_dirs = true # Which database holds the records and accounts: blank, the built-in one, # a file in data_dir; or a PostgreSQL server by URL, with the password in # it or in a passfile it names. Chosen at install; the records do not move # between databases afterwards. $DATABASE_LINE # The Shares folder, for files shared with every machine: Shares among # the files, unless this says otherwise. A folder outside the directories # above has to be in the service's ReadWritePaths as well: after moving # it, run the installer again with --update. $SHARES_LINE host = "$HOST" port = $PORT # The key everything is sealed with at rest. Kept here, away from the data # in $DATA_DIR, so a copy of that directory alone opens nothing. Back it up # with the data, never instead of it: lose it and everything is gone. key_file = "$KEY_FILE" # The URL clients reach this server on, baked into the install page: your # own domain, behind your own reverse proxy. Empty, the page uses the # address it was opened at. public_url = "$PUBLIC_URL" # The reverse proxy, when it is another machine: its address or CIDR range, # so the server believes the proxy's X-Forwarded-Proto. That is what makes # a request https: without it, a proxy on another box reaches the server # as plain http, refused with 426 once public_url is https. A proxy on this # machine (127.0.0.1) is believed without being named. trusted_proxies = [$TRUSTED_PROXIES_LINE] token_ttl_hours = 720 cors_origins = [] EOF run chown "$SERVICE_USER:$SERVICE_USER" "$CONFIG" fi # The sealing key. The service cannot write to /etc (ProtectSystem=strict), # so it is made here, once, and never touched again by this script. if [ -f "$KEY_FILE" ]; then say "keeping the existing key at $KEY_FILE" else say "generating the sealing key at $KEY_FILE" if [ -z "$DRY_RUN" ]; then (umask 077 && head -c 32 /dev/urandom | base64 | tr '+/' '-_' >"$KEY_FILE") fi run chown "$SERVICE_USER:$SERVICE_USER" "$KEY_FILE" run chmod 600 "$KEY_FILE" fi # The client is not on PyPI, so this server is where # machines get Cloudmorrow from. Build the wheel /install.sh will hand out. say "publishing a client wheel for /install.sh" run sudo -u "$SERVICE_USER" -H env CLOUDMORROW_SERVER_CONFIG="$CONFIG" \ "$VENV/bin/cloudmorrow-server" publish --source "$SRC" # --- systemd --------------------------------------------------------------- UNIT="/etc/systemd/system/$SERVICE_NAME.service" say "writing $UNIT" write_file "$UNIT" 0644 <<EOF [Unit] Description=Cloudmorrow API After=network-online.target${CONTAINER_UNIT:+ $CONTAINER_UNIT} Wants=network-online.target [Service] Type=simple User=$SERVICE_USER Group=$SERVICE_USER Environment=CLOUDMORROW_SERVER_CONFIG=$CONFIG Environment=HOME=$DATA_DIR ExecStart=$VENV/bin/cloudmorrow-server serve # always, not on-failure: an API deploy restarts the server by stopping it and # letting systemd start it again on the new code. An explicit \`systemctl stop\` # still stops it — systemd knows the difference. Restart=always RestartSec=3 NoNewPrivileges=true PrivateTmp=true ProtectSystem=strict ProtectHome=true ReadWritePaths=$FILES_DIR $DATA_DIR $PREFIX${SHARES_OUTSIDE:+ $SHARES_OUTSIDE} [Install] WantedBy=multi-user.target EOF # --- a daily dump of the database, when it is the container's --------------- # Into the data directory, so whatever backs that up — the server's own # agent, rsync, a snapshot — carries the database with it, as it does the # built-in one. The dump is ciphertext without the key, like the rest. if [ "$DATABASE" = "container" ]; then say "a daily dump of the database goes into $DUMP_DIR (cloudmorrow-pgdump.timer)" run mkdir -p "$DUMP_DIR" run chown "$SERVICE_USER:$SERVICE_USER" "$DUMP_DIR" write_file /etc/systemd/system/cloudmorrow-pgdump.service 0644 <<EOF [Unit] Description=A dump of Cloudmorrow's database, into its data directory ${CONTAINER_UNIT:+After=$CONTAINER_UNIT} [Service] Type=oneshot ExecStart=/bin/sh -c '$CONTAINER_ENGINE exec $PG_CONTAINER pg_dump -U cloudmorrow -Fc cloudmorrow >$DUMP_DIR/cloudmorrow.dump.new && mv $DUMP_DIR/cloudmorrow.dump.new $DUMP_DIR/cloudmorrow.dump && chown $SERVICE_USER:$SERVICE_USER $DUMP_DIR/cloudmorrow.dump' EOF write_file /etc/systemd/system/cloudmorrow-pgdump.timer 0644 <<EOF [Unit] Description=Dump Cloudmorrow's database every day [Timer] OnCalendar=daily RandomizedDelaySec=1h Persistent=true [Install] WantedBy=timers.target EOF fi # --- the updater your user runs ------------------------------------------- LN="$(command -v ln || echo /bin/ln)" say "installing /usr/local/bin/cloudmorrow-update" write_file /usr/local/bin/cloudmorrow-update 0755 <<EOF #!/bin/sh # Pull the latest Cloudmorrow and restart the server. Installed by install-server.sh. # # The pull runs as $SERVICE_USER, which owns the checkout; only the restart # needs root. Exit 3 from the update means there was nothing to pull, so the # service is left alone. set -eu status=0 sudo -u $SERVICE_USER -H $VENV/bin/cloudmorrow-server update --no-restart --exit-status "\$@" \ || status=\$? [ "\$status" = 0 ] || [ "\$status" = 3 ] || exit "\$status" # The terminal app beside cloudmorrow-server on the PATH. The update above # runs as $SERVICE_USER, which cannot write /usr/local/bin, so whatever it # brought to the venv and could not link is linked here, as root: the # sudoers rule allows exactly these two commands. A cm that is not ours # is left alone. for name in cm cloudmorrow; do if [ -x "$VENV/bin/\$name" ] && [ ! -e "/usr/local/bin/\$name" ] && [ ! -L "/usr/local/bin/\$name" ]; then if sudo -n $LN -s $VENV/bin/\$name /usr/local/bin/\$name 2>/dev/null; then printf 'linked /usr/local/bin/%s\\n' "\$name" else printf 'note: could not link %s onto the PATH; as root: ln -s %s /usr/local/bin/%s\\n' "\$name" "$VENV/bin/\$name" "\$name" fi fi done if [ "\$status" = 3 ]; then exit 0 fi if [ -d /run/systemd/system ]; then sudo systemctl restart $SERVICE_NAME # No sudo: reading a unit's state needs no privilege, and the sudoers rule # matches arguments exactly — with the flags below it would not match, so # sudo would fall through to asking for a password and fail the whole run. systemctl --no-pager --lines=0 status $SERVICE_NAME else printf "note: systemd is not running here; restart the server yourself\\n" fi EOF # --- let the admin user update without a password -------------------------- if [ -n "$ADMIN_USER" ] && id "$ADMIN_USER" >/dev/null 2>&1; then SYSTEMCTL="$(command -v systemctl || echo /usr/bin/systemctl)" SUDOERS="/etc/sudoers.d/cloudmorrow" say "granting $ADMIN_USER the commands cloudmorrow-update needs" write_file "$SUDOERS" 0440 <<EOF # Installed by install-server.sh. Exactly what cloudmorrow-update needs, nothing more. $ADMIN_USER ALL=($SERVICE_USER) NOPASSWD: $VENV/bin/cloudmorrow-server update * $ADMIN_USER ALL=(root) NOPASSWD: $SYSTEMCTL restart $SERVICE_NAME, \\ $SYSTEMCTL start $SERVICE_NAME, $SYSTEMCTL stop $SERVICE_NAME, \\ $LN -s $VENV/bin/cm /usr/local/bin/cm, \\ $LN -s $VENV/bin/cloudmorrow /usr/local/bin/cloudmorrow EOF if [ -z "$DRY_RUN" ] && command -v visudo >/dev/null 2>&1; then visudo -cf "$SUDOERS" >/dev/null || die "the sudoers drop-in did not validate" fi else warn "no --admin user, so nobody but root can run cloudmorrow-update" fi # --- the standard quills -------------------------------------------------- # Chosen before the service first starts, because a running server reads # what is installed when it boots. Every Quill Cloudmorrow makes is offered, # with Files, Calendar, Notes and Tasks ticked; without a terminal those # four are what the server gets. Asked once: a re-run keeps the choice, and # an administrator changes it from Administration afterwards. Quills from # the catalog are downloaded from GitHub; one that cannot be reached is # said, and the rest of the install goes on. if [ -n "$DRY_RUN" ]; then if [ -n "$QUILLS" ]; then printf ' \033[2mwould choose:\033[0m the standard quills %s\n' "$QUILLS" else printf ' \033[2mwould ask:\033[0m which standard quills to have\n' fi else CHOOSE="--once" if [ -n "$QUILLS" ]; then CHOOSE="$CHOOSE --only $QUILLS" elif [ -n "$TTY" ]; then CHOOSE="$CHOOSE --ask" fi # The options are ours, and split on purpose. # shellcheck disable=SC2086 sudo -u "$SERVICE_USER" -H env CLOUDMORROW_SERVER_CONFIG="$CONFIG" \ "$VENV/bin/cloudmorrow-server" quill choose $CHOOSE || warn "the standard quills were not all installed; add them later from Administration, Quills" fi # --- start it -------------------------------------------------------------- if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then run systemctl daemon-reload run systemctl enable --quiet "$SERVICE_NAME" run systemctl restart "$SERVICE_NAME" [ "$DATABASE" != "container" ] || run systemctl enable --quiet --now cloudmorrow-pgdump.timer if [ -n "$DRY_RUN" ]; then : elif problem="$(wait_healthy)"; then say "$SERVICE_NAME is enabled, running and answering" else warn "$SERVICE_NAME was started, but $problem" warn "to see why: journalctl -u $SERVICE_NAME -n 50" fi else warn "systemd is not running here; start the server yourself:" printf ' %s serve\n' "$VENV/bin/cloudmorrow-server" fi # --- the first account ----------------------------------------------------- # Made straight against the database, as the service user, so it is sealed # under the service's key. The first account on a server is its # administrator; the password goes in on stdin, never on the command line. ACCOUNT_MADE="" if [ "$EXISTING_USERS" != "0" ]; then say "this server already has $EXISTING_USERS account(s); none made" elif [ -z "$ACCOUNT" ]; then warn "no first account made — nobody was there to ask" elif [ -z "$ACCOUNT_PASSWORD" ]; then warn "no password for $ACCOUNT — set CLOUDMORROW_ADMIN_PASSWORD or run this on a terminal" elif [ -n "$DRY_RUN" ]; then printf ' \033[2mwould create:\033[0m the account %s, as administrator\n' "$ACCOUNT" else say "creating the account $ACCOUNT (administrator)" printf '%s\n' "$ACCOUNT_PASSWORD" | sudo -u "$SERVICE_USER" -H \ env CLOUDMORROW_SERVER_CONFIG="$CONFIG" \ "$VENV/bin/cloudmorrow-server" user create "$ACCOUNT" --admin --password-stdin ACCOUNT_MADE="1" fi ACCOUNT_PASSWORD="" # --- the server's own agent ------------------------------------------------ # Every other machine enrols by signing in, and nobody signs in here. It # belongs to the first administrator, which is why it comes after the account. AGENT_INSTALLED="" if [ -z "$DRY_RUN" ] && "$VENV/bin/cloudmorrow-server" agent-install \ --config "$CONFIG" --run-as "$SERVICE_USER" \ --url "${PUBLIC_URL:-http://$HEALTH_HOST:$PORT}" >/tmp/cloudmorrow-agent-install.$$ 2>&1; then AGENT_INSTALLED="1" say "the server has an agent of its own (cloudmorrow-agent.service)" sed 's/^/ /' /tmp/cloudmorrow-agent-install.$$ elif [ -z "$DRY_RUN" ]; then warn "no agent on the server yet — it needs an account to belong to" fi rm -f /tmp/cloudmorrow-agent-install.$$ # --- what to do next ------------------------------------------------------- # Headings with short lines under them, never columns: a narrow terminal # wraps a long line wherever it likes, and columns lined up past its edge # read as noise. PUBLIC_HOST="$(printf '%s' "$PUBLIC_URL" | sed -e 's|^[a-z]*://||' -e 's|[:/].*$||')" ADDRESS="${PUBLIC_URL:-http://$LAN_HOST:$PORT}" LISTENING="$HOST" if [ -f "$CONFIG" ] && [ -z "$DRY_RUN" ]; then LISTENING="$(config_value host)" fi if [ -n "$DRY_RUN" ]; then printf '\n %s would be installed.\n' "$CLOUD_NAME" else printf "\n ${GOOD}✓${OFF} %s is installed.\n" "$CLOUD_NAME" fi case "$PUBLIC_URL" in https://*) heading "First, a proxy in front, for TLS" line "With Caddy, this is the whole file:" printf '\n %s {\n reverse_proxy %s:%s\n }\n' "$PUBLIC_HOST" "$HEALTH_HOST" "$PORT" if [ -z "$TRUSTED_PROXIES" ]; then line "On another machine, name it: trusted_proxies" line "in the settings, or --trusted-proxy here." fi ;; esac heading "Open it in a browser" line "$ADDRESS" if [ -z "$PUBLIC_URL" ]; then line "or http://$LABEL.local:$PORT, at home" fi if [ -n "$ACCOUNT_MADE" ]; then line "and sign in as $ACCOUNT" fi heading "Or here, in this terminal" line "cm" if [ -n "$ACCOUNT_MADE" ]; then line "sign in as $ACCOUNT; Administration" line "is on the top bar (f9)" else line "sign in as an administrator;" line "Administration is on the top bar (f9)" fi if [ -z "$PUBLIC_URL" ]; then heading "From outside your network" line "That is yours to set up: a" line "domain and a reverse proxy" line "(public_url in the settings)," line "or a VPN such as Tailscale." fi if [ "$EXISTING_USERS" = "0" ] && [ -z "$ACCOUNT_MADE" ] && [ -z "$DRY_RUN" ]; then heading "Make the first account, the administrator" line "sudo -u $SERVICE_USER \\" line " $VENV/bin/cloudmorrow-server \\" line " user create alice" fi heading "Put it on your computers" line "curl -fsSL $ADDRESS/install.sh | sh" heading "Put it on your phone" line "Open $ADDRESS/app" line "and add it to the home screen." heading "It answers on" for h in $(printf '%s' "$LISTENING" | tr ',' ' '); do case "$h" in 0.0.0.0 | ::) line "every address, port $PORT" ;; *) line "$h, port $PORT" ;; esac done heading "Where things are" item "settings" "$CONFIG" item "data" "$DATA_DIR" case "$DATABASE" in container) item "database" "PostgreSQL in the container $PG_CONTAINER; a dump each day in $DUMP_DIR" ;; postgresql://* | postgres://*) item "database" "$(hide_password "$DATABASE_URL")" ;; *) item "database" "built in, $DATA_DIR/cloudmorrow.db" ;; esac item "files" "$FILES_DIR" item "shares" "${SHARES_DIR:-$FILES_DIR/Shares}" item "code" "$SRC ($BRANCH)" heading "To update it later" item "anywhere" "cloudmorrow update server" item "here" "cloudmorrow-update" if [ -z "$AGENT_INSTALLED" ] && [ -z "$DRY_RUN" ]; then heading "Once there is an account, give the server an agent" line "sudo $VENV/bin/cloudmorrow-server \\" line " agent-install --run-as $SERVICE_USER" fi printf '\n'It asks what your cloud is called, the address people will use (like
https://cloud.example.com), a username and password for your administrator account, and which standard quills to start with.CalendarYour own calendar, the ones you share, one for everybody, and the ones you subscribe to from elsewhere. Offered, ticked CellsSmall spreadsheets: cells, numbers and formulas, worked out the same on the phone, the web, the terminal and the command line. Offered, ticked ChannelsChannels, threads, mentions and reactions: a team's conversations, on your own server. Offered, ticked ChessChess against the people on your cloud, or against Morrow, who gets better each time you beat him — with no limit, 10 minutes or 5 minutes on the clock. Offered, ticked ClipperA video editor for the videos and photos in your drive: drag clips and photos onto video and audio tracks, cut, trim and arrange them, put one small over another, blur what should not be seen, crop, and export. Offered, ticked CRMCustomers for a company: organisations, people, a pipeline of deals, and what to do next. Offered, ticked DevProjectYour software projects: the repository, notes, a board, and the .env secrets on every machine. Offered, ticked DocsDocuments: a basic Word — headings, lists, tables and styles — over .docs files and Word's own, on the phone, the web, the terminal and the command line. Offered, ticked FilesYour own drive on the server, your fileshares, and what is in them. Offered, ticked MailEmail: an inbox for everybody on your server, name@your-cloud between you, and name@your-own-domain to the world through Cloudflare, set up once by an administrator. Offered, ticked MediaEvery photo and video in your drive, as pictures: filter them and give them stars. Offered, ticked MessagesConversations with the people on your cloud, with one person or several, with reactions: your messages, kept on your own cloud. Offered, ticked Morrow AIYour own AI assistant, on your own cloud: a chat with Morrow about anything you can see here, and Quills of your own built when you ask. An administrator connects Claude once, for everybody. Offered, ticked NotesMarkdown notes in folders, with pictures: plain files in the Notes folder of your drive. Offered, ticked RetouchA simple photo editor for the photos in your drive: crop, turn, resize, draw and write on them in layers, copy and paste, and save over the photo or as a new file. Offered, ticked SecretsKeys and passwords in vaults and environments, sealed, and never shown until you ask. Offered, ticked TasksBoards with three lanes: To Do, Doing, Done. Offered, tickedThese are the standard quills in the Quill Catalog today. Leave one out and you can add it any time later with
cm quill add, or from Administration → Quills. -
2
Open the address and sign in
Open your cloud's address in a browser and sign in with the administrator account you just made. Add the people who share it from Administration.
-
3
On each computer, run the command your cloud shows
In your cloud, Me → Add a device shows one command for your computers, with its own address in it:
$ curl -fsSL https://cloud.example.com/install.sh | shWhat this command runs
install.shYour cloud serves this with its own address in place of https://cloud.example.com, and the package it hands out in place of __PACKAGE_SPEC__. 214 lines, from src/cloudmorrow/server/templates/install.sh on GitHub.
#!/bin/sh # Cloudmorrow installer. Generated by https://cloud.example.com # # curl -fsSL https://cloud.example.com/install.sh | sh # curl -fsSL https://cloud.example.com/install.sh | sh -s -- --agent-token bce_xxx --allow-shell # curl -fsSL https://cloud.example.com/install.sh | sh -s -- --no-desktop # # It installs Cloudmorrow and signs you in, with your own name and password. # --no-login leaves signing in for later; --server URL is the cloud; # --dry-run says what it would do, and does none of it. # set -eu CLOUDMORROW_URL="https://cloud.example.com" PACKAGE="__PACKAGE_SPEC__" # The same, with the desktop app's dependencies: added where there is a desktop. DESKTOP_PACKAGE="__DESKTOP_PACKAGE_SPEC__" AGENT_TOKEN="${CLOUDMORROW_AGENT_TOKEN:-}" AGENT_NAME="" ALLOW_SHELL="" NO_AGENT="" NO_DESKTOP="" NO_LOGIN="" DRY_RUN="" while [ $# -gt 0 ]; do case "$1" in --agent-token) AGENT_TOKEN="$2"; shift 2 ;; --agent-name) AGENT_NAME="$2"; shift 2 ;; --allow-shell) ALLOW_SHELL="--allow-shell"; shift ;; --no-agent) NO_AGENT="1"; shift ;; --no-desktop) NO_DESKTOP="1"; shift ;; --no-login) NO_LOGIN="1"; shift ;; --dry-run) DRY_RUN="1"; shift ;; --server) CLOUDMORROW_URL="$2"; shift 2 ;; -h | --help) sed -n '2,11p' "$0" 2>/dev/null || echo "see ${CLOUDMORROW_URL}" exit 0 ;; *) echo "unknown option: $1" >&2; exit 2 ;; esac done say() { printf '\033[36m::\033[0m %s\n' "$*"; } note() { printf '\033[33mnote:\033[0m %s\n' "$*"; } die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; } run() { if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould run:\033[0m %s\n' "$*" else "$@" fi } # The released copy of this script (a GitHub Release asset) comes from no # cloud in particular: the landing page of one runs it with --server. An # address that is not one is no address. case "$CLOUDMORROW_URL" in http://* | https://*) ;; *) CLOUDMORROW_URL="" ;; esac TTY="" if ( : </dev/tty ) 2>/dev/null; then TTY="/dev/tty"; fi if [ -z "$CLOUDMORROW_URL" ]; then [ -n "$TTY" ] || die "which cloud? run it with --server https://<your cloud>" printf 'Your cloud'"'"'s address (https://...): ' >/dev/tty read -r CLOUDMORROW_URL </dev/tty || CLOUDMORROW_URL="" [ -n "$CLOUDMORROW_URL" ] || die "no address given" fi CLOUDMORROW_URL="$(printf '%s' "$CLOUDMORROW_URL" | sed 's|/*$||')" # --- where to install ------------------------------------------------------ if [ "$(id -u)" = "0" ]; then PREFIX="/opt/cloudmorrow" BINDIR="/usr/local/bin" else PREFIX="${XDG_DATA_HOME:-$HOME/.local/share}/cloudmorrow" BINDIR="$HOME/.local/bin" fi VENV="$PREFIX/venv" # --- python ---------------------------------------------------------------- PYTHON="" for candidate in python3.14 python3.13 python3.12 python3.11 python3; do if command -v "$candidate" >/dev/null 2>&1; then if "$candidate" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)' 2>/dev/null; then PYTHON="$candidate" break fi fi done [ -n "$PYTHON" ] || die "Python 3.11 or newer is required. Install it and run this again." say "using $($PYTHON -V) at $(command -v "$PYTHON")" # --- install --------------------------------------------------------------- say "installing into $VENV" run mkdir -p "$PREFIX" "$BINDIR" [ -d "$VENV" ] || run "$PYTHON" -m venv "$VENV" run "$VENV/bin/python" -m pip install --quiet --upgrade pip say "fetching $PACKAGE" # --force-reinstall because the version does not change between commits, so # plain --upgrade would decide it is already satisfied and skip the new code. run "$VENV/bin/python" -m pip install --quiet --upgrade --force-reinstall "$PACKAGE" for tool in cloudmorrow cm cloudmorrow-agent; do if [ -x "$VENV/bin/$tool" ] || [ -n "$DRY_RUN" ]; then run ln -sf "$VENV/bin/$tool" "$BINDIR/$tool" say "linked $BINDIR/$tool" fi done # --- point the CLI at this server ----------------------------------------- # The server this script came from. Without it, `cloudmorrow login` would # look on the home network and list the clouds it finds there instead. run "$BINDIR/cloudmorrow" config set api_url "$CLOUDMORROW_URL" >/dev/null say "CLI configured for $CLOUDMORROW_URL" # --- the desktop app, where there is a desktop ----------------------------- # The web app in a window of its own, which also mounts your shares on this # computer: `cloudmorrow app`. Only for a person at a graphical session — a # machine reached over ssh, a server or a root install gets the terminal app # alone, as before. The extra is installed on top of what is already there, # without --force-reinstall, so running this again does not fetch Qt again. DESKTOP="" if [ -n "$NO_DESKTOP" ]; then say "skipping the desktop app (--no-desktop)" elif [ "$(id -u)" = "0" ]; then : # a root install is for every user and none of them in particular elif [ -n "$DRY_RUN" ]; then say "would add the desktop app, if a desktop is running here" elif [ "$(uname -s)" = "Linux" ]; then if [ -n "${WAYLAND_DISPLAY:-}" ] || [ -n "${DISPLAY:-}" ]; then say "a desktop is running here: adding the desktop app (this is the big download)" if "$VENV/bin/python" -m pip install --quiet "$DESKTOP_PACKAGE" \ && "$BINDIR/cloudmorrow" app --install-launcher; then DESKTOP="1" say "Cloudmorrow is in your applications menu" else note "the desktop app did not install; the terminal app is fine" fi fi fi # TODO(macos): the extra installs on a Mac as it is (pywebview uses the # system's WKWebView there), but there is no launcher yet: that needs an .app # bundle in ~/Applications that runs `cloudmorrow app` — see # cloudmorrow/desktop/launcher.py. Until it exists a Mac gets the terminal app, # and `pip install 'cloudmorrow[desktop]'` into $VENV by hand for the window. # TODO(windows): this script does not run on Windows; that needs a # PowerShell installer that makes the venv and a Start-menu shortcut. # --- optionally enrol the agent ------------------------------------------- if [ -n "$AGENT_TOKEN" ] && [ -z "$NO_AGENT" ]; then say "enrolling the local agent" set -- --server "$CLOUDMORROW_URL" --token "$AGENT_TOKEN" [ -n "$AGENT_NAME" ] && set -- "$@" --name "$AGENT_NAME" [ -n "$ALLOW_SHELL" ] && set -- "$@" "$ALLOW_SHELL" run "$BINDIR/cloudmorrow-agent" enroll "$@" fi # --- signing in ------------------------------------------------------------ # `cloudmorrow login` asks for the name and password. Asked on the terminal, # not stdin: through `curl | sh`, stdin is this script. SIGNED_IN="" if [ -z "$NO_LOGIN" ]; then if [ -n "$DRY_RUN" ]; then run "$BINDIR/cloudmorrow" login --server "$CLOUDMORROW_URL" elif [ -n "$TTY" ]; then say "signing in to $CLOUDMORROW_URL" if "$BINDIR/cloudmorrow" login --server "$CLOUDMORROW_URL" </dev/tty; then SIGNED_IN="1" else note "not signed in; afterwards: cloudmorrow login -u <your account name on this cloud>" fi fi fi case ":$PATH:" in *":$BINDIR:"*) ;; *) note "add $BINDIR to your PATH" ;; esac cat <<EOF Installed. Next: EOF if [ -z "$SIGNED_IN" ]; then cat <<EOF cloudmorrow login sign in, then EOF fi cat <<EOF cloudmorrow open the notes TUI (cm does the same, in two letters) With no server set, \`cloudmorrow login\` lists the clouds it finds on your home network. EOF if [ -n "$DESKTOP" ]; then cat <<EOF The desktop app is in your applications menu, or: cloudmorrow app the web app in a window, mounting shares here EOF fi if [ -n "$AGENT_TOKEN" ] && [ -z "$NO_AGENT" ]; then cat <<EOF The agent is enrolled but not running yet. To keep it running: linux : cloudmorrow-agent run (see ${CLOUDMORROW_URL}/install for a systemd unit) macos : cloudmorrow-agent run EOF fiIt installs the terminal app,
cm, and on a desktop the Cloudmorrow desktop app, which also mounts your fileshares. Thencm login, and you're in. -
4
On your phone, add it to the home screen
Open the address, sign in, and choose Add to Home Screen (Safari's share menu on an iPhone, Chrome's menu on Android). It opens like any other app, with your cloud's name under the icon.
What you need
- A Linux machine with systemd and Python 3.11 or newer: a Raspberry Pi, an old laptop, a NAS or a small VPS. The installer adds git, sudo and Python's venv module if they are missing.
- 1 GB of memory is plenty. An idle cloud uses about 100 MB.
- A name it can be reached by, like
cloud.example.com, and a web server in front of it for HTTPS. Caddy is the easy choice: the installer prints its whole config. - No domain yet? A plain
http://address on your home network works too; each computer then opts in to it. - Reaching it from outside is your own setup: your domain and web server, or a VPN such as Tailscale if you use one. Cloudmorrow needs neither, and no account here.
Updating
- Your cloud:
cm update serverfrom any of your computers, or Update server under Administration → Server. It keeps your settings, data and accounts. - Something wrong? Run the install command again on the machine. If the server is running well it asks whether to update it, and Enter says yes; if not, it updates and reinstalls it, and asks nothing it already knows.
- A computer:
cm update. - Quills: when a quill has a newer release in the catalog, Administration → Quills offers the update, with what changes.
Certificates, Docker and what to back up are in the README.
Taking it off again
The install, in reverse: your phone, then your computers, then the server. One command on each machine, and each lists what it will remove and asks before it touches anything.
-
1
On your phone, remove it from the home screen
Press and hold the Cloudmorrow icon and choose Remove App on an iPhone, or Uninstall on Android. Nothing else of Cloudmorrow's is on the phone.
-
2
On each computer, run cm uninstall
$ cm uninstallIt takes the computer's agent off your cloud, stops its service, and removes the config and sign-in, the backups the agent made, the
cmcommands, the desktop app and its menu entry. Your notes, files and secrets are on the server and stay there.Add
--keep-backupsto leave the agent's backups where they are. Do this before step 3: once the server is gone, the computer cannot take its agent off it. -
3
On the server, run one command
$ curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/uninstall-server.sh | sudo shWhat this command runs
uninstall-server.shIt runs as root on the machine that is your cloud, so read it first if you like. 460 lines, from deploy/uninstall-server.sh on GitHub.
#!/bin/sh # Take the Cloudmorrow server off this machine: the reverse of install-server.sh. # # curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/uninstall-server.sh | sudo sh # # It finds what the installer made — from the systemd unit and the config, # so custom paths are found too — and shows it as a list of boxes to tick: # the services, the code, the config and sealing key, the database, the # files, the system user. Only the services are ticked to begin with; tick # what else should go, and it asks once more before it touches anything. # Without a terminal, say it with a flag: # # curl -fsSL …/uninstall-server.sh | sudo sh -s -- --remove services,code # curl -fsSL …/uninstall-server.sh | sudo sh -s -- --delete-data (all of it) # # Computers that used this cloud keep their own copy of Cloudmorrow; take it # off each of them with `cm uninstall`. set -eu PREFIX="" FILES_DIR="" DATA_DIR="" SERVICE_USER="" SERVICE_NAME="cloudmorrow" AGENT_NAME="cloudmorrow-agent" CONFIG_DIR="" REMOVE_FLAG="" DELETE_DATA="" YES="" DRY_RUN="" # Everything the installer makes, in the order it is removed. The user goes # last: it owns the directories. KINDS="services code config data files shares database user" usage() { sed -n '2,17p' "$0" cat <<EOF Options: --remove LIST what goes, comma-separated, instead of asking: services, code, config, data, files, shares, database (the PostgreSQL container and its volume, when the installer ran one), user --delete-data all of it: the software, the config, the key, the database, everyone's files, and the service user --yes do not ask; take what --remove says, or the services --dry-run print what would be removed, change nothing --prefix DIR where the checkout and venv are (default: from the unit) --files-dir DIR where everyone's files are (default: from the config) --data-dir DIR the database and keys (default: from the config) --config-dir DIR server.toml and the sealing key (default: from the unit) --service-user NAME the system user it runs as (default: from the unit) --service-name NAME the systemd service (default: $SERVICE_NAME) EOF } while [ $# -gt 0 ]; do case "$1" in --remove) REMOVE_FLAG="$2"; shift 2 ;; --delete-data | --all) DELETE_DATA="1"; shift ;; --keep-data) DELETE_DATA=""; shift ;; # once the default; still accepted --yes | -y) YES="1"; shift ;; --dry-run) DRY_RUN="1"; shift ;; --prefix) PREFIX="$2"; shift 2 ;; --files-dir | --notes-dir) FILES_DIR="$2"; shift 2 ;; --data-dir) DATA_DIR="$2"; shift 2 ;; --config-dir) CONFIG_DIR="$2"; shift 2 ;; --service-user) SERVICE_USER="$2"; shift 2 ;; --service-name) SERVICE_NAME="$2"; shift 2 ;; -h | --help) usage; exit 0 ;; *) echo "unknown option: $1" >&2; usage >&2; exit 2 ;; esac done say() { printf '\033[36m::\033[0m %s\n' "$*"; } warn() { printf '\033[33mnote:\033[0m %s\n' "$*"; } die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; } run() { if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould run:\033[0m %s\n' "$*" else "$@" fi } [ -n "$DRY_RUN" ] || [ "$(id -u)" = "0" ] || die "run this with sudo" UNIT="/etc/systemd/system/$SERVICE_NAME.service" AGENT_UNIT="/etc/systemd/system/$AGENT_NAME.service" # --- what the installer made ------------------------------------------------ # The unit says where the venv and the config are and who it runs as; the # config says where the data is. A flag wins over both, and the installer's # defaults are last. unit_value() { sed -n "s/^$1=//p" "$UNIT" 2>/dev/null | head -n 1; } if [ -z "$CONFIG_DIR" ]; then # Environment=CLOUDMORROW_SERVER_CONFIG=/etc/cloudmorrow/server.toml config_now="$(unit_value Environment=CLOUDMORROW_SERVER_CONFIG)" case "$config_now" in /*/server.toml) CONFIG_DIR="$(dirname "$config_now")" ;; *) CONFIG_DIR="/etc/cloudmorrow" ;; esac fi CONFIG="$CONFIG_DIR/server.toml" KEY_FILE="$CONFIG_DIR/cloudmorrow.key" config_value() { sed -n "s/^$1 = \"\\(.*\\)\"\$/\\1/p" "$CONFIG" 2>/dev/null | head -n 1; } if [ -z "$PREFIX" ]; then # ExecStart=/opt/cloudmorrow/venv/bin/cloudmorrow-server serve exec_start="$(unit_value ExecStart | cut -d' ' -f1)" case "$exec_start" in */venv/bin/cloudmorrow-server) PREFIX="${exec_start%/venv/bin/cloudmorrow-server}" ;; *) PREFIX="/opt/cloudmorrow" ;; esac fi [ -n "$SERVICE_USER" ] || SERVICE_USER="$(unit_value User)" [ -n "$SERVICE_USER" ] || SERVICE_USER="cloudmorrow" [ -n "$DATA_DIR" ] || DATA_DIR="$(config_value data_dir)" [ -n "$DATA_DIR" ] || DATA_DIR="/var/lib/cloudmorrow" # Everyone's files: the config's notes_dir, from when notes were all it held. [ -n "$FILES_DIR" ] || FILES_DIR="$(config_value notes_dir)" [ -n "$FILES_DIR" ] || FILES_DIR="$DATA_DIR/files" # The Shares folder is among the files unless the config moved it out; only # then is it a thing of its own to remove. SHARES_DIR="$(config_value shares_dir)" case "$SHARES_DIR" in "" | "$FILES_DIR" | "$FILES_DIR"/*) SHARES_DIR="" ;; esac # The files are under the data directory unless they were moved out; inside # it, removing the data has to leave them be unless they go too. FILES_INSIDE="" case "$FILES_DIR" in "$DATA_DIR"/*) FILES_INSIDE="1" ;; esac CLOUD_NAME="$(config_value name)" # Every directory this deletes wholesale is checked first: absolute, and not # one of the system's own. A config edited by hand to say notes_dir = "/" # must not be able to take the machine with it. check_dir() { case "$1" in /*) ;; *) die "$1 is not an absolute path; give it with a flag" ;; esac case "$(printf '%s' "$1" | sed 's|/*$||')" in "" | /bin | /boot | /dev | /etc | /home | /lib | /lib64 | /media | /mnt | /opt | /proc | /root | \ /run | /sbin | /srv | /sys | /tmp | /usr | /usr/local | /var | /var/lib | "$HOME") die "refusing to delete $1; give the right path with a flag" ;; esac } # --- the list --------------------------------------------------------------- # One row per kind of thing, only for the kinds that are there. A row is # "kind|label|detail"; the detail says what and where, so the list is the # answer to "what is on this machine?" even for somebody removing nothing. FOUND="" found() { FOUND="$FOUND$1 "; } is_found() { case "$FOUND" in *"$1|"*) return 0 ;; *) return 1 ;; esac; } services_detail="" [ -f "$UNIT" ] && services_detail="$SERVICE_NAME" [ -f "$AGENT_UNIT" ] && services_detail="${services_detail:+$services_detail, }$AGENT_NAME" commands="" [ -L /usr/local/bin/cloudmorrow-server ] && commands="cloudmorrow-server" # The terminal app's links, when they point into this server's venv: the # installer's, or an update's. One that points elsewhere is somebody else's. our_link() { [ -L "/usr/local/bin/$1" ] && [ "$(readlink "/usr/local/bin/$1")" = "$PREFIX/venv/bin/$1" ]; } for command in cm cloudmorrow cloudmorrow-agent; do our_link "$command" && commands="${commands:+$commands, }$command" done grep -qs "install-server.sh" /usr/local/bin/cloudmorrow-update && commands="${commands:+$commands, }cloudmorrow-update" grep -qs "install-server.sh" /etc/sudoers.d/cloudmorrow && commands="${commands:+$commands, }the sudo rule" if [ -n "$services_detail" ] || [ -n "$commands" ]; then found "services|Services|${services_detail:+the units $services_detail}${services_detail:+${commands:+; }}${commands:+the commands $commands}" fi [ ! -e "$PREFIX" ] || found "code|Code|$PREFIX: the checkout and the virtualenv" if [ -e "$CONFIG_DIR" ]; then key_note="" [ ! -f "$KEY_FILE" ] || key_note=" and the sealing key" found "config|Config|$CONFIG_DIR: server.toml$key_note" fi [ ! -e "$DATA_DIR" ] || found "data|Data|$DATA_DIR: the database, keys and quills${FILES_INSIDE:+ (not the files in it)}" [ ! -e "$FILES_DIR" ] || found "files|Files|$FILES_DIR: everyone's files and shares" [ -z "$SHARES_DIR" ] || [ ! -e "$SHARES_DIR" ] || found "shares|Shares|$SHARES_DIR: every fileshare" # The PostgreSQL container the installer ran, when it did: it and its volume. PG_CONTAINER="cloudmorrow-postgres" CONTAINER_ENGINE="" for candidate in docker podman; do if command -v "$candidate" >/dev/null 2>&1 && "$candidate" container inspect "$PG_CONTAINER" >/dev/null 2>&1; then CONTAINER_ENGINE="$candidate" break fi done [ -z "$CONTAINER_ENGINE" ] || found "database|Database|the PostgreSQL container $PG_CONTAINER and its volume; the daily dump is in the data" ! id "$SERVICE_USER" >/dev/null 2>&1 || found "user|User|the system user $SERVICE_USER" if [ -z "$FOUND" ]; then say "there is no Cloudmorrow server on this machine" exit 0 fi row_label() { printf '%s\n' "$FOUND" | sed -n "s/^$1|\\([^|]*\\)|.*/\\1/p"; } row_detail() { printf '%s\n' "$FOUND" | sed -n "s/^$1|[^|]*|//p"; } kinds_found() { for kind in $KINDS; do ! is_found "$kind" || printf '%s\n' "$kind" done } # --- the choice -------------------------------------------------------------- # CHOSEN is the kinds that go, space-separated. A flag decides it outright; # otherwise the list is asked on the terminal, with the services ticked. CHOSEN="" chose() { case " $CHOSEN " in *" $1 "*) return 0 ;; *) return 1 ;; esac; } if [ -n "$DELETE_DATA" ]; then CHOSEN="$(kinds_found | tr '\n' ' ')" elif [ -n "$REMOVE_FLAG" ]; then for kind in $(printf '%s' "$REMOVE_FLAG" | tr ',' ' '); do [ "$kind" != "notes" ] || kind="files" # the old name for them case " $KINDS " in *" $kind "*) ;; *) die "--remove does not know \"$kind\"; it takes: $(echo $KINDS | sed 's/ /, /g')" ;; esac ! is_found "$kind" || CHOSEN="$CHOSEN $kind" done else CHOSEN="services" fi # The list of boxes to tick is the one the installer asks with # (cloudmorrow.checklist), run from the checkout when it is still there. # Without it, or without a terminal that can draw, a numbered list does. checklist_python() { if [ -x "$PREFIX/venv/bin/python" ] && "$PREFIX/venv/bin/python" -c "import cloudmorrow.checklist" 2>/dev/null; then echo "$PREFIX/venv/bin/python" elif [ -d "$PREFIX/src" ] && PYTHONPATH="$PREFIX/src" python3 -c "import cloudmorrow.checklist" 2>/dev/null; then echo "env PYTHONPATH=$PREFIX/src python3" fi } ask_with_checklist() { python="$1" set -- --title "What should go from this machine?" for kind in $(kinds_found); do set -- "$@" --item "$kind" "$(row_label "$kind")" "$(row_detail "$kind")" done for kind in $CHOSEN; do set -- "$@" --on "$kind" done status=0 chosen="$($python -m cloudmorrow.checklist "$@")" || status=$? case "$status" in 0) CHOSEN="$(printf '%s\n' "$chosen" | tr '\n' ' ')" ;; 130) die "stopped; nothing was removed" ;; *) return 1 ;; esac } ask_with_numbers() { printf '\n \033[1mWhat should go from this machine?\033[0m\n\n' >/dev/tty n=0 for kind in $(kinds_found); do n=$((n + 1)) if chose "$kind"; then box="x"; else box=" "; fi printf ' %d [%s] %-9s %s\n' "$n" "$box" "$(row_label "$kind")" "$(row_detail "$kind")" >/dev/tty done ticked="$(n=0; for kind in $(kinds_found); do n=$((n + 1)); ! chose "$kind" || printf '%s ' "$n"; done)" printf '\n Type the numbers to remove, separated by spaces; nothing removes nothing.\n' >/dev/tty printf ' Numbers [%s]: ' "$(echo $ticked)" >/dev/tty read -r answer </dev/tty || answer="" [ -n "$answer" ] || answer="$ticked" CHOSEN="" for number in $answer; do kind="$(kinds_found | sed -n "${number}p" 2>/dev/null)" || kind="" [ -n "$kind" ] || die "there is no $number in the list; nothing was removed" CHOSEN="$CHOSEN $kind" done } if [ -n "$DRY_RUN" ] || [ -n "$YES" ] || [ -n "$DELETE_DATA" ] || [ -n "$REMOVE_FLAG" ]; then : else # On the terminal, not stdin: through `curl | sh` the script itself is stdin. ( : </dev/tty ) 2>/dev/null || die "nobody to ask; say what goes with --remove, or --yes for the services" python="$(checklist_python)" if [ -z "$python" ] || ! ask_with_checklist "$python"; then ask_with_numbers fi fi # Only what is there can go: "services" ticked on a machine without the # units is nothing to do. CHOSEN="$(for kind in $CHOSEN; do ! is_found "$kind" || printf '%s ' "$kind"; done)" # A dry run with no choice shows the whole list, so it reads as an inventory. if [ -n "$DRY_RUN" ] && [ -z "$REMOVE_FLAG" ] && [ -z "$DELETE_DATA" ]; then printf '\n On this machine%s:\n\n' "${CLOUD_NAME:+, the Cloudmorrow server \"$CLOUD_NAME\"}" for kind in $(kinds_found); do if chose "$kind"; then box="x"; else box=" "; fi printf ' [%s] %-9s %s\n' "$box" "$(row_label "$kind")" "$(row_detail "$kind")" done printf '\n Ticked is what goes without --remove.\n' fi if [ -z "$(echo $CHOSEN)" ]; then printf '\n Nothing ticked: nothing was removed.\n\n' exit 0 fi # Data is anything a later install would pick up again, and the user that # owns it. Removing it is a different question from removing software. DATA_GOES="" for kind in config data files shares database user; do ! chose "$kind" || DATA_GOES="1" done ! chose code || check_dir "$PREFIX" ! chose data || check_dir "$DATA_DIR" ! chose files || check_dir "$FILES_DIR" ! chose shares || check_dir "$SHARES_DIR" printf '\n %sThis removes from this machine:\n\n' "${CLOUD_NAME:+The Cloudmorrow server \"$CLOUD_NAME\". }" for kind in $(kinds_found); do ! chose "$kind" || printf ' - %-9s %s\n' "$(row_label "$kind")" "$(row_detail "$kind")" done kept="" for kind in $(kinds_found); do chose "$kind" || kept="${kept:+$kept, }$(row_label "$kind" | tr 'A-Z' 'a-z')" done if [ -n "$kept" ]; then printf '\n and keeps the %s, so installing again picks up where it was.\n' "$kept" fi if [ -n "$DATA_GOES" ]; then printf '\n \033[1mWhat is removed cannot be brought back: accounts, files, shares, secrets.\033[0m\n' fi printf '\n' # --- ask once more -------------------------------------------------------------- if [ -n "$DRY_RUN" ]; then : elif [ -z "$YES" ]; then ( : </dev/tty ) 2>/dev/null || die "nobody to ask; run it again with --yes" if [ -z "$DATA_GOES" ]; then printf '\033[1mGo ahead?\033[0m [y/N]: ' >/dev/tty read -r answer </dev/tty || answer="" case "$answer" in y | Y | yes) ;; *) die "nothing was removed" ;; esac else printf '\033[1mType "delete" to remove it\033[0m: ' >/dev/tty read -r answer </dev/tty || answer="" [ "$answer" = "delete" ] || die "nothing was removed" fi fi # --- the services ----------------------------------------------------------- # The agent first: it reports to the server, so it goes before the server does. if chose services; then if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then for name in "$AGENT_NAME" "$SERVICE_NAME"; do if [ -f "/etc/systemd/system/$name.service" ]; then say "stopping $name" run systemctl disable --now --quiet "$name" || warn "$name did not stop cleanly" fi done fi for unit in "$AGENT_UNIT" "$UNIT"; do [ ! -f "$unit" ] || run rm -f "$unit" done if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then run systemctl daemon-reload fi # The commands, and the rule that lets them run. say "removing the commands" [ ! -L /usr/local/bin/cloudmorrow-server ] || run rm -f /usr/local/bin/cloudmorrow-server for command in cm cloudmorrow cloudmorrow-agent; do ! our_link "$command" || run rm -f "/usr/local/bin/$command" done if grep -qs "install-server.sh" /usr/local/bin/cloudmorrow-update; then run rm -f /usr/local/bin/cloudmorrow-update fi if grep -qs "install-server.sh" /etc/sudoers.d/cloudmorrow; then run rm -f /etc/sudoers.d/cloudmorrow fi fi # --- the code ----------------------------------------------------------------- if chose code && [ -e "$PREFIX" ]; then say "removing the code at $PREFIX" run rm -rf "$PREFIX" fi # --- the data ------------------------------------------------------------------- remove_dir() { if [ -e "$1" ]; then say "removing $1" run rm -rf "$1" fi } ! chose config || remove_dir "$CONFIG_DIR" if chose data; then if [ -n "$FILES_INSIDE" ] && ! chose files && [ -e "$FILES_DIR" ]; then # Everything in the data directory but the files, which stay. say "removing $DATA_DIR, but not the files in it" for entry in "$DATA_DIR"/* "$DATA_DIR"/.[!.]* "$DATA_DIR"/..?*; do [ -e "$entry" ] || [ -L "$entry" ] || continue case "$FILES_DIR" in "$entry" | "$entry"/*) continue ;; esac run rm -rf "$entry" done else remove_dir "$DATA_DIR" fi fi if chose files; then remove_dir "$FILES_DIR" # An older install made /srv/cloudmorrow to hold notes/; empty now, it goes too. parent="$(dirname "$FILES_DIR")" if [ "$(basename "$parent")" = "cloudmorrow" ] && [ -d "$parent" ]; then run rmdir "$parent" 2>/dev/null || true fi fi ! chose shares || remove_dir "$SHARES_DIR" if chose database; then say "removing the container $PG_CONTAINER and its volume" if command -v systemctl >/dev/null 2>&1 && [ -f /etc/systemd/system/cloudmorrow-pgdump.timer ]; then run systemctl disable --now --quiet cloudmorrow-pgdump.timer || true run rm -f /etc/systemd/system/cloudmorrow-pgdump.timer /etc/systemd/system/cloudmorrow-pgdump.service fi run "$CONTAINER_ENGINE" rm -f "$PG_CONTAINER" run "$CONTAINER_ENGINE" volume rm "$PG_CONTAINER" [ ! -f "$CONFIG_DIR/postgres.pgpass" ] || run rm -f "$CONFIG_DIR/postgres.pgpass" fi if chose user; then say "removing the system user $SERVICE_USER" run userdel "$SERVICE_USER" || warn "the user $SERVICE_USER is still there; remove it with userdel" fi # --- what is left ----------------------------------------------------------------- if [ -n "$DRY_RUN" ]; then printf '\n That was a dry run: nothing was removed.\n\n' exit 0 fi printf '\n Done.\n' if chose services; then cat <<EOF Left for you, because the installer never made them: - the reverse proxy's entry for this cloud (Caddy, nginx), if you added one - the deploy key on GitHub, if you gave the repository one - Cloudmorrow on each computer that used this cloud: run cm uninstall there EOF fi if [ -n "$kept" ]; then printf '\n Kept: the %s.\n Run this again to remove them, or install again to pick up where it was.\n' "$kept" fi printf '\n'It finds what the installer made, custom paths included, and lists all of it: the services, the code, the
cloudmorrow-serverandcloudmorrow-updatecommands, the config and its key, the database, every note and file, and thecloudmorrowsystem user. Then it asks you to type delete.To take the software off but keep the data for a later install, add
--keep-data:$ curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/uninstall-server.sh | sudo sh -s -- --keep-dataWhat this command runs
uninstall-server.sh460 lines, from deploy/uninstall-server.sh on GitHub.
#!/bin/sh # Take the Cloudmorrow server off this machine: the reverse of install-server.sh. # # curl -fsSL https://raw.githubusercontent.com/Cloudmorrow/cloudmorrow/main/deploy/uninstall-server.sh | sudo sh # # It finds what the installer made — from the systemd unit and the config, # so custom paths are found too — and shows it as a list of boxes to tick: # the services, the code, the config and sealing key, the database, the # files, the system user. Only the services are ticked to begin with; tick # what else should go, and it asks once more before it touches anything. # Without a terminal, say it with a flag: # # curl -fsSL …/uninstall-server.sh | sudo sh -s -- --remove services,code # curl -fsSL …/uninstall-server.sh | sudo sh -s -- --delete-data (all of it) # # Computers that used this cloud keep their own copy of Cloudmorrow; take it # off each of them with `cm uninstall`. set -eu PREFIX="" FILES_DIR="" DATA_DIR="" SERVICE_USER="" SERVICE_NAME="cloudmorrow" AGENT_NAME="cloudmorrow-agent" CONFIG_DIR="" REMOVE_FLAG="" DELETE_DATA="" YES="" DRY_RUN="" # Everything the installer makes, in the order it is removed. The user goes # last: it owns the directories. KINDS="services code config data files shares database user" usage() { sed -n '2,17p' "$0" cat <<EOF Options: --remove LIST what goes, comma-separated, instead of asking: services, code, config, data, files, shares, database (the PostgreSQL container and its volume, when the installer ran one), user --delete-data all of it: the software, the config, the key, the database, everyone's files, and the service user --yes do not ask; take what --remove says, or the services --dry-run print what would be removed, change nothing --prefix DIR where the checkout and venv are (default: from the unit) --files-dir DIR where everyone's files are (default: from the config) --data-dir DIR the database and keys (default: from the config) --config-dir DIR server.toml and the sealing key (default: from the unit) --service-user NAME the system user it runs as (default: from the unit) --service-name NAME the systemd service (default: $SERVICE_NAME) EOF } while [ $# -gt 0 ]; do case "$1" in --remove) REMOVE_FLAG="$2"; shift 2 ;; --delete-data | --all) DELETE_DATA="1"; shift ;; --keep-data) DELETE_DATA=""; shift ;; # once the default; still accepted --yes | -y) YES="1"; shift ;; --dry-run) DRY_RUN="1"; shift ;; --prefix) PREFIX="$2"; shift 2 ;; --files-dir | --notes-dir) FILES_DIR="$2"; shift 2 ;; --data-dir) DATA_DIR="$2"; shift 2 ;; --config-dir) CONFIG_DIR="$2"; shift 2 ;; --service-user) SERVICE_USER="$2"; shift 2 ;; --service-name) SERVICE_NAME="$2"; shift 2 ;; -h | --help) usage; exit 0 ;; *) echo "unknown option: $1" >&2; usage >&2; exit 2 ;; esac done say() { printf '\033[36m::\033[0m %s\n' "$*"; } warn() { printf '\033[33mnote:\033[0m %s\n' "$*"; } die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; } run() { if [ -n "$DRY_RUN" ]; then printf ' \033[2mwould run:\033[0m %s\n' "$*" else "$@" fi } [ -n "$DRY_RUN" ] || [ "$(id -u)" = "0" ] || die "run this with sudo" UNIT="/etc/systemd/system/$SERVICE_NAME.service" AGENT_UNIT="/etc/systemd/system/$AGENT_NAME.service" # --- what the installer made ------------------------------------------------ # The unit says where the venv and the config are and who it runs as; the # config says where the data is. A flag wins over both, and the installer's # defaults are last. unit_value() { sed -n "s/^$1=//p" "$UNIT" 2>/dev/null | head -n 1; } if [ -z "$CONFIG_DIR" ]; then # Environment=CLOUDMORROW_SERVER_CONFIG=/etc/cloudmorrow/server.toml config_now="$(unit_value Environment=CLOUDMORROW_SERVER_CONFIG)" case "$config_now" in /*/server.toml) CONFIG_DIR="$(dirname "$config_now")" ;; *) CONFIG_DIR="/etc/cloudmorrow" ;; esac fi CONFIG="$CONFIG_DIR/server.toml" KEY_FILE="$CONFIG_DIR/cloudmorrow.key" config_value() { sed -n "s/^$1 = \"\\(.*\\)\"\$/\\1/p" "$CONFIG" 2>/dev/null | head -n 1; } if [ -z "$PREFIX" ]; then # ExecStart=/opt/cloudmorrow/venv/bin/cloudmorrow-server serve exec_start="$(unit_value ExecStart | cut -d' ' -f1)" case "$exec_start" in */venv/bin/cloudmorrow-server) PREFIX="${exec_start%/venv/bin/cloudmorrow-server}" ;; *) PREFIX="/opt/cloudmorrow" ;; esac fi [ -n "$SERVICE_USER" ] || SERVICE_USER="$(unit_value User)" [ -n "$SERVICE_USER" ] || SERVICE_USER="cloudmorrow" [ -n "$DATA_DIR" ] || DATA_DIR="$(config_value data_dir)" [ -n "$DATA_DIR" ] || DATA_DIR="/var/lib/cloudmorrow" # Everyone's files: the config's notes_dir, from when notes were all it held. [ -n "$FILES_DIR" ] || FILES_DIR="$(config_value notes_dir)" [ -n "$FILES_DIR" ] || FILES_DIR="$DATA_DIR/files" # The Shares folder is among the files unless the config moved it out; only # then is it a thing of its own to remove. SHARES_DIR="$(config_value shares_dir)" case "$SHARES_DIR" in "" | "$FILES_DIR" | "$FILES_DIR"/*) SHARES_DIR="" ;; esac # The files are under the data directory unless they were moved out; inside # it, removing the data has to leave them be unless they go too. FILES_INSIDE="" case "$FILES_DIR" in "$DATA_DIR"/*) FILES_INSIDE="1" ;; esac CLOUD_NAME="$(config_value name)" # Every directory this deletes wholesale is checked first: absolute, and not # one of the system's own. A config edited by hand to say notes_dir = "/" # must not be able to take the machine with it. check_dir() { case "$1" in /*) ;; *) die "$1 is not an absolute path; give it with a flag" ;; esac case "$(printf '%s' "$1" | sed 's|/*$||')" in "" | /bin | /boot | /dev | /etc | /home | /lib | /lib64 | /media | /mnt | /opt | /proc | /root | \ /run | /sbin | /srv | /sys | /tmp | /usr | /usr/local | /var | /var/lib | "$HOME") die "refusing to delete $1; give the right path with a flag" ;; esac } # --- the list --------------------------------------------------------------- # One row per kind of thing, only for the kinds that are there. A row is # "kind|label|detail"; the detail says what and where, so the list is the # answer to "what is on this machine?" even for somebody removing nothing. FOUND="" found() { FOUND="$FOUND$1 "; } is_found() { case "$FOUND" in *"$1|"*) return 0 ;; *) return 1 ;; esac; } services_detail="" [ -f "$UNIT" ] && services_detail="$SERVICE_NAME" [ -f "$AGENT_UNIT" ] && services_detail="${services_detail:+$services_detail, }$AGENT_NAME" commands="" [ -L /usr/local/bin/cloudmorrow-server ] && commands="cloudmorrow-server" # The terminal app's links, when they point into this server's venv: the # installer's, or an update's. One that points elsewhere is somebody else's. our_link() { [ -L "/usr/local/bin/$1" ] && [ "$(readlink "/usr/local/bin/$1")" = "$PREFIX/venv/bin/$1" ]; } for command in cm cloudmorrow cloudmorrow-agent; do our_link "$command" && commands="${commands:+$commands, }$command" done grep -qs "install-server.sh" /usr/local/bin/cloudmorrow-update && commands="${commands:+$commands, }cloudmorrow-update" grep -qs "install-server.sh" /etc/sudoers.d/cloudmorrow && commands="${commands:+$commands, }the sudo rule" if [ -n "$services_detail" ] || [ -n "$commands" ]; then found "services|Services|${services_detail:+the units $services_detail}${services_detail:+${commands:+; }}${commands:+the commands $commands}" fi [ ! -e "$PREFIX" ] || found "code|Code|$PREFIX: the checkout and the virtualenv" if [ -e "$CONFIG_DIR" ]; then key_note="" [ ! -f "$KEY_FILE" ] || key_note=" and the sealing key" found "config|Config|$CONFIG_DIR: server.toml$key_note" fi [ ! -e "$DATA_DIR" ] || found "data|Data|$DATA_DIR: the database, keys and quills${FILES_INSIDE:+ (not the files in it)}" [ ! -e "$FILES_DIR" ] || found "files|Files|$FILES_DIR: everyone's files and shares" [ -z "$SHARES_DIR" ] || [ ! -e "$SHARES_DIR" ] || found "shares|Shares|$SHARES_DIR: every fileshare" # The PostgreSQL container the installer ran, when it did: it and its volume. PG_CONTAINER="cloudmorrow-postgres" CONTAINER_ENGINE="" for candidate in docker podman; do if command -v "$candidate" >/dev/null 2>&1 && "$candidate" container inspect "$PG_CONTAINER" >/dev/null 2>&1; then CONTAINER_ENGINE="$candidate" break fi done [ -z "$CONTAINER_ENGINE" ] || found "database|Database|the PostgreSQL container $PG_CONTAINER and its volume; the daily dump is in the data" ! id "$SERVICE_USER" >/dev/null 2>&1 || found "user|User|the system user $SERVICE_USER" if [ -z "$FOUND" ]; then say "there is no Cloudmorrow server on this machine" exit 0 fi row_label() { printf '%s\n' "$FOUND" | sed -n "s/^$1|\\([^|]*\\)|.*/\\1/p"; } row_detail() { printf '%s\n' "$FOUND" | sed -n "s/^$1|[^|]*|//p"; } kinds_found() { for kind in $KINDS; do ! is_found "$kind" || printf '%s\n' "$kind" done } # --- the choice -------------------------------------------------------------- # CHOSEN is the kinds that go, space-separated. A flag decides it outright; # otherwise the list is asked on the terminal, with the services ticked. CHOSEN="" chose() { case " $CHOSEN " in *" $1 "*) return 0 ;; *) return 1 ;; esac; } if [ -n "$DELETE_DATA" ]; then CHOSEN="$(kinds_found | tr '\n' ' ')" elif [ -n "$REMOVE_FLAG" ]; then for kind in $(printf '%s' "$REMOVE_FLAG" | tr ',' ' '); do [ "$kind" != "notes" ] || kind="files" # the old name for them case " $KINDS " in *" $kind "*) ;; *) die "--remove does not know \"$kind\"; it takes: $(echo $KINDS | sed 's/ /, /g')" ;; esac ! is_found "$kind" || CHOSEN="$CHOSEN $kind" done else CHOSEN="services" fi # The list of boxes to tick is the one the installer asks with # (cloudmorrow.checklist), run from the checkout when it is still there. # Without it, or without a terminal that can draw, a numbered list does. checklist_python() { if [ -x "$PREFIX/venv/bin/python" ] && "$PREFIX/venv/bin/python" -c "import cloudmorrow.checklist" 2>/dev/null; then echo "$PREFIX/venv/bin/python" elif [ -d "$PREFIX/src" ] && PYTHONPATH="$PREFIX/src" python3 -c "import cloudmorrow.checklist" 2>/dev/null; then echo "env PYTHONPATH=$PREFIX/src python3" fi } ask_with_checklist() { python="$1" set -- --title "What should go from this machine?" for kind in $(kinds_found); do set -- "$@" --item "$kind" "$(row_label "$kind")" "$(row_detail "$kind")" done for kind in $CHOSEN; do set -- "$@" --on "$kind" done status=0 chosen="$($python -m cloudmorrow.checklist "$@")" || status=$? case "$status" in 0) CHOSEN="$(printf '%s\n' "$chosen" | tr '\n' ' ')" ;; 130) die "stopped; nothing was removed" ;; *) return 1 ;; esac } ask_with_numbers() { printf '\n \033[1mWhat should go from this machine?\033[0m\n\n' >/dev/tty n=0 for kind in $(kinds_found); do n=$((n + 1)) if chose "$kind"; then box="x"; else box=" "; fi printf ' %d [%s] %-9s %s\n' "$n" "$box" "$(row_label "$kind")" "$(row_detail "$kind")" >/dev/tty done ticked="$(n=0; for kind in $(kinds_found); do n=$((n + 1)); ! chose "$kind" || printf '%s ' "$n"; done)" printf '\n Type the numbers to remove, separated by spaces; nothing removes nothing.\n' >/dev/tty printf ' Numbers [%s]: ' "$(echo $ticked)" >/dev/tty read -r answer </dev/tty || answer="" [ -n "$answer" ] || answer="$ticked" CHOSEN="" for number in $answer; do kind="$(kinds_found | sed -n "${number}p" 2>/dev/null)" || kind="" [ -n "$kind" ] || die "there is no $number in the list; nothing was removed" CHOSEN="$CHOSEN $kind" done } if [ -n "$DRY_RUN" ] || [ -n "$YES" ] || [ -n "$DELETE_DATA" ] || [ -n "$REMOVE_FLAG" ]; then : else # On the terminal, not stdin: through `curl | sh` the script itself is stdin. ( : </dev/tty ) 2>/dev/null || die "nobody to ask; say what goes with --remove, or --yes for the services" python="$(checklist_python)" if [ -z "$python" ] || ! ask_with_checklist "$python"; then ask_with_numbers fi fi # Only what is there can go: "services" ticked on a machine without the # units is nothing to do. CHOSEN="$(for kind in $CHOSEN; do ! is_found "$kind" || printf '%s ' "$kind"; done)" # A dry run with no choice shows the whole list, so it reads as an inventory. if [ -n "$DRY_RUN" ] && [ -z "$REMOVE_FLAG" ] && [ -z "$DELETE_DATA" ]; then printf '\n On this machine%s:\n\n' "${CLOUD_NAME:+, the Cloudmorrow server \"$CLOUD_NAME\"}" for kind in $(kinds_found); do if chose "$kind"; then box="x"; else box=" "; fi printf ' [%s] %-9s %s\n' "$box" "$(row_label "$kind")" "$(row_detail "$kind")" done printf '\n Ticked is what goes without --remove.\n' fi if [ -z "$(echo $CHOSEN)" ]; then printf '\n Nothing ticked: nothing was removed.\n\n' exit 0 fi # Data is anything a later install would pick up again, and the user that # owns it. Removing it is a different question from removing software. DATA_GOES="" for kind in config data files shares database user; do ! chose "$kind" || DATA_GOES="1" done ! chose code || check_dir "$PREFIX" ! chose data || check_dir "$DATA_DIR" ! chose files || check_dir "$FILES_DIR" ! chose shares || check_dir "$SHARES_DIR" printf '\n %sThis removes from this machine:\n\n' "${CLOUD_NAME:+The Cloudmorrow server \"$CLOUD_NAME\". }" for kind in $(kinds_found); do ! chose "$kind" || printf ' - %-9s %s\n' "$(row_label "$kind")" "$(row_detail "$kind")" done kept="" for kind in $(kinds_found); do chose "$kind" || kept="${kept:+$kept, }$(row_label "$kind" | tr 'A-Z' 'a-z')" done if [ -n "$kept" ]; then printf '\n and keeps the %s, so installing again picks up where it was.\n' "$kept" fi if [ -n "$DATA_GOES" ]; then printf '\n \033[1mWhat is removed cannot be brought back: accounts, files, shares, secrets.\033[0m\n' fi printf '\n' # --- ask once more -------------------------------------------------------------- if [ -n "$DRY_RUN" ]; then : elif [ -z "$YES" ]; then ( : </dev/tty ) 2>/dev/null || die "nobody to ask; run it again with --yes" if [ -z "$DATA_GOES" ]; then printf '\033[1mGo ahead?\033[0m [y/N]: ' >/dev/tty read -r answer </dev/tty || answer="" case "$answer" in y | Y | yes) ;; *) die "nothing was removed" ;; esac else printf '\033[1mType "delete" to remove it\033[0m: ' >/dev/tty read -r answer </dev/tty || answer="" [ "$answer" = "delete" ] || die "nothing was removed" fi fi # --- the services ----------------------------------------------------------- # The agent first: it reports to the server, so it goes before the server does. if chose services; then if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then for name in "$AGENT_NAME" "$SERVICE_NAME"; do if [ -f "/etc/systemd/system/$name.service" ]; then say "stopping $name" run systemctl disable --now --quiet "$name" || warn "$name did not stop cleanly" fi done fi for unit in "$AGENT_UNIT" "$UNIT"; do [ ! -f "$unit" ] || run rm -f "$unit" done if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then run systemctl daemon-reload fi # The commands, and the rule that lets them run. say "removing the commands" [ ! -L /usr/local/bin/cloudmorrow-server ] || run rm -f /usr/local/bin/cloudmorrow-server for command in cm cloudmorrow cloudmorrow-agent; do ! our_link "$command" || run rm -f "/usr/local/bin/$command" done if grep -qs "install-server.sh" /usr/local/bin/cloudmorrow-update; then run rm -f /usr/local/bin/cloudmorrow-update fi if grep -qs "install-server.sh" /etc/sudoers.d/cloudmorrow; then run rm -f /etc/sudoers.d/cloudmorrow fi fi # --- the code ----------------------------------------------------------------- if chose code && [ -e "$PREFIX" ]; then say "removing the code at $PREFIX" run rm -rf "$PREFIX" fi # --- the data ------------------------------------------------------------------- remove_dir() { if [ -e "$1" ]; then say "removing $1" run rm -rf "$1" fi } ! chose config || remove_dir "$CONFIG_DIR" if chose data; then if [ -n "$FILES_INSIDE" ] && ! chose files && [ -e "$FILES_DIR" ]; then # Everything in the data directory but the files, which stay. say "removing $DATA_DIR, but not the files in it" for entry in "$DATA_DIR"/* "$DATA_DIR"/.[!.]* "$DATA_DIR"/..?*; do [ -e "$entry" ] || [ -L "$entry" ] || continue case "$FILES_DIR" in "$entry" | "$entry"/*) continue ;; esac run rm -rf "$entry" done else remove_dir "$DATA_DIR" fi fi if chose files; then remove_dir "$FILES_DIR" # An older install made /srv/cloudmorrow to hold notes/; empty now, it goes too. parent="$(dirname "$FILES_DIR")" if [ "$(basename "$parent")" = "cloudmorrow" ] && [ -d "$parent" ]; then run rmdir "$parent" 2>/dev/null || true fi fi ! chose shares || remove_dir "$SHARES_DIR" if chose database; then say "removing the container $PG_CONTAINER and its volume" if command -v systemctl >/dev/null 2>&1 && [ -f /etc/systemd/system/cloudmorrow-pgdump.timer ]; then run systemctl disable --now --quiet cloudmorrow-pgdump.timer || true run rm -f /etc/systemd/system/cloudmorrow-pgdump.timer /etc/systemd/system/cloudmorrow-pgdump.service fi run "$CONTAINER_ENGINE" rm -f "$PG_CONTAINER" run "$CONTAINER_ENGINE" volume rm "$PG_CONTAINER" [ ! -f "$CONFIG_DIR/postgres.pgpass" ] || run rm -f "$CONFIG_DIR/postgres.pgpass" fi if chose user; then say "removing the system user $SERVICE_USER" run userdel "$SERVICE_USER" || warn "the user $SERVICE_USER is still there; remove it with userdel" fi # --- what is left ----------------------------------------------------------------- if [ -n "$DRY_RUN" ]; then printf '\n That was a dry run: nothing was removed.\n\n' exit 0 fi printf '\n Done.\n' if chose services; then cat <<EOF Left for you, because the installer never made them: - the reverse proxy's entry for this cloud (Caddy, nginx), if you added one - the deploy key on GitHub, if you gave the repository one - Cloudmorrow on each computer that used this cloud: run cm uninstall there EOF fi if [ -n "$kept" ]; then printf '\n Kept: the %s.\n Run this again to remove them, or install again to pick up where it was.\n' "$kept" fi printf '\n'
Before you go
- Removing everything cannot be undone. Every account, note, file and secret on the server goes with it.
- Want a copy? Save
/var/lib/cloudmorrow,/srv/cloudmorrow/notesand/etc/cloudmorrowfirst. The key in/etc/cloudmorrowopens everything else, so keep it with the rest. - Ran it with Docker? In
deploy/docker,docker compose down -vremoves the containers and their data.
Left for you
- The web server in front of it. Take this cloud's entry out of your Caddy or nginx config.
- A deploy key, if you installed from a private repository: remove it on GitHub under Settings → Deploy keys.
- Changed your mind? Install it again. After
--keep-data, it picks up where it was.