#!/bin/bash # A macOS node's start job (launchd, as root, at every boot): what the Linux # node's entrypoint does, minus the fence (the machine's firewall is the # boundary) and minus sshd (launchd starts macOS's own on demand). # # Everything from the machine comes in on a read-only shared folder: the # owner's midas build, node.env and the login keys. Copying the binary in at # every start is how `sudo midas setup ` on the machine updates the node. set -uo pipefail # launchd's PATH has no /usr/local/bin, where git-lfs is: the clone needs it. export PATH=/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin share="/Volumes/My Shared Files/midas" # At every start, not once: each step is idempotent, and a first-boot marker # once recorded a password change that had failed. # sshd admits only `build`, by key, through midas… install -m 644 /usr/local/etc/midas-sshd.conf /etc/ssh/sshd_config.d/000-midas.conf # …which it runs only from a path no other user can write. And where Remote # Login is limited to a group (com.apple.access_ssh, which the base image has, # holding admin), macOS refuses everyone else after their key was accepted. chown root:wheel /usr/local/bin && chmod 755 /usr/local/bin if dscl . -read /Groups/com.apple.access_ssh >/dev/null 2>&1 \ && ! dseditgroup -o checkmember -m build com.apple.access_ssh >/dev/null 2>&1; then dseditgroup -o edit -a build -t user com.apple.access_ssh \ && echo "midas node: build may log in over ssh (com.apple.access_ssh)" fi # The base image's admin password goes, so nothing — `build` included, with # su — gets root with it. Even root must give the old password to change it. if dscl . -authonly admin admin >/dev/null 2>&1; then if dscl . -passwd /Users/admin admin "$(openssl rand -hex 24)"; then echo "midas node: the image's admin password is gone" else echo "midas node: could NOT change the image's admin password" >&2 fi fi # Tools added since a VM's image was built (provision.sh has them for a new # one): Homebrew's admin installs them, in the background, without a rebuild # that would lose the warm build. if [ ! -x /opt/homebrew/bin/bun ]; then (sudo -u admin -H /opt/homebrew/bin/brew install oven-sh/bun/bun >/dev/null 2>&1 \ && echo "midas node: bun installed" || echo "midas node: could not install bun" >&2) & fi if [ ! -x /opt/homebrew/bin/qemu-system-aarch64 ]; then (sudo -u admin -H /opt/homebrew/bin/brew install qemu >/dev/null 2>&1 \ && echo "midas node: qemu installed" || echo "midas node: could not install qemu" >&2) & fi for _ in $(seq 1 60); do [ -f "$share/node.env" ] && break # Tart's shared folders mount themselves; if not yet, ask. mkdir -p "/Volumes/My Shared Files" && mount_virtiofs com.apple.virtio-fs.automount "/Volumes/My Shared Files" 2>/dev/null sleep 2 done [ -f "$share/node.env" ] || { echo "midas node: no $share/node.env" >&2; exit 1; } # This job is the owner's too: a newer one comes in on the share, and takes # over before anything else is done. self=/usr/local/libexec/midas-node-start if [ -f "$share/start.sh" ] && ! cmp -s "$share/start.sh" "$self"; then install -m 755 "$share/start.sh" "$self" && exec "$self" fi # The owner's midas: kept apart as what root here runs, and the one sessions # start with, until the hub's replaces it (`midas node update`, below). install -d -m 755 /usr/local/libexec cmp -s "$share/midas" /usr/local/libexec/midas-owner || install -m 755 "$share/midas" /usr/local/libexec/midas-owner cmp -s "$share/midas" /usr/local/bin/midas || install -m 755 "$share/midas" /usr/local/bin/midas install -d -m 755 /config for f in node.env authorized_keys operator_keys; do install -m 644 "$share/$f" "/config/$f"; done # shellcheck source=/dev/null . /config/node.env mkdir -p /work/.ssh-host /work/checkout [ -f /work/.ssh-host/ssh_host_ed25519_key ] || ssh-keygen -q -t ed25519 -N '' -f /work/.ssh-host/ssh_host_ed25519_key chown build:staff /work /work/checkout # The node's key for the hub's git mirror, and the ssh config that uses it: # the hub is reached through the machine (MIDAS_GIT_HOST:MIDAS_GIT_PORT). sudo -u build -H bash -c ' mkdir -p /work/.ssh-node ~/.ssh && chmod 700 /work/.ssh-node ~/.ssh [ -f /work/.ssh-node/id_ed25519 ] || ssh-keygen -q -t ed25519 -N "" \ -C "midas-node-'"$MIDAS_NODE"'" -f /work/.ssh-node/id_ed25519 cat > ~/.ssh/config <