-
Notifications
You must be signed in to change notification settings - Fork 16
/
sonaric.sh
253 lines (225 loc) · 6.95 KB
/
sonaric.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
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
#!/bin/sh
set -e
APT_KEY_URL="https://us-central1-apt.pkg.dev/doc/repo-signing-key.gpg"
APT_DOWNLOAD_URL="https://us-central1-apt.pkg.dev/projects/sonaric-platform"
RPM_DOWNLOAD_URL="https://us-central1-yum.pkg.dev/projects/sonaric-platform/sonaric-releases-rpm"
DOWNLOAD_URL="https://storage.googleapis.com/sonaric-releases/stable/linux/sonaric-amd64-latest.tar.gz"
command_exists() {
command -v "$@" > /dev/null 2>&1
}
get_distribution() {
lsb_dist=""
# Every system that we officially support has /etc/os-release
if [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
# Returning an empty string here should be alright since the
# case statements don't act unless you provide an actual value
echo "$lsb_dist"
}
# Check if this is a forked Linux distro
check_forked() {
# Check for lsb_release command existence, it usually exists in forked distros
if command_exists lsb_release; then
# Check if the `-u` option is supported
set +e
lsb_release -a -u > /dev/null 2>&1
lsb_release_exit_code=$?
set -e
# Check if the command has exited successfully, it means we're in a forked distro
if [ "$lsb_release_exit_code" = "0" ]; then
# Print info about current distro
cat <<-EOF
You're using '$lsb_dist' version '$dist_version'.
EOF
# Get the upstream release info
lsb_dist=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'id' | cut -d ':' -f 2 | tr -d '[:space:]')
dist_version=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'codename' | cut -d ':' -f 2 | tr -d '[:space:]')
# Print info about upstream distro
cat <<-EOF
Upstream release is '$lsb_dist' version '$dist_version'.
EOF
else
if [ -r /etc/debian_version ] && [ "$lsb_dist" != "ubuntu" ] && [ "$lsb_dist" != "raspbian" ]; then
if [ "$lsb_dist" = "osmc" ]; then
# OSMC runs Raspbian
lsb_dist=raspbian
else
lsb_dist=debian
fi
dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
case "$dist_version" in
12)
dist_version="bookworm"
;;
11)
dist_version="bullseye"
;;
10)
dist_version="buster"
;;
9)
dist_version="stretch"
;;
8)
dist_version="jessie"
;;
esac
fi
fi
fi
}
do_install() {
echo "# Executing Sonaric install script"
user="$(id -un 2>/dev/null || true)"
sh_c='sh -c'
if [ "$user" != 'root' ]; then
if command_exists sudo; then
sh_c='sudo -E sh -c'
elif command_exists su; then
sh_c='su -c'
else
cat >&2 <<-'EOF'
Error: this installer needs the ability to run commands as root.
We are unable to find either "sudo" or "su" available to make this happen.
EOF
exit 1
fi
fi
# check if systemctl unit is present and if it is active
if command_exists systemctl && systemctl list-units --full --all sonaricd.service | grep -Fq 'sonaricd.service'; then
$sh_c 'systemctl start sonaricd' || echo "Failed to start sonaricd"
fi
# perform some very rudimentary platform detection
lsb_dist=$( get_distribution )
lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"
case "$lsb_dist" in
ubuntu)
if command_exists lsb_release; then
dist_version="$(lsb_release --codename | cut -f2)"
fi
if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
fi
;;
debian|raspbian)
dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
case "$dist_version" in
12)
dist_version="bookworm"
;;
11)
dist_version="bullseye"
;;
10)
dist_version="buster"
;;
9)
dist_version="stretch"
;;
8)
dist_version="jessie"
;;
esac
;;
centos|rhel|sles)
if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
fi
;;
*)
if command_exists lsb_release; then
dist_version="$(lsb_release --release | cut -f2)"
fi
if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
fi
;;
esac
# Check if this is a forked Linux distro
check_forked
# Run setup for each distro accordingly
case "$lsb_dist" in
ubuntu|debian|raspbian)
$sh_c 'apt-get update -qq >/dev/null'
if command_exists sonaricd; then
echo "Sonaric is already installed"
$sh_c 'apt-get install sonaricd sonaric'
for try in {1..30} ; do
$sh_c "sonaric version" > /dev/null 2>&1 && break || sleep 2
done
$sh_c "sonaric update --nocolor --nofancy --all"
exit 0
fi
# Check if apt satisfies the version requirement
$sh_c "apt satisfy --dry-run 'podman (>=3.4.0)'" || (
echo "ERROR: Available podman version is too old, please upgrade to a supported distro"
exit 1
)
pre_reqs="apt-transport-https ca-certificates curl"
if ! command -v gpg > /dev/null; then
pre_reqs="$pre_reqs gnupg"
fi
apt_repo="deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/sonaric.gpg] $APT_DOWNLOAD_URL sonaric-releases-apt main"
(
$sh_c 'apt-get update -qq >/dev/null'
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $pre_reqs >/dev/null"
$sh_c 'install -m 0755 -d /etc/apt/keyrings'
$sh_c "curl -fsSL \"$APT_KEY_URL\" | gpg --dearmor --yes -o /etc/apt/keyrings/sonaric.gpg"
$sh_c "chmod a+r /etc/apt/keyrings/sonaric.gpg"
$sh_c "echo \"$apt_repo\" > /etc/apt/sources.list.d/sonaric.list"
$sh_c 'apt-get update -qq >/dev/null'
)
$sh_c "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq sonaric >/dev/null"
exit 0
;;
centos|fedora|rhel|rocky)
# use dnf for fedora or rocky linux, yum for centos or rhel
if [ "$lsb_dist" = "fedora" ] || [ "$lsb_dist" = "rocky" ]; then
pkg_manager="dnf"
pre_reqs="dnf-plugins-core"
elif [ "$lsb_dist" = "centos" ]; then
pkg_manager="yum"
pre_reqs="yum-utils"
fi
if command_exists sonaricd; then
echo "Sonaric is already installed"
$sh_c "$pkg_manager update --refresh -y -q sonaricd sonaric"
$sh_c 'systemctl start sonaricd' || echo "Failed to start sonaricd"
for try in {1..30} ; do
$sh_c "sonaric version" > /dev/null 2>&1 && break || sleep 2
done
$sh_c "sonaric update --nocolor --nofancy --all"
exit 0
fi
(
$sh_c "$pkg_manager install -y -q $pre_reqs"
$sh_c "tee -a /etc/yum.repos.d/artifact-registry.repo << EOF
[sonaric-releases-rpm]
name=sonaric-releases-rpm
baseurl=$RPM_DOWNLOAD_URL
enabled=1
repo_gpgcheck=0
gpgcheck=0
EOF"
# Enable the repository
$sh_c "$pkg_manager makecache"
)
(
pkgs="sonaricd sonaric"
$sh_c "$pkg_manager install -y -q $pkgs"
)
exit 0
;;
*)
echo
echo "ERROR: Unsupported distribution '$lsb_dist'"
echo
exit 1
;;
esac
exit 1
}
# wrapped up in a function so that we have some protection against only getting
# half the file during "curl | sh"
do_install