-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·132 lines (104 loc) · 2.64 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env sh
#
# Script to install Better DNS as a service on Linux systems.
#
# Primary goal is to support Raspberry Pi devices on Raspbian, but others may be supported in the future.
#
# Defaults and static variables
ARCH="unknown"
BINARY="better-dns-unknown"
BINARY_DST="/usr/sbin/better-dns"
CONFIG="/etc/better-dns.yaml"
OS="linux"
SOURCE="https://github.com/lietu/better-dns/releases/latest/download/"
# Composite variables
CONFIG_SOURCE="$SOURCE/better-dns-server.yaml"
# ----- TEMPLATES ----- #
function make_systemd_config {
cat <<-EOF
[Unit]
Description=Better DNS
After=network.target
[Service]
ExecStart=sh -c "sleep 30; exec $BINARY_DST -config $CONFIG"
ExecStop=kill -2 \$MAINPID
Restart=always
User=root
Type=idle
[Install]
WantedBy=multi-user.target
EOF
}
# ----- FUNCTIONS ----- #
function detect_arch() {
ARCH=$(uname -m)
# TODO: Probably need a lot more special cases here
if [[ "$ARCH" == "armv7l" ]]; then
ARCH="arm"
fi
if [[ "$ARCH" == "x86_64" ]]; then
ARCH="amd64"
fi
echo "Detected architecture as $ARCH"
}
function pick_binary() {
BINARY="better-dns-${OS}-${ARCH}"
echo "Will use $BINARY"
}
function download() {
src="$1"
dst="$2"
echo "Downloading $src to $dst"
if (which curl >/dev/null); then
curl -L -o "$dst" "$src"
elif (which wget >/dev/null); then
wget "$src" -O "$dst"
else
echo "No curl or wget found, dunno how to download the binary."
exit 1
fi
}
function install_binary() {
download "$SOURCE/$BINARY" "$BINARY_DST"
chmod 0700 "$BINARY_DST"
}
function install_config() {
download "$CONFIG_SOURCE" "$CONFIG"
chmod 0600 "$CONFIG"
}
function install_service() {
if (which systemctl >/dev/null); then
SERVICE="better-dns.service"
SERVICE_CONF="/etc/systemd/system/$SERVICE"
echo "Installing systemd service $SERVICE to $SERVICE_CONF"
make_systemd_config > "$SERVICE_CONF"
chmod 0600 "$SERVICE_CONF"
echo "Enabling and starting service via systemd"
systemctl enable "$SERVICE"
systemctl start "$SERVICE"
echo "To update configuration after changes run:"
echo " systemctl restart $SERVICE"
else
echo "Don't know how to install service."
exit 1
fi
}
# ----- LOGIC ----- #
# Try to self-elevate privileges if needed
if [ "$UID" -ne 0 ]; then
# Try to self-elevate
echo "Root privileges required for installation, trying to self-elevate via sudo."
exec sudo bash "$0" "$@"
fi
# Detect where we're running on
detect_arch
pick_binary
# Install
install_binary
install_config
install_service
echo
echo "Binary is installed at $BINARY_DST"
echo "Configuration is at $CONFIG"
echo
echo "All done! Enjoy Better DNS."