mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
135 lines
3.5 KiB
Bash
Executable file
135 lines
3.5 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
. /usr/share/yunohost/helpers
|
|
|
|
do_init_regen() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "You must be root to run this script" 1>&2
|
|
exit 1
|
|
fi
|
|
|
|
cd /usr/share/yunohost/templates/nginx
|
|
|
|
nginx_dir="/etc/nginx"
|
|
nginx_conf_dir="${nginx_dir}/conf.d"
|
|
mkdir -p "$nginx_conf_dir"
|
|
|
|
# install plain conf files
|
|
cp plain/* "$nginx_conf_dir"
|
|
|
|
# probably run with init: just disable default site, restart NGINX and exit
|
|
rm -f "${nginx_dir}/sites-enabled/default"
|
|
|
|
export compatibility="intermediate"
|
|
ynh_render_template "yunohost_admin.conf" "${nginx_conf_dir}/yunohost_admin.conf"
|
|
|
|
# Restart nginx if conf looks good, otherwise display error and exit unhappy
|
|
nginx -t 2>/dev/null && service nginx restart || (nginx -t && exit 1)
|
|
|
|
exit 0
|
|
}
|
|
|
|
do_pre_regen() {
|
|
pending_dir=$1
|
|
|
|
cd /usr/share/yunohost/templates/nginx
|
|
|
|
nginx_dir="${pending_dir}/etc/nginx"
|
|
nginx_conf_dir="${nginx_dir}/conf.d"
|
|
mkdir -p "$nginx_conf_dir"
|
|
|
|
# install / update plain conf files
|
|
cp plain/* "$nginx_conf_dir"
|
|
|
|
# retrieve variables
|
|
main_domain=$(cat /etc/yunohost/current_host)
|
|
domain_list=$(sudo yunohost domain list --output-as plain --quiet)
|
|
|
|
# Support different strategy for security configurations
|
|
export compatibility="$(yunohost settings get 'security.nginx.compatibility')"
|
|
|
|
# add domain conf files
|
|
for domain in $domain_list; do
|
|
domain_conf_dir="${nginx_conf_dir}/${domain}.d"
|
|
mkdir -p "$domain_conf_dir"
|
|
mail_autoconfig_dir="${pending_dir}/var/www/.well-known/${domain}/autoconfig/mail/"
|
|
mkdir -p "$mail_autoconfig_dir"
|
|
|
|
# NGINX server configuration
|
|
export domain
|
|
export domain_cert_ca=$(yunohost domain cert-status $domain --json \
|
|
| jq ".certificates.\"$domain\".CA_type" \
|
|
| tr -d '"')
|
|
|
|
ynh_render_template "server.tpl.conf" "${nginx_conf_dir}/${domain}.conf"
|
|
ynh_render_template "autoconfig.tpl.xml" "${mail_autoconfig_dir}/config-v1.1.xml"
|
|
|
|
[[ $main_domain != $domain ]] \
|
|
&& touch "${domain_conf_dir}/yunohost_local.conf" \
|
|
|| cp yunohost_local.conf "${domain_conf_dir}/yunohost_local.conf"
|
|
|
|
done
|
|
|
|
ynh_render_template "yunohost_admin.conf" "${nginx_conf_dir}/yunohost_admin.conf"
|
|
|
|
# remove old domain conf files
|
|
conf_files=$(ls -1 /etc/nginx/conf.d \
|
|
| awk '/^[^\.]+\.[^\.]+.*\.conf$/ { print $1 }')
|
|
for file in $conf_files; do
|
|
domain=${file%.conf}
|
|
[[ $domain_list =~ $domain ]] \
|
|
|| touch "${nginx_conf_dir}/${file}"
|
|
done
|
|
|
|
# remove old mail-autoconfig files
|
|
autoconfig_files=$(ls -1 /var/www/.well-known/*/autoconfig/mail/config-v1.1.xml 2>/dev/null || true)
|
|
for file in $autoconfig_files; do
|
|
domain=$(basename $(readlink -f $(dirname $file)/../..))
|
|
[[ $domain_list =~ $domain ]] \
|
|
|| (mkdir -p "$(dirname ${pending_dir}/${file})" && touch "${pending_dir}/${file}")
|
|
done
|
|
|
|
# disable default site
|
|
mkdir -p "${nginx_dir}/sites-enabled"
|
|
touch "${nginx_dir}/sites-enabled/default"
|
|
}
|
|
|
|
do_post_regen() {
|
|
regen_conf_files=$1
|
|
|
|
[ -z "$regen_conf_files" ] && exit 0
|
|
|
|
# retrieve variables
|
|
domain_list=$(sudo yunohost domain list --output-as plain --quiet)
|
|
|
|
# create NGINX conf directories for domains
|
|
for domain in $domain_list; do
|
|
sudo mkdir -p "/etc/nginx/conf.d/${domain}.d"
|
|
done
|
|
|
|
# Reload nginx configuration
|
|
pgrep nginx && sudo service nginx reload
|
|
}
|
|
|
|
FORCE=${2:-0}
|
|
DRY_RUN=${3:-0}
|
|
|
|
case "$1" in
|
|
pre)
|
|
do_pre_regen $4
|
|
;;
|
|
post)
|
|
do_post_regen $4
|
|
;;
|
|
init)
|
|
do_init_regen
|
|
;;
|
|
*)
|
|
echo "hook called with unknown argument \`$1'" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
exit 0
|