Files
cs53-portfolio/lab-01-trace-request/scenario/setup.sh
T
2026-09-22 17:32:15 +00:00

150 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LAB_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$LAB_DIR/../.." && pwd)"
source "$SCRIPT_DIR/config.sh"
source "$REPO_ROOT/shared/minicorp/lib/minicorp.sh"
source "$REPO_ROOT/shared/minicorp/topologies/client-router-server.sh"
printf '\n'
mc_log "Setting up $LAB_NAME"
if [[ "$LAB_TOPOLOGY" != "client-router-server" ]]; then
mc_error "Unsupported topology: $LAB_TOPOLOGY"
exit 1
fi
# Verify the tools required by this lab.
for command_name in python3 dnsmasq ss curl getent dig; do
mc_require_command "$command_name"
done
# Build the MiniCorp network from a known state.
# Suppress topology details so setup does not reveal investigation answers.
mc_log "Building MiniCorp virtual network"
mc_topology_client_router_server_up >/dev/null
# Verify the topology without exposing the investigation answers.
if ! mc_topology_client_router_server_check >/dev/null; then
mc_error "MiniCorp topology validation failed."
exit 1
fi
# Remove stale service output from a previous run.
sudo rm -f \
"$LAB_WEB_STDOUT" \
"$LAB_DNS_STDOUT"
#
# Web service
#
mc_log "Starting MiniCorp web service"
sudo ip netns exec "$MC_SERVER_NS" env \
MINICORP_WEB_HOST="$MC_SERVER_ADDR" \
MINICORP_WEB_PORT="$LAB_WEB_PORT" \
python3 -u "$SCRIPT_DIR/web_server.py" \
>"$LAB_WEB_STDOUT" 2>&1 &
web_ready=0
for attempt in {1..20}; do
if mc_exec "$MC_SERVER_NS" ss -ltn |
grep -F "$MC_SERVER_ADDR:$LAB_WEB_PORT" >/dev/null
then
web_ready=1
break
fi
sleep 0.1
done
if [[ "$web_ready" -ne 1 ]]; then
mc_error "Web service failed to start."
if [[ -f "$LAB_WEB_STDOUT" ]]; then
cat "$LAB_WEB_STDOUT" >&2
fi
exit 1
fi
#
# DNS service
#
mc_log "Starting MiniCorp DNS service"
sudo ip netns exec "$MC_ROUTER_NS" dnsmasq \
--no-daemon \
--interface="$MC_ROUTER_CLIENT_IF" \
--listen-address="$MC_ROUTER_CLIENT_ADDR" \
--bind-interfaces \
--no-resolv \
--no-hosts \
--local=/minicorp.test/ \
--host-record="$LAB_WEB_HOSTNAME,$MC_SERVER_ADDR" \
--log-facility=- \
>"$LAB_DNS_STDOUT" 2>&1 &
dns_ready=0
for attempt in {1..20}; do
if mc_exec "$MC_ROUTER_NS" ss -lun |
grep -F "$MC_ROUTER_CLIENT_ADDR:53" >/dev/null
then
dns_ready=1
break
fi
sleep 0.1
done
if [[ "$dns_ready" -ne 1 ]]; then
mc_error "DNS service failed to start."
if [[ -f "$LAB_DNS_STDOUT" ]]; then
cat "$LAB_DNS_STDOUT" >&2
fi
exit 1
fi
#
# Client resolver
#
mc_log "Configuring client DNS resolver"
sudo mkdir -p "$LAB_CLIENT_RESOLV_DIR"
printf 'nameserver %s\n' "$MC_ROUTER_CLIENT_ADDR" |
sudo tee "$LAB_CLIENT_RESOLV_CONF" >/dev/null
#
# Final health check
#
if ! mc_exec "$MC_CLIENT_NS" \
getent ahostsv4 "$LAB_WEB_HOSTNAME" >/dev/null
then
mc_error "Client name resolution check failed."
exit 1
fi
if ! mc_exec "$MC_CLIENT_NS" \
curl --noproxy '*' -fsS \
"http://$LAB_WEB_HOSTNAME:$LAB_WEB_PORT/" >/dev/null
then
mc_error "Client web request check failed."
exit 1
fi
printf '\n'
mc_log "$LAB_NAME is ready."