#!/usr/bin/env bash
# Real demos for P131 "How to Fix Python SSL: CERTIFICATE_VERIFY_FAILED Errors". Made on the Mac (Docker Desktop).
# Chris runs his family dairy farm's milk-tank script on the farm PC: it posts tank readings to the dairy co-op's
# portal (portal.coop.lan), whose certificate comes from the co-op's own certificate authority. Sarah works at the
# co-op office, whose network inspects HTTPS through a proxy. Four containers on one Docker network (lab/Dockerfile):
# the portal (stock nginx), the office proxy (stock mitmproxy), the farm PC and Sarah's laptop (stock Ubuntu 24.04
# with Ubuntu's Python 3.12). Packages come from PyPI on camera. Every command shown is typed into a real
# interactive bash in a pty (demo_driver.py); every line of output is real.
set -euo pipefail
cd "$(dirname "$0")"
NET=ce-p131
cleanup() { docker rm -f ce-p131-portal ce-p131-proxy ce-p131-pc ce-p131-laptop >/dev/null 2>&1 || true; docker network rm $NET >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
for t in portal proxy pc; do docker build -q --target $t -t ce-p131-$t lab >/dev/null; done
docker network create $NET >/dev/null
docker run -d --name ce-p131-portal --network $NET --network-alias portal.coop.lan --network-alias tank.coop.lan \
  --network-alias portal ce-p131-portal >/dev/null
docker run -d --name ce-p131-proxy --network $NET --network-alias proxy.coop.lan ce-p131-proxy >/dev/null
docker run -d --name ce-p131-pc --hostname farm-pc --network $NET ce-p131-pc >/dev/null
docker run -d --name ce-p131-laptop --hostname office-laptop --network $NET ce-p131-pc >/dev/null
sleep 2
python3 demo_driver.py
