#!/bin/bash # # Purpose : Install Supermarket's server edition on this machine: one team, # signing in, at its own domain, with Postgres and Caddy (HTTPS), # all in Docker (compose.server.yaml). Safe to run again: it keeps # the settings it wrote, and only rebuilds and restarts. # # Usage : ./install-server.sh # # It asks for the domain, how the certificate is made, and the mail server # (for invitations and password resets), and makes the secret key and the # database password. They go in server.env, readable only by you: keep a # copy of SUPERMARKET_SECRET_KEY somewhere safe, as nothing encrypted in the # database can be read without it. set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" ENV_FILE=server.env COMPOSE=(docker compose -f compose.server.yaml --env-file "$ENV_FILE") if ! command -v docker > /dev/null || ! docker compose version > /dev/null 2>&1; then echo "Supermarket's server edition needs Docker with the compose plugin." exit 1 fi if ! docker info > /dev/null 2>&1; then echo "Docker isn't running, or you can't use it: start it, or run this as a user in the docker group." exit 1 fi ask() { # ask [default] local answer if [ -n "${2:-}" ]; then read -r -p "$1 [$2]: " answer echo "${answer:-$2}" else while [ -z "${answer:-}" ]; do read -r -p "$1: " answer; done echo "$answer" fi } random_hex() { head -c "$1" /dev/urandom | od -An -tx1 | tr -d ' \n'; } if [ -f "$ENV_FILE" ]; then echo "Using the settings in $ENV_FILE (delete it to start again)." else echo "Supermarket's server edition: a team signs in to it at its own address." echo domain=$(ask "The domain people will open (e.g. supermarket.example.com)") echo echo "The HTTPS certificate:" echo " 1) From Let's Encrypt: this server can be reached from the internet on ports 80 and 443 (the usual)" echo " 2) Caddy's own certificate authority: a private network or VPN (browsers will warn until you trust its root)" echo " 3) Your own certificate authority's: put cert.pem and key.pem in server/certs/" tls_choice=$(ask "Which" "1") tls="" acme="" case "$tls_choice" in 2) tls="tls internal" ;; 3) tls="tls /certs/cert.pem /certs/key.pem" ;; *) email=$(ask "An email for Let's Encrypt's notices (or leave empty)" "-") [ "$email" != "-" ] && acme="email $email" ;; esac echo echo "The mail server Supermarket sends invitations and password resets through," echo "as smtp://user:password@host:587 (STARTTLS) or smtps://user:password@host:465." echo "Write @ in the user name as %40." mail=$(ask "Mail server") from=$(ask "Send as" "Supermarket ") tz=$(ask "Time zone" "$(cat /etc/timezone 2>/dev/null || echo UTC)") umask 077 cat > "$ENV_FILE" </dev/null || echo dev) EOF echo echo "Wrote $ENV_FILE. Keep a copy of its SUPERMARKET_SECRET_KEY somewhere safe." fi # The version, from git, each time. if version=$(git describe --tags --always --dirty 2>/dev/null); then sed -i "s/^SUPERMARKET_VERSION=.*/SUPERMARKET_VERSION=$version/" "$ENV_FILE" fi echo "Building and starting Supermarket, Postgres and Caddy…" "${COMPOSE[@]}" up -d --build echo "Waiting for Supermarket to start…" for _ in $(seq 1 60); do if "${COMPOSE[@]}" exec -T supermarket wget -q -O /dev/null http://127.0.0.1:8080/healthz 2> /dev/null; then break fi sleep 2 done domain=$(grep '^SUPERMARKET_DOMAIN=' "$ENV_FILE" | cut -d= -f2-) if link=$("${COMPOSE[@]}" exec -T supermarket supermarket setup-link 2> /dev/null | tail -1) && [[ "$link" == https://* ]]; then echo echo "Supermarket is running. To create its owner, open this link (it works once, for a day):" echo " $link" else echo echo "Supermarket is running at https://$domain" fi echo echo "Its logs: docker compose -f compose.server.yaml --env-file $ENV_FILE logs -f supermarket" echo "Its settings: docker compose -f compose.server.yaml --env-file $ENV_FILE exec supermarket supermarket config" echo "To update: git pull && ./install-server.sh"