#!/usr/bin/env bash
# Real demos for P183 "Every PostgreSQL Setting to Change After You Install It". Runs on render01.
# Two Ubuntu 24.04 LXD system containers on render01's lxdbr0 bridge: "bakesale-db", the school bake sale's database
# server (4 GB of memory, 2 CPUs, so the sizes in the video are a real small server's), with PostgreSQL from
# Ubuntu's own archive, and "orders", the server that runs the bake sale's order page. Every command shown is typed
# into Chris's (or Sarah's) real login shell in a pty (demo_driver.py). Nothing outside the two containers is touched.
#
# Needs: the LXD snap on render01 (ubuntu:24.04 image) and internet for the Ubuntu archive.
set -euo pipefail
cd "$(dirname "$0")"
D=ce-p183-db O=ce-p183-orders
cleanup() { lxc delete -f $D $O >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
lxc launch -q ubuntu:24.04 $D --network lxdbr0 -c limits.memory=4GiB -c limits.cpu=2 \
  -c user.user-data="$(printf '#cloud-config\nhostname: bakesale-db\n')"
lxc launch -q ubuntu:24.04 $O --network lxdbr0 -c user.user-data="$(printf '#cloud-config\nhostname: orders\n')"
for c in $D $O; do lxc exec $c -- cloud-init status --wait >/dev/null || true; done
lxc file push -q lab/seed.sql $D/root/
python3 - "$D" "$O" <demo_driver.py
