# 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) main_checkout=$(git -C "$repo" worktree list --porcelain | awk '/^worktree /{print $2; exit}') 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" }