Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions doc/source/start/container_run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <site>/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

Expand Down
93 changes: 93 additions & 0 deletions scripts/spear-client-setup.sh
Original file line number Diff line number Diff line change
@@ -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 <workstation.conf> [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 <workstation.conf> [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
Loading