mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Update dnsmasq conf_regen hook and use loopback address by default
This commit is contained in:
parent
8269788172
commit
41f9b2c76c
3 changed files with 64 additions and 53 deletions
|
@ -1,53 +0,0 @@
|
|||
set -e
|
||||
|
||||
force=$1
|
||||
|
||||
. /usr/share/yunohost/helpers
|
||||
|
||||
function safe_copy () {
|
||||
if [[ "$force" == "True" ]]; then
|
||||
sudo yunohost service safecopy \
|
||||
-s dnsmasq $1 $2 --force
|
||||
else
|
||||
sudo yunohost service safecopy \
|
||||
-s dnsmasq $1 $2
|
||||
fi
|
||||
}
|
||||
|
||||
cd /usr/share/yunohost/templates/dnsmasq
|
||||
|
||||
# Get IPv4 address
|
||||
ip=$(curl -s -4 https://ip.yunohost.org 2>/dev/null || true)
|
||||
ynh_validate_ip4 $ip || ip='0.0.0.0'
|
||||
|
||||
# Get IPv6 IP address
|
||||
ipv6=$(curl -s -6 http://ip6.yunohost.org 2>/dev/null || true)
|
||||
ynh_validate_ip6 $ipv6 || ipv6=''
|
||||
|
||||
sudo mkdir -p /etc/dnsmasq.d
|
||||
|
||||
domain_list=$(sudo yunohost domain list --output-as plain)
|
||||
|
||||
# Copy a configuration file for each YunoHost domain
|
||||
for domain in $domain_list; do
|
||||
cat domain.sed \
|
||||
| sed "s/{{ domain }}/$domain/g" \
|
||||
| sed "s/{{ ip }}/$ip/g" \
|
||||
| sudo tee $domain
|
||||
|
||||
if [[ "$ipv6" != "" ]]; then
|
||||
echo "address=/$domain/$ipv6" | sudo tee -a $domain
|
||||
fi
|
||||
|
||||
safe_copy $domain /etc/dnsmasq.d/$domain
|
||||
done
|
||||
|
||||
# Remove old domains files
|
||||
for file in /etc/dnsmasq.d/*.*; do
|
||||
domain=$(echo $file | sed 's|/etc/dnsmasq.d/||')
|
||||
[[ $domain_list =~ $domain ]] \
|
||||
|| sudo yunohost service saferemove -s dnsmasq $file
|
||||
done
|
||||
|
||||
sudo service dnsmasq reload \
|
||||
|| sudo service dnsmasq restart
|
64
data/hooks/conf_regen/43-dnsmasq
Executable file
64
data/hooks/conf_regen/43-dnsmasq
Executable file
|
@ -0,0 +1,64 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
do_pre_regen() {
|
||||
pending_dir=$1
|
||||
|
||||
# source ip helpers
|
||||
. /usr/share/yunohost/helpers.d/ip
|
||||
|
||||
cd /usr/share/yunohost/templates/dnsmasq
|
||||
|
||||
# create directory for pending conf
|
||||
dnsmasq_dir="${pending_dir}/etc/dnsmasq.d"
|
||||
mkdir -p "$dnsmasq_dir"
|
||||
|
||||
# retrieve variables
|
||||
ipv4=$(curl -s -4 https://ip.yunohost.org 2>/dev/null || true)
|
||||
ynh_validate_ip4 "$ipv4" || ipv4='127.0.0.1'
|
||||
ipv6=$(curl -s -6 http://ip6.yunohost.org 2>/dev/null || true)
|
||||
ynh_validate_ip6 "$ipv6" || ipv6=''
|
||||
domain_list=$(sudo yunohost domain list --output-as plain --quiet)
|
||||
|
||||
# add domain conf files
|
||||
for domain in $domain_list; do
|
||||
cat domain.tpl \
|
||||
| sed "s/{{ domain }}/${domain}/g" \
|
||||
| sed "s/{{ ip }}/${ipv4}/g" \
|
||||
> "${dnsmasq_dir}/${domain}"
|
||||
[[ -n $ipv6 ]] \
|
||||
&& echo "address=/${domain}/${ipv6}" >> "${dnsmasq_dir}/${domain}"
|
||||
done
|
||||
|
||||
# remove old domain conf files
|
||||
conf_files=$(ls -1 /etc/dnsmasq.d \
|
||||
| awk '/^[^\.]+\.[^\.]+.*$/ { print $1 }')
|
||||
for domain in $conf_files; do
|
||||
[[ $domain_list =~ $domain ]] \
|
||||
|| touch "${dnsmasq_dir}/${domain}"
|
||||
done
|
||||
}
|
||||
|
||||
do_post_regen() {
|
||||
# TODO: only restart if conf changed
|
||||
sudo service dnsmasq reload \
|
||||
|| sudo service dnsmasq restart
|
||||
}
|
||||
|
||||
FORCE=$2
|
||||
|
||||
case "$1" in
|
||||
pre)
|
||||
do_pre_regen $3
|
||||
;;
|
||||
post)
|
||||
do_post_regen
|
||||
;;
|
||||
*)
|
||||
echo "hook called with unknown argument \`$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
Loading…
Add table
Reference in a new issue