Skip to content

Commit 1711d98

Browse files
committed
wb | support creating non-local Nomad clusters for testing
1 parent db60eb7 commit 1711d98

File tree

2 files changed

+87
-43
lines changed

2 files changed

+87
-43
lines changed

nix/workbench/backend/nomad.sh

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,7 +3346,8 @@ backend_nomad() {
33463346
# (Task Drivers are also called plugins because they are pluggable).
33473347
nomad_create_server_config() {
33483348
local name=$1
3349-
local http_port=$2 rpc_port=$3 serv_port=$4
3349+
local addr=$2
3350+
local http_port=$3 rpc_port=$4 serv_port=$5
33503351
local state_dir=$(wb_nomad server state-dir-path "${name}")
33513352
local config_file=$(wb_nomad server config-file-path "${name}")
33523353
# Config:
@@ -3387,7 +3388,7 @@ data_dir = "${state_dir}/data"
33873388
# bind all network services to the same address. It is also possible to bind the
33883389
# individual services to different addresses using the "addresses" configuration
33893390
# option. Dev mode (-dev) defaults to localhost.
3390-
bind_addr = "127.0.0.1"
3391+
bind_addr = "${addr}"
33913392
# Specifies the network ports used for different services required by the Nomad
33923393
# agent.
33933394
ports = {
@@ -3416,19 +3417,19 @@ ports = {
34163417
advertise {
34173418
# The address to advertise for the HTTP interface. This should be reachable by
34183419
# all the nodes from which end users are going to use the Nomad CLI tools.
3419-
http = "127.0.0.1"
3420+
http = "${addr}"
34203421
# The address used to advertise to Nomad clients for connecting to Nomad
34213422
# servers for RPC. This allows Nomad clients to connect to Nomad servers from
34223423
# behind a NAT gateway. This address much be reachable by all Nomad client
34233424
# nodes. When set, the Nomad servers will use the advertise.serf address for
34243425
# RPC connections amongst themselves. Setting this value on a Nomad client has
34253426
# no effect.
3426-
rpc = "127.0.0.1"
3427+
rpc = "${addr}"
34273428
# The address advertised for the gossip layer. This address must be reachable
34283429
# from all server nodes. It is not required that clients can reach this
34293430
# address. Nomad servers will communicate to each other over RPC using the
34303431
# advertised Serf IP and advertised RPC Port.
3431-
serf = "127.0.0.1"
3432+
serf = "${addr}"
34323433
}
34333434
# The tls stanza configures Nomad's TLS communication via HTTP and RPC to
34343435
# enforce secure cluster communication between servers, clients, and between.
@@ -3580,7 +3581,9 @@ EOF
35803581

35813582
nomad_create_client_config() {
35823583
local name=$1
3583-
local http_port=$2 rpc_port=$3 serv_port=$4
3584+
local addr=$2
3585+
local http_port=$3 rpc_port=$4 serv_port=$5
3586+
local datacenter=${6:-loopback}
35843587
local cni_plugins_path=$(dirname $(which bridge))
35853588
local state_dir=$(wb_nomad client state-dir-path "${name}")
35863589
local config_file=$(wb_nomad client config-file-path "${name}")
@@ -3590,15 +3593,26 @@ nomad_create_client_config() {
35903593
for server_name in $(ls "${nomad_servers_dir}"); do
35913594
if wb_nomad server is-running "${server_name}"
35923595
then
3593-
local port=$(wb_nomad server port rpc "${server_name}")
3596+
local server_addr=$(wb_nomad server port addr "${server_name}")
3597+
local server_port=$(wb_nomad server port rpc "${server_name}")
35943598
if test -z "${servers_addresses}"
35953599
then
3596-
servers_addresses="${servers_addresses} \"127.0.0.1:${port}\""
3600+
servers_addresses="\"${server_addr}:${server_port}\""
35973601
else
3598-
servers_addresses="${servers_addresses}, \"127.0.0.1:${port}\""
3602+
servers_addresses="${servers_addresses}, \"${server_addr}:${server_port}\""
35993603
fi
36003604
fi
36013605
done
3606+
# If NOMAD_ADDR is not unset and not empty, add it to the list of servers.
3607+
if ! test -z "${NOMAD_ADDR+set}" && test -n "${NOMAD_ADDR}"
3608+
then
3609+
if test -z "${servers_addresses}"
3610+
then
3611+
servers_addresses="\"${NOMAD_ADDR}\""
3612+
else
3613+
servers_addresses="${servers_addresses}, \"${NOMAD_ADDR}\""
3614+
fi
3615+
fi
36023616
# Config:
36033617
# - Nomad agent configuration docs:
36043618
# - - https://developer.hashicorp.com/nomad/docs/configuration
@@ -3616,7 +3630,7 @@ nomad_create_client_config() {
36163630
region = "global"
36173631
# Specifies the data center of the local agent. All members of a datacenter
36183632
# should share a local LAN connection.
3619-
datacenter = "loopback"
3633+
datacenter = "${datacenter}"
36203634
# Specifies the name of the local node. This value is used to identify
36213635
# individual agents. When specified on a server, the name must be unique within
36223636
# the region.
@@ -3643,7 +3657,7 @@ plugin_dir = "${state_dir}/data/plugins"
36433657
# bind all network services to the same address. It is also possible to bind the
36443658
# individual services to different addresses using the "addresses" configuration
36453659
# option. Dev mode (-dev) defaults to localhost.
3646-
bind_addr = "127.0.0.1"
3660+
bind_addr = "${addr}"
36473661
# Specifies the network ports used for different services required by the Nomad
36483662
# agent.
36493663
ports = {
@@ -3670,14 +3684,14 @@ ports = {
36703684
advertise {
36713685
# The address to advertise for the HTTP interface. This should be reachable by
36723686
# all the nodes from which end users are going to use the Nomad CLI tools.
3673-
http = "127.0.0.1"
3687+
http = "${addr}"
36743688
# The address used to advertise to Nomad clients for connecting to Nomad
36753689
# servers for RPC. This allows Nomad clients to connect to Nomad servers from
36763690
# behind a NAT gateway. This address much be reachable by all Nomad client
36773691
# nodes. When set, the Nomad servers will use the advertise.serf address for
36783692
# RPC connections amongst themselves. Setting this value on a Nomad client has
36793693
# no effect.
3680-
rpc = "127.0.0.1"
3694+
rpc = "${addr}"
36813695
}
36823696
# The tls stanza configures Nomad's TLS communication via HTTP and RPC to
36833697
# enforce secure cluster communication between servers, clients, and between.

nix/workbench/nomad.sh

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ usage_nomad() {
3131
Location of the Agent's everything folder
3232
$(helpcmd \(server/client\) config-file-path NAME)
3333
Location of the Agent's config file (using only one)
34-
$(helpcmd \(server/client\) configure NAME HTTP-PORT RPC-PORT SERV-PORT)
35-
$(helpcmd \(server/client\) port \(http\|rcp\|serv\) NAME)
34+
$(helpcmd \(server/client\) configure NAME ADDR HTTP-PORT RPC-PORT SERV-PORT)
35+
$(helpcmd \(server/client\) port \(addr\|http\|rcp\|serv\) NAME)
3636
Getter for the previously configured ports
3737
$(helpcmd \(server/client\) pid-filepath NAME)
3838
Location of the Agent's "running" flag
@@ -425,7 +425,7 @@ EOL
425425
local server_name=${1:?$usage}; shift
426426
local client_name=${1:?$usage}; shift
427427
# Create config files for the server and start it.
428-
if ! wb_nomad server configure "${server_name}" 4646 4647 4648
428+
if ! wb_nomad server configure "${server_name}" 127.0.0.1 4646 4647 4648
429429
then
430430
fatal "Failed to configure Nomad server \"${server_name}\""
431431
fi
@@ -437,7 +437,7 @@ EOL
437437
# WARNING: Actually the client is configured to connect to all the
438438
# running servers, so if there are no servers ready the Nomad
439439
# cluster state is uknown (at least to me with the actual config).
440-
if ! wb_nomad client configure "${client_name}" 14646 14647 14648
440+
if ! wb_nomad client configure "${client_name}" 127.0.0.1 14646 14647 14648 "loopback"
441441
then
442442
wb_nomad server stop "${server_name}" || true
443443
fatal "Failed to configure Nomad client \"${client_name}\""
@@ -496,9 +496,10 @@ EOL
496496
;;
497497
####### server -> configure )###################################################
498498
configure )
499-
local usage="USAGE: wb nomad ${op} ${subop} SERVER-NAME HTTP-PORT RPC-PORT SERV-PORT"
499+
local usage="USAGE: wb nomad ${op} ${subop} SERVER-NAME ADDR HTTP-PORT RPC-PORT SERV-PORT"
500500
local name=${1:?$usage}; shift
501-
# Ports
501+
# Address and ports
502+
local addr=${1:?$usage}; shift
502503
local http_port=${1:?$usage}; shift
503504
local rpc_port=${1:?$usage}; shift
504505
local serv_port=${1:?$usage}; shift
@@ -516,28 +517,31 @@ EOL
516517
mkdir -p "${state_dir}"/config
517518
mkdir -p "${state_dir}"/data/server
518519
# Store the ports config
519-
echo "{\"http\": ${http_port}, \"rpc\": ${rpc_port}, \"serv\": ${serv_port}}" > "${state_dir}"/ports.json
520+
echo "{\"addr\": \"${addr}\", \"http\": ${http_port}, \"rpc\": ${rpc_port}, \"serv\": ${serv_port}}" > "${state_dir}"/ports.json
520521
# Configure
521522
nomad_create_server_config "${name}" \
522-
"${http_port}" "${rpc_port}" "${serv_port}"
523+
"${addr}" "${http_port}" "${rpc_port}" "${serv_port}"
523524
fi
524525
;;
525526
####### server -> port )########################################################
526527
port )
527-
local usage="USAGE: wb nomad ${op} ${subop} (http|rcp|serv) SERVER-NAME"
528+
local usage="USAGE: wb nomad ${op} ${subop} (addr|http|rcp|serv) SERVER-NAME"
528529
local port=${1:?$usage}; shift
529530
local name=${1:?$usage}; shift
530531
local state_dir=$(wb_nomad server state-dir-path "${name}")
531532
local ports_file="${state_dir}"/ports.json
532533
case "$port" in
534+
addr )
535+
jq -r .addr "${ports_file}"
536+
;;
533537
http )
534-
jq .http "${ports_file}"
538+
jq .http "${ports_file}"
535539
;;
536540
rpc )
537-
jq .rpc "${ports_file}"
541+
jq .rpc "${ports_file}"
538542
;;
539543
serv )
540-
jq .serv "${ports_file}"
544+
jq .serv "${ports_file}"
541545
;;
542546
* )
543547
false
@@ -602,16 +606,17 @@ EOL
602606
msg "$(green "Nomad server \"${name}\" started with PID ${pid_number}")"
603607
# Even if Nomad server was already running, try to connect to it!
604608
local i=0 patience=25
609+
local addr=$(wb_nomad server port addr "${name}")
605610
local http_port=$(wb_nomad server port http "${name}")
606611
msg "$(blue Waiting) for the listening HTTP server (${patience}s) ..."
607-
until curl -Isf 127.0.0.1:"${http_port}" 2>&1 | head --lines=1 | grep --quiet "HTTP/1.1"
612+
until curl -Isf "${addr}":"${http_port}" 2>&1 | head --lines=1 | grep --quiet "HTTP/1.1"
608613
do printf "%3d" $i; sleep 1
609614
i=$((i+1))
610615
if test $i -ge ${patience}
611616
then echo
612617
# Not using `fatal` here, let the caller decide!
613618
msg "$(red "FATAL: Nomad server startup did not succeed")"
614-
msg "$(yellow "port \"127.0.0.1:${http_port}\" not ready")"
619+
msg "$(yellow "port \"${addr}:${http_port}\" not ready")"
615620
msg "$(yellow "Check logs (${state_dir})")"
616621
# Let the "stop" subcommand clean everything!
617622
wb_nomad server stop "${name}" || true
@@ -702,12 +707,14 @@ EOL
702707
;;
703708
####### client -> configure )###################################################
704709
configure )
705-
local usage="USAGE: wb nomad ${op} ${subop} CLIENT-NAME HTTP-PORT RPC-PORT SERV-PORT [GENESIS-DIR]"
710+
local usage="USAGE: wb nomad ${op} ${subop} CLIENT-NAME ADDR HTTP-PORT RPC-PORT SERV-PORT [DATACENTER]"
706711
local name=${1:?$usage}; shift
707-
# Ports
712+
# Address and ports
713+
local addr=${1:?$usage}; shift
708714
local http_port=${1:?$usage}; shift
709715
local rpc_port=${1:?$usage}; shift
710716
local serv_port=${1:?$usage}; shift
717+
local datacenter=${1:-loopback}; shift
711718
# Checks
712719
# Assume the presence of the PID file means "running" because it
713720
# can represent an abnormal exit / uknown state!
@@ -724,28 +731,32 @@ EOL
724731
mkdir -p "${state_dir}"/config
725732
mkdir -p "${state_dir}"/data/{client,plugins,alloc}
726733
# Store the ports config
727-
echo "{\"http\": ${http_port}, \"rpc\": ${rpc_port}, \"serv\": ${serv_port}}" > "${state_dir}"/ports.json
734+
echo "{\"addr\": \"${addr}\", \"http\": ${http_port}, \"rpc\": ${rpc_port}, \"serv\": ${serv_port}}" > "${state_dir}"/ports.json
728735
# Create configuration file
729736
nomad_create_client_config "${name}" \
730-
"${http_port}" "${rpc_port}" "${serv_port}"
737+
"${addr}" "${http_port}" "${rpc_port}" "${serv_port}" \
738+
"${datacenter}"
731739
fi
732740
;;
733741
####### client -> port )########################################################
734742
port )
735-
local usage="USAGE: wb nomad ${op} ${subop} (http|rcp|serv) CLIENT-NAME"
743+
local usage="USAGE: wb nomad ${op} ${subop} (addr|http|rcp|serv) CLIENT-NAME"
736744
local port=${1:?$usage}; shift
737745
local name=${1:?$usage}; shift
738746
local state_dir=$(wb_nomad client state-dir-path "${name}")
739747
local ports_file="${state_dir}"/ports.json
740748
case "$port" in
749+
addr )
750+
jq -r .addr "${ports_file}"
751+
;;
741752
http )
742-
jq .http "${ports_file}"
753+
jq .http "${ports_file}"
743754
;;
744755
rpc )
745-
jq .rpc "${ports_file}"
756+
jq .rpc "${ports_file}"
746757
;;
747758
serv )
748-
jq .serv "${ports_file}"
759+
jq .serv "${ports_file}"
749760
;;
750761
* )
751762
false
@@ -840,28 +851,47 @@ EOL
840851
msg "$(green "Nomad client \"${name}\" started with PID ${pid_number}")"
841852
# Even if Nomad server was already running, try to connect to it!
842853
local i=0 patience=25
854+
local addr=$(wb_nomad client port addr "${name}")
843855
local http_port=$(wb_nomad client port http "${name}")
844856
msg "$(blue Waiting) for the listening HTTP server (${patience}s) ..."
845-
until curl -Isf 127.0.0.1:"${http_port}" 2>&1 | head --lines=1 | grep --quiet "HTTP/1.1"
857+
until curl -Isf "${addr}":"${http_port}" 2>&1 | head --lines=1 | grep --quiet "HTTP/1.1"
846858
do printf "%3d" $i; sleep 1
847859
i=$((i+1))
848860
if test $i -ge ${patience}
849861
then echo
850862
# Not using `fatal` here, let the caller decide!
851863
msg "$(red "FATAL: Nomad client startup did not succeed")"
852-
msg "$(yellow "port \"127.0.0.1:${http_port}\" not ready")"
864+
msg "$(yellow "port \"${addr}:${http_port}\" not ready")"
853865
msg "$(yellow "Check logs (${state_dir})")"
854866
# Let the "stop" subcommand clean everything!
855867
wb_nomad client stop "${name}" || true
856868
return 1
857869
fi
858870
echo -ne "\b\b\b"
859871
done >&2
860-
# Now check that the server and client are connected and the
861-
# client as eligible
872+
# Grab the connection details of the first running server found.
873+
# Clients configs are created to connect to all the running servers.
874+
local nomadAddress=""
875+
local nomad_servers_dir="$(wb_nomad dir-path server)"
876+
for server_name in $(ls "${nomad_servers_dir}"); do
877+
if wb_nomad server is-running "${server_name}"
878+
then
879+
local server_addr=$(wb_nomad server port addr "${server_name}")
880+
local server_port=$(wb_nomad server port rpc "${server_name}")
881+
nomadAddress="-address=${server_addr}:${server_port}"
882+
break
883+
fi
884+
done
885+
# If no running server found and no NOMAD_ADDR is not set or empty.
886+
if test "${nomadAddress}" != "" && (test -z "${NOMAD_ADDR+set}" || test -z "${NOMAD_ADDR}")
887+
then
888+
# Use the default
889+
nomadAddress="-address=http://127.0.0.1:4646"
890+
fi
891+
# Now check server and client are connected and client is eligible.
862892
local i=0 patience=25
863893
msg "$(blue Waiting) until the Nomad server sees the client (${patience}s) ..."
864-
until nomad node status -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq -r '.[0].Status' | grep --quiet "^ready"
894+
until nomad node status "${nomadAddress}" -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq -r '.[0].Status' | grep --quiet "^ready"
865895
do printf "%3d" $i; sleep 1
866896
i=$((i+1))
867897
if test $i -ge ${patience}
@@ -879,18 +909,18 @@ EOL
879909
done >&2
880910
# TODO: List the known server addresses of the client node.
881911
# nomad node config -servers
882-
local client_id=$(nomad node status -filter "\"workbench-nomad-client-cli1\" in Name" -json | jq -r '.[0].ID')
912+
local client_id=$(nomad node status "${nomadAddress}" -filter "\"workbench-nomad-client-cli1\" in Name" -json | jq -r '.[0].ID')
883913
# TODO: Configure the node?
884914
# nomad node eligibility -enable "${client_id}"
885915
# nomad node drain -disable "${client_id}"
886916
# Look for "Drivers":{"exec": {"Detected":true,"Healthy":true}}
887-
if ! test $(nomad node status -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq '.[0].Drivers.exec.Detected') = "true"
917+
if ! test $(nomad node status "${nomadAddress}" -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq '.[0].Drivers.exec.Detected') = "true"
888918
then
889919
# Not using `fatal` here, let the caller decide!
890920
msg "$(red "FATAL: Task driver \"exec\" was not detected")"
891921
return 1
892922
fi
893-
if ! test $(nomad node status -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq '.[0].Drivers.exec.Healthy') = "true"
923+
if ! test $(nomad node status "${nomadAddress}" -filter "\"workbench-nomad-client-${name}\" in Name" -json | jq '.[0].Drivers.exec.Healthy') = "true"
894924
then
895925
# Not using `fatal` here, let the caller decide!
896926
msg "$(red "FATAL: Task driver \"exec\" is not healthy")"

0 commit comments

Comments
 (0)