From 9c6d1f35419e13ded3c8c0c58d665487a54cf6c9 Mon Sep 17 00:00:00 2001 From: Lol3rrr Date: Fri, 2 Oct 2026 19:45:56 +0200 Subject: [PATCH] Update nomad-scale --- nomad-scale/Dockerfile | 4 +++ nomad-scale/README.md | 9 +++++ nomad-scale/script.sh | 75 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 nomad-scale/README.md create mode 100644 nomad-scale/script.sh diff --git a/nomad-scale/Dockerfile b/nomad-scale/Dockerfile index 1a55357..f1a015b 100644 --- a/nomad-scale/Dockerfile +++ b/nomad-scale/Dockerfile @@ -1,3 +1,7 @@ FROM alpine:3.24 RUN apk add --no-cache bash curl jq + +COPY script.sh /script.sh + +ENTRYPOINT ["/bin/bash", "/script.sh"] diff --git a/nomad-scale/README.md b/nomad-scale/README.md new file mode 100644 index 0000000..5465e92 --- /dev/null +++ b/nomad-scale/README.md @@ -0,0 +1,9 @@ +# Nomad-Scale + +## Environment Variables +| Name | Default | Description | +|---|---|---| +| TARGET_NAMESPACE | default | The namespace of the target job | +| TARGET_JOB | - | TODO | +| TARGET_GROUP | - | TODO | +| TARGET_COUNT | 0 | TODO | diff --git a/nomad-scale/script.sh b/nomad-scale/script.sh new file mode 100644 index 0000000..0f6f541 --- /dev/null +++ b/nomad-scale/script.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +set -euo pipefail + +NOMAD_UNIX_ADDR="${NOMAD_UNIX_ADDR:-}" +NOMAD_TOKEN="${NOMAD_TOKEN:-}" +NOMAD_ADDR="${NOMAD_ADDR:-localhost}" + +TARGET_NAMESPACE="${TARGET_NAMESPACE:-default}" +TARGET_JOB="${TARGET_JOB:-}" +TARGET_GROUP="${TARGET_GROUP:-}" + +TARGET_COUNT="${TARGET_COUNT:-0}" + +CURL_ARGS=( + --silent + --show-error + --fail-with-body +) + +if [[ -n "${NOMAD_TOKEN}" ]]; then + CURL_ARGS+=(-H "Authorization: Bearer ${NOMAD_TOKEN}") +fi +if [[ -n "${NOMAD_UNIX_ADDR}" ]]; then + CURL_ARGS+=(--unix-socket "${NOMAD_UNIX_ADDR}") +fi + +if ! [[ "$TARGET_COUNT" =~ ^[+-]?[0-9]+$ ]]; then + echo "$TARGET_COUNT is not a number" + exit 1 +fi + +curl "${CURL_ARGS[@]}" \ + -X POST \ + "${NOMAD_ADDR}/v1/job/${TARGET_JOB}/scale?namespace=${TARGET_NAMESPACE}" \ + -H "Content-Type: application/json" \ + -d "$(jq -n \ + --arg group "$TARGET_GROUP" \ + --arg count "$TARGET_COUNT" \ + '{ + Count: ($count|tonumber), + Target: { + "Group": ($group) + }, + Message: "Scaling up to perform backup" + }' + )" + +while true; do + allocations=$( + curl "${CURL_ARGS[@]}" \ + "${NOMAD_ADDR}/v1/job/${TARGET_JOB}/allocations?namespace=${TARGET_NAMESPACE}" + ) + + count=$( + jq --arg group "$TARGET_GROUP" ' + [ + .[] + | select(.TaskGroup == $group) + | select( + .ClientStatus != "complete" + and .ClientStatus != "dead" + ) + ] | length + ' <<< "$allocations" + ) + + echo "Remaining allocations: ${count}" + + if [[ "$count" -eq $TARGET_COUNT ]]; then + break + fi + + sleep 5 +done