[WIP] Docker templater mostly working (to be squashed)
This commit is contained in:
parent
d1c6d54a22
commit
ed71970153
7 changed files with 77 additions and 56 deletions
|
@ -2,19 +2,25 @@ version: "3.4"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
hostman:
|
hostman:
|
||||||
image: apteno/alpine-jq
|
image: julienlecomte/docker-make
|
||||||
command: ["./hostman.sh", "&", "wait", "$!" ]
|
command: ["./hostman.sh", "&", "wait", "$!" ]
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||||
- /etc/hosts:/tmp/hosts:rw
|
- /etc/hosts:/config/hosts:rw
|
||||||
- ./script/hostman.sh:/hostman.sh:ro
|
- ./script/hostman.sh:/hostman.sh:ro
|
||||||
nginx-proxy:
|
- ./script/docker-templater.sh:/docker-templater.sh:ro
|
||||||
image: jwilder/nginx-proxy
|
- ./templates:/templates
|
||||||
|
- ./config:/config
|
||||||
|
networks:
|
||||||
|
- proxy
|
||||||
|
proxy:
|
||||||
|
image: caddy:2.6.2-alpine
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "80:80"
|
||||||
- "443:443"
|
- "443:443"
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||||
|
- ./config/Caddyfile:/etc/caddy/Caddyfile
|
||||||
networks:
|
networks:
|
||||||
- proxy
|
- proxy
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ fi
|
||||||
|
|
||||||
for row in $(jq -r '.[] | @base64' <<< "${DOCKER_DATA}")
|
for row in $(jq -r '.[] | @base64' <<< "${DOCKER_DATA}")
|
||||||
do
|
do
|
||||||
CONTAINER_DATA=$(base64 --decode <<< "${row}");
|
CONTAINER_DATA=$(base64 -d <<< "${row}");
|
||||||
# Set non Label values
|
# Set non Label values
|
||||||
if [ -z "${LOCAL_IP}" ]
|
if [ -z "${LOCAL_IP}" ]
|
||||||
then
|
then
|
||||||
|
@ -91,7 +91,7 @@ do
|
||||||
if template_hook
|
if template_hook
|
||||||
then
|
then
|
||||||
# FIX with printf
|
# FIX with printf
|
||||||
RESULT+="${PARTIAL_RESULT}"
|
RESULT=$(printf '%s' "${RESULT}" "${RESULT:+$SEPARATOR}" "${PARTIAL_RESULT}")
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
RESULT=$(printf '%s' "${RESULT}" "${RESULT:+$SEPARATOR}" "${PARTIAL_RESULT}")
|
RESULT=$(printf '%s' "${RESULT}" "${RESULT:+$SEPARATOR}" "${PARTIAL_RESULT}")
|
||||||
|
@ -100,23 +100,19 @@ do
|
||||||
# Unset label-environment variables for next container
|
# Unset label-environment variables for next container
|
||||||
for var in $(envsubst -v "${TEMPLATE}")
|
for var in $(envsubst -v "${TEMPLATE}")
|
||||||
do
|
do
|
||||||
if [ "${var}" != "LOCAL_IP" ]
|
unset "${var}"
|
||||||
then
|
|
||||||
unset "${!var}"
|
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
RESULT="${WRAP_START}${RESULT}${WRAP_END}"
|
RESULT="$(printf "%b" "${WRAP_START}" "${RESULT}" "${WRAP_END}")"
|
||||||
|
|
||||||
|
|
||||||
if [ -z "${OUT}" ]
|
if [ -z "${OUT}" ]
|
||||||
then
|
then
|
||||||
printf "%b\n" "${RESULT}";
|
printf "%b" "${RESULT}";
|
||||||
else
|
else
|
||||||
if ! diff --brief "${OUT}" - > /dev/null 2>&1 <<< "${RESULT}"
|
if [ "$(cat "${OUT}")" != "${RESULT}" ]
|
||||||
then
|
then
|
||||||
printf "Template Task: '%s' has been written to: '%s'\n" "${TEMPLATE_SRC}" "${OUT}"
|
printf "%s | Template Task: '%s' has been written to: '%s'\n" "$(date --iso-8601)" "${TEMPLATE_SRC}" "${OUT}"
|
||||||
printf "%b\n" "${RESULT}" > "${OUT}";
|
printf "%b" "${RESULT}" > "${OUT}";
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -1,46 +1,33 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
# Configurable Variables
|
# Configurable Variables
|
||||||
[ -z $DOCKER_SOCK_PATH ] && DOCKER_SOCK_PATH="/tmp/docker.sock"
|
DOCKER_SOCK_PATH="${DOCKER_SOCK_PATH:-/tmp/docker.sock}"
|
||||||
[ -z $NETWORK_NAME ] && NETWORK_NAME="proxy"
|
NETWORK_NAME="${NETWORK_NAME:-proxy}"
|
||||||
[ -z $RESOLVE_DOCKERHOST ] && RESOLVE_DOCKERHOST=false
|
TEMPLATER_PATH="${TEMPLATER_PATH:-/docker-templater.sh}"
|
||||||
[ -z $HOST_CONF_PATH ] && HOST_CONF_PATH="/tmp/hosts"
|
TEMPLATE_FOLDER_PATH="${TEMPLATE_FOLDER_PATH:-/templates}"
|
||||||
[ -z $DOCKER_HOSTNAME_VAR ] && DOCKER_HOSTNAME_VAR="VIRTUAL_HOST"
|
|
||||||
|
|
||||||
query_docker () {
|
query_docker () {
|
||||||
curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1$2
|
curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1$2
|
||||||
}
|
}
|
||||||
|
|
||||||
get_host_list() {
|
update_templates() {
|
||||||
PROXY_HOST_CONF=""
|
CONTAINER_LIST=$(query_docker "containers/json" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}")
|
||||||
CONTAINER_LIST=$(query_docker "containers/json" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id')
|
if [ "${CONTAINER_LIST}" != "${OLD_CONTAINER_LIST}" ]
|
||||||
|
|
||||||
for id in $CONTAINER_LIST
|
|
||||||
do
|
|
||||||
# Query individual container to access relevant data
|
|
||||||
CONTAINER_DATA=$(query_docker "containers/${id}/json")
|
|
||||||
if $RESOLVE_DOCKERHOST
|
|
||||||
then
|
then
|
||||||
HOST_IP=$(echo $CONTAINER_DATA | jq -cr '.NetworkSettings.Networks.proxy.IPAddress')
|
for template in "${TEMPLATE_FOLDER_PATH}"/*
|
||||||
else
|
|
||||||
HOST_IP="127.0.0.1"
|
|
||||||
fi
|
|
||||||
# Filter Env for HOSTNAME, remove list parenthesis and split/only keep values
|
|
||||||
HOST_NAMES=$(echo $CONTAINER_DATA | jq -cr ".Config.Env[] | select(contains(\"$DOCKER_HOSTNAME_VAR=\")) | split(\"=\")[1]")
|
|
||||||
|
|
||||||
for hostname in $HOST_NAMES
|
|
||||||
do
|
do
|
||||||
PROXY_HOST_CONF="$PROXY_HOST_CONF\n$HOST_IP $hostname # Added by hostman"
|
if ! "${TEMPLATER_PATH}" "${template}" "${CONTAINER_LIST}"
|
||||||
|
then
|
||||||
|
echo "Error Processing Template: ${template}"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
done
|
OLD_CONTAINER_LIST="${CONTAINER_LIST}"
|
||||||
echo $PROXY_HOST_CONF
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
update_host_list() {
|
# Initial Generation
|
||||||
FILTERED_HOSTS=$(grep -ve "# Added by hostman$" $HOST_CONF_PATH)
|
update_templates
|
||||||
echo -e "$FILTERED_HOSTS$(get_host_list)" > $HOST_CONF_PATH
|
|
||||||
}
|
|
||||||
|
|
||||||
update_host_list
|
|
||||||
# cannot filter because reailine no longer recognized lines otherwise (check how IFS changes)
|
# cannot filter because reailine no longer recognized lines otherwise (check how IFS changes)
|
||||||
query_docker "events" | while true
|
query_docker "events" | while true
|
||||||
do
|
do
|
||||||
|
@ -48,7 +35,7 @@ do
|
||||||
# wait for related events to finish
|
# wait for related events to finish
|
||||||
while [ $? -eq 0 ]
|
while [ $? -eq 0 ]
|
||||||
do
|
do
|
||||||
read -t 5 -r;
|
read -t 8 -r
|
||||||
done
|
done
|
||||||
update_host_list
|
update_templates
|
||||||
done
|
done
|
||||||
|
|
|
@ -5,4 +5,4 @@ chown -R sqlproxy:sqlproxy /etc/ssh/.ssh
|
||||||
chmod 0700 /etc/ssh/.ssh
|
chmod 0700 /etc/ssh/.ssh
|
||||||
chmod 0600 /etc/ssh/.ssh/authorized_keys
|
chmod 0600 /etc/ssh/.ssh/authorized_keys
|
||||||
|
|
||||||
source ./hostman.sh
|
sleep infinity
|
||||||
|
|
|
@ -1,16 +1,33 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -e
|
set -e
|
||||||
|
DOCKER_SOCK_PATH="${DOCKER_SOCK_PATH:-/tmp/docker.sock}"
|
||||||
|
DOCKER_CADDY_NAME="${DOCKER_CADDY_NAME:-proxy}"
|
||||||
|
DOCKER_CADDY_PORT="${DOCKER_CADDY_PORT:-2020}"
|
||||||
|
|
||||||
TEMPLATE='reverse_proxy $LOCAL_WEB_HOST ${LOCAL_IP}:$LOCAL_WEB_PORT'
|
WRAP_START='{\n admin :2020\n}\n'
|
||||||
|
WRAP_END='\n'
|
||||||
|
TEMPLATE='${LOCAL_WEB_HOST} {\n reverse_proxy ${LOCAL_IP}:$LOCAL_WEB_PORT\n}'
|
||||||
|
SEPARATOR='\n'
|
||||||
|
OUT='/config/Caddyfile'
|
||||||
|
|
||||||
label_hook() {
|
label_hook() {
|
||||||
LOCAL_WEB_PORT="${LOCAL_WEB_PORT:-80}"
|
LOCAL_WEB_PORT="${LOCAL_WEB_PORT:-80}"
|
||||||
}
|
}
|
||||||
|
|
||||||
template_hook() {
|
template_hook() {
|
||||||
if grep -q '^reverse_proxy [^\s] [:.0-9a-e]\+:\d+$' <<< "${PARTIAL_RESULT}"
|
printf "%s" "${PARTIAL_RESULT}"
|
||||||
|
if grep '^[^ ]\+ {\\n reverse_proxy \(?::\|[.0-9a-e]\)\+\:[0-9]\+\\n}$' <<< "${PARTIAL_RESULT}"
|
||||||
then
|
then
|
||||||
return 0;
|
return 0;
|
||||||
fi
|
fi
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# finally_hook() {
|
||||||
|
# if curl --silent -g "${DOCKER_CADDY_NAME}:${DOCKER_CADDY_PORT}/load"
|
||||||
|
# then
|
||||||
|
# printf "Updated Caddy Config"
|
||||||
|
# else
|
||||||
|
# printf "Failed to update Caddy Config"
|
||||||
|
# fi
|
||||||
|
# }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -e
|
set -e
|
||||||
HOST_CONF_PATH="${HOST_CONF_PATH:-/tmp/hosts}"
|
HOST_CONF_PATH="${HOST_CONF_PATH:-/config/hosts}"
|
||||||
RESOLVE_DOCKERHOST="${RESOLVE_DOCKERHOST:-false}"
|
RESOLVE_DOCKERHOST="${RESOLVE_DOCKERHOST:-false}"
|
||||||
DOCKER_HOSTNAME_VAR="${DOCKER_HOSTNAME_VAR:-LOCAL_WEB_HOST}"
|
DOCKER_HOSTNAME_VAR="${DOCKER_HOSTNAME_VAR:-LOCAL_WEB_HOST}"
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ else
|
||||||
OUT="${HOST_CONF_OUT}"
|
OUT="${HOST_CONF_OUT}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check_template() {
|
template_hook() {
|
||||||
if grep -q '^[:.0-9a-e]\+ [^\s]\+ # Added by hostman$' <<< "${PARTIAL_RESULT}"
|
if grep -q '^[:.0-9a-e]\+ [^ ]\+ # Added by hostman$' <<< "${PARTIAL_RESULT}"
|
||||||
then
|
then
|
||||||
return 0;
|
return 0;
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
EXCLUDE_USERPASS="${EXCLUDE_USERPASS:-false}"
|
||||||
set -e
|
set -e
|
||||||
WRAP_START='[\n'
|
WRAP_START='[\n'
|
||||||
|
if ${EXCLUDE_USERPASS}
|
||||||
|
then
|
||||||
|
TEMPLATE=' { type: "${LOCAL_DB_TYPE}", host: "${LOCAL_DB_HOST}", port: "${LOCAL_DB_PORT}" }'
|
||||||
|
else
|
||||||
TEMPLATE=' { type: "${LOCAL_DB_TYPE}", user: "${LOCAL_DB_USER}", password: "${LOCAL_DB_PASSWORD}", host: "${LOCAL_DB_HOST}", port: "${LOCAL_DB_PORT}" }'
|
TEMPLATE=' { type: "${LOCAL_DB_TYPE}", user: "${LOCAL_DB_USER}", password: "${LOCAL_DB_PASSWORD}", host: "${LOCAL_DB_HOST}", port: "${LOCAL_DB_PORT}" }'
|
||||||
|
fi
|
||||||
SEPARATOR=',\n'
|
SEPARATOR=',\n'
|
||||||
WRAP_END='\n]'
|
WRAP_END='\n]'
|
||||||
|
OUT="/config/sqlproxy.json"
|
||||||
|
|
||||||
|
template_hook() {
|
||||||
|
if grep -q 'type: "(?mysql|psql)".*host: ".+", port: "\d+"'
|
||||||
|
then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue