diff --git a/scripts/system/worktree/lib.sh b/scripts/system/worktree/lib.sh index 65ea51167f..2878a6bd14 100755 --- a/scripts/system/worktree/lib.sh +++ b/scripts/system/worktree/lib.sh @@ -57,15 +57,48 @@ worktree_slot_of() { printf '%s' "${slot}" } +WORKTREE_MAX_SLOT=254 +WORKTREE_NET_PREFIX=172.30 +WORKTREE_LAB_PREFIX=10.160 + +worktree_used_subnets() { + local -a ids=() + command -v docker >/dev/null 2>&1 || return 0 + mapfile -t ids < <(docker network ls -q 2>/dev/null) + [ "${#ids[@]}" -gt 0 ] || return 0 + docker network inspect "${ids[@]}" \ + --format '{{range .IPAM.Config}}{{.Subnet}}{{"\n"}}{{end}}' 2>/dev/null || return 0 +} + +# Param slot: the candidate slot. +# Param subnets: newline-separated subnets docker networks already hold. +# Returns: 0 when a network holds the slot's compose or swarm lab subnet. +worktree_slot_taken() { + local subnet + while read -r subnet; do + case "${subnet}" in + "${WORKTREE_NET_PREFIX}.$1."* | "${WORKTREE_LAB_PREFIX}.$1."*) return 0 ;; + esac + done <<<"$2" + return 1 +} + worktree_next_slot() { - local max=0 path slot + local used=" " path slot subnets while read -r path; do - slot="$(worktree_slot_of "${path}")" - if [ "${slot}" -gt "${max}" ]; then - max="${slot}" - fi + used+="$(worktree_slot_of "${path}") " done < <(git worktree list --porcelain | awk '/^worktree /{print $2}') - printf '%s' "$((max + 1))" + subnets="$(worktree_used_subnets)" + for ((slot = 1; slot <= WORKTREE_MAX_SLOT; slot++)); do + case "${used}" in + *" ${slot} "*) continue ;; + esac + if ! worktree_slot_taken "${slot}" "${subnets}"; then + printf '%s' "${slot}" + return 0 + fi + done + printf '%s' "$((WORKTREE_MAX_SLOT + 1))" } worktree_meta_dir() { diff --git a/scripts/system/worktree/up.sh b/scripts/system/worktree/up.sh index e11ac134d7..11d953d599 100755 --- a/scripts/system/worktree/up.sh +++ b/scripts/system/worktree/up.sh @@ -18,7 +18,6 @@ source "${SCRIPT_DIR}/lib.sh" branch="${1:?branch name required}" base="${2:-}" -max_slot=255 cd "${REPO_ROOT}" @@ -38,8 +37,8 @@ if [ -e "${path}" ]; then 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 +if [ "${slot}" -gt "${WORKTREE_MAX_SLOT}" ]; then + echo "FAILURE: no free slot left (max ${WORKTREE_MAX_SLOT}); tear down an existing worktree" >&2 exit 1 fi diff --git a/tests/lint/filesystem/env/test_worktree_swarm_lab_subnet.py b/tests/lint/filesystem/env/test_worktree_swarm_lab_subnet.py index 7395b5f012..4b86c55908 100644 --- a/tests/lint/filesystem/env/test_worktree_swarm_lab_subnet.py +++ b/tests/lint/filesystem/env/test_worktree_swarm_lab_subnet.py @@ -20,8 +20,9 @@ from utils.env.parser import parse_static_env from . import PROJECT_ROOT _UP_SH = PROJECT_ROOT / "scripts/system/worktree/up.sh" +_LIB_SH = PROJECT_ROOT / "scripts/system/worktree/lib.sh" _NETWORKS = PROJECT_ROOT / "group_vars/all/08_networks.yml" -_MAX_SLOT_RE = re.compile(r"^max_slot=(?P\d+)$", re.MULTILINE) +_MAX_SLOT_RE = re.compile(r"^WORKTREE_MAX_SLOT=(?P\d+)$", re.MULTILINE) _LAB_PREFIX_RE = re.compile(r'^lab_prefix="(?P