|
| 1 | +#!/bin/bash |
| 2 | +# Get current job count from Nomad API to preserve autoscaler-managed values. |
| 3 | +# This prevents Terraform from resetting count on job updates. |
| 4 | +# |
| 5 | +# IMPORTANT: This script fails on Nomad API errors (network, auth, TLS) to prevent |
| 6 | +# accidental scale-downs. Only a 404 (job not found) falls back to min_count. |
| 7 | +# |
| 8 | +# Based on: https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external#processing-json-in-shell-scripts |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +# Extract arguments from the input into shell variables. |
| 13 | +eval "$(jq -r '@sh "ADDR=\(.nomad_addr) TOKEN=\(.nomad_token) JOB=\(.job_name) MIN=\(.min_count)"')" |
| 14 | + |
| 15 | +# Fetch job info and capture HTTP status code |
| 16 | +RESPONSE=$(curl -s -w "\n---HTTP_STATUS:%{http_code}" -H "X-Nomad-Token: $TOKEN" \ |
| 17 | + "$ADDR/v1/job/$JOB" 2>&1) |
| 18 | +CURL_EXIT=$? |
| 19 | + |
| 20 | +# Extract HTTP code and body |
| 21 | +HTTP_CODE=$(echo "$RESPONSE" | grep '^---HTTP_STATUS:' | sed 's/---HTTP_STATUS://') |
| 22 | +BODY=$(echo "$RESPONSE" | grep -v '^---HTTP_STATUS:') |
| 23 | + |
| 24 | +# Handle curl-level failures (network, TLS, DNS, etc.) |
| 25 | +if [ $CURL_EXIT -ne 0 ]; then |
| 26 | + echo "ERROR: Failed to connect to Nomad API at $ADDR (curl exit code: $CURL_EXIT)" >&2 |
| 27 | + echo "This may indicate a network issue, TLS error, or DNS failure." >&2 |
| 28 | + echo "Refusing to proceed to prevent accidental scale-down." >&2 |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +# Handle HTTP error responses |
| 33 | +if [ -z "$HTTP_CODE" ]; then |
| 34 | + echo "ERROR: Could not determine HTTP status code from Nomad API response" >&2 |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +if [ "$HTTP_CODE" = "404" ]; then |
| 39 | + # Job doesn't exist yet - use minimum count |
| 40 | + COUNT="$MIN" |
| 41 | +elif [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then |
| 42 | + # Success - parse the count from response |
| 43 | + COUNT=$(echo "$BODY" | jq -r '.TaskGroups[0].Count // empty' 2>/dev/null) |
| 44 | + if ! [[ "$COUNT" =~ ^[0-9]+$ ]]; then |
| 45 | + echo "ERROR: Failed to parse job count from Nomad API response" >&2 |
| 46 | + echo "Response body: $BODY" >&2 |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +else |
| 50 | + # Any other HTTP error (403, 500, 502, etc.) - fail to prevent bad state |
| 51 | + echo "ERROR: Nomad API returned HTTP $HTTP_CODE for job/$JOB" >&2 |
| 52 | + echo "Response: $BODY" >&2 |
| 53 | + echo "Refusing to proceed to prevent accidental scale-down." >&2 |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +# Ensure COUNT is at least MIN |
| 58 | +if [ "$COUNT" -lt "$MIN" ]; then |
| 59 | + COUNT="$MIN" |
| 60 | +fi |
| 61 | + |
| 62 | +# Safely produce a JSON object containing the result value. |
| 63 | +jq -n --arg count "$COUNT" '{"count":$count}' |
| 64 | + |
0 commit comments