Files
irc-bot/build_deb.sh
tocmo0nlord ff3f6fe05b Add Debian .deb package support
- debian/control, changelog, conffiles — package metadata
- debian/postinst — creates irc-bot system user, installs Python venv,
  symlinks runtime dirs, enables systemd services
- debian/prerm/postrm — clean stop/uninstall with purge support
- debian/systemd/ — hardened systemd units for bot and portal
- build_deb.sh — assembles and builds the .deb via dpkg-deb
- Path resolver in irc_client.py, memory.py, config_manager.py, portal/app.py:
  uses /etc/irc-bot + /var/lib/irc-bot when installed as .deb, relative
  paths otherwise (Docker/venv unchanged)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-17 22:16:05 -04:00

81 lines
3.8 KiB
Bash

#!/bin/bash
# Build a .deb package for irc-bot.
# Run on any Debian/Ubuntu host: bash build_deb.sh
set -euo pipefail
PACKAGE=irc-bot
VERSION=1.0.0
ARCH=all
DEB_NAME="${PACKAGE}_${VERSION}_${ARCH}.deb"
BUILD_DIR="$(mktemp -d)"
PKG_ROOT="${BUILD_DIR}/${PACKAGE}"
echo "==> Building ${DEB_NAME} in ${BUILD_DIR}"
# ── Directory layout inside the .deb ─────────────────────────────────────────
install -d "${PKG_ROOT}/DEBIAN"
install -d "${PKG_ROOT}/opt/irc-bot/bot"
install -d "${PKG_ROOT}/opt/irc-bot/portal/templates"
install -d "${PKG_ROOT}/opt/irc-bot/portal/static"
install -d "${PKG_ROOT}/opt/irc-bot/config"
install -d "${PKG_ROOT}/lib/systemd/system"
# Runtime dirs are created by postinst, not shipped in the package
# (avoids dpkg owning /var/lib/irc-bot with wrong permissions before user exists)
# ── Application source ────────────────────────────────────────────────────────
cp bot/__init__.py "${PKG_ROOT}/opt/irc-bot/bot/"
cp bot/irc_client.py "${PKG_ROOT}/opt/irc-bot/bot/"
cp bot/llm_client.py "${PKG_ROOT}/opt/irc-bot/bot/"
cp bot/memory.py "${PKG_ROOT}/opt/irc-bot/bot/"
cp bot/message_handler.py "${PKG_ROOT}/opt/irc-bot/bot/"
cp portal/__init__.py "${PKG_ROOT}/opt/irc-bot/portal/"
cp portal/app.py "${PKG_ROOT}/opt/irc-bot/portal/"
cp portal/config_manager.py "${PKG_ROOT}/opt/irc-bot/portal/"
cp -r portal/templates/ "${PKG_ROOT}/opt/irc-bot/portal/"
cp -r portal/static/ "${PKG_ROOT}/opt/irc-bot/portal/"
cp requirements.txt "${PKG_ROOT}/opt/irc-bot/"
cp .env.example "${PKG_ROOT}/opt/irc-bot/"
cp README.md "${PKG_ROOT}/opt/irc-bot/"
# Default config.json — marked as conffile so dpkg won't overwrite on upgrade
install -d "${PKG_ROOT}/etc/irc-bot"
cp config/config.json "${PKG_ROOT}/etc/irc-bot/config.json"
# ── Systemd units ─────────────────────────────────────────────────────────────
cp debian/systemd/irc-bot.service "${PKG_ROOT}/lib/systemd/system/"
cp debian/systemd/irc-bot-portal.service "${PKG_ROOT}/lib/systemd/system/"
# ── DEBIAN control files ──────────────────────────────────────────────────────
cp debian/control "${PKG_ROOT}/DEBIAN/control"
cp debian/conffiles "${PKG_ROOT}/DEBIAN/conffiles"
cp debian/postinst "${PKG_ROOT}/DEBIAN/postinst"
cp debian/prerm "${PKG_ROOT}/DEBIAN/prerm"
cp debian/postrm "${PKG_ROOT}/DEBIAN/postrm"
chmod 755 "${PKG_ROOT}/DEBIAN/postinst"
chmod 755 "${PKG_ROOT}/DEBIAN/prerm"
chmod 755 "${PKG_ROOT}/DEBIAN/postrm"
# ── Installed-Size (approximate, in KB) ──────────────────────────────────────
SIZE_KB=$(du -sk "${PKG_ROOT}" | cut -f1)
sed -i "s/^Installed-Size:.*/Installed-Size: ${SIZE_KB}/" "${PKG_ROOT}/DEBIAN/control" 2>/dev/null || true
printf "\nInstalled-Size: ${SIZE_KB}\n" >> "${PKG_ROOT}/DEBIAN/control"
# ── Build ─────────────────────────────────────────────────────────────────────
dpkg-deb --build --root-owner-group "${PKG_ROOT}" "${DEB_NAME}"
echo ""
echo "==> Built: ${DEB_NAME}"
echo ""
echo "Install with:"
echo " sudo dpkg -i ${DEB_NAME}"
echo " sudo apt-get install -f # resolve any missing deps"
echo ""
echo "Then edit /etc/irc-bot/.env and start:"
echo " sudo systemctl start irc-bot irc-bot-portal"
# Cleanup
rm -rf "${BUILD_DIR}"