Update hostman script.

This commit is contained in:
Kevin Baensch 2022-11-11 15:39:33 +01:00
parent 31cf99a670
commit 2ae3bff9d7

View file

@ -1,31 +1,43 @@
#!/usr/bin/env sh #!/usr/bin/env sh
# Configurable Variables # Configurable Variables
DOCKER_SOCK_PATH="/tmp/docker.sock" [ -z $DOCKER_SOCK_PATH ] && DOCKER_SOCK_PATH="/tmp/docker.sock"
HOST_CONF_PATH="/tmp/hosts" [ -z $NETWORK_NAME ] && NETWORK_NAME="proxy"
NETWORK_NAME="proxy" [ -z $RESOLVE_DOCKERHOST ] && RESOLVE_DOCKERHOST=false
[ -z $HOST_CONF_PATH ] && HOST_CONF_PATH="/tmp/hosts"
[ -z $DOCKER_HOSTNAME_VAR ] && DOCKER_HOSTNAME_VAR="VIRTUAL_HOST"
query_docker () { query_docker () {
echo $(curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1/json$2) echo $(curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1/json$2)
} }
while true get_host_list() {
do
PROXY_HOST_CONF="" PROXY_HOST_CONF=""
CONTAINER_LIST=$(query_docker "containers" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id') CONTAINER_LIST=$(query_docker "containers" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id')
for id in $CONTAINER_LIST for id in $CONTAINER_LIST
do do
# Query individual container to access Env variables # Query individual container to access relevant data
CONTAINER_DATA=$(query_docker "containers/${id}")
if $RESOLVE_LOCALHOST
then
HOST_IP=$(echo $CONTAINER_DATA | jq -cr '.NetworkSettings.Networks.proxy.IPAddress')
else
HOST_IP="127.0.0.1"
fi
# Filter Env for HOSTNAME, remove list parenthesis and split/only keep values # Filter Env for HOSTNAME, remove list parenthesis and split/only keep values
HOST_NAMES=$(query_docker "containers/${id}" | jq -cr '.Config.Env[] | select(contains("VIRTUAL_HOST=")) | split("=")[1]') HOST_NAMES=$(echo $CONTAINER_DATA | jq -cr ".Config.Env[] | select(contains(\"$DOCKER_HOSTNAME_VAR=\")) | split(\"=\")[1]")
for hostname in $HOST_NAMES for hostname in $HOST_NAMES
do do
PROXY_HOST_CONF="$PROXY_HOST_CONF\n127.0.0.1 $hostname # Added by hostman" PROXY_HOST_CONF="$PROXY_HOST_CONF\n$HOST_IP $hostname # Added by hostman"
done done
done done
echo $PROXY_HOST_CONF
}
while true
do
FILTERED_HOSTS=$(grep -ve "# Added by hostman$" $HOST_CONF_PATH) FILTERED_HOSTS=$(grep -ve "# Added by hostman$" $HOST_CONF_PATH)
echo -e "$FILTERED_HOSTS$PROXY_HOST_CONF" > $HOST_CONF_PATH echo -e "$FILTERED_HOSTS$(get_host_list)" > $HOST_CONF_PATH
sleep 10 sleep 10
done done