Files
dokku/plugins/domains/commands

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/domains/functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/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"
disable_app_vhost $APP --no-restart $APP
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
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