#!/usr/bin/env bash
# Real demos for P175 "How to Fix Python 'Address Already in Use' Errors". Made on the Mac (Docker Desktop).
# Two containers from one stock Ubuntu 24.04 image (lab/Dockerfile) on one Docker network: the greenhouse's small
# computer (192.168.40.10, Ana and Ben each have the two programs in ~/greenhouse: a Flask dashboard on port 5000
# and a sensor collector on port 9000 written with Python's socket module) and the bench sensor box (192.168.40.21,
# sends one reading at a time). The greenhouse box gets CAP_SYS_PTRACE so root's `ss -p` can name another user's
# process, as on a normal Ubuntu machine. Every command shown is typed into Ana's real interactive bash in a pty
# (demo_driver.py), in two terminal windows.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p175
cleanup() { docker rm -f $P-greenhouse $P-sensors >/dev/null 2>&1 || true; docker network rm $P-net >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P -f lab/Dockerfile lab/files >/dev/null
docker network create --subnet 192.168.40.0/24 $P-net >/dev/null
docker run -d --name $P-greenhouse --hostname greenhouse --network $P-net --ip 192.168.40.10 --cap-add SYS_PTRACE $P >/dev/null
docker run -d --name $P-sensors --hostname sensors --network $P-net --ip 192.168.40.21 $P >/dev/null
python3 demo_driver.py $P
