Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 1c8d6b5

Browse files
Add our logging library, and use it
Signed-off-by: Christopher Maier <[email protected]>
1 parent 48ba7a9 commit 1c8d6b5

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

hooks/environment

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
set -euo pipefail
44

5+
# shellcheck source-path=SCRIPTDIR
6+
source "$(dirname "${BASH_SOURCE[0]}")/../lib/log.sh"
7+
58
readonly default_image="hashicorp/envconsul"
69
readonly default_tag="latest"
710
readonly image="${BUILDKITE_PLUGIN_VAULT_ENV_IMAGE:-${default_image}}:${BUILDKITE_PLUGIN_VAULT_ENV_TAG:-${default_tag}}"
811

912
# Fail if there is no Vault token; gotta log in first
1013
########################################################################
1114
if [ -z "${VAULT_TOKEN:-}" ]; then
12-
echo "--- :skull_and_crossbones: Could not find 'VAULT_TOKEN' in the environment!"
13-
exit 1
15+
raise_error "Could not find 'VAULT_TOKEN' in the environment!"
1416
fi
1517

1618
# Resolve Vault address
@@ -20,8 +22,7 @@ if [ -n "${BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS:-}" ]; then
2022
export VAULT_ADDR
2123
fi
2224
if [ -z "${VAULT_ADDR:-}" ]; then
23-
echo "--- :skull_and_crossbones: Could not find 'VAULT_ADDR' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS' was not specified!"
24-
exit 1
25+
raise_error "Could not find 'VAULT_ADDR' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_ADDRESS' was not specified!"
2526
fi
2627

2728
# Resolve Vault namespace
@@ -31,8 +32,7 @@ if [ -n "${BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE:-}" ]; then
3132
export VAULT_NAMESPACE
3233
fi
3334
if [ -z "${VAULT_NAMESPACE:-}" ]; then
34-
echo "--- :skull_and_crossbones: Could not find 'VAULT_NAMESPACE' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE' was not specified!"
35-
exit 1
35+
raise_error "Could not find 'VAULT_NAMESPACE' in the environment, and 'BUILDKITE_PLUGIN_VAULT_ENV_NAMESPACE' was not specified!"
3636
fi
3737

3838
# Resolve secret prefix
@@ -61,8 +61,7 @@ plugin_read_list_into_result() {
6161
local parameter="${prefix}_${i}"
6262

6363
if [[ -n "${!prefix:-}" ]]; then
64-
echo ":rotating_light: Plugin received a string for $prefix, expected an array" >&2
65-
exit 1
64+
raise_error "Plugin received a string for $prefix, expected an array"
6665
fi
6766

6867
while [[ -n "${!parameter:-}" ]]; do
@@ -87,7 +86,7 @@ envconsul_env() {
8786

8887
# Explicitly *not* using `--rm` so we can output the container
8988
# logs in case of a failure.
90-
docker run \
89+
log_and_run docker run \
9190
--env VAULT_TOKEN \
9291
--name="${container_name}" \
9392
-- \
@@ -105,23 +104,23 @@ envconsul_env() {
105104
}
106105

107106
cleanup() {
108-
docker container rm --force "${container_name}" > /dev/null 2>&1
107+
log_and_run docker container rm --force "${container_name}" > /dev/null 2>&1
109108
}
110109

111110
trap cleanup EXIT INT QUIT
112111

113-
echo "--- :vault: Pulling secrets from Vault"
114-
echo "Using Docker image: ${image}"
115-
echo "VAULT_ADDR=${VAULT_ADDR}"
116-
echo "VAULT_NAMESPACE=${VAULT_NAMESPACE}"
112+
log "--- :vault: Pulling secrets from Vault"
113+
log "Using Docker image: ${image}"
114+
log "VAULT_ADDR=${VAULT_ADDR}"
115+
log "VAULT_NAMESPACE=${VAULT_NAMESPACE}"
117116

118117
if vault_env=$(envconsul_env); then
119118
set -o allexport
120119
eval "${vault_env}"
121120
set +o allexport
122121
else
123122
retval=$?
124-
echo "--- :skull_and_crossbones: Failed to retrieve secrets from Vault"
125-
docker container logs "${container_name}"
123+
log "--- :skull_and_crossbones: Failed to retrieve secrets from Vault"
124+
log_and_run docker container logs "${container_name}"
126125
exit ${retval}
127126
fi

lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shell_sources()

lib/log.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
# Various logging functions and helpers to make logging nice.
4+
5+
# ANSI Formatting Codes
6+
########################################################################
7+
# Because who wants to remember all those fiddly details?
8+
9+
# CSI = "Control Sequence Introducer"
10+
CSI="\e["
11+
END=m
12+
13+
NORMAL=0
14+
BOLD=1
15+
16+
WHITE=37
17+
18+
RESET="${CSI}${NORMAL}${END}"
19+
20+
function _bold_color() {
21+
color="${1}"
22+
shift
23+
echo "${CSI}${BOLD};${color}${END}${*}${RESET}"
24+
}
25+
26+
function bright_white() {
27+
_bold_color "${WHITE}" "${@}"
28+
}
29+
30+
# Logging
31+
########################################################################
32+
# NOTE: All logs get sent to standard error
33+
34+
function log() {
35+
echo -e "${@}" >&2
36+
}
37+
38+
# Handy for logging the exact command to be run, and then running it
39+
function log_and_run() {
40+
log ❯❯ "$(bright_white "$(printf "%q " "${@}")")"
41+
"$@"
42+
}
43+
44+
raise_error() {
45+
log "--- :rotating_light:" "${@}"
46+
# Yes, these numbers are correct :/
47+
if [ -z "${BASH_SOURCE[2]:-}" ]; then
48+
# If we're calling raise_error from a script directly, we'll
49+
# have a shorter call stack.
50+
log "Failed in ${FUNCNAME[1]}() at [${BASH_SOURCE[1]}:${BASH_LINENO[0]}]"
51+
else
52+
log "Failed in ${FUNCNAME[1]}() at [${BASH_SOURCE[1]}:${BASH_LINENO[0]}], called from [${BASH_SOURCE[2]}:${BASH_LINENO[1]}]"
53+
fi
54+
exit 1
55+
}

0 commit comments

Comments
 (0)