~damien/infrastructure

ref: 0891707e4707c437c5b6ad0340ede511cf09239c infrastructure/tools/set-cluster-env -rwxr-xr-x 1.8 KiB
0891707e — Damien Radtke I forget 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
#
# This script sets the environment variables necessary for Consul, Nomad, and Vault
# CLI tools. It queries Terraform state for node addresses, so this script will need
# to be re-invoked after switching workspaces.
#
# In order for CLI tools to work immediately, add something like this to your .bashrc:
#
#     $ source "$(which set-cluster-env)" "${HOME}/infrastructure/terraform/"
#

if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
	echo "$(tput setaf 1)set-cluster-env should be sourced, not executed$(tput sgr0)"
	exit 1
fi

state_dir="${1:-.}"

if [[ ! -d "${state_dir}" ]]; then
	echo "directory '${state_dir}' not found"

	# This puts everything else into an else case here rather than exiting
	# because it is intended to be sourced, and running "exit" in your
	# .bashrc is an easy way to deny access to your server ever again.
else
	pushd "${state_dir}" >/dev/null

	consul_server="$(terraform output -json consul_server_ips | jq -r '.[0]')"
	nomad_server="$(terraform output -json nomad_server_ips | jq -r '.[0]')"
	vault_server="$(terraform output -json vault_server_ips | jq -r '.[0]')"

	export CONSUL_HTTP_ADDR="https://[${consul_server}]:8501"
	export CONSUL_CACERT="/etc/ssl/consul/ca.pem"
	export CONSUL_CLIENT_CERT="${HOME}/consul-cli.pem"
	export CONSUL_CLIENT_KEY="${HOME}/consul-cli-key.pem"

	export NOMAD_ADDR="https://[${nomad_server}]:4646"
	export NOMAD_CACERT="/etc/ssl/nomad/ca.pem"
	export NOMAD_CLIENT_CERT="${HOME}/nomad-cli.pem"
	export NOMAD_CLIENT_KEY="${HOME}/nomad-cli-key.pem"

	export VAULT_ADDR="https://[${vault_server}]:8200"
	export VAULT_CACERT="/etc/ssl/vault/ca.pem"
	export VAULT_CLIENT_CERT="${HOME}/vault-cli.pem"
	export VAULT_CLIENT_KEY="${HOME}/vault-cli-key.pem"

	echo "$(tput dim)cluster environment set for workspace \"$(terraform workspace show)$(tput sgr0)\""

	popd >/dev/null
fi