mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-05 17:28:21 +00:00
Merge upstream/main into feature/role-stalwart-mail-swarm
This commit is contained in:
971 files changed
+24099
-4900
No files matched your search
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck shell=bash
|
||||
#
|
||||
# Tear down the worktree a branch was checked out into: stop its compose
|
||||
# stack, then release the checkout and free its slot. Refuses to drop a
|
||||
# worktree with uncommitted changes unless force=true is passed.
|
||||
#
|
||||
# Use `make worktree-down` rather than calling this script directly.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
|
||||
|
||||
# shellcheck source=scripts/system/worktree/lib.sh
|
||||
source "${SCRIPT_DIR}/lib.sh"
|
||||
|
||||
branch="${1:?branch name required}"
|
||||
base="${2:-}"
|
||||
force="${3:-false}"
|
||||
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
base="${base:-$(worktree_default_base)}"
|
||||
path="$(worktree_path "${branch}" "${base}")"
|
||||
|
||||
if [ ! -d "${path}" ]; then
|
||||
echo "FAILURE: no worktree at ${path} for branch '${branch}'" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "${force}" != "true" ]; then
|
||||
dirty=""
|
||||
if ! dirty="$(git -C "${path}" status --porcelain)"; then
|
||||
echo "FAILURE: cannot read the git status of ${path}; pass force=true to drop it anyway" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -n "${dirty}" ]; then
|
||||
echo "FAILURE: ${path} has uncommitted changes; commit them or pass force=true" >&2
|
||||
git -C "${path}" status --short >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
slot="$(worktree_slot_of "${path}")"
|
||||
meta="$(worktree_meta_dir "${path}")"
|
||||
echo ">>> Stopping the compose stack in ${path} (slot ${slot})"
|
||||
if ! make -C "${path}" compose-down; then
|
||||
echo ">>> WARNING: compose-down failed; removing the worktree anyway"
|
||||
fi
|
||||
|
||||
echo ">>> Removing worktree ${path}"
|
||||
remove_rc=0
|
||||
if [ "${force}" = "true" ]; then
|
||||
git worktree remove --force "${path}" || remove_rc=$?
|
||||
else
|
||||
git worktree remove "${path}" || remove_rc=$?
|
||||
fi
|
||||
|
||||
if [ "${remove_rc}" -ne 0 ]; then
|
||||
if [ -e "${path}" ]; then
|
||||
echo "FAILURE: git worktree remove failed and ${path} is untouched" >&2
|
||||
exit "${remove_rc}"
|
||||
fi
|
||||
if [ -z "${meta}" ]; then
|
||||
echo "FAILURE: could not resolve the metadata dir of ${path}; run 'make worktree-prune'" >&2
|
||||
exit "${remove_rc}"
|
||||
fi
|
||||
echo ">>> git deleted the checkout but could not drop ${meta}; unregistering by hand"
|
||||
unregister_rc=0
|
||||
worktree_unregister "${meta}" || unregister_rc=$?
|
||||
case "${unregister_rc}" in
|
||||
1) worktree_report_held "${meta}" ;;
|
||||
2)
|
||||
echo "FAILURE: ${meta} still registers branch '${branch}'; clear it outside the sandbox: rm -rf ${meta}" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
echo "Slot ${slot} released."
|
||||
Executable
+99
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck shell=bash
|
||||
#
|
||||
# Shared helpers for the worktree slot manager. A slot is an integer that
|
||||
# isolates one checkout from every other: it shifts the compose subnet, the
|
||||
# host bind IP and the swarm lab subnet, so several worktrees can run their
|
||||
# stacks side by side. Slot 0 is the primary checkout and owns the shared
|
||||
# cache stack; every worktree gets the next free slot and joins that cache
|
||||
# over an external docker network.
|
||||
#
|
||||
# Volumes are deliberately not slotted: compose already namespaces them by
|
||||
# project, and INFINITO_DOCKER_VOLUME names a top-level compose key, so
|
||||
# renaming it would point the mount at an undeclared volume.
|
||||
#
|
||||
# The pins are written to custom.env AND passed to the generator in a cleared
|
||||
# environment: custom.env keeps them reproducible across later `make dotenv`
|
||||
# runs, while the explicit env covers branches whose generator predates
|
||||
# custom.env support. Inheriting the caller's environment would leak the
|
||||
# invoking worktree's own slot into the new one.
|
||||
#
|
||||
# Slot discovery reads custom.env first: a branch whose default.env predates
|
||||
# INFINITO_INSTANCE never materialises the key into .env, which would make the
|
||||
# checkout look like slot 0 and hand its number out twice.
|
||||
#
|
||||
# Sourced by up.sh, down.sh and prune.sh; not meant to be executed directly.
|
||||
|
||||
worktree_slug() {
|
||||
printf '%s' "$1" | sed -e 's#[^a-zA-Z0-9._-]#-#g' -e 's#^[^a-zA-Z0-9]*##' -e 's#[-.]*$##'
|
||||
}
|
||||
|
||||
worktree_default_base() {
|
||||
local url
|
||||
url="$(git remote get-url origin)"
|
||||
url="${url%.git}"
|
||||
url="${url#*://}"
|
||||
url="${url#*@}"
|
||||
printf '%s/.local/share/worktrees/%s/%s' "${HOME}" "${url%%[:/]*}" "${url#*[:/]}"
|
||||
}
|
||||
|
||||
worktree_path() {
|
||||
printf '%s/%s' "$2" "$1"
|
||||
}
|
||||
|
||||
worktree_slot_of() {
|
||||
local slot=0 file
|
||||
for file in "$1/custom.env" "$1/.env"; do
|
||||
if [ -f "${file}" ]; then
|
||||
slot="$(awk -F= '/^INFINITO_INSTANCE=/{print $2}' "${file}" | tail -n1)"
|
||||
if [ -n "${slot}" ]; then
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
case "${slot}" in
|
||||
'' | *[!0-9]*) slot=0 ;;
|
||||
esac
|
||||
printf '%s' "${slot}"
|
||||
}
|
||||
|
||||
worktree_next_slot() {
|
||||
local max=0 path slot
|
||||
while read -r path; do
|
||||
slot="$(worktree_slot_of "${path}")"
|
||||
if [ "${slot}" -gt "${max}" ]; then
|
||||
max="${slot}"
|
||||
fi
|
||||
done < <(git worktree list --porcelain | awk '/^worktree /{print $2}')
|
||||
printf '%s' "$((max + 1))"
|
||||
}
|
||||
|
||||
worktree_meta_dir() {
|
||||
sed -n 's/^gitdir: //p' "$1/.git" 2>/dev/null
|
||||
}
|
||||
|
||||
# Param entry: a .git/worktrees/<id> directory.
|
||||
# Returns: 0 fully removed, 1 unregistered but the directory is pinned,
|
||||
# 2 still registered.
|
||||
worktree_unregister() {
|
||||
local entry="$1"
|
||||
rm -f "${entry}/gitdir" "${entry}/HEAD" 2>/dev/null
|
||||
if [ -e "${entry}/gitdir" ] || [ -e "${entry}/HEAD" ]; then
|
||||
return 2
|
||||
fi
|
||||
rm -rf "${entry}" 2>/dev/null || return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
worktree_report_held() {
|
||||
[ "$#" -gt 0 ] || return 0
|
||||
echo ">>> Unregistered, but the sandbox pins these metadata dirs; git ignores them now:"
|
||||
printf ' %s\n' "$@"
|
||||
echo ">>> Clear them outside the sandbox: rm -rf $*"
|
||||
}
|
||||
|
||||
worktree_cache_network() {
|
||||
docker inspect infinito-registry-cache \
|
||||
--format '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}{{"\n"}}{{end}}' 2>/dev/null |
|
||||
head -n1
|
||||
}
|
||||
Executable
+113
@@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck shell=bash
|
||||
#
|
||||
# Drop the registrations of worktrees whose checkout directory is gone, so the
|
||||
# branches they still claim can be checked out again.
|
||||
#
|
||||
# `git worktree prune` alone is not enough under the agent sandbox: it deletes
|
||||
# the metadata directory as a whole, and the sandbox holds some of its files
|
||||
# (config.worktree, commondir) as read-only bind mounts, so the unlink fails
|
||||
# with EBUSY and the entry survives. Removing gitdir + HEAD unregisters the
|
||||
# worktree without needing the pinned files gone.
|
||||
#
|
||||
# Use `make worktree-prune` rather than calling this script directly.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
|
||||
|
||||
# shellcheck source=scripts/system/worktree/lib.sh
|
||||
source "${SCRIPT_DIR}/lib.sh"
|
||||
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
if ! git worktree prune -v 2>/dev/null; then
|
||||
echo ">>> git could not delete every stale entry; clearing the metadata by hand"
|
||||
fi
|
||||
|
||||
meta_dir="$(git rev-parse --git-common-dir)/worktrees"
|
||||
if [ ! -d "${meta_dir}" ]; then
|
||||
echo "No stale worktree registrations left."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
live_ids=()
|
||||
while IFS= read -r line; do
|
||||
case "${line}" in
|
||||
"worktree "*) ;;
|
||||
*) continue ;;
|
||||
esac
|
||||
checkout="${line#worktree }"
|
||||
[ -f "${checkout}/.git" ] || continue
|
||||
live_ids+=("$(basename "$(worktree_meta_dir "${checkout}")")")
|
||||
done < <(git worktree list --porcelain)
|
||||
|
||||
is_live() {
|
||||
local known
|
||||
for known in ${live_ids[@]+"${live_ids[@]}"}; do
|
||||
[ "${known}" = "${1}" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
pruned=0
|
||||
held=()
|
||||
stuck=()
|
||||
unverifiable=()
|
||||
for entry in "${meta_dir}"/*; do
|
||||
[ -d "${entry}" ] || continue
|
||||
if is_live "$(basename "${entry}")"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
gitdir=""
|
||||
if [ -f "${entry}/gitdir" ]; then
|
||||
gitdir="$(cat "${entry}/gitdir")"
|
||||
fi
|
||||
|
||||
if [ -n "${gitdir}" ]; then
|
||||
checkout="$(dirname "${gitdir}")"
|
||||
base="$(dirname "${checkout}")"
|
||||
if [ -e "${checkout}" ] || [ ! -d "${base}" ] || [ ! -x "${base}" ]; then
|
||||
unverifiable+=("$(basename "${entry}") -> ${checkout}")
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
registered=false
|
||||
if [ -e "${entry}/gitdir" ] || [ -e "${entry}/HEAD" ]; then
|
||||
registered=true
|
||||
fi
|
||||
|
||||
unregister_rc=0
|
||||
worktree_unregister "${entry}" || unregister_rc=$?
|
||||
case "${unregister_rc}" in
|
||||
1) held+=("${entry}") ;;
|
||||
2) stuck+=("${entry}") ;;
|
||||
esac
|
||||
|
||||
if [ "${registered}" = true ]; then
|
||||
echo "Pruned $(basename "${entry}")"
|
||||
pruned=$((pruned + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "${pruned}" -eq 0 ] && [ "${#unverifiable[@]}" -eq 0 ]; then
|
||||
echo "No stale worktree registrations left."
|
||||
fi
|
||||
|
||||
worktree_report_held ${held[@]+"${held[@]}"}
|
||||
|
||||
if [ "${#stuck[@]}" -gt 0 ]; then
|
||||
echo ">>> Still registered — gitdir/HEAD could not be removed, so these keep claiming their branch:" >&2
|
||||
printf ' %s\n' "${stuck[@]}" >&2
|
||||
echo ">>> Clear them outside the sandbox: rm -rf ${stuck[*]}" >&2
|
||||
fi
|
||||
|
||||
if [ "${#unverifiable[@]}" -gt 0 ]; then
|
||||
echo ">>> Kept registered — their checkout is still on disk (or its parent is not"
|
||||
echo ">>> readable from here), so 'gone' cannot be established:"
|
||||
printf ' %s\n' "${unverifiable[@]}"
|
||||
echo ">>> Release them with 'make worktree-down branch=<name>' instead."
|
||||
fi
|
||||
Executable
+117
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck shell=bash
|
||||
#
|
||||
# Create an isolated git worktree for a branch and pin it to a free slot, so
|
||||
# its compose stack no longer collides with the primary checkout on subnet,
|
||||
# host ports, container names or volumes. The cache stack is not duplicated:
|
||||
# the worktree joins the running one over an external network.
|
||||
#
|
||||
# Use `make worktree-up` rather than calling this script directly.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
|
||||
|
||||
# shellcheck source=scripts/system/worktree/lib.sh
|
||||
source "${SCRIPT_DIR}/lib.sh"
|
||||
|
||||
branch="${1:?branch name required}"
|
||||
base="${2:-}"
|
||||
max_slot=255
|
||||
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
base="${base:-$(worktree_default_base)}"
|
||||
|
||||
slug="$(worktree_slug "${branch}")"
|
||||
if [ -z "${slug}" ]; then
|
||||
echo "FAILURE: branch '${branch}' slugs to an empty name" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
path="$(worktree_path "${branch}" "${base}")"
|
||||
mkdir -p "$(dirname "${path}")"
|
||||
if [ -e "${path}" ]; then
|
||||
echo "FAILURE: ${path} already exists; run 'make worktree-down branch=${branch}' first" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
slot="$(worktree_next_slot)"
|
||||
if [ "${slot}" -gt "${max_slot}" ]; then
|
||||
echo "FAILURE: no free slot left (max ${max_slot}); tear down an existing worktree" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cache_network="$(worktree_cache_network)"
|
||||
if [ -z "${cache_network}" ]; then
|
||||
echo ">>> WARNING: no running cache stack found; the worktree will start without a shared cache"
|
||||
fi
|
||||
|
||||
echo ">>> Creating worktree for '${branch}' at ${path} (slot ${slot})"
|
||||
git worktree add "${path}" "${branch}"
|
||||
|
||||
git_common_dir="$(git rev-parse --path-format=absolute --git-common-dir)"
|
||||
|
||||
lab_octet="$((244 + slot))"
|
||||
|
||||
echo ">>> Pinning slot ${slot} in ${path}/custom.env"
|
||||
cat >"${path}/custom.env" <<PINS
|
||||
INFINITO_INSTANCE=${slot}
|
||||
INFINITO_GIT_COMMON_DIR=${git_common_dir}
|
||||
INFINITO_SUBNET=172.30.${slot}.0/24
|
||||
INFINITO_GATEWAY=172.30.${slot}.1
|
||||
INFINITO_DNS_IP=172.30.${slot}.53
|
||||
INFINITO_IP4=172.30.${slot}.10
|
||||
INFINITO_BIND_IP=127.0.0.$((slot + 1))
|
||||
INFINITO_SWARM_LAB_SUBNET=192.168.${lab_octet}.0/24
|
||||
INFINITO_SWARM_MGR_IP=192.168.${lab_octet}.10
|
||||
INFINITO_SWARM_WRK1_IP=192.168.${lab_octet}.11
|
||||
INFINITO_SWARM_WRK2_IP=192.168.${lab_octet}.12
|
||||
INFINITO_SWARM_NFS_IP=192.168.${lab_octet}.13
|
||||
INFINITO_SWARM_BACKUP_IP=192.168.${lab_octet}.14
|
||||
INFINITO_RUNNER_PREFIX=infinito-${slug}
|
||||
INFINITO_CONTAINER=infinito_nexus_${slug//[.-]/_}
|
||||
INFINITO_CACHE_NETWORK=${cache_network}
|
||||
PINS
|
||||
|
||||
echo ">>> Generating .env from the pins"
|
||||
env -i -C "${path}" \
|
||||
HOME="${HOME}" \
|
||||
PATH="${PATH}" \
|
||||
INFINITO_INSTANCE="${slot}" \
|
||||
INFINITO_GIT_COMMON_DIR="${git_common_dir}" \
|
||||
INFINITO_SUBNET="172.30.${slot}.0/24" \
|
||||
INFINITO_GATEWAY="172.30.${slot}.1" \
|
||||
INFINITO_DNS_IP="172.30.${slot}.53" \
|
||||
INFINITO_IP4="172.30.${slot}.10" \
|
||||
INFINITO_BIND_IP="127.0.0.$((slot + 1))" \
|
||||
INFINITO_SWARM_LAB_SUBNET="192.168.${lab_octet}.0/24" \
|
||||
INFINITO_SWARM_MGR_IP="192.168.${lab_octet}.10" \
|
||||
INFINITO_SWARM_WRK1_IP="192.168.${lab_octet}.11" \
|
||||
INFINITO_SWARM_WRK2_IP="192.168.${lab_octet}.12" \
|
||||
INFINITO_SWARM_NFS_IP="192.168.${lab_octet}.13" \
|
||||
INFINITO_SWARM_BACKUP_IP="192.168.${lab_octet}.14" \
|
||||
INFINITO_RUNNER_PREFIX="infinito-${slug}" \
|
||||
INFINITO_CACHE_NETWORK="${cache_network}" \
|
||||
python3 -m cli.meta.env
|
||||
|
||||
cat <<SUMMARY
|
||||
|
||||
Worktree ready.
|
||||
|
||||
branch ${branch}
|
||||
path ${path}
|
||||
slot ${slot}
|
||||
subnet 172.30.${slot}.0/24
|
||||
bind IP 127.0.0.$((slot + 1))
|
||||
container infinito_nexus_${slug//[.-]/_}
|
||||
cache net ${cache_network:-<none>}
|
||||
|
||||
Next:
|
||||
cd ${path}
|
||||
make compose-up
|
||||
|
||||
Release with:
|
||||
make worktree-down branch=${branch}
|
||||
SUMMARY
|
||||
Reference in new issue
Block a user