44
55# function that prints info message, if TUNNEL_LOGLEVEL is DEBUG or INFO
66printInfo () {
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
1819if [ -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"
2021fi
2122
2223if [ -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" ;
2425fi
2526
2627if [ -z " ${TUNNEL_HOSTNAME} " ]; then
27- echo >&2 " You need to specify TUNNEL_HOSTNAME" && exit 1
28+ printAndExit $? " You need to specify TUNNEL_HOSTNAME" ;
2829fi
2930
3031if [ -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
3241fi
3342
3443export 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
3948existing_tunnels_json=$( cloudflared --loglevel ' error' tunnel list --name " ${TUNNEL_NAME} " --output json)
4049if [ " ${existing_tunnels_json} " != " []" ]; then
@@ -43,10 +52,13 @@ if [ "${existing_tunnels_json}" != "[]" ]; then
4352else
4453 printInfo " No existing named tunnel found"
4554fi
55+
4656printInfo " Creating new named tunnel '${TUNNEL_NAME} '"
4757cloudflared --loglevel ' error' tunnel create " ${TUNNEL_NAME} " 1> /dev/null
58+
4859printInfo " Routing dns from '${TUNNEL_NAME} ' to tunnel"
4960cloudflared --loglevel ' error' tunnel route dns --overwrite-dns " ${TUNNEL_NAME} " " ${derived_tunnel_name} "
61+
5062unset TUNNEL_HOSTNAME
5163
5264exec " $@ "
0 commit comments