-
Notifications
You must be signed in to change notification settings - Fork 2k
/
install.sh
197 lines (183 loc) · 4.95 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
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
#!/bin/sh
# Copyright © 2023 sealos.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
set -o noglob
# Usage:
# curl ... | ENV_VAR=... sh -
# curl -sfL https://raw.githubusercontent.com/labring/sealos/main/scripts/install.sh | sh -s v4.1.8-alpha2 labring/sealos
#
FILE_NAME=sealos
BIN_DIR=/usr/bin
# --- helper functions for logs ---
info()
{
echo '[INFO] ' "$@"
}
warn()
{
echo '[WARN] ' "$@" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
# --- ensures $SEALOS_URL is empty or begins with https://, exiting fatally otherwise ---
verify_url() {
case "${SEALOS_URL}" in
"")
;;
https://*)
;;
http://*)
;;
*)
fatal "Only https:// URLs are supported for SEALOS_URL (have ${SEALOS_URL})"
;;
esac
}
# --- verify an executable sealos binary is installed ---
verify_is_executable() {
if [ ! -x ${BIN_DIR}/sealos ]; then
fatal "Executable sealos binary not found at ${BIN_DIR}/sealos"
fi
}
# --- set arch and suffix, fatal if architecture not supported ---
setup_verify_arch() {
if [ -z "$ARCH" ]; then
ARCH=$(uname -m)
fi
case $ARCH in
amd64)
ARCH=amd64
SUFFIX=
;;
x86_64)
ARCH=amd64
SUFFIX=
;;
arm64)
ARCH=arm64
SUFFIX=-${ARCH}
;;
aarch64)
ARCH=arm64
SUFFIX=-${ARCH}
;;
*)
fatal "Unsupported architecture $ARCH"
esac
}
# --- verify existence of network downloader executable ---
verify_downloader() {
# Return failure if it doesn't exist or is no executable
[ -x "$(command -v $1)" ] || return 1
DOWNLOADER=$1
# Set verified executable as our downloader program and return success
DOWNLOADER_PREFIX=https://github.com/${OWN_REPO}/releases/download/
if [ -n "$PROXY_PREFIX" ]; then
DOWNLOADER_PREFIX="${PROXY_PREFIX%/}/${DOWNLOADER_PREFIX}"
fi
DOWNLOADER_URL=${DOWNLOADER_PREFIX}${VERSION}/${FILE_NAME}_${VERSION##v}_linux_${ARCH}.tar.gz
return 0
}
get_release_version() {
VERSION=$1
# If the incoming version number is latest, get the latest version number from github
if [ "$VERSION" = "latest" ]; then
VERSION=$(curl -s https://api.github.com/repos/${OWN_REPO}/releases/latest | grep tag_name | cut -d '"' -f 4)
fi
info "Using ${VERSION} as release"
OWN_REPO=$2
if [ -z "$OWN_REPO" ]; then
warn "OWN_REPO is empty, using default repo: labring/sealos"
OWN_REPO=labring/sealos
fi
info "Using ${OWN_REPO} as your repo"
}
# --- download from github url ---
download() {
[ $# -eq 2 ] || fatal 'download needs exactly 2 arguments'
info "Downloading sealos, waiting..."
case $DOWNLOADER in
curl)
curl -o $1 -sfL $2
;;
wget)
wget -qO $1 $2
;;
*)
fatal "Incorrect executable '$DOWNLOADER'"
;;
esac
# Abort if download command failed
[ $? -eq 0 ] || fatal 'Download failed'
}
# --- download binary from github url ---
download_binary() {
info "Downloading tar ${DOWNLOADER} ${DOWNLOADER_URL}"
download ${TMP_TAR} ${DOWNLOADER_URL}
}
setup_tmp() {
TMP_DIR=$(mktemp -d -t sealos-install.XXXXXXXXXX)
TMP_TAR=${TMP_DIR}/sealos.tar.gz
TMP_BIN=${TMP_DIR}/sealos
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf ${TMP_DIR}
if [ $code -ne 0 ]; then
warn "Failed to install sealos"
fi
exit $code
}
trap cleanup INT EXIT
}
cleanup_tmp() {
rm -rf ${TMP_DIR}
}
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf ${TMP_DIR}
exit $code
}
# --- setup permissions and move binary to system directory ---
setup_binary() {
cd ${TMP_DIR}
chmod 755 ${TMP_TAR}
tar -zxvf ${TMP_TAR} ${FILE_NAME}
chmod 755 ${FILE_NAME}
info "Installing sealos to ${BIN_DIR}/${FILE_NAME}"
sudo chown $(whoami):$(whoami) ${TMP_BIN}
sudo mv -f ${TMP_BIN} ${BIN_DIR}/${FILE_NAME}
}
verify_binary() {
${FILE_NAME} version
[ $? -eq 0 ] || fatal 'Download failed'
}
# --- run the install process --
{
setup_verify_arch
get_release_version $1 $2
verify_downloader curl || verify_downloader wget || fatal 'Can not find curl or wget for downloading files'
setup_tmp
download_binary
setup_binary
verify_binary
cleanup_tmp
}