124 lines
4.1 KiB
Bash
Executable File
124 lines
4.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
|
|
|
restart_nginx () {
|
|
case "$DOKKU_DISTRO" in
|
|
ubuntu)
|
|
sudo /etc/init.d/nginx reload > /dev/null
|
|
;;
|
|
|
|
opensuse)
|
|
sudo /sbin/service nginx reload > /dev/null
|
|
;;
|
|
esac
|
|
}
|
|
|
|
case "$1" in
|
|
nginx:build-config)
|
|
APP="$2"; PORT="$3"
|
|
[[ -z "$PORT" ]] && PORT=$(< "$DOKKU_ROOT/$APP/PORT")
|
|
VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
|
|
WILDCARD_SSL="$DOKKU_ROOT/tls"
|
|
SSL="$DOKKU_ROOT/$APP/tls"
|
|
|
|
[[ -f "$DOKKU_ROOT/$APP/ENV" ]] && source $DOKKU_ROOT/$APP/ENV
|
|
|
|
if [[ ! -n "$NO_VHOST" ]]; then
|
|
NONSSL_VHOSTS=`cat $VHOST_PATH`
|
|
if [[ -e "$SSL/server.crt" ]] && [[ -e "$SSL/server.key" ]]; then
|
|
SSL_INUSE="$SSL"
|
|
SSL_DIRECTIVES=$(cat <<EOF
|
|
ssl_certificate $SSL_INUSE/server.crt;
|
|
ssl_certificate_key $SSL_INUSE/server.key;
|
|
EOF
|
|
)
|
|
elif [[ -e "$WILDCARD_SSL/server.crt" ]] && [[ -e "$WILDCARD_SSL/server.key" ]]; then
|
|
SSL_INUSE="$WILDCARD_SSL"
|
|
SSL_DIRECTIVES=""
|
|
fi
|
|
|
|
NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.conf"
|
|
SCHEME="http"
|
|
if [[ -n "$SSL_INUSE" ]]; then
|
|
NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.ssl.conf"
|
|
SCHEME="https"
|
|
|
|
SSL_HOSTNAME=`openssl x509 -in $SSL_INUSE/server.crt -noout -subject | tr '/' '\n' | grep CN= | cut -c4-`
|
|
SSL_HOSTNAME=`echo "$SSL_HOSTNAME" | sed 's|\.|\\.|g' | sed 's/\*/\.\*/g'`
|
|
SSL_VHOSTS=`egrep ^"$SSL_HOSTNAME"$ $VHOST_PATH`
|
|
NONSSL_VHOSTS=`egrep -v ^"$SSL_HOSTNAME"$ $VHOST_PATH || exit 0`
|
|
|
|
while read line; do
|
|
echo "-----> Configuring SSL for $line..."
|
|
SSL_SERVER_NAME=$line
|
|
eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf"
|
|
done <<< "$SSL_VHOSTS"
|
|
fi
|
|
|
|
APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template"
|
|
if [[ -f $APP_NGINX_TEMPLATE ]]; then
|
|
echo "-----> Overriding default nginx.conf with detected nginx.conf.template"
|
|
NGINX_CONF=$APP_NGINX_TEMPLATE
|
|
fi
|
|
|
|
cat $VHOST_PATH | xargs -i echo "-----> Configuring {}..."
|
|
# Include SSL_VHOSTS so we can redirect http to https on that hostname as well
|
|
NOSSL_SERVER_NAME=`echo $NONSSL_VHOSTS $SSL_VHOSTS| tr '\n' ' '`
|
|
|
|
echo "-----> Creating $SCHEME nginx.conf"
|
|
echo "upstream $APP { server 127.0.0.1:$PORT; }" > $DOKKU_ROOT/$APP/nginx.conf
|
|
eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf"
|
|
|
|
echo "-----> Running nginx-pre-reload"
|
|
pluginhook nginx-pre-reload $APP $PORT
|
|
|
|
echo " Reloading nginx"
|
|
restart_nginx
|
|
else
|
|
if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
|
|
echo "-----> NO_VHOST set, deleting $APP/VHOST"
|
|
rm "$DOKKU_ROOT/$APP/VHOST"
|
|
fi
|
|
if [[ -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then
|
|
echo "-----> NO_VHOST set, deleting nginx.conf"
|
|
rm "$DOKKU_ROOT/$APP/nginx.conf"
|
|
|
|
echo "-----> NO_VHOST set, reloading nginx after nginx.conf deletion"
|
|
restart_nginx
|
|
fi
|
|
fi
|
|
;;
|
|
|
|
nginx:import-ssl)
|
|
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
|
|
[[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1
|
|
[[ -t 0 ]] && echo "Tar archive containing server.crt and server.key expected on stdin" && exit 1
|
|
APP="$2"
|
|
|
|
TEMP_DIR=`mktemp -d`
|
|
cd $TEMP_DIR
|
|
tar xvf - <&0
|
|
[[ ! -f "$TEMP_DIR/server.crt" ]] && echo "Tar archive missing server.crt" && exit 1
|
|
[[ ! -f "$TEMP_DIR/server.key" ]] && echo "Tar archive missing server.key" && exit 1
|
|
|
|
mkdir -p "$DOKKU_ROOT/$APP/tls"
|
|
mv "$TEMP_DIR/server.crt" "$DOKKU_ROOT/$APP/tls/server.crt"
|
|
mv "$TEMP_DIR/server.key" "$DOKKU_ROOT/$APP/tls/server.key"
|
|
cd $DOKKU_ROOT
|
|
rm -rf $TEMP_DIR
|
|
dokku nginx:build-config $APP
|
|
;;
|
|
|
|
help | nginx:help)
|
|
cat && cat<<EOF
|
|
nginx:import-ssl <app> Imports a tarball from stdin; should contain server.crt and server.key
|
|
nginx:build-config <app> (Re)builds nginx config for given app
|
|
EOF
|
|
;;
|
|
|
|
*)
|
|
exit $DOKKU_NOT_IMPLEMENTED_EXIT
|
|
;;
|
|
|
|
esac
|