refactor: move domain manipulation into triggers

This allows other plugins to avoid understanding the underlying details of how domains might be integrated with.
This commit is contained in:
Jose Diaz-Gonzalez
2020-05-06 00:16:51 -04:00
parent 5458d5b341
commit 61a306664f
22 changed files with 226 additions and 49 deletions

View File

@@ -531,6 +531,96 @@ verify_app_name "$APP"
# TODO
```
### `domains-add`
- Description: Adds a domain to an app
- Invoked by:
- Arguments: `$APP` `$DOMAIN`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `domains-disable`
- Description: Disables domains for an app
- Invoked by:
- Arguments: `$APP` `$RESTART_APP`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `domains-enable`
- Description: Enables domains for an app
- Invoked by:
- Arguments: `$APP` `$RESTART_APP`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `domains-list`
- Description: Lists all domains for an app
- Invoked by:
- Arguments: `$APP`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `domains-setup`
- Description: Initializes domains for an app if enabled
- Invoked by:
- Arguments: `$APP`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `domains-vhost-enabled`
- Description: Checks if a virtual hosts are enabled for an app
- Invoked by:
- Arguments: `$APP`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `git-deploy-branch`
- Description: Outputs the deploy branch for an app, inherited or not

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -0,0 +1 @@
hook

View File

@@ -2,7 +2,6 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
cmd-certs-generate() {
declare desc="generates a self-signed SSL certificate/key combo"
@@ -29,7 +28,7 @@ cmd-certs-generate() {
chmod 750 "$APP_SSL_PATH"
chmod 640 "$APP_SSL_PATH/server.crt" "$APP_SSL_PATH/server.csr" "$APP_SSL_PATH/server.key"
plugn trigger post-certs-update "$APP"
[[ -n "$DOMAIN" ]] && (domains_add "$APP" "$DOMAIN" || plugn trigger post-domains-update "$APP")
[[ -n "$DOMAIN" ]] && (plugn trigger domains-add "$APP" "$DOMAIN" || plugn trigger post-domains-update "$APP")
dokku_log_info1 "The following is a certificate signing request that can be used"
dokku_log_info1 "to generate an 'officially' signed SSL certificate for $APP at $DOMAIN"
dokku_log_info1 "by a CA of your choosing."

View File

@@ -849,8 +849,7 @@ internal_get_app_urls() {
declare desc="print an app's available urls"
source "$PLUGIN_AVAILABLE_PATH/certs/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
local APP="$2"
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")"
local URLS_FILE="$DOKKU_ROOT/$APP/URLS"
@@ -860,7 +859,7 @@ internal_get_app_urls() {
local app_urls="$(grep -v -E "^#" "$URLS_FILE")"
if [[ -n "$DOKKU_PROXY_PORT_MAP" ]]; then
local port_map app_vhost
local app_vhosts=$(get_app_domains "$APP")
local app_vhosts=$(plugn trigger domains-list "$APP")
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")"

14
plugins/domains/domains-add Executable file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-add() {
declare desc="adds a domain to an app"
declare trigger="domains-add"
declare APP="$1" DOMAIN="$2"
domains_add "$APP" "$DOMAIN"
}
trigger-domains-domains-add "$@"

18
plugins/domains/domains-disable Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-disable() {
declare desc="disables domains for an app"
declare trigger="domains-disable"
declare APP="$1" RESTART_APP="$2"
if [[ "$RESTART_APP" == "true" ]]; then
disable_app_vhost "$APP"
else
disable_app_vhost "$APP" --no-restart
fi
}
trigger-domains-domains-disable "$@"

18
plugins/domains/domains-enable Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-enable() {
declare desc="enables domains for an app"
declare trigger="domains-enable"
declare APP="$1" RESTART_APP="$2"
if [[ "$RESTART_APP" == "true" ]]; then
enable_app_vhost "$APP"
else
enable_app_vhost "$APP" --no-restart
fi
}
trigger-domains-domains-enable "$@"

14
plugins/domains/domains-list Executable file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-list() {
declare desc="returns all domains for an app"
declare trigger="domains-list"
declare APP="$1"
get_app_domains "$APP"
}
trigger-domains-domains-list "$@"

19
plugins/domains/domains-setup Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-setup() {
declare desc="creates vhost files as necessary"
declare trigger="domains-setup"
declare APP="$1"
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
dokku_log_info1 "App virtual host support disabled, skipping domains setup"
return
fi
domains_setup "$APP"
}
trigger-domains-domains-setup "$@"

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-domains-domains-vhost-enabled() {
declare desc="returns whether vhosts are enabled or not"
declare trigger="domains-vhost-enabled"
declare APP="$1"
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
return 1
fi
}
trigger-domains-domains-vhost-enabled "$@"

View File

@@ -1,28 +1,23 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
disable_app_vhost() {
declare desc="disable vhost support for given application"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
local APP=$1
declare APP=$1 RESTART_APP="$2"
verify_app_name "$APP"
local APP_VHOST_FILE="$DOKKU_ROOT/$APP/VHOST"
local APP_URLS_FILE="$DOKKU_ROOT/$APP/URLS"
plugn trigger pre-disable-vhost "$APP"
if [[ -f "$APP_VHOST_FILE" ]]; then
dokku_log_info1 "VHOST support disabled, deleting $APP/VHOST"
rm "$APP_VHOST_FILE"
fi
if [[ -f "$APP_URLS_FILE" ]]; then
dokku_log_info1 "VHOST support disabled, deleting $APP/URLS"
rm "$APP_URLS_FILE"
if [[ -f "$APP_VHOST_FILE" ]] || [[ -f "$APP_URLS_FILE" ]]; then
dokku_log_info1 "App virtual host support disabled"
rm -f "$APP_VHOST_FILE" "$APP_URLS_FILE"
fi
[[ "$2" == "--no-restart" ]] && local CONFIG_SET_ARGS=$2
[[ "$RESTART_APP" == "--no-restart" ]] && local CONFIG_SET_ARGS=$RESTART_APP
# shellcheck disable=SC2086
DOKKU_QUIET_OUTPUT=1 config_set $CONFIG_SET_ARGS $APP NO_VHOST=1
}
@@ -39,10 +34,10 @@ domains_setup() {
if [[ ! -f $APP_VHOST_PATH ]]; then
if [[ -n "$DEFAULT_VHOSTS" ]]; then
dokku_log_info1 "Creating new $APP_VHOST_PATH..."
dokku_log_info1 "Creating new app virtual host file..."
echo "$DEFAULT_VHOSTS" >"$APP_VHOST_PATH"
else
dokku_log_info2 "no global VHOST set. disabling vhost support"
dokku_log_info2 "Global server virtual host not set, disabling app vhost..."
disable_app_vhost "$APP" --no-restart
fi
fi
@@ -129,14 +124,14 @@ domains_disable() {
if [[ "$(is_app_vhost_enabled "$APP")" == "true" ]]; then
disable_app_vhost "$APP"
else
dokku_log_info1 "domains (VHOST) support is already disabled for app ($APP)"
dokku_log_info1 "Domains (VHOST) support is already disabled for app ($APP)"
fi
}
domains_enable() {
declare desc="enable domains/VHOST support"
verify_app_name "$1"
local APP="$1"
local APP="$1" RESTART_APP="$2"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
local DEFAULT_VHOSTS="$(get_default_vhosts "$APP")"
@@ -145,10 +140,10 @@ domains_enable() {
if [[ -n "$DEFAULT_VHOSTS" ]]; then
echo "$DEFAULT_VHOSTS" >"$APP_VHOST_PATH"
fi
[[ "$2" == "--no-restart" ]] && local ENABLE_APP_VHOST_ARGS=$2
[[ "$RESTART_APP" == "--no-restart" ]] && local ENABLE_APP_VHOST_ARGS=$RESTART_APP
enable_app_vhost "$APP" "$ENABLE_APP_VHOST_ARGS"
else
dokku_log_info1 "domains (VHOST) support is already enabled for app ($APP)"
dokku_log_info1 "Domains (VHOST) support is already enabled for app ($APP)"
fi
}
@@ -212,13 +207,11 @@ domains_set_global() {
enable_app_vhost() {
declare desc="enable vhost support for given application"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
local APP=$1
declare APP=$1 RESTART_APP="$2"
verify_app_name "$APP"
plugn trigger pre-enable-vhost "$APP"
[[ "$2" == "--no-restart" ]] && local CONFIG_SET_ARGS=$2
[[ "$RESTART_APP" == "--no-restart" ]] && local CONFIG_SET_ARGS=$RESTART_APP
# shellcheck disable=SC2086
DOKKU_QUIET_OUTPUT=1 config_set $CONFIG_SET_ARGS "$APP" NO_VHOST=0
}
@@ -278,7 +271,6 @@ get_global_vhosts() {
is_app_vhost_enabled() {
declare desc="returns true or false if vhost support is enabled for a given application"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
local APP=$1
verify_app_name "$APP"

View File

@@ -2,7 +2,6 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
trigger-nginx-vhosts-core-post-deploy() {
@@ -17,12 +16,7 @@ trigger-nginx-vhosts-core-post-deploy() {
HAS_NETWORK_CONFIG="$(plugn trigger network-config-exists "$APP")"
if [[ "$HAS_NETWORK_CONFIG" == "true" ]]; then
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
dokku_log_info1 "VHOST support disabled. Skipping domains setup"
elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
domains_setup "$APP"
fi
plugn trigger domains-setup "$APP"
nginx_build_config "$APP"
fi
}

View File

@@ -4,7 +4,6 @@ set -eo pipefail
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/certs/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/internal-functions"
@@ -112,8 +111,9 @@ configure_nginx_ports() {
local DOKKU_PROXY_PORT=$(config_get "$APP" DOKKU_PROXY_PORT)
local DOKKU_PROXY_SSL_PORT=$(config_get "$APP" DOKKU_PROXY_SSL_PORT)
local DOKKU_PROXY_PORT_MAP=$(config_get "$APP" DOKKU_PROXY_PORT_MAP)
local IS_APP_VHOST_ENABLED="$(is_app_vhost_enabled "$APP")"
local IS_APP_VHOST_ENABLED=true
local UPSTREAM_PORT="5000"
plugn trigger domains-vhost-enabled "$APP" || IS_APP_VHOST_ENABLED=false
if [[ -z "$DOKKU_PROXY_PORT" ]] && [[ -z "$RAW_TCP_PORTS" ]]; then
if [[ "$IS_APP_VHOST_ENABLED" == "false" ]]; then
@@ -164,6 +164,7 @@ validate_ssl_domains() {
declare desc="check configured domains against SSL cert contents and show warning if mismatched"
local APP=$1
verify_app_name "$APP"
local VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
local SSL_HOSTNAME=$(get_ssl_hostnames "$APP")
local SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | xargs | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g' | sed 's/ /|/g')
@@ -317,7 +318,8 @@ nginx_build_config() {
local RAW_TCP_PORTS="$(get_app_raw_tcp_ports "$APP")"
local DOKKU_APP_LISTENERS
local IS_APP_VHOST_ENABLED=$(is_app_vhost_enabled "$APP")
local IS_APP_VHOST_ENABLED=true
plugn trigger domains-vhost-enabled "$APP" || IS_APP_VHOST_ENABLED=false
if [[ "$(plugn trigger proxy-is-enabled "$APP")" == "true" ]]; then
if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -z "$DOKKU_APP_LISTEN_IP" ]]; then
@@ -342,7 +344,7 @@ nginx_build_config() {
local PROXY_UPSTREAM_PORTS="$(echo "$PROXY_UPSTREAM_PORTS" | xargs)"
local SSL_INUSE=
local NONSSL_VHOSTS=$(get_app_domains "$APP")
local NONSSL_VHOSTS=$(plugn trigger domains-list "$APP")
local NOSSL_SERVER_NAME=$(echo "$NONSSL_VHOSTS" | xargs)
if is_ssl_enabled "$APP"; then
local SSL_INUSE=true

View File

@@ -3,6 +3,7 @@ set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
trigger-nginx-vhosts-post-domains-update() {
declare desc="calls nginx build_config when domains are updated"
declare trigger="post-domains-update"

View File

@@ -2,8 +2,6 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
trigger-nginx-vhosts-proxy-disable() {
declare desc="disable nginx proxy"
@@ -12,8 +10,8 @@ trigger-nginx-vhosts-proxy-disable() {
verify_app_name "$APP"
if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then
disable_app_vhost "$APP" --no-restart
ps_restart "$APP"
plugn trigger domains-disable "$APP" false
plugn trigger app-restart "$APP"
fi
}

View File

@@ -2,8 +2,6 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
trigger-nginx-vhosts-proxy-enable() {
declare desc="enable nginx proxy"
@@ -12,8 +10,8 @@ trigger-nginx-vhosts-proxy-enable() {
verify_app_name "$APP"
if [[ "$(plugn trigger proxy-type "$APP")" == "nginx" ]]; then
enable_app_vhost "$APP" --no-restart
ps_restart "$APP"
plugn trigger domains-enable "$APP" "false"
plugn trigger app-restart "$APP"
fi
}

View File

@@ -2,7 +2,6 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
cmd-ps-restart() {
declare desc="restarts app via command line"
@@ -11,7 +10,7 @@ cmd-ps-restart() {
declare APP="$1"
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
ps_restart "$APP"
plugn trigger app-restart "$APP"
}
cmd-ps-restart "$@"