Files
stack/scripts/proxy-service.sh
T

50 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Explicit user-unit rendering/install. Never enables, starts or restarts a unit.
set -euo pipefail
REPO=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
. "$REPO/scripts/instance.sh"
instance_args "$@"
set -- "${INSTANCE_ARGS[@]}"
instance_select
OP=${1:-}; [ $# -eq 0 ] || shift
UNIT_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user
RELOAD=1
COPY_ID=""
COPIES="${XDG_DATA_HOME:-$HOME/.local/share}/mosaic-stack/copies"
while [ $# -gt 0 ]; do
case "$1" in
--dir) [ $# -ge 2 ] || exit 4; UNIT_DIR=$2; shift 2 ;;
--no-reload) RELOAD=0; shift ;;
--copy) [ $# -ge 2 ] || exit 4; COPY_ID=$2; shift 2 ;;
--copies) [ $# -ge 2 ] || exit 4; COPIES=$2; shift 2 ;;
*) echo 'proxy-service: proxy-unit-usage' >&2; exit 4 ;;
esac
done
case "$OP" in render|install) ;; *) echo 'proxy-service: proxy-unit-usage' >&2; exit 4 ;; esac
[ "$MOSAIC_INSTANCE_PROXY_CONFIGURED" = 1 ] || { echo 'proxy-service: proxy-not-configured' >&2; exit 3; }
ROOT=$REPO
if [ -n "$COPY_ID" ]; then
[[ "$COPY_ID" =~ ^[0-9a-f]{40}$ ]] && [ -f "$COPIES/$COPY_ID/packages/model-proxy/PIN" ] || { echo 'proxy-service: proxy-unit-copy' >&2; exit 3; }
ROOT="$COPIES/$COPY_ID"
fi
case "$ROOT" in /*) ;; *) echo 'proxy-service: proxy-unit-path' >&2; exit 3 ;; esac
NODE=$(command -v node)
case "$NODE:$ROOT:$UNIT_DIR:$MOSAIC_INSTANCE_CONFIG" in *[!A-Za-z0-9/._+:-]*) echo 'proxy-service: proxy-unit-path' >&2; exit 3 ;; esac
render() {
# Both default and named instances bind their exact selected config, never a service-manager default.
sed -e "s|@REPO@|$ROOT|g" -e 's|@PATH@|/usr/local/bin:/usr/bin:/bin|g' \
-e "s|@NODE@|$NODE|g" -e "s|^@CONFIG_ENV@$|Environment=MOSAIC_CONFIG=$MOSAIC_INSTANCE_CONFIG|" \
-e "s|^Description=Mosaic model proxy$|Description=Mosaic model proxy [$MOSAIC_INSTANCE]|" \
"$REPO/packages/cli/systemd/mosaic-proxy.service.in"
}
if [ "$OP" = render ]; then render; exit 0; fi
# Refuse replacement and drop-ins: installation is explicit creation only.
[ ! -e "$UNIT_DIR/$PROXY_UNIT" ] && [ ! -L "$UNIT_DIR/$PROXY_UNIT" ] || { echo 'proxy-service: proxy-unit-exists' >&2; exit 3; }
for DROP in "$PROXY_UNIT.d" mosaic-proxy-.service.d mosaic-.service.d service.d; do
[ ! -e "$UNIT_DIR/$DROP" ] || { echo 'proxy-service: proxy-unit-dropin' >&2; exit 3; }
done
mkdir -p "$UNIT_DIR"
# noclobber refuses a concurrent install; no host service is started or enabled.
(set -o noclobber; render > "$UNIT_DIR/$PROXY_UNIT") || { echo 'proxy-service: proxy-unit-exists' >&2; exit 3; }
[ "$RELOAD" = 0 ] || systemctl --user daemon-reload