From 0a5be250b515ba73d5987afc8e447b048bd5cc9c Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Mon, 16 Nov 2020 15:52:21 -0800 Subject: [PATCH] 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. --- scripts/docker_check.sh | 18 +++++++++++++++--- scripts/docker_healthcheck.sh | 14 +++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/docker_check.sh b/scripts/docker_check.sh index 4633574..c47b8d7 100755 --- a/scripts/docker_check.sh +++ b/scripts/docker_check.sh @@ -11,6 +11,7 @@ set -e # To override, export DOCKER_HOST to a new hostname DOCKER_HOST="${DOCKER_HOST:=socket}" container_name="$1" +num_log_lines="$2" # Curls Docker either using a socket or URL function curl_docker { @@ -31,21 +32,32 @@ function get_container_id { # Returns container JSON function inspect_container { - local container_id=$1 + local container_id="$1" 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 - 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" exit 1 fi -container_id=$(get_container_id $container_name) +container_id=$(get_container_id "$container_name") if [ -z "$container_id" ]; then echo "ERROR: Could not find container with name: $container_name" exit 1 fi 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" diff --git a/scripts/docker_healthcheck.sh b/scripts/docker_healthcheck.sh index ec4ace4..560e440 100755 --- a/scripts/docker_healthcheck.sh +++ b/scripts/docker_healthcheck.sh @@ -11,6 +11,7 @@ set -e # To override, export DOCKER_HOST to a new hostname DOCKER_HOST="${DOCKER_HOST:=socket}" container_name="$1" +num_log_lines="$2" # Curls Docker either using a socket or URL function curl_docker { @@ -35,8 +36,15 @@ function inspect_container { 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 - echo "Usage: $0 container_name" + echo "Usage: $0 container_name [num_log_lines]" echo "Will return results of healthcheck for continer with provided name" exit 1 fi @@ -48,6 +56,10 @@ if [ -z "$container_id" ]; then fi 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 null) echo "No healthcheck results"