Last active
June 9, 2022 22:28
-
-
Save istarkov/34bfd356a0e15aae4339eb89af0715de to your computer and use it in GitHub Desktop.
Bash alias for terraform, switches all subfolder workspaces loads secrets sets NOMAD and CONSUL envs
This file contains 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
# ------------------------------------------------------------------------------ | |
# `hashi` - shows all subfolders workspaces | |
# `hashi test` - switch all subfolders terraform workspaces to test, load test secrets and sets nomad and consul env vars | |
# `hashi -init-all` - execute terraform init at all subfolders terraform workspaces | |
# `hashi <tab>` - autocomplete workspaces | |
# `hashi .<tab>` - autocomlete and cd into | |
# `hashi -<tab>` - autocomlete commands | |
alias gitclean="git remote prune origin && git branch -vv | grep 'origin/.*: gone]' | awk '{print \$1}' | xargs git branch -D" | |
declare -A HASHI_DIRNAMES=() | |
declare -A HASHI_SERVER_IPS=( | |
["prod"]="116.203.10.0" | |
["test"]="116.203.11.244" | |
) | |
HASHI_WORKSPACES=${!HASHI_SERVER_IPS[@]} | |
hashi () { | |
GREEN='\033[0;32m' | |
NC='\033[0m' | |
if [[ -z "$1" ]]; then | |
find . -type d -name ".terraform" \ | |
| xargs -I {} readlink -f "{}/.." \ | |
| xargs -I {} bash -c "cd {} && echo -e \"${GREEN}\$(terraform workspace show)${NC} - {}\"" | |
for dir in $(find . -type d -name ".terraform" | xargs -I {} dirname "{}"); do | |
base=$(basename $dir) | |
HASHI_DIRNAMES[".${base}"]="$PWD/$dir" | |
done | |
else | |
if [[ $1 == .* ]]; then | |
cd "${HASHI_DIRNAMES[$1]}" | |
return 0 | |
fi | |
if [[ $1 == -init-all* ]]; then | |
find . -type d -name ".terraform" \ | |
| xargs -I {} readlink -f "{}/.." \ | |
| xargs -I {} bash -c "cd {} && terraform init -migrate-state" | |
return 0 | |
fi | |
SERVER_IP=${HASHI_SERVER_IPS["$1"]} | |
if [[ -z "$SERVER_IP" ]]; then | |
echo "Env $1 not found, use 'hashi (${HASHI_WORKSPACES/ / | })'" | |
return 1 | |
fi | |
echo "Loading secrets and set consul and nomad keys to server = ${SERVER_IP}" | |
set -o allexport; source <(gsutil cat gs://realadvisor-terraform-state/.secrets/.hashi-secrets-$1); set +o allexport | |
set -o allexport; source <(echo -e "NOMAD_ADDR=http://${SERVER_IP}:4646\nNOMAD_HTTP_AUTH=admin:${TF_VAR_haproxy_password}"); set +o allexport | |
set -o allexport; source <(echo -e "CONSUL_HTTP_ADDR=http://${SERVER_IP}:8500\nCONSUL_HTTP_AUTH=admin:${TF_VAR_haproxy_password}"); set +o allexport | |
find . -type d -name ".terraform" \ | |
| xargs -I {} readlink -f "{}/.." \ | |
| xargs -I {} bash -c "cd {} && terraform workspace select $1" | |
hashi | |
fi | |
} | |
hashi_complete () { | |
COMPREPLY=() | |
local cur="${COMP_WORDS[COMP_CWORD]}" | |
if [[ $cur == .* ]]; then | |
COMPREPLY+=( $( compgen -W "${!HASHI_DIRNAMES[*]}" -- $cur ) ) | |
elif [[ $cur == -* ]]; then | |
COMPREPLY+=( $( compgen -W "-init-all" -- $cur ) ) | |
else | |
COMPREPLY+=( $( compgen -W "${HASHI_WORKSPACES}" -- $cur ) ) | |
fi | |
} | |
complete -F hashi_complete 'hashi' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment