-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-autoupdate.sh
More file actions
executable file
·115 lines (97 loc) · 3.03 KB
/
install-autoupdate.sh
File metadata and controls
executable file
·115 lines (97 loc) · 3.03 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
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
log() {
printf '%s %s\n' "$(date -Is)" "$*"
}
err() {
log "ERROR: $*" >&2
}
require_root() {
if [[ $(id -u) -ne 0 ]]; then
err "this installer must run as root; re-run with sudo or as root"
exit 1
fi
}
ensure_command() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
err "required command '$cmd' not found; install it and retry"
exit 1
fi
}
main() {
require_root
ensure_command install
ensure_command apt-get
local script_dir target_dir service_unit timer_unit
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
target_dir="/usr/local/sbin"
service_unit="/etc/systemd/system/autoupdate.service"
timer_unit="/etc/systemd/system/autoupdate.timer"
# List of required scripts
local required_scripts=(
"autoupdate-and-reboot.sh"
"autoupdate-and-shutdown.sh"
"cleanshutdown"
"autoupdate.sh"
"check-if-already-updating.sh"
"remove-old-kernels.sh"
"remove-old-snaps.sh"
"remove-all-old-packages.sh"
"reboot-if-required.sh"
"check-requirements.sh"
"update-firmware.sh"
)
# Verify all required scripts exist
for script in "${required_scripts[@]}"; do
if [[ ! -f "$script_dir/$script" ]]; then
err "$script not found in $script_dir"
exit 1
fi
done
if [[ ! -f "$script_dir/systemd/autoupdate.service" || ! -f "$script_dir/systemd/autoupdate.timer" ]]; then
err "systemd units missing; run from repository root"
exit 1
fi
# Stop services before updating files
if command -v systemctl >/dev/null 2>&1; then
if systemctl is-active --quiet autoupdate.timer; then
log "stopping autoupdate.timer before update"
systemctl stop autoupdate.timer
fi
if systemctl is-active --quiet autoupdate.service; then
log "stopping autoupdate.service before update"
systemctl stop autoupdate.service
fi
fi
log "installing scripts to ${target_dir}"
for script in "${required_scripts[@]}"; do
log " installing $script"
install -Dm755 "$script_dir/$script" "$target_dir/$script"
done
log "installing systemd units"
install -Dm644 "$script_dir/systemd/autoupdate.service" "$service_unit"
install -Dm644 "$script_dir/systemd/autoupdate.timer" "$timer_unit"
if [[ ! -e /var/log/autoupdate.log ]]; then
log "creating /var/log/autoupdate.log"
touch /var/log/autoupdate.log
if getent group adm >/dev/null 2>&1; then
chown root:adm /var/log/autoupdate.log || true
fi
chmod 640 /var/log/autoupdate.log || true
fi
if command -v systemctl >/dev/null 2>&1; then
log "reloading systemd units"
systemctl daemon-reload
log "enabling and starting autoupdate.timer with updated files"
systemctl enable autoupdate.timer
systemctl start autoupdate.timer
log "disabling apt-daily-upgrade.timer to avoid lock contention"
systemctl disable --now apt-daily-upgrade.timer || true
else
log "systemctl not detected; enable the service manually if needed"
fi
log "installation complete"
}
main "$@"