Skip to content

Commit f9cdb62

Browse files
nrdxpblaggacao
authored andcommitted
nomad-exec: pick allocation by supplied tag
If no tag matches, the script will still fall back to the picker. When combined with `-j` and `-t` flags, this should allow the script to now function non-interactively.
1 parent 437b99c commit f9cdb62

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

nomad-exec.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -euo pipefail
33

44
PACKAGE="nomad-exec"
55

6-
while getopts 'c:u:n:j:t:a:e:psvh' c; do
6+
while getopts 'c:u:n:j:t:a:e:S:psvh' c; do
77
case "$c" in
88
c) CONSUL_HTTP_ADDR="$OPTARG" ;;
99
u) NOMAD_ADDR="$OPTARG" ;;
@@ -17,10 +17,11 @@ while getopts 'c:u:n:j:t:a:e:psvh' c; do
1717
e) CMD="$OPTARG" ;;
1818
p) PROMPT="true" ;;
1919
s) SHOW_TAGS="true" ;;
20+
S) CONSUL_TAG="$OPTARG" ;;
2021
v) VERBOSE="true" ;;
2122
*)
2223
echo "This command makes exec into a nomad allocation easier."
23-
echo "usage: $PACKAGE [-c CONSUL_HTTP_ADDR] [-u NOMAD_ADDR] [-n NOMAD_NAMESPACE] [-j JOB] [-t TASK] [-a ALLOC] [-e CMD] [-p] [-s] [-v] [-h]"
24+
echo "usage: $PACKAGE [-c CONSUL_HTTP_ADDR] [-u NOMAD_ADDR] [-n NOMAD_NAMESPACE] [-j JOB] [-t TASK] [-a ALLOC] [-e CMD] [-S CONSUL_TAG] [-s] [-p] [-v] [-h]"
2425
echo
2526
echo " -c CONSUL_HTTP_ADDR for consul upstream server. Default: env CONSUL_HTTP_ADDR (${CONSUL_HTTP_ADDR:-"UNSET"})"
2627
echo " -u NOMAD_ADDR for nomad upstream server. Default: env NOMAD_ADDR (${NOMAD_ADDR:-"UNSET"})"
@@ -29,8 +30,9 @@ while getopts 'c:u:n:j:t:a:e:psvh' c; do
2930
echo " -t TASK to select the allocation from"
3031
echo " -a ALLOCATION for exec"
3132
echo ' -e CMD for exec. Default: "/bin/debug"'
32-
echo " -p prompt for some defaults if not declared as options (NOMAD_NAMESPACE, CMD)"
33+
echo " -S CONSUL_TAG to select the first matching allocation from (implies '-s')"
3334
echo " -s show consul service allocation associated addresses and tags (requires services to be tagged with the NOMAD_ALLOC_ID)"
35+
echo " -p prompt for some defaults if not declared as options (NOMAD_NAMESPACE, CMD)"
3436
echo " -v verbose"
3537
echo " -h help"
3638
echo
@@ -124,7 +126,7 @@ fi
124126

125127
if [ -z "${ALLOC:-}" ]; then
126128
mapfile -t SHOW_ALLOCS < <(curl -s -H "X-Nomad-Token: $NOMAD_TOKEN" "$NOMAD_ADDR/v1/job/$JOB/allocations?namespace=$NOMAD_NAMESPACE" | jq -r "map(select(.TaskStates | keys | select(index(\"$TASK\")))) | map(select(.TaskStates.\"$TASK\".State == \"running\")) | map(.ID) | unique | sort[]")
127-
if [ "${SHOW_TAGS:-}" == "true" ]; then
129+
if [ "${SHOW_TAGS:-}" == "true" ] || [ -n "${CONSUL_TAG:-}" ]; then
128130
SERVICE_CATALOG="$(curl -s -H "X-Consul-Token: $CONSUL_HTTP_TOKEN" "$CONSUL_HTTP_ADDR/v1/catalog/services")"
129131
COUNT="1"
130132
CACHE_SERVICE=()
@@ -158,17 +160,27 @@ if [ -z "${ALLOC:-}" ]; then
158160
TAGS="$(jq -r "map(select(.ServiceTags[] == \"$SHOW_ALLOC\"))[] | .ServiceTags | del(.[] | select(. == \"$SHOW_ALLOC\")) | unique | sort[]" <<<"$SERVICE_JSON")"
159161
for TAG in $TAGS; do
160162
echo " $SERVICE:$TAG"
163+
if [ "${CONSUL_TAG:-}" == "$TAG" ]; then
164+
ALLOC="$SHOW_ALLOC"
165+
break 3
166+
fi
161167
done
162168
echo
163169
done
164170
echo
165171
done
166172
fi
167-
choose \
168-
"allocation" \
169-
"nostrip" \
170-
"for SHOW_ALLOC in ${SHOW_ALLOCS[*]}; do echo \$SHOW_ALLOC; done"
171-
ALLOC="$CHOICE"
173+
174+
if [ -z "${ALLOC:-}" ]; then
175+
if [ -n "${CONSUL_TAG:-}" ]; then
176+
echo "warning: '$CONSUL_TAG' does not match any specified tags, falling back to alloc picker"
177+
fi
178+
choose \
179+
"allocation" \
180+
"nostrip" \
181+
"for SHOW_ALLOC in ${SHOW_ALLOCS[*]}; do echo \$SHOW_ALLOC; done"
182+
ALLOC="$CHOICE"
183+
fi
172184
fi
173185
[ -z "${VERBOSE:-}" ] || echo "ALLOC is: $ALLOC"
174186

0 commit comments

Comments
 (0)