Skip to content

Commit 2fbd89c

Browse files
committed
Simplify entrypoint
1 parent 3ae3646 commit 2fbd89c

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

Dockerfile.entrypoint

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,46 @@ set -e
44

55
# function that prints info message, if TUNNEL_LOGLEVEL is DEBUG or INFO
66
printInfo() {
7-
if [ "${TUNNEL_LOGLEVEL}" != 'info' ] && [ "${TUNNEL_LOGLEVEL}" != 'debug' ]; then return 0; fi
7+
echo "${TUNNEL_LOGLEVEL}" | grep -q -E '^(debug|info)$' || return 0
88
formattedDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
99
echo "$formattedDate INF ${1}"
1010
}
1111

12-
# Prevent message about missing config file.
13-
if [ ! -f /etc/cloudflared/config.yml ]; then
14-
printInfo "/etc/cloudflared/config.yml not found autogenerating one"
15-
echo "---" >/etc/cloudflared/config.yml
16-
fi
12+
# function that prints fatal message and exits
13+
printAndExit() {
14+
formattedDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
15+
echo >&2 "$formattedDate FAT ${2}"
16+
exit "${1}"
17+
}
1718

1819
if [ -n "${TUNNEL_NAME}" ]; then
19-
echo >&2 "You cannot use TUNNEL_NAME it's derived from TUNNEL_HOSTNAME" && exit 1
20+
printAndExit $? "You cannot use TUNNEL_NAME it's derived from TUNNEL_HOSTNAME"
2021
fi
2122

2223
if [ -n "${TUNNEL_FORCE_PROVISIONING_DNS}" ]; then
23-
echo >&2 "You cannot use TUNNEL_FORCE_PROVISIONING_DNS it's determined via route dns command" && exit 1
24+
printAndExit $? "You cannot use TUNNEL_FORCE_PROVISIONING_DNS it's determined via route dns command";
2425
fi
2526

2627
if [ -z "${TUNNEL_HOSTNAME}" ]; then
27-
echo >&2 "You need to specify TUNNEL_HOSTNAME" && exit 1
28+
printAndExit $? "You need to specify TUNNEL_HOSTNAME";
2829
fi
2930

3031
if [ -z "${TUNNEL_URL}" ] && [ -z "${TUNNEL_UNIX_SOCKET}" ]; then
31-
echo >&2 "You need to specify TUNNEL_URL or TUNNEL_UNIX_SOCKET" && exit 1
32+
printAndExit $? "You need to specify TUNNEL_URL or TUNNEL_UNIX_SOCKET";
33+
fi
34+
35+
echo "${TUNNEL_LOGLEVEL}" | grep -q -E '^(debug|info|warn|error|fatal)$' || printAndExit $? "TUNNEL_LOGLEVEL must be debug|info|warn|error|fatal"
36+
37+
# Prevent message about missing config file.
38+
if [ ! -f /etc/cloudflared/config.yml ]; then
39+
printInfo "/etc/cloudflared/config.yml not found autogenerating one"
40+
echo "---" >/etc/cloudflared/config.yml
3241
fi
3342

3443
export TUNNEL_FORCE_PROVISIONING_DNS='false'
35-
derived_tunnel_name=$(echo "$TUNNEL_HOSTNAME" | sed "s,^.*://,,g")
44+
derived_tunnel_name=$(echo "${TUNNEL_HOSTNAME}" | sed "s,^.*://,,g")
3645

37-
export TUNNEL_NAME="${TUNNEL_NAME:-$derived_tunnel_name}"
46+
export TUNNEL_NAME="${TUNNEL_NAME:-${derived_tunnel_name}}"
3847

3948
existing_tunnels_json=$(cloudflared --loglevel 'error' tunnel list --name "${TUNNEL_NAME}" --output json)
4049
if [ "${existing_tunnels_json}" != "[]" ]; then
@@ -43,10 +52,13 @@ if [ "${existing_tunnels_json}" != "[]" ]; then
4352
else
4453
printInfo "No existing named tunnel found"
4554
fi
55+
4656
printInfo "Creating new named tunnel '${TUNNEL_NAME}'"
4757
cloudflared --loglevel 'error' tunnel create "${TUNNEL_NAME}" 1>/dev/null
58+
4859
printInfo "Routing dns from '${TUNNEL_NAME}' to tunnel"
4960
cloudflared --loglevel 'error' tunnel route dns --overwrite-dns "${TUNNEL_NAME}" "${derived_tunnel_name}"
61+
5062
unset TUNNEL_HOSTNAME
5163

5264
exec "$@"

0 commit comments

Comments
 (0)