Files
dokku/plugins/domains/functions
2019-05-20 21:03:55 -07:00

322 lines
8.4 KiB
Bash
Executable File

#!/usr/bin/env bash
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
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"
fi
[[ "$2" == "--no-restart" ]] && local CONFIG_SET_ARGS=$2
# shellcheck disable=SC2086
DOKKU_QUIET_OUTPUT=1 config_set $CONFIG_SET_ARGS $APP NO_VHOST=1
}
domains_setup() {
declare desc="setup domains to default state"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
local RE_IPV4="$(get_ipv4_regex)"
local RE_IPV6="$(get_ipv6_regex)"
local DEFAULT_VHOSTS="$(get_default_vhosts "$APP")"
if [[ ! -f $APP_VHOST_PATH ]]; then
if [[ -n "$DEFAULT_VHOSTS" ]]; then
dokku_log_info1 "Creating new $APP_VHOST_PATH..."
echo "$DEFAULT_VHOSTS" >"$APP_VHOST_PATH"
else
dokku_log_info2 "no global VHOST set. disabling vhost support"
disable_app_vhost "$APP" --no-restart
fi
fi
}
domains_add() {
declare desc="add list of domains to app"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
shift 1
for DOMAIN in "$@"; do
if ! (is_valid_hostname "$DOMAIN"); then
dokku_log_fail "$DOMAIN is invalid. exiting..."
fi
done
for DOMAIN in "$@"; do
if [[ $(
egrep -w "^$DOMAIN$" "$APP_VHOST_PATH" >/dev/null 2>&1
echo $?
) -eq 0 ]]; then
dokku_log_info1 "Skipping: $DOMAIN already added to $APP"
else
echo "$DOMAIN" >>"$APP_VHOST_PATH"
dokku_log_info1 "Added $DOMAIN to $APP"
fi
done
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
domains_enable "$APP" --no-restart
fi
plugn trigger post-domains-update "$APP" "add" "$@"
}
domains_remove() {
declare desc="remove list of app domains"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
shift 1
touch "$APP_VHOST_PATH"
for DOMAIN in "$@"; do
sed -i "\|^$DOMAIN\$|d" "$APP_VHOST_PATH"
dokku_log_info1 "Removed $DOMAIN from $APP"
done
plugn trigger post-domains-update "$APP" "remove" "$@"
}
domains_set() {
declare desc="set list of domains for app"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
shift 1
touch "$APP_VHOST_PATH"
for DOMAIN in "$@"; do
if ! (is_valid_hostname "$DOMAIN"); then
dokku_log_fail "$DOMAIN is invalid. exiting..."
fi
done
echo "$*" | tr " " "\n" >"$APP_VHOST_PATH"
dokku_log_info1 "Set $* for $APP"
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
domains_enable "$APP" --no-restart
fi
plugn trigger post-domains-update "$APP" "set" "$@"
}
domains_disable() {
declare desc="disable domains/VHOST support"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
touch "$APP_VHOST_PATH"
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)"
fi
}
domains_enable() {
declare desc="enable domains/VHOST support"
verify_app_name "$1"
local APP="$1"
local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
local DEFAULT_VHOSTS="$(get_default_vhosts "$APP")"
touch "$APP_VHOST_PATH"
if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]]; then
if [[ -n "$DEFAULT_VHOSTS" ]]; then
echo "$DEFAULT_VHOSTS" >"$APP_VHOST_PATH"
fi
[[ "$2" == "--no-restart" ]] && local ENABLE_APP_VHOST_ARGS=$2
enable_app_vhost "$APP" "$ENABLE_APP_VHOST_ARGS"
else
dokku_log_info1 "domains (VHOST) support is already enabled for app ($APP)"
fi
}
domains_add_global() {
declare desc="add list of global domains"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
touch "$GLOBAL_VHOST_PATH"
for DOMAIN in "$@"; do
if ! (is_valid_hostname "$DOMAIN"); then
dokku_log_fail "$DOMAIN is invalid. exiting..."
fi
done
for DOMAIN in "$@"; do
if [[ $(
egrep -w "^$DOMAIN$" "$GLOBAL_VHOST_PATH" >/dev/null 2>&1
echo $?
) -eq 0 ]]; then
dokku_log_info1 "Skipping: $DOMAIN already added"
else
sed -i -e "\$a\\$DOMAIN" "$GLOBAL_VHOST_PATH"
dokku_log_info1 "Added $DOMAIN"
fi
done
}
domains_clear_global() {
declare desc="remove all domains"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
rm "$GLOBAL_VHOST_PATH"
touch "$GLOBAL_VHOST_PATH"
}
domains_remove_global() {
declare desc="remove list of domains"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
touch "$GLOBAL_VHOST_PATH"
for DOMAIN in "$@"; do
sed -i "\|^$DOMAIN\$|d" "$GLOBAL_VHOST_PATH"
dokku_log_info1 "Removed $DOMAIN"
done
}
domains_set_global() {
declare desc="set list of global domains"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
touch "$GLOBAL_VHOST_PATH"
for DOMAIN in "$@"; do
if ! (is_valid_hostname "$DOMAIN"); then
dokku_log_fail "$DOMAIN is invalid. exiting..."
fi
done
echo "$*" | tr " " "\n" >"$GLOBAL_VHOST_PATH"
dokku_log_info1 "Set $*"
}
enable_app_vhost() {
declare desc="enable vhost support for given application"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
local APP=$1
verify_app_name "$APP"
plugn trigger pre-enable-vhost "$APP"
[[ "$2" == "--no-restart" ]] && local CONFIG_SET_ARGS=$2
# shellcheck disable=SC2086
DOKKU_QUIET_OUTPUT=1 config_set $CONFIG_SET_ARGS "$APP" NO_VHOST=0
}
get_app_domains() {
declare desc="return app domains"
verify_app_name "$1"
local APP=$1
local APP_VHOST_FILE="$DOKKU_ROOT/$APP/VHOST"
local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
local GLOBAL_HOSTNAME_PATH="$DOKKU_ROOT/HOSTNAME"
if [[ "$(is_app_vhost_enabled "$APP")" == "true" ]]; then
if [[ -f "$APP_VHOST_FILE" ]]; then
cat "$APP_VHOST_FILE"
elif [[ -f "$GLOBAL_VHOST_PATH" ]]; then
cat "$GLOBAL_VHOST_PATH"
elif [[ -f "$GLOBAL_HOSTNAME_PATH" ]]; then
cat "$GLOBAL_HOSTNAME_PATH"
fi
fi
}
get_default_vhosts() {
declare desc="return default vhosts"
verify_app_name "$1"
local APP="$1"
local RE_IPV4="$(get_ipv4_regex)"
local RE_IPV6="$(get_ipv6_regex)"
if [[ "$(is_global_vhost_enabled)" == "true" ]]; then
local VHOSTS=$(get_global_vhosts)
while read -r VHOST; do
if ! ([[ "$VHOST" =~ $RE_IPV4 ]] || [[ "$VHOST" =~ $RE_IPV6 ]]); then
local SUBDOMAIN=${APP/%\.${VHOST}/}
local hostname=$(: | plugn trigger nginx-hostname "$APP" "$SUBDOMAIN" "$VHOST")
if [[ -z $hostname ]]; then
if [[ "$APP" == *.* ]]; then
local hostname="${APP/\//-}"
else
local hostname="${APP/\//-}.$VHOST"
fi
fi
echo "$hostname"
fi
done <<<"$VHOSTS"
fi
}
get_global_vhosts() {
declare desc="return global vhosts"
local GLOBAL_VHOST_FILE="$DOKKU_ROOT/VHOST"
[[ -f "$GLOBAL_VHOST_FILE" ]] && local GLOBAL_VHOSTS=$(<"$GLOBAL_VHOST_FILE")
echo "$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"
local NO_VHOST=$(config_get "$APP" NO_VHOST)
local APP_VHOST_ENABLED=true
if [[ "$NO_VHOST" == "1" ]]; then
local APP_VHOST_ENABLED=false
fi
echo $APP_VHOST_ENABLED
}
is_global_vhost_enabled() {
declare desc="returns true if we have a valid global vhost set; otherwise returns false"
local GLOBAL_VHOSTS=$(get_global_vhosts)
local GLOBAL_VHOSTS_ENABLED=false
local RE_IPV4="$(get_ipv4_regex)"
local RE_IPV6="$(get_ipv6_regex)"
while read -r GLOBAL_VHOST; do
if (is_valid_hostname "$GLOBAL_VHOST"); then
local GLOBAL_VHOSTS_ENABLED=true
break
fi
done <<<"$GLOBAL_VHOSTS"
echo $GLOBAL_VHOSTS_ENABLED
}
is_valid_hostname() {
declare desc="return 0 if argument is a valid hostname; else return 1"
local hostname_string="${1,,}"
local hostname_regex='^([a-z0-9\.\*-]+\.)*[a-z0-9\*-]+$'
if [[ $hostname_string =~ $hostname_regex ]]; then
return 0
else
return 1
fi
}