#!/usr/bin/env bash
# Real demos for P063 "How to Fix Docker Port Already Allocated Errors". Runs on render01.
# One Ubuntu 24.04 LXD system container, "laptop" (Ana's laptop), with Ubuntu's own Docker (docker.io) and
# Compose v2, on render01's lxdbr0 bridge (security.nesting=true, as in P034). Ana's tea shop website is a tiny
# nginx image built off-screen in two versions (shop:v1, shop:v2; lab/Dockerfile). Version 1 was started last
# week (off-screen, the same docker run Ana types later); everything after that is typed on screen into real
# login shells in a pty (demo_driver.py), one per terminal window. Nothing outside the container is touched.
#
# Needs: the LXD snap on render01 (ubuntu:24.04 image), internet for the Ubuntu archive and Docker Hub (nginx).
set -euo pipefail
cd "$(dirname "$0")"
C=ce-p063-laptop
cleanup() { lxc delete -f $C >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
lxc launch -q ubuntu:24.04 $C --network lxdbr0 -c security.nesting=true \
  -c security.syscalls.intercept.mknod=true -c security.syscalls.intercept.setxattr=true \
  -c user.user-data="$(printf '#cloud-config\nhostname: laptop\n')"
lxc exec $C -- cloud-init status --wait >/dev/null || true
lxc exec $C -- mkdir -p /root/lab
lxc file push -q lab/Dockerfile lab/compose.yaml $C/root/lab/
python3 - "$C" <demo_driver.py
