-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·421 lines (356 loc) · 9.95 KB
/
install.sh
File metadata and controls
executable file
·421 lines (356 loc) · 9.95 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/bin/sh
set -eu
OWNER="${OWNER:-we11adam}"
REPO="${REPO:-uddns}"
VERSION="${UDDNS_VERSION:-latest}"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
SERVICE_NAME="${SERVICE_NAME:-uddns}"
CONFIG_FILE="${UDDNS_CONFIG:-/etc/uddns.yaml}"
SERVICE_INTERVAL="${UDDNS_INTERVAL:-30s}"
LOG_DIR="${UDDNS_LOG_DIR:-}"
LOG_RETENTION_DAYS="${UDDNS_LOG_RETENTION_DAYS:-}"
INSTALL_SYSTEMD="${UDDNS_INSTALL_SYSTEMD:-}"
usage() {
cat <<EOF
Install UDDNS from GitHub releases.
Usage:
curl -fsSL https://raw.githubusercontent.com/${OWNER}/${REPO}/main/install.sh | sh
curl -fsSL https://raw.githubusercontent.com/${OWNER}/${REPO}/main/install.sh | sh -s -- [options]
Options:
--version <tag> Release tag to install instead of latest
--install-dir <dir> Install directory (default: ${INSTALL_DIR})
--systemd Install or update the systemd service without prompting
--no-systemd Skip systemd service installation
--config <path> Config file path used by the systemd service
--interval <duration> UDDNS_INTERVAL used by the systemd service
--log-dir <dir> Enable rotated file logging in the systemd service
--log-retention-days <n>
Calendar days of logs to keep (application default: 7)
-h, --help Show this help
Environment:
UDDNS_VERSION, INSTALL_DIR, UDDNS_INSTALL_SYSTEMD, UDDNS_CONFIG, UDDNS_INTERVAL,
UDDNS_LOG_DIR, UDDNS_LOG_RETENTION_DAYS
EOF
}
log() {
printf '%s\n' "$*" >&2
}
fail() {
printf 'error: %s\n' "$*" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "required command not found: $1"
}
run_as_root() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
elif "$@" 2>/dev/null; then
return 0
elif command -v sudo >/dev/null 2>&1; then
sudo "$@"
else
fail "root permissions are required for: $*"
fi
}
is_tty_available() {
[ -r /dev/tty ] && [ -w /dev/tty ]
}
prompt_yes_no() {
question="$1"
default="${2:-n}"
if ! is_tty_available; then
[ "$default" = "y" ]
return
fi
while :; do
if [ "$default" = "y" ]; then
suffix="[Y/n]"
else
suffix="[y/N]"
fi
printf '%s %s ' "$question" "$suffix" >/dev/tty
IFS= read -r answer </dev/tty || answer=""
answer="${answer:-$default}"
case "$answer" in
y | Y | yes | YES | Yes) return 0 ;;
n | N | no | NO | No) return 1 ;;
*) printf 'Please answer yes or no.\n' >/dev/tty ;;
esac
done
}
prompt_text() {
question="$1"
default="$2"
if ! is_tty_available; then
printf '%s\n' "$default"
return
fi
printf '%s [%s] ' "$question" "$default" >/dev/tty
IFS= read -r answer </dev/tty || answer=""
printf '%s\n' "${answer:-$default}"
}
systemd_available() {
command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]
}
systemd_unit_exists() {
unit="${SERVICE_NAME}.service"
[ -e "/etc/systemd/system/${unit}" ] && return 0
[ -e "/usr/lib/systemd/system/${unit}" ] && return 0
[ -e "/lib/systemd/system/${unit}" ] && return 0
systemctl list-unit-files "$unit" --no-legend 2>/dev/null | grep -q "^${unit}[[:space:]]"
}
ask_systemd_preference() {
case "$INSTALL_SYSTEMD" in
1 | true | TRUE | yes | YES)
systemd_available || fail "UDDNS_INSTALL_SYSTEMD is set, but systemd is not available"
return 0
;;
0 | false | FALSE | no | NO)
return 1
;;
"") ;;
*)
fail "invalid UDDNS_INSTALL_SYSTEMD value: $INSTALL_SYSTEMD"
;;
esac
systemd_available || return 1
if systemd_unit_exists; then
prompt_yes_no "Existing ${SERVICE_NAME}.service detected. Update the systemd service?" "n"
else
prompt_yes_no "systemd detected. Install UDDNS as a systemd service?" "n"
fi
}
parse_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
--version)
[ "$#" -ge 2 ] || fail "--version requires a value"
VERSION="$2"
shift 2
;;
--install-dir)
[ "$#" -ge 2 ] || fail "--install-dir requires a value"
INSTALL_DIR="$2"
shift 2
;;
--systemd)
INSTALL_SYSTEMD=1
shift
;;
--no-systemd)
INSTALL_SYSTEMD=0
shift
;;
--config)
[ "$#" -ge 2 ] || fail "--config requires a value"
CONFIG_FILE="$2"
shift 2
;;
--interval)
[ "$#" -ge 2 ] || fail "--interval requires a value"
SERVICE_INTERVAL="$2"
shift 2
;;
--log-dir)
[ "$#" -ge 2 ] || fail "--log-dir requires a value"
LOG_DIR="$2"
shift 2
;;
--log-retention-days)
[ "$#" -ge 2 ] || fail "--log-retention-days requires a value"
LOG_RETENTION_DAYS="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
fail "unknown option: $1"
;;
esac
done
}
detect_os() {
case "$(uname -s)" in
Linux) printf 'linux\n' ;;
Darwin) printf 'darwin\n' ;;
FreeBSD) printf 'freebsd\n' ;;
*) fail "unsupported operating system: $(uname -s)" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64 | amd64) printf 'amd64\n' ;;
i386 | i686) printf '386\n' ;;
aarch64 | arm64) printf 'arm64\n' ;;
armv5* | armv5tel) printf 'armv5\n' ;;
armv6* | armv6l) printf 'armv6\n' ;;
armv7* | armv7l) printf 'armv7\n' ;;
mips64le) printf 'mips64le\n' ;;
mips64) printf 'mips64\n' ;;
mipsle) printf 'mipsle\n' ;;
mips) printf 'mips\n' ;;
s390x) printf 's390x\n' ;;
riscv64) printf 'riscv64\n' ;;
*) fail "unsupported architecture: $(uname -m)" ;;
esac
}
os_regex() {
case "$1" in
linux) printf 'linux|Linux' ;;
darwin) printf 'darwin|Darwin|macOS|MacOS' ;;
freebsd) printf 'freebsd|FreeBSD' ;;
esac
}
arch_regex() {
case "$1" in
amd64) printf 'amd64|x86_64' ;;
386) printf '386|i386|i686' ;;
arm64) printf 'arm64|aarch64' ;;
armv5) printf 'armv5|arm_5|arm-5|arm5' ;;
armv6) printf 'armv6|arm_6|arm-6|arm6' ;;
armv7) printf 'armv7|arm_7|arm-7|arm7' ;;
mips64le) printf 'mips64le' ;;
mips64) printf 'mips64' ;;
mipsle) printf 'mipsle' ;;
mips) printf 'mips' ;;
s390x) printf 's390x' ;;
riscv64) printf 'riscv64' ;;
esac
}
release_api_url() {
if [ "$VERSION" = "latest" ]; then
printf 'https://api.github.com/repos/%s/%s/releases/latest\n' "$OWNER" "$REPO"
else
printf 'https://api.github.com/repos/%s/%s/releases/tags/%s\n' "$OWNER" "$REPO" "$VERSION"
fi
}
extract_urls() {
sed -n 's/.*"browser_download_url"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$1"
}
extract_tag() {
sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$1" | head -n 1
}
find_asset_url() {
release_json="$1"
os="$2"
arch="$3"
os_re="$(os_regex "$os")"
arch_re="$(arch_regex "$arch")"
candidates="$(extract_urls "$release_json" | grep -E '(\.tar\.gz|\.tgz|\.zip)$' | grep -E "(${os_re})" | grep -E "(${arch_re})" || true)"
if [ "$arch" = "amd64" ]; then
without_v3="$(printf '%s\n' "$candidates" | grep -Ev 'amd64[_-]?v3|x86_64[_-]?v3' || true)"
if [ -n "$without_v3" ]; then
candidates="$without_v3"
fi
fi
printf '%s\n' "$candidates" | head -n 1
}
download_release() {
tmpdir="$1"
os="$2"
arch="$3"
release_json="${tmpdir}/release.json"
api_url="$(release_api_url)"
log "Fetching release metadata from ${api_url}"
curl -fsSL "$api_url" -o "$release_json"
resolved_version="$(extract_tag "$release_json")"
[ -n "$resolved_version" ] || resolved_version="$VERSION"
asset_url="$(find_asset_url "$release_json" "$os" "$arch")"
[ -n "$asset_url" ] || fail "no release asset found for ${os}/${arch} in ${resolved_version}"
archive="${tmpdir}/archive"
log "Downloading ${REPO} ${resolved_version} for ${os}/${arch}"
curl -fL "$asset_url" -o "$archive"
extract_dir="${tmpdir}/extract"
mkdir -p "$extract_dir"
case "$asset_url" in
*.tar.gz | *.tgz)
need_cmd tar
tar -xzf "$archive" -C "$extract_dir"
;;
*.zip)
need_cmd unzip
unzip -q "$archive" -d "$extract_dir"
;;
*)
fail "unsupported archive type: $asset_url"
;;
esac
binary="$(find "$extract_dir" -type f \( -name "$REPO" -o -name "${REPO}.exe" \) | head -n 1)"
[ -n "$binary" ] || fail "archive did not contain a ${REPO} binary"
printf '%s\n' "$binary"
}
install_binary() {
binary="$1"
target="${INSTALL_DIR}/${REPO}"
log "Installing ${REPO} to ${target}"
run_as_root mkdir -p "$INSTALL_DIR"
run_as_root install -m 0755 "$binary" "$target"
}
install_systemd_service() {
binary_path="${INSTALL_DIR}/${REPO}"
unit_path="/etc/systemd/system/${SERVICE_NAME}.service"
unit_file="${tmpdir}/${SERVICE_NAME}.service"
if [ "$CONFIG_FILE" = "/etc/uddns.yaml" ] && is_tty_available; then
CONFIG_FILE="$(prompt_text "Config file path for the systemd service" "$CONFIG_FILE")"
fi
cat >"$unit_file" <<EOF
[Unit]
Description=UDDNS dynamic DNS updater
Documentation=https://github.com/${OWNER}/${REPO}
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
Environment=UDDNS_CONFIG=${CONFIG_FILE}
Environment=UDDNS_INTERVAL=${SERVICE_INTERVAL}
EOF
if [ -n "$LOG_DIR" ]; then
printf 'Environment=UDDNS_LOG_DIR=%s\n' "$LOG_DIR" >>"$unit_file"
fi
if [ -n "$LOG_RETENTION_DAYS" ]; then
printf 'Environment=UDDNS_LOG_RETENTION_DAYS=%s\n' "$LOG_RETENTION_DAYS" >>"$unit_file"
fi
cat >>"$unit_file" <<EOF
ExecStart=${binary_path}
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
EOF
log "Installing systemd unit to ${unit_path}"
run_as_root install -m 0644 "$unit_file" "$unit_path"
run_as_root systemctl daemon-reload
if [ -r "$CONFIG_FILE" ]; then
run_as_root systemctl enable --now "${SERVICE_NAME}.service"
log "systemd service enabled and started: ${SERVICE_NAME}.service"
else
run_as_root systemctl enable "${SERVICE_NAME}.service"
log "systemd service enabled but not started because ${CONFIG_FILE} is not readable"
log "Create the config file, then run: sudo systemctl start ${SERVICE_NAME}.service"
fi
}
parse_args "$@"
want_systemd=0
if ask_systemd_preference; then
want_systemd=1
fi
need_cmd curl
need_cmd uname
need_cmd grep
need_cmd sed
need_cmd find
need_cmd head
need_cmd install
tmpdir="$(mktemp -d 2>/dev/null || mktemp -d -t uddns)"
trap 'rm -rf "$tmpdir"' EXIT INT TERM
os="$(detect_os)"
arch="$(detect_arch)"
binary="$(download_release "$tmpdir" "$os" "$arch")"
install_binary "$binary"
if [ "$want_systemd" -eq 1 ]; then
install_systemd_service
fi
log "UDDNS installed successfully."