lesavka/scripts/install/server.sh

167 lines
5.4 KiB
Bash
Raw Normal View History

2025-06-01 13:31:22 -05:00
#!/usr/bin/env bash
2025-06-27 22:51:50 -05:00
# scripts/install/server.sh - install and setup all server related apps and environments
2025-06-01 13:31:22 -05:00
set -euo pipefail
2025-06-01 21:30:55 -05:00
ORIG_USER=${SUDO_USER:-$(id -un)}
2025-07-04 18:24:48 -05:00
REF=${LESAVKA_REF:-master} # fallback
while [[ $# -gt 0 ]]; do
case $1 in
-r|--ref) REF="$2"; shift 2 ;;
-h|--help)
echo "Usage: $0 [--ref <branch|commit>]"; exit 0 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
echo "==> Using git ref: $REF"
2025-06-23 00:26:02 -05:00
echo "==> 1a. Base packages"
2025-06-29 22:57:54 -05:00
sudo pacman -Syq --needed --noconfirm git \
rustup \
protobuf \
gcc \
pipewire \
pipewire-pulse \
tailscale \
base-devel \
gstreamer \
gst-plugins-base \
2025-06-30 02:03:12 -05:00
gst-plugins-base-libs \
2025-06-29 22:57:54 -05:00
gst-plugins-good \
gst-plugins-bad \
2025-06-30 02:03:12 -05:00
gst-plugins-bad-libs \
2025-06-29 22:57:54 -05:00
gst-plugins-ugly \
2025-06-30 22:27:49 -05:00
gst-libav \
2025-07-01 10:23:51 -05:00
tcpdump \
lsof
2025-06-01 21:30:55 -05:00
if ! command -v yay >/dev/null 2>&1; then
2025-06-23 00:26:02 -05:00
echo "==> 1b. installing yay from AUR ..."
2025-06-01 21:30:55 -05:00
sudo -u "$ORIG_USER" bash -c '
cd /tmp && git clone --depth 1 https://aur.archlinux.org/yay.git &&
cd yay && makepkg -si --noconfirm'
2025-06-01 16:04:00 -05:00
fi
2025-06-28 00:05:13 -05:00
# yay -S --noconfirm grpcurl-bin
2025-06-01 14:18:42 -05:00
2025-06-28 15:45:35 -05:00
echo "==> 2a. Kernel-driver tweaks"
2025-06-23 00:26:02 -05:00
cat <<'EOF' | sudo tee /etc/modprobe.d/gc311-stream.conf >/dev/null
options uvcvideo quirks=0x200 timeout=10000
EOF
echo "==> 2b. Predictable /dev names for each capture card"
2025-06-25 07:46:50 -05:00
# probe all v4l2 devices, keep only the two GC311 capture cards
2025-06-25 08:25:13 -05:00
mapfile -t GC_VIDEOS < <(
2025-06-25 08:32:17 -05:00
sudo v4l2-ctl --list-devices |
2025-06-25 08:25:13 -05:00
awk '/Live Gamer MINI/{getline; print $1}'
2025-06-25 07:46:50 -05:00
)
2025-06-23 00:26:02 -05:00
2025-06-25 08:25:13 -05:00
if [ "${#GC_VIDEOS[@]}" -ne 2 ]; then
echo "❌ Exactly two GC311 capture cards (index0) must be attached!" >&2
printf ' Detected: %s\n' "${GC_VIDEOS[@]}"
2025-06-25 07:46:50 -05:00
exit 1
fi
2025-06-25 08:25:13 -05:00
mapfile -t TAGS < <(
for v in "${GC_VIDEOS[@]}"; do
2025-06-25 09:21:39 -05:00
sudo udevadm info -q property -n "$v" |
2025-06-25 08:25:13 -05:00
awk -F= '/^ID_PATH_TAG=/{print $2}'
done
)
printf ' ↪ Left card: %s (%s)\n' "${GC_VIDEOS[0]}" "${TAGS[0]}"
printf ' ↪ Right card: %s (%s)\n' "${GC_VIDEOS[1]}" "${TAGS[1]}"
2025-06-25 07:46:50 -05:00
LEFT_TAG=${TAGS[0]}
RIGHT_TAG=${TAGS[1]}
2025-06-25 08:25:13 -05:00
sudo tee /etc/udev/rules.d/85-gc311.rules >/dev/null <<EOF
2025-06-27 22:51:50 -05:00
# auto-generated by lesavka/scripts/daemon/install-server.sh - DO NOT EDIT
2025-06-28 00:24:23 -05:00
SUBSYSTEM=="video4linux", ATTR{index}=="0", ENV{ID_PATH_TAG}=="$LEFT_TAG", SYMLINK+="lesavka_l_eye"
SUBSYSTEM=="video4linux", ATTR{index}=="0", ENV{ID_PATH_TAG}=="$RIGHT_TAG", SYMLINK+="lesavka_r_eye"
2025-06-25 07:46:50 -05:00
EOF
2025-06-25 08:25:13 -05:00
2025-06-23 00:26:02 -05:00
sudo udevadm control --reload
sudo udevadm trigger --subsystem-match=video4linux
2025-06-25 09:21:39 -05:00
sudo udevadm settle
2025-06-23 00:26:02 -05:00
echo "==> 3. Rust toolchain"
2025-06-01 21:30:55 -05:00
sudo rustup default stable
sudo -u "$ORIG_USER" rustup default stable
2025-06-01 13:31:22 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 4a. Source checkout"
SRC_DIR=/var/src/lesavka
REPO_URL=ssh://git@scm.bstein.dev:2242/brad_stein/lesavka.git
2025-06-01 21:30:55 -05:00
if [[ ! -d $SRC_DIR ]]; then
sudo mkdir -p /var/src
sudo chown "$ORIG_USER":"$ORIG_USER" /var/src
2025-06-01 14:18:42 -05:00
fi
2025-06-01 21:30:55 -05:00
if [[ -d $SRC_DIR/.git ]]; then
sudo -u "$ORIG_USER" git -C "$SRC_DIR" pull --ff-only
2025-06-01 14:18:42 -05:00
else
2025-06-01 22:12:09 -05:00
sudo -u "$ORIG_USER" git clone "$REPO_URL" "$SRC_DIR"
2025-07-04 18:24:48 -05:00
fip
if sudo -u "$ORIG_USER" git -C "$SRC_DIR" rev-parse --verify --quiet "origin/$REF" >/dev/null; then
sudo -u "$ORIG_USER" git -C "$SRC_DIR" checkout -B "$REF" "origin/$REF"
else
sudo -u "$ORIG_USER" git -C "$SRC_DIR" checkout --force "$REF"
2025-06-01 14:18:42 -05:00
fi
2025-06-01 13:31:22 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 4b. Source build"
2025-06-16 21:47:01 -05:00
sudo -u "$ORIG_USER" bash -c "cd '$SRC_DIR/server' && cargo clean && cargo build --release"
2025-06-01 13:31:22 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 5. Install binaries"
2025-06-23 21:56:11 -05:00
sudo install -Dm755 "$SRC_DIR/server/target/release/lesavka-server" /usr/local/bin/lesavka-server
2025-06-27 22:51:50 -05:00
sudo install -Dm755 "$SRC_DIR/scripts/daemon/lesavka-core.sh" /usr/local/bin/lesavka-core.sh
2025-06-01 14:18:42 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 6a. Systemd units - lesavka-core"
cat <<'UNIT' | sudo tee /etc/systemd/system/lesavka-core.service >/dev/null
2025-06-01 13:31:22 -05:00
[Unit]
2025-06-23 00:26:02 -05:00
Description=lesavka USB gadget bring-up
2025-06-06 00:04:55 -05:00
After=sys-kernel-config.mount
Requires=sys-kernel-config.mount
2025-06-23 00:26:02 -05:00
2025-06-01 13:31:22 -05:00
[Service]
2025-06-01 14:18:42 -05:00
Type=oneshot
2025-06-23 00:26:02 -05:00
ExecStart=/usr/local/bin/lesavka-core.sh
2025-06-01 14:18:42 -05:00
RemainAfterExit=yes
2025-06-06 00:04:55 -05:00
CapabilityBoundingSet=CAP_SYS_ADMIN
2025-06-06 00:41:32 -05:00
MountFlags=slave
2025-06-23 00:26:02 -05:00
2025-06-01 13:31:22 -05:00
[Install]
WantedBy=multi-user.target
2025-06-01 21:30:55 -05:00
UNIT
2025-06-01 13:31:22 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 6b. Systemd units - lesavka-server"
cat <<'UNIT' | sudo tee /etc/systemd/system/lesavka-server.service >/dev/null
2025-06-01 13:31:22 -05:00
[Unit]
2025-06-23 00:26:02 -05:00
Description=lesavka gRPC relay
After=network.target lesavka-core.service
2025-06-01 13:31:22 -05:00
[Service]
2025-06-23 00:26:02 -05:00
ExecStart=/usr/local/bin/lesavka-server
2025-06-01 14:18:42 -05:00
Restart=always
2025-07-04 18:00:49 -05:00
Environment=RUST_LOG=lesavka_server=info,lesavka_server::audio=info,lesavka_server::video=debug,lesavka_server::gadget=info
2025-06-25 20:00:34 -05:00
Environment=RUST_BACKTRACE=1
2025-07-01 10:23:51 -05:00
Environment=GST_DEBUG="*:2,alsasink:6,alsasrc:6"
2025-06-25 20:00:34 -05:00
Restart=always
2025-06-25 22:24:58 -05:00
RestartSec=5
2025-06-28 19:40:48 -05:00
StandardError=append:/tmp/lesavka-server.stderr
2025-06-25 20:00:34 -05:00
StartLimitIntervalSec=30
StartLimitBurst=10
2025-06-01 14:18:42 -05:00
User=root
2025-06-23 00:26:02 -05:00
2025-06-01 13:31:22 -05:00
[Install]
WantedBy=multi-user.target
2025-06-01 21:30:55 -05:00
UNIT
2025-06-01 13:31:22 -05:00
2025-06-23 00:26:02 -05:00
echo "==> 6c. Systemd units - initialization"
2025-06-25 20:36:42 -05:00
sudo truncate -s 0 /tmp/lesavka-server.log
2025-06-01 13:31:22 -05:00
sudo systemctl daemon-reload
2025-06-23 00:26:02 -05:00
sudo systemctl enable --now lesavka-core
sudo systemctl restart lesavka-core
echo "✅ lesavka-core installed and restarted..."
2025-06-06 20:45:09 -05:00
2025-06-23 00:26:02 -05:00
sudo systemctl enable --now lesavka-server
sudo systemctl restart lesavka-server
echo "✅ lesavka-server installed and restarted..."