#!/usr/bin/env bash # Bring up the dockerized fleet: sbfleet-central (salt-master + salt-minion id strawberry-central) # and sbfleet-sensor-1..3 (salt-minion ids sensor-1..3), each a systemd-as-PID-1 container. # # Salt comes from the host onedir (/opt/saltstack, mounted read-only). The release tarball built # by build_tarball.sh is served to the minions via salt://strawberry/release (a writable dir that # also holds salt://strawberry/dist, where the central cycle publishes). # # systemd in ROOTLESS docker: the container cgroup2 is read-only and docker rejects # writable-cgroups under rootless, so each container gets --cgroupns=host plus a private, # delegated cgroup directory bind-mounted at /sys/fs/cgroup (no --privileged). Because that # moves systemd out of docker's own scope, the --cpus/--memory caps are applied to that # directory directly (cpu.max / memory.max). # # The sensor unit's CapabilityBoundingSet/AmbientCapabilities need the caps in the container # (rootless: they are scoped to the container's user namespace, not the host). # # TEST ONLY: the master runs with auto_accept: True. set -euo pipefail # shellcheck source=fleet_common.sh . "$(dirname "${BASH_SOURCE[0]}")/fleet_common.sh" CPUS=${FLEET_CPUS:-1} # cpus per container MEM=${FLEET_MEM:-2g} # 1. release tarball (cached per HEAD) + pin "$FLEET_HERE/build_tarball.sh" head12=$(git -C "$FLEET_REPO" rev-parse --short=12 HEAD) tgz=$(ls -t "$FLEET_DIR"/dist/strawberry-*+g"${head12}"-noble-amd64.tgz | head -1) base=$(basename "$tgz") ver=${base#strawberry-}; ver=${ver%-noble-amd64.tgz} sha=$(cut -d' ' -f1 "$tgz.sha256") echo "release $ver sha256 $sha" # 2. generated master state: release pillar + served tarball (+ empty dist) find "$FLEET_GEN" -mindepth 1 -delete 2>/dev/null || true mkdir -p "$FLEET_GEN/pillar" "$FLEET_GEN/srv-salt/strawberry/release/v$ver" "$FLEET_GEN/srv-salt/strawberry/dist" command cp -f "$tgz" "$FLEET_GEN/srv-salt/strawberry/release/v$ver/$base" cat >"$FLEET_GEN/pillar/strawberry_release.sls" </dev/null 2>&1 || \ nice -n 19 docker build --cpu-quota=200000 --cpu-period=100000 --memory=4g \ -t "$FLEET_IMAGE" -f "$FLEET_HERE/Dockerfile.node" "$FLEET_HERE" >"$FLEET_LOGS/02-image.log" 2>&1 docker network inspect "$FLEET_NET" >/dev/null 2>&1 || docker network create "$FLEET_NET" >/dev/null # 4. containers start_node() { local c=$1; shift docker rm -f "$c" >/dev/null 2>&1 || true local cg=$CG_ROOT/$c mkdir -p "$cg" # cap the whole subtree (systemd moves itself out of docker's scope, see the header) echo "+cpu +memory +pids" >"$CG_ROOT/cgroup.subtree_control" 2>/dev/null || true echo "$((CPUS * 100000)) 100000" >"$cg/cpu.max" case "$MEM" in *g) echo $(( ${MEM%g} * 1024 * 1024 * 1024 )) >"$cg/memory.max" ;; *m) echo $(( ${MEM%m} * 1024 * 1024 )) >"$cg/memory.max" ;; esac docker run -d --name "$c" --hostname "$c" --network "$FLEET_NET" \ --cpus="$CPUS" --memory="$MEM" \ --cgroupns=host -v "$cg":/sys/fs/cgroup:rw \ --tmpfs /run --tmpfs /run/lock --tmpfs /tmp \ --stop-signal SIGRTMIN+3 \ --cap-add SYS_ADMIN --cap-add BPF --cap-add PERFMON --cap-add SYS_RESOURCE \ --cap-add SYS_PTRACE --cap-add DAC_READ_SEARCH --cap-add KILL \ -v /opt/saltstack:/opt/saltstack:ro \ "$@" "$FLEET_IMAGE" >/dev/null } start_node "$FLEET_CENTRAL" \ -v "$FLEET_GEN/srv-salt":/srv/salt:rw \ -v "$FLEET_GEN":/opt/fleet-gen:ro \ -v "$FLEET_REPO/deploy":/opt/repo/deploy:ro for c in "${FLEET_SENSORS[@]}"; do start_node "$c"; done for c in "${FLEET_ALL[@]}"; do for _ in $(seq 1 60); do st=$(docker exec "$c" systemctl is-system-running 2>/dev/null || true) case "$st" in running|degraded) break ;; esac sleep 1 done echo "$c systemd: $st" case "$st" in running|degraded) ;; *) echo "systemd did not boot in $c" >&2; exit 1 ;; esac done # Name resolution without Docker's embedded DNS (127.0.0.11 stopped answering inside the systemd # containers on 2026-11-14 while IP connectivity was fine): pin every fleet name in /etc/hosts. hosts="" for c in "${FLEET_ALL[@]}"; do ip=$(docker inspect -f "{{(index .NetworkSettings.Networks \"$FLEET_NET\").IPAddress}}" "$c") [ -n "$ip" ] || { echo "no IP for $c on $FLEET_NET" >&2; exit 1; } hosts+="$ip $c"$'\n' done for c in "${FLEET_ALL[@]}"; do printf '%s' "$hosts" | docker exec -i "$c" bash -c 'cat >>/etc/hosts'; done docker exec "${FLEET_SENSORS[0]}" getent hosts "$FLEET_CENTRAL" >/dev/null || { echo "hosts pinning failed" >&2; exit 1; } # 5. salt master (central) + minions docker exec -i "$FLEET_CENTRAL" bash -c 'mkdir -p /etc/salt/master.d && cat >/etc/salt/master.d/fleet.conf' <<'EOT' interface: 0.0.0.0 worker_threads: 3 file_recv: True # lets minions cp.push their bundles auto_accept: True # TEST ONLY file_roots: base: - /srv/salt # strawberry/dist (written by the cycle), strawberry/release (tarball) - /opt/repo/deploy/salt/salt # salt://strawberry/*, salt://orch/* - /opt/repo/deploy/salt # salt://files/* pillar_roots: base: - /opt/fleet-gen/pillar # generated release pin - /opt/repo/deploy/salt/test/pillar # test overrides - /opt/repo/deploy/salt/pillar EOT unit() { # docker exec -i "$1" bash -c "cat >/etc/systemd/system/$2.service" </etc/salt/minion.d/fleet.conf' </dev/null | grep -c 'True' || true) [ "${n:-0}" -ge 4 ] && break sleep 3 done [ "${n:-0}" -ge 4 ] || { echo "only ${n:-0}/4 minions answered test.ping" >&2; exit 1; } echo "fleet up: master+central, sensor-1..3 (4/4 minions up), release $ver"