@@ -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
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
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} \" "
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,49 @@ 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+ else
891+ nomadAddress=" -address=${NOMAD_ADDR} "
892+ fi
893+ # Now check server and client are connected and client is eligible.
862894 local i=0 patience=25
863895 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"
896+ until nomad node status " ${nomadAddress} " -filter " \" workbench-nomad-client-${name} \" in Name" -json | jq -r ' .[0].Status' | grep --quiet " ^ready"
865897 do printf " %3d" $i ; sleep 1
866898 i=$(( i+ 1 ))
867899 if test $i -ge ${patience}
@@ -879,18 +911,18 @@ EOL
879911 done >&2
880912 # TODO: List the known server addresses of the client node.
881913 # nomad node config -servers
882- local client_id=$( nomad node status -filter " \" workbench-nomad-client-cli1\" in Name" -json | jq -r ' .[0].ID' )
914+ local client_id=$( nomad node status " ${nomadAddress} " -filter " \" workbench-nomad-client-cli1\" in Name" -json | jq -r ' .[0].ID' )
883915 # TODO: Configure the node?
884916 # nomad node eligibility -enable "${client_id}"
885917 # nomad node drain -disable "${client_id}"
886918 # 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"
919+ if ! test $( nomad node status " ${nomadAddress} " -filter " \" workbench-nomad-client-${name} \" in Name" -json | jq ' .[0].Drivers.exec.Detected' ) = " true"
888920 then
889921 # Not using `fatal` here, let the caller decide!
890922 msg " $( red " FATAL: Task driver \" exec\" was not detected" ) "
891923 return 1
892924 fi
893- if ! test $( nomad node status -filter " \" workbench-nomad-client-${name} \" in Name" -json | jq ' .[0].Drivers.exec.Healthy' ) = " true"
925+ if ! test $( nomad node status " ${nomadAddress} " -filter " \" workbench-nomad-client-${name} \" in Name" -json | jq ' .[0].Drivers.exec.Healthy' ) = " true"
894926 then
895927 # Not using `fatal` here, let the caller decide!
896928 msg " $( red " FATAL: Task driver \" exec\" is not healthy" ) "
0 commit comments