Skip to content

Commit c54f4ee

Browse files
committed
sigil fixes and post-deploy hook
Sigil should respect the IP address and enable loadbalancing when web scale > 1.
1 parent 8938e7c commit c54f4ee

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

functions

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33
[[ $DOKKU_TRACE ]] && set -x
4+
source "$PLUGIN_AVAILABLE_PATH/config/functions"
5+
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
46

57
nginx_stream_build_config() {
68
local APP="$1"
79
local STREAM_TEMPLATE_NAME="stream.conf.sigil"
810
local STREAM_TEMPLATE="$PLUGIN_AVAILABLE_PATH/nginx-stream/templates/$STREAM_TEMPLATE_NAME"
911
local SCHEME=tcp
10-
if [[ "$(plugn trigger proxy-is-enabled "$APP")" == "true" ]]; then
11-
local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP)
12-
13-
14-
local PORT_MAP PROXY_PORT_MAP
15-
for PORT_MAP in $DOKKU_PROXY_PORT_MAP; do
16-
local PROXY_SCHEME="$(awk -F ':' '{ print $1 }' <<<"$PORT_MAP")"
17-
if [[ "$PROXY_SCHEME" == "tcp" ]]; then
18-
local PROXY_PORT_MAP+="$PORT_MAP"
19-
fi
20-
done
21-
local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)"
22-
23-
local STREAM_BUILD_CONFIG_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
24-
local STREAM_CONF=$(mktemp --tmpdir="${STREAM_BUILD_CONFIG_TMP_WORK_DIR}" "stream.conf.XXXXXX")
25-
26-
local SIGIL_PARAMS=(-f "$STREAM_TEMPLATE" APP="$APP"
27-
DOKKU_APP_LISTEN_IP="$DOKKU_APP_LISTEN_IP"
28-
PROXY_PORT_MAP="$PROXY_PORT_MAP")
29-
echo "-----> Configuring NGINX Stream plugin"
30-
sigil "${SIGIL_PARAMS[@]}" | cat -s >"$STREAM_CONF"
31-
mv "$STREAM_CONF" "$DOKKU_ROOT/$APP/stream.conf"
32-
33-
else
12+
if [[ "$(plugn trigger proxy-is-enabled "$APP")" != "true" ]]; then
3413
dokku_log_fail "Enable proxy for this to work"
14+
exit 0
3515
fi
36-
}
16+
local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP)
3717

38-
Upstream ports....
18+
19+
local PORT_MAP PROXY_PORT_MAP
20+
for PORT_MAP in $DOKKU_PROXY_PORT_MAP; do
21+
local PROXY_SCHEME="$(awk -F ':' '{ print $1 }' <<<"$PORT_MAP")"
22+
if [[ "$PROXY_SCHEME" == "tcp" ]]; then
23+
local PROXY_PORT_MAP+="$PORT_MAP"
24+
fi
25+
done
26+
local PROXY_PORT_MAP="$(echo "$PROXY_PORT_MAP" | xargs)"
27+
28+
local PROXY_UPSTREAM_PORTS
29+
for PORT_MAP in $PROXY_PORT_MAP; do
30+
local PROXY_UPSTREAM_PORT="$(awk -F ':' '{ print $3 }' <<<"$PORT_MAP")"
31+
if [[ "$(is_val_in_list "$PROXY_UPSTREAM_PORT" "$PROXY_UPSTREAM_PORTS" " ")" == "false" ]]; then
32+
local PROXY_UPSTREAM_PORTS+="$PROXY_UPSTREAM_PORT "
33+
fi
34+
done
35+
local PROXY_UPSTREAM_PORTS="$(echo "$PROXY_UPSTREAM_PORTS" | xargs)"
36+
37+
local STREAM_BUILD_CONFIG_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
38+
local STREAM_CONF=$(mktemp --tmpdir="${STREAM_BUILD_CONFIG_TMP_WORK_DIR}" "stream.conf.XXXXXX")
39+
40+
local SIGIL_PARAMS=(-f "$STREAM_TEMPLATE" APP="$APP"
41+
DOKKU_APP_LISTEN_IP="$DOKKU_APP_LISTEN_IP"
42+
PROXY_PORT_MAP="$PROXY_PORT_MAP")
43+
44+
local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
45+
while read -r line || [[ -n "$line" ]]; do
46+
[[ "$line" =~ ^#.* ]] && continue
47+
line="$(strip_inline_comments "$line")"
48+
PROC_TYPE=${line%%=*}
49+
LISTENERS="$(plugn trigger network-get-listeners "$APP" "$PROC_TYPE" | xargs)"
50+
UPP_PROC_TYPE="${PROC_TYPE^^}"
51+
UPP_PROC_TYPE="${UPP_PROC_TYPE//-/_}"
52+
SIGIL_PARAMS+=( "DOKKU_APP_${UPP_PROC_TYPE}_LISTENERS=$LISTENERS" )
53+
done <"$DOKKU_SCALE_FILE"
54+
55+
echo "-----> Configuring NGINX Stream plugin"
56+
sigil "${SIGIL_PARAMS[@]}" | cat -s >"$STREAM_CONF"
57+
mv "$STREAM_CONF" "$DOKKU_ROOT/$APP/stream.conf"
58+
validate_nginx && restart_nginx >/dev/null
59+
}

post-deploy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33
[[ $DOKKU_TRACE ]] && set -x
4+
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
45
source "$PLUGIN_AVAILABLE_PATH/nginx-stream/functions"
56

67
trigger-nginx-stream-post-deploy() {
78
declare desc="nginx-stream post-deploy plugin trigger"
9+
declare trigger="nginx_stream_post_deploy"
810
declare APP="$1"
9-
10-
nginx_stream_build_config "$APP"
11+
if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then
12+
nginx_stream_build_config "$APP"
13+
fi
1114
}
1215

1316
trigger-nginx-stream-post-deploy "$@"

post-proxy-ports-update

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
[[ $DOKKU_TRACE ]] && set -x
4+
source "$PLUGIN_AVAILABLE_PATH/nginx-stream/functions"
5+
6+
trigger-nginx-stream-post-deploy() {
7+
declare desc="nginx-stream post-deploy plugin trigger"
8+
declare trigger="nginx_stream_core_post_deploy"
9+
declare APP="$1"
10+
11+
if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then
12+
nginx_stream_build_config "$APP"
13+
fi
14+
}
15+
16+
trigger-nginx-stream-post-deploy "$@"

templates/stream.conf.sigil

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
{{ $listen_port := index $port_map_list 1 }}
55
{{ $upstream_port := index $port_map_list 2 }}
66

7-
upstream {{$.APP}}-{{ $upstream_port }} {
8-
server {{ $.DOKKU_APP_LISTEN_IP }}:{{ $upstream_port }};
9-
}
10-
117
server {
128
listen {{ $listen_port }};
139
proxy_pass {{$.APP}}-{{ $upstream_port }};
1410
}
1511

16-
{{ end }}
12+
{{ end }}
13+
14+
15+
{{ if $.DOKKU_APP_WEB_LISTENERS }}
16+
{{ range $upstream_port := $.PROXY_UPSTREAM_PORTS | split " " }}
17+
upstream {{ $.APP }}-{{ $upstream_port }} {
18+
{{ range $listeners := $.DOKKU_APP_WEB_LISTENERS | split " " }}
19+
{{ $listener_list := $listeners | split ":" }}
20+
{{ $listener_ip := index $listener_list 0 }}
21+
server {{ $listener_ip }}:{{ $upstream_port }};{{ end }}
22+
}
23+
{{ end }}{{ end }}

0 commit comments

Comments
 (0)