Files
dokku/plugins/domains/commands
Jose Diaz-Gonzalez 70511c340d Check if command is implemented in a plugin before executing plugin code
Because of how plugin commands are implemented, their output can be incredibly verbose. Rather than executing even the `set -eo pipefail` parts of a plugin, we immediately check if the command is implemented by a plugin. If it is not, then we continue on as normal.

One side-effect of this change is that plugin commands need to be duplicated again:

- once in the command array
- once for the actual body of the command
- once in the help output

This is also quite hackish, and probably not the best way to decrease trace output. Note that we drop approximately 2k lines worth of logs with this change.
2015-09-18 16:09:59 -04:00

136 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
[[ " domains domains:setup domains:add domains:clear domains:remove help domains:help " == *" $1 "* ]] || exit $DOKKU_NOT_IMPLEMENTED_EXIT
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/domains/functions"
case "$1" in
domains)
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$2"
APP="$2"
dokku domains:setup $APP
if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
dokku_log_info2_quiet "$APP Domain Names"
get_app_domains "$APP"
else
dokku_log_fail "No domain names set for $APP"
fi
;;
domains:setup)
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$2"
APP="$2"; VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
RE_IPV4="$(get_ipv4_regex)"; RE_IPV6="$(get_ipv6_regex)"
if [[ ! -f $VHOST_PATH ]]; then
if [[ -f "$DOKKU_ROOT/VHOST" ]]; then
VHOST=$(< "$DOKKU_ROOT/VHOST")
else
VHOST=$(< "$DOKKU_ROOT/HOSTNAME")
fi
if [[ "$VHOST" =~ $RE_IPV4 ]] || [[ "$VHOST" =~ $RE_IPV6 ]]; then
dokku_log_info2 "unsupported vhost config found. disabling vhost support"
config_set --no-restart $APP NO_VHOST=1
else
if [[ -f "$DOKKU_ROOT/VHOST" ]]; then
dokku_log_info1 "Creating new $VHOST_PATH..."
SUBDOMAIN=${APP/%\.${VHOST}/}
hostname=$(: | plugn trigger nginx-hostname $APP $SUBDOMAIN $VHOST)
if [[ ! -n $hostname ]]; then
if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then
hostname="${APP/\//-}"
else
hostname="${APP/\//-}.$VHOST"
fi
fi
echo "$hostname" > $VHOST_PATH
fi
fi
fi
;;
domains:add)
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$2"
APP="$2"
if [[ -z "${*:3}" ]]; then
echo "Usage: dokku $1 $APP DOMAIN [DOMAIN ...]"
echo "Must specify DOMAIN."
exit 1
fi
if [[ $(egrep ^"$3"$ "$DOKKU_ROOT/$APP/VHOST" > /dev/null 2>&1; echo $?) -eq 0 ]]; then
echo "$3 is already defined for $APP"
exit 1
fi
shift 2
dokku domains:setup $APP
for DOMAIN in "$@"; do
echo "$DOMAIN" >> "$DOKKU_ROOT/$APP/VHOST"
done
# we need to restart the app to make sure we're binding to the appropriate network interface
dokku nginx:build-config $APP
plugn trigger post-domains-update $APP
for DOMAIN in "$@"; do
dokku_log_info1 "Added $DOMAIN to $APP"
done
;;
domains:clear)
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$2"
APP="$2"
rm -f "$DOKKU_ROOT/$APP/VHOST"
dokku domains:setup $APP
plugn trigger post-domains-update $APP
dokku_log_info1 "Cleared domains in $APP"
;;
domains:remove)
[[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$2"
APP="$2"
if [[ -z "${*:3}" ]]; then
echo "Usage: dokku $1 $APP DOMAIN [DOMAIN ...]"
echo "Must specify DOMAIN."
exit 1
fi
shift 2
dokku domains:setup $APP
for DOMAIN in "$@"; do
sed -i "/^$DOMAIN$/d" "$DOKKU_ROOT/$APP/VHOST"
done
plugn trigger post-domains-update $APP
for DOMAIN in "$@"; do
dokku_log_info1 "Removed $DOMAIN from $APP"
done
;;
help | domains:help)
cat<<EOF
domains <app>, List custom domains for app
domains:add <app> DOMAIN, Add a custom domain to app
domains:clear <app>, Clear all custom domains for app
domains:remove <app> DOMAIN, Remove a custom domain from app
EOF
;;
*)
exit $DOKKU_NOT_IMPLEMENTED_EXIT
;;
esac