`git worktree list --porcelain | awk '...{print $2; exit}'` exits after
the first line; with 27 worktrees git is still writing, gets SIGPIPE, and
under `set -o pipefail` every bin/ helper (deploy, vault-run, worktree)
exited 141 with no output. Let awk read to the end instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
27 lines
1.2 KiB
Bash
27 lines
1.2 KiB
Bash
# Shared by the bin/ helpers. The main checkout holds the venv and the
|
|
# (gitignored) vault files that every worktree uses.
|
|
repo=$(git rev-parse --show-toplevel)
|
|
# awk reads to the end: exiting early kills git with SIGPIPE, which pipefail
|
|
# turns into a failed script once there are enough worktrees.
|
|
main_checkout=$(git -C "$repo" worktree list --porcelain | awk '/^worktree /&&!found{print $2; found=1}')
|
|
lock_dir=${XDG_RUNTIME_DIR:-/tmp}/ansible-selfhosting
|
|
mkdir -p "$lock_dir" && chmod 700 "$lock_dir"
|
|
|
|
# take_lock NAME: wait for, then hold, a lock shared by every agent on this machine.
|
|
take_lock() {
|
|
local name=$1
|
|
exec 9>"$lock_dir/$name.lock"
|
|
if ! flock -n 9; then
|
|
echo "Waiting for $name lock, held by: $(cat "$lock_dir/$name.owner" 2>/dev/null || echo unknown)" >&2
|
|
flock 9
|
|
fi
|
|
echo "$(basename "$repo") (pid $$, $(date '+%H:%M:%S'))" > "$lock_dir/$name.owner"
|
|
}
|
|
|
|
# Worktrees need the gitignored vault files from the main checkout.
|
|
link_vault() {
|
|
[ "$repo" = "$main_checkout" ] && return
|
|
[ -e "$repo/group_vars/all/vault.yml" ] || ln -s "$main_checkout/group_vars/all/vault.yml" "$repo/group_vars/all/vault.yml"
|
|
[ -e "$repo/vault_pass" ] || ln -s "$main_checkout/vault_pass" "$repo/vault_pass"
|
|
}
|