forked from rrrnld/dokku-nginx-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
executable file
·83 lines (71 loc) · 3.24 KB
/
functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
nginx_stream_build_config() {
local APP="$1"
local STREAM_TEMPLATE_NAME="stream.conf.sigil"
local STREAM_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-stream/templates/$STREAM_TEMPLATE_NAME"
local SCHEME=tcp
if [[ "$(plugn trigger proxy-is-enabled "$APP")" != "true" ]]; then
dokku_log_fail "Enable proxy for this to work"
exit 0
fi
local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP)
local PORT_MAP TCP_PROXY_PORT_MAP UDP_PROXY_PORT_MAP
for PORT_MAP in $DOKKU_PROXY_PORT_MAP; do
local PROXY_SCHEME="$(awk -F ':' '{ print $1 }' <<<"$PORT_MAP")"
if [[ "$PROXY_SCHEME" == "tcp" ]]; then
local TCP_PROXY_PORT_MAP+="$PORT_MAP "
fi
if [[ "$PROXY_SCHEME" == "udp" ]]; then
local UDP_PROXY_PORT_MAP+="$PORT_MAP "
fi
done
local TCP_PROXY_PORT_MAP="$(echo "$TCP_PROXY_PORT_MAP" | xargs)"
local UDP_PROXY_PORT_MAP="$(echo "$UDP_PROXY_PORT_MAP" | xargs)"
if [[ -z "$TCP_PROXY_PORT_MAP" ]] && [[ -z "$UDP_PROXY_PORT_MAP" ]]; then
echo "No stream (tcp/udp) ports specified."
return 0
fi
dokku_log_info1 "Configuring NGINX Stream plugin"
local TCP_PROXY_UPSTREAM_PORTS
for PORT_MAP in $TCP_PROXY_PORT_MAP; do
local PROXY_UPSTREAM_PORT="$(awk -F ':' '{ print $3 }' <<<"$PORT_MAP")"
if [[ "$(is_val_in_list "$PROXY_UPSTREAM_PORT" "$TCP_PROXY_UPSTREAM_PORTS" " ")" == "false" ]]; then
local TCP_PROXY_UPSTREAM_PORTS+="$PROXY_UPSTREAM_PORT "
fi
done
local UDP_PROXY_UPSTREAM_PORTS
for PORT_MAP in $UDP_PROXY_PORT_MAP; do
local PROXY_UPSTREAM_PORT="$(awk -F ':' '{ print $3 }' <<<"$PORT_MAP")"
if [[ "$(is_val_in_list "$PROXY_UPSTREAM_PORT" "$UDP_PROXY_UPSTREAM_PORTS" " ")" == "false" ]]; then
local UDP_PROXY_UPSTREAM_PORTS+="$PROXY_UPSTREAM_PORT "
fi
done
TCP_PROXY_UPSTREAM_PORTS="$(echo "$TCP_PROXY_UPSTREAM_PORTS" | xargs)"
UDP_PROXY_UPSTREAM_PORTS="$(echo "$UDP_PROXY_UPSTREAM_PORTS" | xargs)"
local STREAM_BUILD_CONFIG_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
local STREAM_CONF=$(mktemp --tmpdir="${STREAM_BUILD_CONFIG_TMP_WORK_DIR}" "stream.conf.XXXXXX")
local SIGIL_PARAMS=(-f "$STREAM_TEMPLATE" APP="$APP"
DOKKU_APP_LISTEN_IP="$DOKKU_APP_LISTEN_IP"
TCP_PROXY_PORT_MAP="$TCP_PROXY_PORT_MAP"
TCP_PROXY_UPSTREAM_PORTS="$TCP_PROXY_UPSTREAM_PORTS"
UDP_PROXY_PORT_MAP="$UDP_PROXY_PORT_MAP"
UDP_PROXY_UPSTREAM_PORTS="$UDP_PROXY_UPSTREAM_PORTS")
while read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.* ]] && continue
line="$(strip_inline_comments "$line")"
PROC_TYPE=${line%%=*}
[[ "$PROC_TYPE" == "web" ]] || continue
LISTENERS="$(plugn trigger network-get-listeners "$APP" "$PROC_TYPE" | xargs)"
UPP_PROC_TYPE="${PROC_TYPE^^}"
UPP_PROC_TYPE="${UPP_PROC_TYPE//-/_}"
SIGIL_PARAMS+=( "DOKKU_APP_${UPP_PROC_TYPE}_LISTENERS=$LISTENERS" )
done < <(plugn trigger ps-current-scale "$APP")
sigil "${SIGIL_PARAMS[@]}" | cat -s >"$STREAM_CONF"
sudo /bin/mv "$STREAM_CONF" "/etc/nginx/app-stream/$APP.conf"
validate_nginx && restart_nginx >/dev/null
}