Skip to content
This repository was archived by the owner on Dec 9, 2019. It is now read-only.

Commit ac0fa01

Browse files
committed
Convert Valet DNS scripts to services
1 parent 7492d52 commit ac0fa01

File tree

4 files changed

+271
-73
lines changed

4 files changed

+271
-73
lines changed

cli/stubs/get-dns-servers

Lines changed: 0 additions & 73 deletions
This file was deleted.

cli/stubs/init/systemd

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[Unit]
2+
Description=Valet DNS Resolution
3+
After=network-online.target
4+
Wants=network-online.target systemd-networkd-wait-online.service
5+
6+
[Service]
7+
Restart=on-abnormal
8+
9+
; User and group the process will run as.
10+
User=root
11+
Group=root
12+
13+
Type=forking
14+
ExecStart=/opt/valet-linux/valet-dns start
15+
ExecStop=/opt/valet-linux/valet-dns stop
16+
ExecReload=/opt/valet-linux/valet-dns restart
17+
18+
KillMode=process
19+
20+
; Use a minimal /dev
21+
PrivateDevices=true
22+
; Hide /home, /root, and /run/user. Nobody will steal your SSH-keys.
23+
ProtectHome=true
24+
25+
[Install]
26+
WantedBy=multi-user.target

cli/stubs/init/sysvinit

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
### BEGIN INIT INFO
3+
# Provides: valet-dns
4+
# Required-Start: $local_fs $network $named $time $syslog
5+
# Required-Stop: $local_fs $network $named $time $syslog
6+
# Default-Start: 2 3 4 5
7+
# Default-Stop: 0 1 6
8+
# Short-Description: starts Valet DNS Resolution
9+
# Description: starts Valet DNS Resolution using start-stop-daemon
10+
### END INIT INFO
11+
12+
# Original Author: Carlos Priego
13+
# Maintainer: Carlos Priego
14+
15+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
16+
PIDFILE=/opt/valet-linux/watch.pid
17+
DAEMON=/opt/valet-linux/valet-dns
18+
NAME=valet-dns
19+
DESC="Valet DNS Resolution"
20+
21+
STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
22+
23+
test -x $DAEMON || exit 0
24+
25+
. /lib/init/vars.sh
26+
. /lib/lsb/init-functions
27+
28+
start() {
29+
test -f $PIDFILE && rm $PIDFILE
30+
start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON start || return 2
31+
}
32+
33+
stop() {
34+
start-stop-daemon --stop --retry=$STOP_SCHEDULE --pidfile $PIDFILE --remove-pidfile || return 1
35+
}
36+
37+
case "$1" in
38+
start)
39+
start
40+
;;
41+
stop)
42+
stop
43+
;;
44+
restart)
45+
stop
46+
start
47+
;;
48+
status)
49+
exec $DAEMON status
50+
;;
51+
*)
52+
echo "Usage: $0 {start|stop|restart|status}"
53+
exit 2
54+
;;
55+
esac
56+
57+
exit 0

cli/stubs/valet-dns

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/usr/bin/env bash
2+
3+
WORKDIR="/opt/valet-linux"
4+
DNSFILE="${WORKDIR}/dns-servers"
5+
DNSHEAD="${WORKDIR}/custom-nameservers"
6+
LOGFILE="${WORKDIR}/watch.log"
7+
VRESOLV="${WORKDIR}/resolv.conf"
8+
RESOLV="/etc/resolv.conf"
9+
10+
function unique() {
11+
# Function to remove duplicated lines (even when they are not contiguous)
12+
# cat -n puts line numbers
13+
# sort -uk2 sort and remove duplicates (ignoring line numbers)
14+
# sort -nk1 sort by line number
15+
# cut -f2- remove line numbers
16+
cat -n | sort -uk2 | sort -nk1 | cut -f2-
17+
}
18+
19+
function symlinkResolv() {
20+
if [[ $(readlink -f "$RESOLV") != "$VRESOLV" ]]; then
21+
if [[ ! -f "${RESOLV}.bak" ]]; then
22+
mv "$RESOLV" "${RESOLV}.bak"
23+
fi
24+
25+
ln -sf "$VRESOLV" "${RESOLV}.tmp"
26+
mv "${RESOLV}.tmp" "$RESOLV"
27+
fi
28+
}
29+
30+
function getDirs() {
31+
DIRS=()
32+
local TARRAY=()
33+
34+
readarray -t TARRAY <<< "$(find /run -path '/run/user' -prune -o ! -readable -prune -o -name 'resolv.conf' -print)"
35+
36+
# Find nameserver files in the /run/NetworkManager folder (as they do not have a standard name)
37+
if [[ ! -f "/run/NetworkManager/resolv.conf" && -d "/run/NetworkManager" ]]; then
38+
TARRAY=(${TARRAY[@]} '/run/NetworkManager/')
39+
fi
40+
41+
# Find nameserver files in the /run/resolvconf/interface folder (as they do not have a standard name)
42+
if [[ -d "/run/resolvconf/interface" ]]; then
43+
TARRAY=(${TARRAY[@]} '/run/resolvconf/interface/')
44+
fi
45+
46+
for ENTRY in "${TARRAY[@]}"; do
47+
local TMP=${ENTRY%/*}
48+
DIRS=(${DIRS[@]} "$TMP")
49+
done
50+
}
51+
52+
function updateNameservers() {
53+
# Read all of the nameserver files at once, filter lines starting with 'nameserver'
54+
# and exclude the ones containing 127.0.0.1 (localhost)
55+
56+
getFiles
57+
58+
echo "${FILES[@]}" | tee "${WORKDIR}/resolvfiles.log" &>/dev/null
59+
60+
cat "$DNSHEAD" | unique | tee "$DNSFILE" &>/dev/null
61+
cat "${FILES[@]}" | grep -i '^nameserver' | grep -v '127.0.0.1' | unique | tee -a "$DNSFILE" &>/dev/null
62+
63+
symlinkResolv
64+
65+
cat "${FILES[@]}" | grep -v '^nameserver' | grep -v '^#' | unique | tee "$VRESOLV" &>/dev/null
66+
echo 'nameserver 127.0.0.1' >> "$VRESOLV"
67+
68+
# Add "search" and "domain" directives to /etc/resolv.conf
69+
# chattr -i "$RESOLV" && \
70+
# cat "${FILES[@]}" | grep -v '^nameserver' | grep -v '^#' | unique | tee "$VRESOLV" &>/dev/null && \
71+
# echo 'nameserver 127.0.0.1' >> "$VRESOLV" && \
72+
# chattr +i "$RESOLV"
73+
}
74+
75+
function getFiles() {
76+
FILES=()
77+
local TARRAY=()
78+
79+
for DIR in "${DIRS[@]}"; do
80+
readarray -t TARRAY <<< "$(find ${DIR} -path ! -readable -prune -o -name 'resolv.conf' -print)"
81+
82+
# Find nameserver files in the /run/resolvconf/interface folder (as they do not have a standard name)
83+
if [[ "$DIR" = "/run/resolvconf/interface" ]]; then
84+
readarray -t TARRAY <<< "$(find ${DIR} ! -readable -prune -o -type f -print)"
85+
fi
86+
87+
FILES=(${FILES[@]} ${TARRAY[@]})
88+
done
89+
}
90+
91+
function watchDirs() {
92+
local WATCHERS=(${DIRS[@]} "$DNSHEAD")
93+
94+
# Log which directories are being watched
95+
echo "Watching the following directories:" >> "$LOGFILE"
96+
97+
for DIR in "${DIRS[@]}"; do
98+
echo " - ${1}" >> "$LOGFILE"
99+
done
100+
101+
# Watch directories for changes in files
102+
inotifywait -q -m -e modify -e create -e delete --format "%w%f" "${WATCHERS[@]}" | while read change; do
103+
updateNameservers
104+
done &
105+
106+
pgrep -f 'inotifywait -q -m -e modify' | tee "${WORKDIR}/watch.pid" &>/dev/null
107+
}
108+
109+
function main() {
110+
# Create dns file in case it does not exists
111+
touch "$DNSHEAD"
112+
touch "$DNSFILE"
113+
114+
# Clear log file
115+
if [[ -f "$LOGFILE" ]]; then
116+
rm "$LOGFILE"
117+
fi
118+
119+
touch "$LOGFILE"
120+
121+
getDirs
122+
updateNameservers
123+
watchDirs
124+
}
125+
126+
################################################################################
127+
128+
function start {
129+
if [[ $(pgrep -f 'inotifywait -q -m -e modify') ]]; then
130+
echo -e "Valet DNS Watcher is already running..."
131+
else
132+
echo -e "Starting Valet DNS Watcher..."
133+
main
134+
echo -e "Valet DNS Watcher started succesfully."
135+
fi
136+
}
137+
138+
function stop {
139+
echo -e "Stopping Valet DNS Watcher...\n"
140+
141+
pkill -f "inotifywait -q -m -e modify"
142+
143+
rm "$LOGFILE" && touch "$LOGFILE"
144+
145+
if [[ ! $(pgrep -f 'inotifywait -q -m -e modify') ]]; then
146+
echo -e "\nValet DNS Watcher stopped succesfully."
147+
fi
148+
}
149+
150+
function restart {
151+
echo -e "Stopping Valet DNS Watcher..."
152+
153+
if [[ $(pgrep -f 'inotifywait -q -m -e modify') ]]; then
154+
pkill -f "inotifywait -q -m -e modify"
155+
fi
156+
157+
echo -e "Starting Valet DNS Watcher..."
158+
159+
main
160+
161+
if [[ $(pgrep -f 'inotifywait -q -m -e modify') ]]; then
162+
echo -e "Valet DNS Watcher restarted succesfully."
163+
fi
164+
}
165+
166+
function status {
167+
echo -e "Watching for changes in the following directories:\n"
168+
cat '/opt/valet-linux/watch.log'
169+
}
170+
171+
case "$1" in
172+
start)
173+
start
174+
;;
175+
stop)
176+
stop
177+
;;
178+
restart)
179+
restart
180+
;;
181+
status)
182+
status
183+
;;
184+
*)
185+
echo "Usage: $0 {start|stop|restart|status}"
186+
esac
187+
188+
exit 0

0 commit comments

Comments
 (0)