Update docker scripts to work with a proxy

This commit is contained in:
IamTheFij 2019-07-29 15:01:07 -07:00
parent e5687ed83e
commit e8f7ba6801
2 changed files with 41 additions and 14 deletions

View File

@ -7,19 +7,32 @@ set -e
# Checks the most recent state exit code of a Docker container # Checks the most recent state exit code of a Docker container
################# #################
container_name=$1 # Docker host will default to a socket
# To override, export DOCKER_HOST to a new hostname
DOCKER_HOST="${DOCKER_HOST:=socket}"
container_name="$1"
# Curls Docker either using a socket or URL
function curl_docker {
local path="$1"
if [ "$DOCKER_HOST" == "socket" ]; then
curl --unix-socket /var/run/docker.sock "http://localhost/$path" 2>/dev/null
else
curl "http://${DOCKER_HOST}/$path" 2>/dev/null
fi
}
# Returns caintainer ID for a given container name # Returns caintainer ID for a given container name
function get_container_id { function get_container_id {
local container_name=$1 local container_name="$1"
curl --unix-socket /var/run/docker.sock 'http://localhost/containers/json?all=1' 2>/dev/null \ curl_docker 'containers/json?all=1' \
| jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id" | jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id"
} }
# Returns container JSON # Returns container JSON
function inspect_container { function inspect_container {
local container_id=$1 local container_id=$1
curl --unix-socket /var/run/docker.sock http://localhost/containers/$container_id/json 2>/dev/null curl_docker "containers/$container_id/json"
} }
if [ -z "$container_name" ]; then if [ -z "$container_name" ]; then
@ -33,6 +46,6 @@ 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)
exit $exit_code exit "$exit_code"

View File

@ -1,4 +1,5 @@
#! /bin/bash #! /bin/bash
set -e
################# #################
# docker_healthcheck.sh # docker_healthcheck.sh
@ -6,19 +7,32 @@
# Returns the results of a Docker Healthcheck for a container # Returns the results of a Docker Healthcheck for a container
################# #################
container_name=$1 # Docker host will default to a socket
# To override, export DOCKER_HOST to a new hostname
DOCKER_HOST="${DOCKER_HOST:=socket}"
container_name="$1"
# Curls Docker either using a socket or URL
function curl_docker {
local path="$1"
if [ "$DOCKER_HOST" == "socket" ]; then
curl --unix-socket /var/run/docker.sock "http://localhost/$path" 2>/dev/null
else
curl "http://${DOCKER_HOST}/$path" 2>/dev/null
fi
}
# Returns caintainer ID for a given container name # Returns caintainer ID for a given container name
function get_container_id { function get_container_id {
local container_name=$1 local container_name="$1"
curl --unix-socket /var/run/docker.sock 'http://localhost/containers/json?all=1' 2>/dev/null \ curl_docker 'containers/json?all=1' \
| jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id" | jq -r ".[] | {Id, Name: .Names[]} | select(.Name == \"/${container_name}\") | .Id"
} }
# Returns container JSON # Returns container JSON
function inspect_container { function inspect_container {
local container_id=$1 local container_id="$1"
curl --unix-socket /var/run/docker.sock http://localhost/containers/$container_id/json 2>/dev/null curl_docker "containers/$container_id/json"
} }
if [ -z "$container_name" ]; then if [ -z "$container_name" ]; then
@ -27,14 +41,14 @@ if [ -z "$container_name" ]; then
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
health=$(inspect_container $container_id | jq -r '.State.Health.Status') health=$(inspect_container "$container_id" | jq -r '.State.Health.Status')
case $health in case "$health" in
"null") "null")
echo "No healthcheck results" echo "No healthcheck results"
;; ;;