Last active
February 23, 2023 06:54
-
-
Save Himura2la/9d6f4501c5b9ea67c2cda4423195d6fc to your computer and use it in GitHub Desktop.
Discover HA cluster of HashiCorp tools (Consul, Nomad, and Vault)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
hash curl || exit 1 | |
hash jq || exit 2 | |
usage_msg=" | |
Usage: | |
$0 <leader|followers> <consul|nomad|vault> <host_address> [<query_string>] | |
Examples: | |
$0 leader vault my-vault.example.com | |
$0 leader consul my-consul.example.com '?dc=us-dc' | |
$0 followers nomad my-nomad.example.com '?region=us' | |
" | |
role="${1?"$usage_msg"}" | |
service="${2?"$usage_msg"}" | |
host_address="${3?"$usage_msg"}" | |
query_string="$4" | |
case "$service" in | |
consul|nomad) | |
leader_response="$(curl --silent "https://$host_address/v1/status/leader$query_string")" | |
leader_response="$(jq -r "." <<< "$leader_response")" | |
;; | |
vault) | |
if [[ "$role" == "followers" ]] | |
then | |
echo "Not supported." 1>&2 ; exit 3 | |
fi | |
leader_response="$(curl --silent "https://$host_address/v1/sys/leader$query_string")" | |
leader_response="$(jq -r ".leader_cluster_address" <<< "$leader_response")" | |
leader_response="${leader_response#*//}" | |
;; | |
*) echo "Error: service '$service' not in <consul|nomad|vault>" 1>&2 ; exit 4 ;; | |
esac | |
case "$role" in | |
leader) | |
echo "$leader_response" | |
;; | |
followers) | |
peers_response="$(curl --silent "https://$host_address/v1/status/peers$query_string")" | |
cluster_followers="$(jq -c "[ .[] | select(. != \"$leader_response\") ]" <<< "$peers_response")" | |
echo "$cluster_followers" | |
;; | |
*) echo "Error: role '$role' not in <leader|followers> (leader: $leader_response)" 1>&2 ; exit 5 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment