Scripts: Add echoing log lines to helper scripts

Rather than only returning the status of whether or not a container is
healhthy, the helper scripts will now optionally echo some of the latest
log lines.
This commit is contained in:
IamTheFij 2020-11-16 15:52:21 -08:00
parent 88f77aa27c
commit 0a5be250b5
2 changed files with 28 additions and 4 deletions

View File

@ -11,6 +11,7 @@ set -e
# To override, export DOCKER_HOST to a new hostname # To override, export DOCKER_HOST to a new hostname
DOCKER_HOST="${DOCKER_HOST:=socket}" DOCKER_HOST="${DOCKER_HOST:=socket}"
container_name="$1" container_name="$1"
num_log_lines="$2"
# Curls Docker either using a socket or URL # Curls Docker either using a socket or URL
function curl_docker { function curl_docker {
@ -31,21 +32,32 @@ function get_container_id {
# Returns container JSON # Returns container JSON
function inspect_container { function inspect_container {
local container_id=$1 local container_id="$1"
curl_docker "containers/$container_id/json" curl_docker "containers/$container_id/json"
} }
# Gets some lines from docker log
function get_logs {
container_id="$1"
num_lines="$2"
curl_docker "containers/$container_id/logs?stdout=1&stderr=1" | tail -n "$num_lines"
}
if [ -z "$container_name" ]; then if [ -z "$container_name" ]; then
echo "Usage: $0 container_name" echo "Usage: $0 container_name [num_log_lines]"
echo "Will exit with the last status code of continer with provided name" echo "Will exit with the last status code of continer with provided name"
exit 1 exit 1
fi fi
container_id=$(get_container_id $container_name) container_id=$(get_container_id "$container_name")
if [ -z "$container_id" ]; then if [ -z "$container_id" ]; then
echo "ERROR: Could not find container with name: $container_name" echo "ERROR: Could not find container with name: $container_name"
exit 1 exit 1
fi fi
exit_code=$(inspect_container "$container_id" | jq -r .State.ExitCode) exit_code=$(inspect_container "$container_id" | jq -r .State.ExitCode)
if [ -n "$num_log_lines" ]; then
get_logs "$container_id" "$num_log_lines"
fi
exit "$exit_code" exit "$exit_code"

View File

@ -11,6 +11,7 @@ set -e
# To override, export DOCKER_HOST to a new hostname # To override, export DOCKER_HOST to a new hostname
DOCKER_HOST="${DOCKER_HOST:=socket}" DOCKER_HOST="${DOCKER_HOST:=socket}"
container_name="$1" container_name="$1"
num_log_lines="$2"
# Curls Docker either using a socket or URL # Curls Docker either using a socket or URL
function curl_docker { function curl_docker {
@ -35,8 +36,15 @@ function inspect_container {
curl_docker "containers/$container_id/json" curl_docker "containers/$container_id/json"
} }
# Gets some lines from docker log
function get_logs {
container_id="$1"
num_lines="$2"
curl_docker "containers/$container_id/logs?stdout=1&stderr=1" | tail -n "$num_lines"
}
if [ -z "$container_name" ]; then if [ -z "$container_name" ]; then
echo "Usage: $0 container_name" echo "Usage: $0 container_name [num_log_lines]"
echo "Will return results of healthcheck for continer with provided name" echo "Will return results of healthcheck for continer with provided name"
exit 1 exit 1
fi fi
@ -48,6 +56,10 @@ if [ -z "$container_id" ]; then
fi fi
health=$(inspect_container "$container_id" | jq -r '.State.Health.Status') health=$(inspect_container "$container_id" | jq -r '.State.Health.Status')
if [ -n "$num_log_lines" ]; then
get_logs "$container_id" "$num_log_lines"
fi
case "$health" in case "$health" in
null) null)
echo "No healthcheck results" echo "No healthcheck results"