@@ -31,8 +31,8 @@ usage_nomad() {
31
31
Location of the Agent's everything folder
32
32
$( helpcmd \( server/client\) config-file-path NAME)
33
33
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)
36
36
Getter for the previously configured ports
37
37
$( helpcmd \( server/client\) pid-filepath NAME)
38
38
Location of the Agent's "running" flag
425
425
local server_name=${1:? $usage } ; shift
426
426
local client_name=${1:? $usage } ; shift
427
427
# 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
429
429
then
430
430
fatal " Failed to configure Nomad server \" ${server_name} \" "
431
431
fi
437
437
# WARNING: Actually the client is configured to connect to all the
438
438
# running servers, so if there are no servers ready the Nomad
439
439
# 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 "
441
441
then
442
442
wb_nomad server stop " ${server_name} " || true
443
443
fatal " Failed to configure Nomad client \" ${client_name} \" "
496
496
;;
497
497
# ###### server -> configure )###################################################
498
498
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"
500
500
local name=${1:? $usage } ; shift
501
- # Ports
501
+ # Address and ports
502
+ local addr=${1:? $usage } ; shift
502
503
local http_port=${1:? $usage } ; shift
503
504
local rpc_port=${1:? $usage } ; shift
504
505
local serv_port=${1:? $usage } ; shift
@@ -516,28 +517,31 @@ EOL
516
517
mkdir -p " ${state_dir} " /config
517
518
mkdir -p " ${state_dir} " /data/server
518
519
# 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
520
521
# Configure
521
522
nomad_create_server_config " ${name} " \
522
- " ${http_port} " " ${rpc_port} " " ${serv_port} "
523
+ " ${addr} " " ${ http_port}" " ${rpc_port} " " ${serv_port} "
523
524
fi
524
525
;;
525
526
# ###### server -> port )########################################################
526
527
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"
528
529
local port=${1:? $usage } ; shift
529
530
local name=${1:? $usage } ; shift
530
531
local state_dir=$( wb_nomad server state-dir-path " ${name} " )
531
532
local ports_file=" ${state_dir} " /ports.json
532
533
case " $port " in
534
+ addr )
535
+ jq -r .addr " ${ports_file} "
536
+ ;;
533
537
http )
534
- jq .http " ${ports_file} "
538
+ jq .http " ${ports_file} "
535
539
;;
536
540
rpc )
537
- jq .rpc " ${ports_file} "
541
+ jq .rpc " ${ports_file} "
538
542
;;
539
543
serv )
540
- jq .serv " ${ports_file} "
544
+ jq .serv " ${ports_file} "
541
545
;;
542
546
* )
543
547
false
@@ -602,16 +606,17 @@ EOL
602
606
msg " $( green " Nomad server \" ${name} \" started with PID ${pid_number} " ) "
603
607
# Even if Nomad server was already running, try to connect to it!
604
608
local i=0 patience=25
609
+ local addr=$( wb_nomad server port addr " ${name} " )
605
610
local http_port=$( wb_nomad server port http " ${name} " )
606
611
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"
608
613
do printf " %3d" $i ; sleep 1
609
614
i=$(( i+ 1 ))
610
615
if test $i -ge ${patience}
611
616
then echo
612
617
# Not using `fatal` here, let the caller decide!
613
618
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" ) "
615
620
msg " $( yellow " Check logs (${state_dir} )" ) "
616
621
# Let the "stop" subcommand clean everything!
617
622
wb_nomad server stop " ${name} " || true
@@ -702,12 +707,14 @@ EOL
702
707
;;
703
708
# ###### client -> configure )###################################################
704
709
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 ]"
706
711
local name=${1:? $usage } ; shift
707
- # Ports
712
+ # Address and ports
713
+ local addr=${1:? $usage } ; shift
708
714
local http_port=${1:? $usage } ; shift
709
715
local rpc_port=${1:? $usage } ; shift
710
716
local serv_port=${1:? $usage } ; shift
717
+ local datacenter=${1:- loopback} ; shift
711
718
# Checks
712
719
# Assume the presence of the PID file means "running" because it
713
720
# can represent an abnormal exit / uknown state!
@@ -724,28 +731,32 @@ EOL
724
731
mkdir -p " ${state_dir} " /config
725
732
mkdir -p " ${state_dir} " /data/{client,plugins,alloc}
726
733
# 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
728
735
# Create configuration file
729
736
nomad_create_client_config " ${name} " \
730
- " ${http_port} " " ${rpc_port} " " ${serv_port} "
737
+ " ${addr} " " ${http_port} " " ${rpc_port} " " ${serv_port} " \
738
+ " ${datacenter} "
731
739
fi
732
740
;;
733
741
# ###### client -> port )########################################################
734
742
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"
736
744
local port=${1:? $usage } ; shift
737
745
local name=${1:? $usage } ; shift
738
746
local state_dir=$( wb_nomad client state-dir-path " ${name} " )
739
747
local ports_file=" ${state_dir} " /ports.json
740
748
case " $port " in
749
+ addr )
750
+ jq -r .addr " ${ports_file} "
751
+ ;;
741
752
http )
742
- jq .http " ${ports_file} "
753
+ jq .http " ${ports_file} "
743
754
;;
744
755
rpc )
745
- jq .rpc " ${ports_file} "
756
+ jq .rpc " ${ports_file} "
746
757
;;
747
758
serv )
748
- jq .serv " ${ports_file} "
759
+ jq .serv " ${ports_file} "
749
760
;;
750
761
* )
751
762
false
@@ -840,28 +851,47 @@ EOL
840
851
msg " $( green " Nomad client \" ${name} \" started with PID ${pid_number} " ) "
841
852
# Even if Nomad server was already running, try to connect to it!
842
853
local i=0 patience=25
854
+ local addr=$( wb_nomad client port addr " ${name} " )
843
855
local http_port=$( wb_nomad client port http " ${name} " )
844
856
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"
846
858
do printf " %3d" $i ; sleep 1
847
859
i=$(( i+ 1 ))
848
860
if test $i -ge ${patience}
849
861
then echo
850
862
# Not using `fatal` here, let the caller decide!
851
863
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" ) "
853
865
msg " $( yellow " Check logs (${state_dir} )" ) "
854
866
# Let the "stop" subcommand clean everything!
855
867
wb_nomad client stop " ${name} " || true
856
868
return 1
857
869
fi
858
870
echo -ne " \b\b\b"
859
871
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.
862
892
local i=0 patience=25
863
893
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"
865
895
do printf " %3d" $i ; sleep 1
866
896
i=$(( i+ 1 ))
867
897
if test $i -ge ${patience}
@@ -879,18 +909,18 @@ EOL
879
909
done >&2
880
910
# TODO: List the known server addresses of the client node.
881
911
# 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' )
883
913
# TODO: Configure the node?
884
914
# nomad node eligibility -enable "${client_id}"
885
915
# nomad node drain -disable "${client_id}"
886
916
# 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"
888
918
then
889
919
# Not using `fatal` here, let the caller decide!
890
920
msg " $( red " FATAL: Task driver \" exec\" was not detected" ) "
891
921
return 1
892
922
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"
894
924
then
895
925
# Not using `fatal` here, let the caller decide!
896
926
msg " $( red " FATAL: Task driver \" exec\" is not healthy" ) "
0 commit comments