From 3038c9374edc6f4218a59490dc2a4fbe2dbdfddf Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 1 Dec 2026 08:50:29 +0100 Subject: [PATCH] Ship a generic workstation setup script scripts/spear-client-setup.sh prepares a workstation to run a SPEAR image from a private registry. It left the public tree because it named one site's host and carried its certificate; it comes back with neither. The site hands over its launcher configuration, workstation.conf, which already names the image and the inference host. The script derives the registry from the image name, trusts a registry-ca.crt found beside the file, logs in (SPEAR_REGISTRY_USER from the file, the password from SPEAR_REGISTRY_PASSWORD or asked), pulls the image, installs the launcher and copies the file to local.conf. A local.conf that was there before and differs is kept as local.conf.bak. A second argument overrides the image. --- doc/source/start/container_run.rst | 19 +++++- scripts/spear-client-setup.sh | 93 ++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100755 scripts/spear-client-setup.sh diff --git a/doc/source/start/container_run.rst b/doc/source/start/container_run.rst index 6586e6b..2a4c3a5 100644 --- a/doc/source/start/container_run.rst +++ b/doc/source/start/container_run.rst @@ -105,8 +105,23 @@ container reaches it through an SSH tunnel you open on your machine. file rather than on every command line: ``~/.config/spear/local.conf`` (``client/local.conf`` when the launcher runs from the repository), one shell assignment per line. ``REDS_HOST`` and ``SPEAR_REDS_KEY`` there - save the two options above, ``SPEAR_IMAGE`` saves ``--image``. Without - the script, the same run by hand: + save the two options above, ``SPEAR_IMAGE`` saves ``--image``. + + A site that serves its image from a private registry hands over that + file as ``workstation.conf``, with the registry's ``registry-ca.crt`` + beside it when the certificate is its own. Steps 2 and 3 are then one + command, to run again to update the image: + + .. code-block:: console + + $ scripts/spear-client-setup.sh /workstation.conf + + It trusts the certificate, logs in (``SPEAR_REGISTRY_USER`` from the file, + the password from ``SPEAR_REGISTRY_PASSWORD`` or asked), pulls the image, + installs the launcher and copies the file to ``local.conf``, keeping a + different one as ``local.conf.bak``. + + Without the launcher, the same run by hand: .. code-block:: console diff --git a/scripts/spear-client-setup.sh b/scripts/spear-client-setup.sh new file mode 100755 index 0000000..b0521f3 --- /dev/null +++ b/scripts/spear-client-setup.sh @@ -0,0 +1,93 @@ +#!/bin/bash +# Prepares a workstation to run a SPEAR image from a private registry: trusts +# the registry, logs in, pulls the image, installs the spear launcher and +# writes its configuration. +# +# spear-client-setup.sh [image] +# +# workstation.conf is the launcher configuration the site hands over, copied +# to ~/.config/spear/local.conf. It names the image (SPEAR_IMAGE) and, for +# spear-chat, the inference host (REDS_HOST, SPEAR_REDS_KEY). It may also set +# SPEAR_REGISTRY_USER. A registry-ca.crt next to it is trusted for the +# registry. The password is read from SPEAR_REGISTRY_PASSWORD, or asked. +# +# Needs docker (your user in the docker group) and sudo for the certificate. +# Run it again to update the image. + +set -e + +usage() { + echo "usage: $0 [image]" >&2 + exit 1 +} + +[ $# -ge 1 ] && [ -r "$1" ] || usage +SITE_CONF=$(realpath "$1") + +SPEAR_IMAGE= +SPEAR_REGISTRY_USER= +. "$SITE_CONF" + +IMAGE=${2:-$SPEAR_IMAGE} +[ -n "$IMAGE" ] || { echo "$SITE_CONF sets no SPEAR_IMAGE; give the image" >&2; exit 1; } + +# The registry is the image name's first component, when it is a host. + +REG= +case "${IMAGE%%/*}" in + *.*|*:*|localhost) [ "$IMAGE" != "${IMAGE%%/*}" ] && REG=${IMAGE%%/*} ;; +esac + +CA="$(dirname "$SITE_CONF")/registry-ca.crt" +if [ -n "$REG" ] && [ -r "$CA" ]; then + sudo mkdir -p "/etc/docker/certs.d/$REG" + sudo cp "$CA" "/etc/docker/certs.d/$REG/ca.crt" +fi + +if [ -n "$REG" ]; then + login=(docker login "$REG") + [ -n "$SPEAR_REGISTRY_USER" ] && login+=(-u "$SPEAR_REGISTRY_USER") + if [ -n "${SPEAR_REGISTRY_PASSWORD:-}" ]; then + printf '%s' "$SPEAR_REGISTRY_PASSWORD" | "${login[@]}" --password-stdin + else + "${login[@]}" + fi +fi +docker pull "$IMAGE" + +# The launcher travels in the image. + +CONF="${XDG_CONFIG_HOME:-$HOME/.config}/spear/local.conf" +had_conf= +[ -e "$CONF" ] && had_conf=1 + +tmp=$(mktemp) +docker run --rm --entrypoint cat "$IMAGE" /opt/spear/spear > "$tmp" +bash "$tmp" --install --image "$IMAGE" +rm -f "$tmp" + +# The site's configuration replaces the one --install wrote; a different +# file that was there before is kept as local.conf.bak. + +new=$(mktemp) +if [ "$IMAGE" = "$SPEAR_IMAGE" ]; then + cp "$SITE_CONF" "$new" +else + { grep -v '^SPEAR_IMAGE=' "$SITE_CONF"; echo "SPEAR_IMAGE=$IMAGE"; } > "$new" +fi +mkdir -p "$(dirname "$CONF")" +if ! cmp -s "$new" "$CONF"; then + [ -n "$had_conf" ] && cp "$CONF" "$CONF.bak" && echo "kept the former $CONF as $CONF.bak" + mv "$new" "$CONF" + echo "wrote $CONF" +else + rm -f "$new" +fi + +echo +echo "Done. Then, in a checkout or anywhere: spear (a shell in the image)" +if [ -n "${REDS_HOST:-}" ]; then + key=${SPEAR_REDS_KEY:-$HOME/.ssh/id_ed25519} + [ -r "$key.pub" ] || echo "note: no $key.pub: run ssh-keygen, or set SPEAR_REDS_KEY in $CONF" + echo "spear-chat needs that public key authorised on ${REDS_HOST#*@}" +fi