#!/bin/sh # Crucible relay server installer (Linux). # # Install or upgrade to the latest release: # curl -fsSL https://github.com/devforge-io/crucible/releases/latest/download/install.sh | sh # # Options, as environment variables: # CRUCIBLE_VERSION=v0.2.0 install that release instead of the latest # CRUCIBLE_NO_SERVICE=1 install the binary and config only, without the systemd service # # A fresh install runs `crucible init` and `crucible service install`, then # lists what's left (a TLS certificate, then `sudo crucible start`). An upgrade # replaces the binary, refreshes the service and restarts it if it was running; # the config in /etc/crucible and the data in /var/lib/crucible are kept. set -eu REPO="devforge-io/crucible" BINARY="crucible" SERVICE="crucible" INSTALL_DIR="/usr/local/bin" CONFIG="/etc/crucible/crucible.toml" LEGACY_ENV="/etc/crucible/crucible.env" # written by the v0.1.0 installer UNIT="/etc/systemd/system/${SERVICE}.service" fail() { echo "Error: $*" >&2 exit 1 } # Run with sudo when not already root. run() { if [ "$(id -u)" -eq 0 ]; then "$@" else sudo "$@" fi } # Detect OS [ "$(uname -s)" = "Linux" ] || fail "Crucible's server runs on Linux only (found $(uname -s))." # Detect architecture ARCH="$(uname -m)" case "$ARCH" in x86_64|amd64) ARCH="x86_64" ;; arm64|aarch64) ARCH="aarch64" ;; *) fail "unsupported architecture: $ARCH" ;; esac TARGET="${ARCH}-unknown-linux-musl" # Requirements for cmd in curl tar sha256sum install; do command -v "$cmd" >/dev/null 2>&1 || fail "'$cmd' is required." done if [ "$(id -u)" -ne 0 ]; then command -v sudo >/dev/null 2>&1 || fail "run as root, or install sudo." fi SYSTEMD="" if [ -z "${CRUCIBLE_NO_SERVICE:-}" ] && [ -d /run/systemd/system ]; then SYSTEMD=1 fi # Pick the release if [ -n "${CRUCIBLE_VERSION:-}" ]; then TAG="$CRUCIBLE_VERSION" case "$TAG" in v*) ;; *) TAG="v$TAG" ;; esac else echo "Fetching latest release..." TAG=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name"' | head -1 | cut -d'"' -f4) [ -n "$TAG" ] || fail "could not determine the latest release." fi ARCHIVE="${BINARY}-${TAG}-${TARGET}.tar.gz" BASE_URL="https://github.com/${REPO}/releases/download/${TAG}" TMPDIR=$(mktemp -d) trap 'rm -rf "$TMPDIR"' EXIT echo "Downloading Crucible ${TAG} for ${TARGET}..." curl -fsSL "${BASE_URL}/${ARCHIVE}" -o "${TMPDIR}/${ARCHIVE}" || fail "download failed: ${BASE_URL}/${ARCHIVE}" curl -fsSL "${BASE_URL}/SHA256SUMS" -o "${TMPDIR}/SHA256SUMS" || fail "could not download SHA256SUMS." echo "Verifying checksum..." (cd "$TMPDIR" && grep " ${ARCHIVE}\$" SHA256SUMS | sha256sum -c -) >/dev/null || fail "checksum mismatch for ${ARCHIVE}." tar -xzf "${TMPDIR}/${ARCHIVE}" -C "$TMPDIR" [ -f "${TMPDIR}/${BINARY}" ] || fail "${ARCHIVE} does not contain ${BINARY}." # Remember whether the server is running, to restart it after the swap. WAS_ACTIVE="" if [ -n "$SYSTEMD" ] && [ -f "$UNIT" ] && systemctl is-active --quiet "$SERVICE"; then WAS_ACTIVE=1 fi echo "Installing binary to ${INSTALL_DIR}..." run install -m 755 "${TMPDIR}/${BINARY}" "${INSTALL_DIR}/${BINARY}" CRUCIBLE="${INSTALL_DIR}/${BINARY}" echo "" if run test -f "$CONFIG"; then # Upgrade: keep the config, refresh the unit (it names this binary). if [ -n "$SYSTEMD" ] && [ -f "$UNIT" ]; then run "$CRUCIBLE" service install >/dev/null if [ -n "$WAS_ACTIVE" ]; then run "$CRUCIBLE" restart fi fi echo "Crucible upgraded to ${TAG}. Kept ${CONFIG}." elif run test -f "$LEGACY_ENV"; then # A v0.1.0 install: its unit reads crucible.env, which the new binary still # honours, so it keeps running on loopback as before. if [ -n "$WAS_ACTIVE" ]; then run systemctl restart "$SERVICE" fi echo "Crucible upgraded to ${TAG}, still on the v0.1.0 settings in ${LEGACY_ENV}." echo "" echo "To serve over TLS with the new layout:" echo " sudo crucible stop" echo " sudo crucible init --service # writes ${CONFIG}, replaces the unit" echo " sudo rm ${LEGACY_ENV}" echo " ...then follow the steps it prints." elif [ -n "$SYSTEMD" ]; then run "$CRUCIBLE" init --service else run "$CRUCIBLE" init fi echo "" echo " crucible --help all commands"