Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / infra / agents / host-setup.sh
host-setup.sh171 lines · 7.6 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # --------------------------------------------------------------------------- |
| 3 | # agents host setup: one-time, idempotent preparation of the maproj host for |
| 4 | # the `agents` compose project (infra/agents/compose.yml). |
| 5 | # |
| 6 | # sudo bash infra/agents/host-setup.sh # apply |
| 7 | # sudo bash infra/agents/host-setup.sh --dry-run # print actions only |
| 8 | # |
| 9 | # Steps: |
| 10 | # (a) create /root/agent-world (0700), |
| 11 | # (b) add an 8 GiB swapfile /swapfile-agents alongside any existing swap |
| 12 | # (never touches existing swap), |
| 13 | # (c) create a sparse 60 GiB ext4 image /root/agent-world/builds.img, |
| 14 | # loop-mount it at /root/agent-world/builds via fstab, and create the |
| 15 | # gradle-cache / cargo-home / android-sdk / slots subdirs, |
| 16 | # (d) install a systemd oneshot (agents-firewall.service) that idempotently |
| 17 | # blocks sandbox-egress abuse: drops 172.31.250.0/24 -> RFC1918/APIPA |
| 18 | # in DOCKER-USER (established replies allowed first) and drops |
| 19 | # 172.31.250.0/24 -> host in INPUT (container->host services such as |
| 20 | # the wg-mail tunnel's 10.66.0.2:25 hit INPUT, not FORWARD). |
| 21 | # |
| 22 | # It never runs a docker command that changes state, and never touches |
| 23 | # /root/location_share_server, existing swap, or snap Docker itself. |
| 24 | # --------------------------------------------------------------------------- |
| 25 | set -euo pipefail |
| 26 | |
| 27 | AGENT_WORLD=/root/agent-world |
| 28 | BUILDS_DIR=$AGENT_WORLD/builds |
| 29 | BUILDS_IMG=$AGENT_WORLD/builds.img |
| 30 | SWAPFILE=/swapfile-agents |
| 31 | EGRESS_SUBNET=172.31.250.0/24 |
| 32 | FIREWALL_SERVICE=/etc/systemd/system/agents-firewall.service |
| 33 | FIREWALL_SCRIPT=/usr/local/sbin/agents-firewall.sh |
| 34 | |
| 35 | DRY_RUN=0 |
| 36 | if [[ "${1:-}" == "--dry-run" ]]; then |
| 37 | DRY_RUN=1 |
| 38 | elif [[ -n "${1:-}" ]]; then |
| 39 | echo "usage: $0 [--dry-run]" >&2 |
| 40 | exit 2 |
| 41 | fi |
| 42 | |
| 43 | log() { echo "[host-setup] $*"; } |
| 44 | |
| 45 | if [[ "$(uname -s)" != "Linux" ]]; then |
| 46 | echo "host-setup: refusing: not Linux ($(uname -s))" >&2 |
| 47 | exit 1 |
| 48 | fi |
| 49 | if [[ $EUID -ne 0 ]]; then |
| 50 | echo "host-setup: refusing: must run as root" >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | if [[ $DRY_RUN -eq 1 ]]; then |
| 54 | log "dry-run mode: printing actions without changing state" |
| 55 | fi |
| 56 | |
| 57 | # -- (a) agent world dir ----------------------------------------------------- |
| 58 | log "ensuring $AGENT_WORLD (0700)" |
| 59 | if [[ $DRY_RUN -eq 1 ]]; then |
| 60 | echo "[dry-run] would run: mkdir -p $AGENT_WORLD && chmod 0700 $AGENT_WORLD" |
| 61 | else |
| 62 | mkdir -p "$AGENT_WORLD" |
| 63 | chmod 0700 "$AGENT_WORLD" |
| 64 | fi |
| 65 | |
| 66 | # -- (b) extra swap (added alongside the host's existing swap) ---------------- |
| 67 | if swapon --show=NAME --noheadings 2>/dev/null | grep -qx "$SWAPFILE"; then |
| 68 | log "$SWAPFILE already active" |
| 69 | else |
| 70 | log "adding 8 GiB $SWAPFILE (existing swap left untouched)" |
| 71 | if [[ $DRY_RUN -eq 1 ]]; then |
| 72 | echo "[dry-run] would run: fallocate -l 8G $SWAPFILE && chmod 600 $SWAPFILE && mkswap $SWAPFILE && swapon $SWAPFILE" |
| 73 | echo "[dry-run] would append to /etc/fstab: $SWAPFILE none swap sw 0 0" |
| 74 | else |
| 75 | if [[ ! -f "$SWAPFILE" ]]; then |
| 76 | fallocate -l 8G "$SWAPFILE" |
| 77 | chmod 600 "$SWAPFILE" |
| 78 | mkswap "$SWAPFILE" |
| 79 | fi |
| 80 | swapon "$SWAPFILE" |
| 81 | grep -qs "^$SWAPFILE " /etc/fstab || echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab |
| 82 | fi |
| 83 | fi |
| 84 | |
| 85 | # -- (c) size-bounded builds image ------------------------------------------- |
| 86 | log "ensuring 60 GiB ext4 image $BUILDS_IMG mounted at $BUILDS_DIR" |
| 87 | if [[ $DRY_RUN -eq 1 ]]; then |
| 88 | echo "[dry-run] would run: truncate -s 60G $BUILDS_IMG && mkfs.ext4 -F $BUILDS_IMG" |
| 89 | echo "[dry-run] would append to /etc/fstab: $BUILDS_IMG $BUILDS_DIR ext4 loop,nofail,x-systemd.before=snap.docker.dockerd.service 0 0" |
| 90 | echo "[dry-run] would run: mkdir -p $BUILDS_DIR && mount $BUILDS_DIR" |
| 91 | echo "[dry-run] would run: mkdir -p $BUILDS_DIR/{gradle-cache,cargo-home,android-sdk,slots} + .on-loop-fs sentinels" |
| 92 | else |
| 93 | if [[ ! -f "$BUILDS_IMG" ]]; then |
| 94 | truncate -s 60G "$BUILDS_IMG" |
| 95 | mkfs.ext4 -F "$BUILDS_IMG" |
| 96 | fi |
| 97 | # Ordered before Docker (so restarted containers bind the loop fs, not the |
| 98 | # bare dir on /) without making prod Docker depend on it (nofail). |
| 99 | FSTAB_LINE="$BUILDS_IMG $BUILDS_DIR ext4 loop,nofail,x-systemd.before=snap.docker.dockerd.service 0 0" |
| 100 | grep -qsF "$BUILDS_IMG $BUILDS_DIR" /etc/fstab || echo "$FSTAB_LINE" >> /etc/fstab |
| 101 | systemctl daemon-reload |
| 102 | mkdir -p "$BUILDS_DIR" |
| 103 | mountpoint -q "$BUILDS_DIR" || mount "$BUILDS_DIR" |
| 104 | mountpoint -q "$BUILDS_DIR" || { echo "host-setup: $BUILDS_DIR is not mounted" >&2; exit 1; } |
| 105 | # Sentinels mark the loop fs: if it is ever unmounted, builder-init sees |
| 106 | # them missing and refuses to start instead of writing to /. Only in dirs |
| 107 | # Docker never seeds (a non-empty volume skips image seeding, so |
| 108 | # cargo-home/android-sdk stay empty; builder-init checks those by device). |
| 109 | for sub in gradle-cache cargo-home android-sdk slots; do |
| 110 | mkdir -p "$BUILDS_DIR/$sub" |
| 111 | done |
| 112 | touch "$BUILDS_DIR/gradle-cache/.on-loop-fs" "$BUILDS_DIR/slots/.on-loop-fs" |
| 113 | fi |
| 114 | |
| 115 | # -- (d) egress firewall ------------------------------------------------------ |
| 116 | log "installing $FIREWALL_SERVICE" |
| 117 | if [[ $DRY_RUN -eq 1 ]]; then |
| 118 | echo "[dry-run] would write $FIREWALL_SCRIPT (idempotent iptables script)" |
| 119 | echo "[dry-run] would write $FIREWALL_SERVICE (oneshot, After/Requires snap.docker.dockerd.service)" |
| 120 | echo "[dry-run] would run: systemctl daemon-reload && systemctl enable --now agents-firewall.service" |
| 121 | else |
| 122 | cat > "$FIREWALL_SCRIPT" <<EOF |
| 123 | #!/usr/bin/env bash |
| 124 | # agents-firewall: confine the agents-egress subnet ($EGRESS_SUBNET). |
| 125 | # Idempotent: rules carry a comment marker and are only added when absent. |
| 126 | set -euo pipefail |
| 127 | MARK="agents-egress-guard" |
| 128 | have() { iptables -C "\$@" -m comment --comment "\$MARK" 2>/dev/null; } |
| 129 | # Replies to connections the sandbox initiated must keep working. |
| 130 | have DOCKER-USER -s $EGRESS_SUBNET -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT \\ |
| 131 | || iptables -I DOCKER-USER 1 -s $EGRESS_SUBNET -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT -m comment --comment "\$MARK" |
| 132 | # No sandbox -> private-network traffic (lateral movement, metadata, proxies). |
| 133 | for NET in 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 169.254.0.0/16; do |
| 134 | have DOCKER-USER -s $EGRESS_SUBNET -d "\$NET" -j DROP \\ |
| 135 | || iptables -I DOCKER-USER 2 -s $EGRESS_SUBNET -d "\$NET" -j DROP -m comment --comment "\$MARK" |
| 136 | done |
| 137 | # No sandbox -> host services (container-to-host packets traverse INPUT; |
| 138 | # e.g. the wg-mail tunnel's 10.66.0.2:25/:8081 must stay unreachable). |
| 139 | have INPUT -s $EGRESS_SUBNET -j DROP \\ |
| 140 | || iptables -I INPUT 1 -s $EGRESS_SUBNET -j DROP -m comment --comment "\$MARK" |
| 141 | EOF |
| 142 | chmod 0755 "$FIREWALL_SCRIPT" |
| 143 | cat > "$FIREWALL_SERVICE" <<EOF |
| 144 | [Unit] |
| 145 | Description=Confine agents sandbox egress (agents-egress subnet) |
| 146 | After=snap.docker.dockerd.service |
| 147 | Requires=snap.docker.dockerd.service |
| 148 | |
| 149 | [Service] |
| 150 | Type=oneshot |
| 151 | RemainAfterExit=yes |
| 152 | ExecStart=$FIREWALL_SCRIPT |
| 153 | |
| 154 | [Install] |
| 155 | WantedBy=multi-user.target snap.docker.dockerd.service |
| 156 | EOF |
| 157 | systemctl daemon-reload |
| 158 | systemctl enable --now agents-firewall.service |
| 159 | fi |
| 160 | |
| 161 | # -- (e) next steps ----------------------------------------------------------- |
| 162 | cat <<'NEXT' |
| 163 | |
| 164 | host-setup done. Next steps (on the host, as root): |
| 165 | 1. Clone the deploy checkout: git clone <Modern-Apps> /root/agent-world/Modern-Apps |
| 166 | 2. Create /root/agent-world/.env from infra/agents/.env.example (fill secrets). |
| 167 | 3. Ensure the base image exists: docker compose -f /root/location_share_server/compose.yml --profile release build builder |
| 168 | (produces location-share-builder:local; never touch the running project). |
| 169 | 4. Review, then start: cd /root/agent-world/Modern-Apps && docker compose -p agents -f infra/agents/compose.yml up -d --build |
| 170 | 5. Verify: curl the runner via agentd; see infra/agents/README.md. |
| 171 | NEXT |