Files

79 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install dogebot for the current user (rootless podman + quadlets).
#
# DOGEBOT_DISCORD_TOKEN=... DOGEBOT_YOUTUBE_REFRESH_TOKEN=... \
# DOGEBOT_PRELOAD_TOKEN=... deploy/install.sh [--start]
#
# - Creates the podman secrets that don't exist yet. The Discord token, the
# YouTube refresh token and the media-preload token come from the
# environment; the Postgres, Lavalink and yt-cipher passwords are generated.
# - Builds the Red and Lavalink images.
# - Installs the quadlets and the daily update timer, and starts Postgres,
# yt-cipher and Lavalink.
# - Set HTTPS_PROXY to build through a proxy (podman passes it into the image
# build). In the home lab: HTTPS_PROXY=http://10.77.0.1:3128.
# - Starts the bot itself only with --start, so a second copy never logs in
# with the same token by accident.
set -euo pipefail
repo=$(cd "$(dirname "$0")/.." && pwd)
quadlet_dir=${XDG_CONFIG_HOME:-$HOME/.config}/containers/systemd/dogebot
unit_dir=${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user
start_bot=false
[[ ${1:-} == --start ]] && start_bot=true
secret() { # name, value-or-empty-to-generate
local name=$1 value=${2:-}
if podman secret exists "$name"; then
echo "secret $name: exists, kept"
return
fi
if [[ -z $value ]]; then
value=$(head -c 32 /dev/urandom | base64 | tr -d '/+=' | head -c 40)
fi
printf %s "$value" | podman secret create "$name" - >/dev/null
echo "secret $name: created"
}
need() { # name of a secret that can't be generated, env var with its value
if ! podman secret exists "$1" && [[ -z ${!2:-} ]]; then
echo "$2 must be set the first time (secret $1 doesn't exist yet)" >&2
exit 1
fi
secret "$1" "${!2:-}"
}
need dogebot-discord-token DOGEBOT_DISCORD_TOKEN
need dogebot-youtube-refresh-token DOGEBOT_YOUTUBE_REFRESH_TOKEN
need dogebot-preload-token DOGEBOT_PRELOAD_TOKEN
secret dogebot-postgres-password
secret dogebot-lavalink-password
secret dogebot-yt-cipher-token
cd "$repo"
podman build -t localhost/dogebot-red:latest -f containers/red/Containerfile .
if ! podman image exists localhost/dogebot-yt-cipher:bun-912033c; then
podman build -t localhost/dogebot-yt-cipher:bun-912033c -f containers/yt-cipher/Containerfile .
fi
if ! podman image exists localhost/dogebot-lavalink:current; then
mise exec -- uv run --frozen dogebot-update --build-only
fi
mkdir -p "$quadlet_dir" "$unit_dir"
install -m 644 deploy/quadlet/* "$quadlet_dir/"
sed "s|%h/src/dogebot|$repo|" deploy/systemd/dogebot-update.service >"$unit_dir/dogebot-update.service"
install -m 644 deploy/systemd/dogebot-update.timer "$unit_dir/"
systemctl --user daemon-reload
systemctl --user start dogebot-postgres.service dogebot-yt-cipher.service dogebot-lavalink.service
systemctl --user enable --now dogebot-update.timer
if $start_bot; then
systemctl --user start dogebot-red.service
echo "dogebot started: journalctl --user -u dogebot-red -f"
else
echo "Ready. Start the bot with: systemctl --user start dogebot-red"
fi
if [[ $(loginctl show-user "$USER" -p Linger --value 2>/dev/null) != yes ]]; then
echo "Note: run 'loginctl enable-linger $USER' so dogebot runs without a login session."
fi