refactor: simplify how url generation works

The URLS file is no longer necessary as urls are now generated directly from the app VHOST file.

This also moves all the url generation logic to the domains plugin.
This commit is contained in:
Jose Diaz-Gonzalez
2022-12-02 02:19:59 -05:00
parent f4f0a29446
commit edd6e476b3
23 changed files with 247 additions and 179 deletions

35
plugins/domains/domains-urls Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-urls() {
declare desc="gets an app's urls"
declare trigger="domains-urls"
declare APP="$1" URL_TYPE="$2"
local urls
local SCHEME="http"
local DEFAULT_LISTEN_PORT="80"
if [[ "$(plugn trigger certs-exists "$APP")" == "true" ]]; then
SCHEME="https"
DEFAULT_LISTEN_PORT="443"
fi
urls=$(plugn trigger app-urls "$APP" "$URL_TYPE")
if [[ -n "$urls" ]]; then
if [[ "$URL_TYPE" == "url" ]]; then
echo "$urls" | tr ' ' '\n' | grep "$SCHEME://" | head -n1
else
echo "$urls" | tr ' ' '\n' | sort
fi
else
if [[ "$URL_TYPE" == "url" ]]; then
fn-domains-generate-urls "$APP" "$SCHEME" "$DEFAULT_LISTEN_PORT" | tr ' ' '\n' | grep "$SCHEME://" | head -n1
else
fn-domains-generate-urls "$APP" "$SCHEME" "$DEFAULT_LISTEN_PORT" | tr ' ' '\n' | sort
fi
fi
}
trigger-domains-domains-urls "$@"

View File

@@ -8,12 +8,11 @@ disable_app_vhost() {
declare desc="disable vhost support for given application"
declare APP=$1 RESTART_APP="$2"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
local APP_URLS_PATH="$DOKKU_ROOT/$APP/URLS"
plugn trigger pre-disable-vhost "$APP"
if [[ -f "$APP_VHOST_PATH" ]] || [[ -f "$APP_URLS_PATH" ]]; then
if [[ -f "$APP_VHOST_PATH" ]]; then
dokku_log_info1 "App virtual host support disabled"
rm -f "$APP_VHOST_PATH" "$APP_URLS_PATH"
rm -f "$APP_VHOST_PATH"
fi
[[ "$RESTART_APP" == "--no-restart" ]] && local CONFIG_SET_ARGS=$RESTART_APP

View File

@@ -10,6 +10,7 @@ trigger-domains-install() {
shopt -s nullglob
for app in $(dokku_apps "false" 2>/dev/null); do
domains_setup "$app"
rm -f "$DOKKU_ROOT/$APP/URLS"
done
}

View File

@@ -114,3 +114,70 @@ fn-domains-global-vhosts() {
get_global_vhosts | tr '\n' ' '
fi
}
fn-domains-generate-urls() {
declare APP="$1" SCHEME="$2" DEFAULT_LISTEN_PORT="$3"
local app_vhosts="$(plugn trigger domains-list "$APP")"
if [[ -n "$app_vhosts" ]]; then
for app_vhost in $app_vhosts; do
fn-domains-generate-urls-from-config "$APP" "$SCHEME" "$app_vhost" "$DEFAULT_LISTEN_PORT"
done
else
if [[ -s "$DOKKU_ROOT/VHOST" ]]; then
while read -r VHOST || [[ -n "$VHOST" ]]; do
fn-domains-generate-urls-from-config "$APP" "$SCHEME" "$VHOST" "$DEFAULT_LISTEN_PORT"
done <"$DOKKU_ROOT/VHOST"
else
fn-domains-generate-urls-from-config "$APP" "$SCHEME" "$(hostname -f)" "$DEFAULT_LISTEN_PORT"
fi
fi
}
fn-domains-generate-urls-from-config() {
declare APP="$1" SCHEME="$2" VHOST="$3" DEFAULT_LISTEN_PORT="$4"
local DOKKU_PROXY_PORT_MAP=$(plugn trigger config-get "$APP" DOKKU_PROXY_PORT_MAP || true)
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")"
if [[ "$(plugn trigger proxy-is-enabled "$APP")" == "false" ]]; then
if [[ -n "$RAW_TCP_PORTS" ]]; then
local APP_CONTAINER_PORTS="$(get_container_ports "$APP")"
local app_port
for listen_port in $APP_CONTAINER_PORTS; do
fn-domains-generate-url "$SCHEME" "$VHOST" "$listen_port"
done
else
local DOKKU_APP_WEB_LISTENERS PORT
DOKKU_APP_WEB_LISTENERS="$(plugn trigger network-get-listeners "$APP" "web" | xargs)"
for DOKKU_APP_WEB_LISTENER in $DOKKU_APP_WEB_LISTENERS; do
listen_port="$(echo "$DOKKU_APP_WEB_LISTENER" | cut -d ':' -f2)"
fn-domains-generate-url "$SCHEME" "$VHOST" "$listen_port"
done
shopt -u nullglob
fi
elif [[ -n "$DOKKU_PROXY_PORT_MAP" ]]; then
local port_map
for port_map in $DOKKU_PROXY_PORT_MAP; do
local scheme="$(awk -F ':' '{ print $1 }' <<<"$port_map")"
local listen_port="$(awk -F ':' '{ print $2 }' <<<"$port_map")"
fn-domains-generate-url "$SCHEME" "$VHOST" "$listen_port"
done
elif [[ -n "$RAW_TCP_PORTS" ]]; then
for listen_port in $RAW_TCP_PORTS; do
fn-domains-generate-url "$SCHEME" "$VHOST" "$listen_port"
done
else
fn-domains-generate-url "$SCHEME" "$VHOST" "$DEFAULT_LISTEN_PORT"
fi
}
fn-domains-generate-url() {
declare SCHEME="$1" VHOST="$2" PORT="$3"
if [[ "$PORT" == "80" ]]; then
echo "http://$VHOST"
elif [[ "$PORT" == "443" ]]; then
echo "https://$VHOST"
else
echo "$SCHEME://$VHOST:$PORT"
fi
}

View File

@@ -8,7 +8,7 @@ trigger-domains-post-app-clone-setup() {
declare OLD_APP="$1" NEW_APP="$2"
local APP_ROOT="$DOKKU_ROOT/$NEW_APP"
rm -rf "$APP_ROOT/URLS" "$APP_ROOT/VHOST"
rm -rf "$APP_ROOT/VHOST"
}
trigger-domains-post-app-clone-setup "$@"

View File

@@ -3,12 +3,10 @@ set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-post-app-rename-setup() {
declare desc="updates VHOST and URLS files during rename"
declare desc="updates VHOST files during rename"
declare trigger="post-app-rename-setup"
declare OLD_APP="$1" NEW_APP="$2"
rm -f "$DOKKU_ROOT/$NEW_APP/URLS"
if [[ -f "$DOKKU_ROOT/$NEW_APP/VHOST" ]]; then
while read -r VHOST || [[ -n "$VHOST" ]]; do
sed -i -e "s/$OLD_APP.$VHOST/$NEW_APP.$VHOST/g" "$DOKKU_ROOT/$NEW_APP/VHOST"