-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·221 lines (194 loc) · 5.88 KB
/
install.sh
File metadata and controls
executable file
·221 lines (194 loc) · 5.88 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
#! /bin/sh
# based on https://tailscale.com/install.sh
# original copyright:
# Copyright (c) Tailscale Inc & AUTHORS
# SPDX-License-Identifier: BSD-3-Clause
set -e
# Function to compare semantic versions
# Returns: -1 if version1 < version2, 0 if equal, 1 if version1 > version2
version_compare() {
local version1="$1"
local version2="$2"
# Remove 'v' prefix if present
version1=$(echo "$version1" | sed 's/^v//')
version2=$(echo "$version2" | sed 's/^v//')
# Handle release candidates
local v1_base v1_rc v2_base v2_rc
if echo "$version1" | grep -q -- "-rc-"; then
v1_base=$(echo "$version1" | cut -d'-' -f1)
v1_rc=$(echo "$version1" | cut -d'-' -f3)
else
v1_base="$version1"
v1_rc="9999" # Treat non-rc as higher than any rc
fi
if echo "$version2" | grep -q -- "-rc-"; then
v2_base=$(echo "$version2" | cut -d'-' -f1)
v2_rc=$(echo "$version2" | cut -d'-' -f3)
else
v2_base="$version2"
v2_rc="9999" # Treat non-rc as higher than any rc
fi
# Compare base versions using sort -V (version sort)
local base_cmp
if [ "$v1_base" = "$v2_base" ]; then
base_cmp="0"
elif printf '%s\n%s\n' "$v1_base" "$v2_base" | sort -V | head -n1 | grep -q "^$v1_base$"; then
base_cmp="-1" # v1_base < v2_base
else
base_cmp="1" # v1_base > v2_base
fi
# If base versions are equal, compare RC numbers
if [ "$base_cmp" = "0" ]; then
if [ "$v1_rc" -lt "$v2_rc" ]; then
echo "-1"
elif [ "$v1_rc" -gt "$v2_rc" ]; then
echo "1"
else
echo "0"
fi
else
echo "$base_cmp"
fi
}
main() {
OS=
if type uname >/dev/null 2>&1; then
case "$(uname)" in
Darwin)
OS="darwin"
echo "macos is not supported yet, follow https://github.com/leptonai/gpud/blob/main/docs/INSTALL.md to build by yourself"
exit 1
;;
Linux)
OS="linux"
;;
*)
echo "OS $(uname) is not supported, follow https://github.com/leptonai/gpud/blob/main/docs/INSTALL.md to build by yourself"
exit 1
;;
esac
fi
ARCH=
if type uname >/dev/null 2>&1; then
case "$(uname -m)" in
x86_64)
ARCH="amd64"
;;
arm64|aarch64)
ARCH="arm64"
;;
*)
echo "Processor $(uname -m) is not supported, follow https://github.com/leptonai/gpud/blob/main/docs/INSTALL.md to build by yourself"
exit 1
;;
esac
fi
CAN_ROOT=
SUDO=
if [ "$(id -u)" = 0 ]; then
CAN_ROOT=1
SUDO=""
elif type sudo >/dev/null; then
CAN_ROOT=1
SUDO="sudo"
elif type doas >/dev/null; then
CAN_ROOT=1
SUDO="doas"
fi
if [ "$CAN_ROOT" != "1" ]; then
echo "This installer needs to run commands as root."
echo "We tried looking for 'sudo' and 'doas', but couldn't find them."
echo "Either re-run this script as root, or set up sudo/doas."
exit 1
fi
TRACK="${TRACK:-unstable}"
if [ -n "$1" ]; then
APP_VERSION="$1"
else
# e.g., https://pkg.gpud.dev/unstable_latest.txt
APP_VERSION=$(curl -fsSL https://pkg.gpud.dev/"$TRACK"_latest.txt)
fi
if ! type lsb_release >/dev/null 2>&1; then
# Try /etc/os-release first
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_NAME=$(echo "$ID" | tr '[:upper:]' '[:lower:]')
OS_VERSION=$(echo "$VERSION_ID" | tr -d '"')
# Fall back to /etc/lsb-release if it exists
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS_NAME=$(echo "${DISTRIB_ID:-}" | tr '[:upper:]' '[:lower:]')
OS_VERSION=$(echo "${DISTRIB_RELEASE:-}" | tr -d '"')
else
echo "Could not determine OS version. Neither lsb_release command, /etc/os-release, nor /etc/lsb-release were found."
exit 1
fi
else
# e.g., ubuntu22.04, ubuntu24.04
OS_NAME=$(lsb_release -i -s | tr '[:upper:]' '[:lower:]' 2>/dev/null)
OS_VERSION=$(lsb_release -r -s 2>/dev/null || echo "")
fi
if [ "$OS_NAME" = "ubuntu" ]; then
case "$OS_VERSION" in
22.04|24.04)
OS_DISTRO="_${OS_NAME}${OS_VERSION}"
;;
*)
echo "GPUd $APP_VERSION supports Ubuntu 22.04 and 24.04. Current version $OS_VERSION is not supported."
exit 1
;;
esac
elif [ "$OS_NAME" = "amzn" ]; then
case "$OS_VERSION" in
2|2023)
OS_DISTRO="_${OS_NAME}${OS_VERSION}"
;;
*)
echo "GPUd $APP_VERSION supports Amazon Linux 2 and 2023. Current version $OS_VERSION is not supported."
exit 1
;;
esac
else
OS_DISTRO=""
fi
FILEBASE=gpud_"$APP_VERSION"_"$OS"_"$ARCH""$OS_DISTRO"
FILENAME=$FILEBASE.tgz
if [ -e "$FILENAME" ]; then
echo "file '$FILENAME' already exists"
exit 1
fi
# same release always same contents
# thus safe to remove in case of previous installation failure
DIR=/tmp/$FILEBASE
rm -rf "$DIR"
mkdir "$DIR"
DLPATH=/tmp/"$FILENAME"
if ! curl -fsSL "https://pkg.gpud.dev/$FILENAME" -o "$DLPATH" 2>/tmp/gpud_curl_error.log; then
echo "failed to download file from 'https://pkg.gpud.dev/$FILENAME'"
echo "\nerror message:"
cat /tmp/gpud_curl_error.log
rm -f "$DLPATH" /tmp/gpud_curl_error.log
exit 1
fi
rm -f /tmp/gpud_curl_error.log
tar xzf "$DLPATH" -C "$DIR"
# some os distros have "/usr/sbin" as read-only
# we want to use "/usr/local/bin" instead
# ref. https://fedoraproject.org/wiki/Changes/Unify_bin_and_sbin
BIN_PATH="/usr/local/bin"
# only "$APP_VERSION" after >= v0.5.0-rc-34, >= v0.5 supports "/usr/local/bin/gpud"
# "v0.5.0-rc-33" does not support "/usr/local/bin/gpud", thus we need to use "/usr/sbin/gpud"
# ref. https://github.com/leptonai/gpud/pull/846
# check if version supports /usr/local/bin
# Supported: >= v0.5.0-rc-34
if [ "$(version_compare "$APP_VERSION" "0.5.0-rc-34")" -ge 0 ]; then
BIN_PATH="/usr/local/bin"
else
BIN_PATH="/usr/sbin"
fi
$SUDO cp -f "$DIR"/gpud $BIN_PATH
echo "installed gpud version $APP_VERSION in the path $BIN_PATH/gpud"
rm /tmp/"$FILENAME"
rm -rf "$DIR"
}
main "$@"