forked from rancher/rancherd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·436 lines (384 loc) · 12.2 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/bin/sh
set -e
set -o noglob
# Usage:
# curl ... | ENV_VAR=... sh -
# or
# ENV_VAR=... ./install.sh
#
# Environment variables:
# - RANCHERD_*
# Environment variables which begin with RANCHERD_ will be preserved for the
# systemd service to use. Setting RANCHERD_URL without explicitly setting
# a systemd exec command will default the command to "agent", and we
# enforce that RANCHERD_TOKEN or RANCHERD_CLUSTER_SECRET is also set.
#
# - INSTALL_RANCHERD_SKIP_DOWNLOAD
# If set to true will not download rancherd hash or binary.
#
# - INSTALL_RANCHERD_FORCE_RESTART
# If set to true will always restart the rancherd service
#
# - INSTALL_RANCHERD_SKIP_ENABLE
# If set to true will not enable or start rancherd service.
#
# - INSTALL_RANCHERD_SKIP_START
# If set to true will not start rancherd service.
#
# - INSTALL_RANCHERD_VERSION
# Version of rancherd to download from github. Will attempt to download from the
# stable channel if not specified.
#
# - INSTALL_RANCHERD_BIN_DIR
# Directory to install rancherd binary, links, and uninstall script to, or use
# /usr/local/bin as the default
#
# - INSTALL_RANCHERD_BIN_DIR_READ_ONLY
# If set to true will not write files to INSTALL_RANCHERD_BIN_DIR, forces
# setting INSTALL_RANCHERD_SKIP_DOWNLOAD=true
#
# - INSTALL_RANCHERD_SYSTEMD_DIR
# Directory to install systemd service and environment files to, or use
# /etc/systemd/system as the default
#
GITHUB_URL=https://github.com/rancher/rancherd/releases
DOWNLOADER=
# --- helper functions for logs ---
info()
{
echo '[INFO] ' "$@"
}
warn()
{
echo '[WARN] ' "$@" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
# --- fatal if no systemd ---
verify_system() {
if [ ! -d /run/systemd ]; then
fatal 'Can not find systemd to use as a process supervisor for rancherd'
fi
}
# --- add quotes to command arguments ---
quote() {
for arg in "$@"; do
printf '%s\n' "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
done
}
# --- add indentation and trailing slash to quoted args ---
quote_indent() {
printf ' \\\n'
for arg in "$@"; do
printf '\t%s \\\n' "$(quote "$arg")"
done
}
# --- escape most punctuation characters, except quotes, forward slash, and space ---
escape() {
printf '%s' "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
}
# --- escape double quotes ---
escape_dq() {
printf '%s' "$@" | sed -e 's/"/\\"/g'
}
# --- define needed environment variables ---
setup_env() {
SYSTEM_NAME=rancherd
# --- check for invalid characters in system name ---
valid_chars=$(printf '%s' "${SYSTEM_NAME}" | sed -e 's/[][!#$%&()*;<=>?\_`{|}/[:space:]]/^/g;' )
if [ "${SYSTEM_NAME}" != "${valid_chars}" ]; then
invalid_chars=$(printf '%s' "${valid_chars}" | sed -e 's/[^^]/ /g')
fatal "Invalid characters for system name:
${SYSTEM_NAME}
${invalid_chars}"
fi
# --- use sudo if we are not already root ---
SUDO=sudo
if [ $(id -u) -eq 0 ]; then
SUDO=
fi
# --- use binary install directory if defined or create default ---
if [ -n "${INSTALL_RANCHERD_BIN_DIR}" ]; then
BIN_DIR=${INSTALL_RANCHERD_BIN_DIR}
else
# --- use /usr/local/bin if root can write to it, otherwise use /opt/bin if it exists
BIN_DIR=/usr/local/bin
if ! $SUDO sh -c "touch ${BIN_DIR}/rancherd-ro-test && rm -rf ${BIN_DIR}/rancherd-ro-test"; then
if [ -d /opt/bin ]; then
BIN_DIR=/opt/bin
fi
fi
fi
# --- use systemd directory if defined or create default ---
if [ -n "${INSTALL_RANCHERD_SYSTEMD_DIR}" ]; then
SYSTEMD_DIR="${INSTALL_RANCHERD_SYSTEMD_DIR}"
else
SYSTEMD_DIR=/etc/systemd/system
fi
# --- set related files from system name ---
SERVICE_RANCHERD=${SYSTEM_NAME}.service
# --- use service or environment location depending on systemd ---
FILE_RANCHERD_SERVICE=${SYSTEMD_DIR}/${SERVICE_RANCHERD}
FILE_RANCHERD_ENV=${SYSTEMD_DIR}/${SERVICE_RANCHERD}.env
# --- get hash of config & exec for currently installed rancherd ---
PRE_INSTALL_HASHES=$(get_installed_hashes)
# --- if bin directory is read only skip download ---
if [ "${INSTALL_RANCHERD_BIN_DIR_READ_ONLY}" = true ]; then
INSTALL_RANCHERD_SKIP_DOWNLOAD=true
fi
}
# --- check if skip download environment variable set ---
can_skip_download() {
if [ "${INSTALL_RANCHERD_SKIP_DOWNLOAD}" != true ]; then
return 1
fi
}
# --- verify an executable rancherd binary is installed ---
verify_rancherd_is_executable() {
if [ ! -x ${BIN_DIR}/rancherd ]; then
fatal "Executable rancherd binary not found at ${BIN_DIR}/rancherd"
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=-${ARCH}
;;
x86_64)
ARCH=amd64
SUFFIX=-amd64
;;
#arm64)
# ARCH=arm64
# SUFFIX=-${ARCH}
# ;;
#aarch64)
# ARCH=arm64
# SUFFIX=-${ARCH}
# ;;
#arm*)
# ARCH=arm
# SUFFIX=-${ARCH}hf
# ;;
*)
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
# Set verified executable as our downloader program and return success
DOWNLOADER=$1
return 0
}
# --- create temporary directory and cleanup when done ---
setup_tmp() {
TMP_DIR=$(mktemp -d -t rancherd-install.XXXXXXXXXX)
TMP_HASH=${TMP_DIR}/rancherd.hash
TMP_BIN=${TMP_DIR}/rancherd.bin
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf ${TMP_DIR}
exit $code
}
trap cleanup INT EXIT
}
# --- use desired rancherd version if defined or find version from channel ---
get_release_version() {
if [ -n "${INSTALL_RANCHERD_VERSION}" ]; then
VERSION_RANCHERD=${INSTALL_RANCHERD_VERSION}
else
version_url="${GITHUB_URL}/latest"
case $DOWNLOADER in
curl)
VERSION_RANCHERD=$(curl -w '%{url_effective}' -L -s -S ${version_url} -o /dev/null | sed -e 's|.*/||')
;;
wget)
VERSION_RANCHERD=$(wget -SqO /dev/null ${version_url} 2>&1 | grep -i Location | sed -e 's|.*/||')
;;
*)
fatal "Incorrect downloader executable '$DOWNLOADER'"
;;
esac
fi
info "Using ${VERSION_RANCHERD} as release"
}
# --- download from github url ---
download() {
[ $# -eq 2 ] || fatal 'download needs exactly 2 arguments'
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 hash from github url ---
download_hash() {
HASH_URL=${GITHUB_URL}/download/${VERSION_RANCHERD}/sha256sum-${ARCH}.txt
info "Downloading hash ${HASH_URL}"
download ${TMP_HASH} ${HASH_URL}
HASH_EXPECTED=$(grep " rancherd${SUFFIX}$" ${TMP_HASH})
HASH_EXPECTED=${HASH_EXPECTED%%[[:blank:]]*}
}
# --- check hash against installed version ---
installed_hash_matches() {
if [ -x ${BIN_DIR}/rancherd ]; then
HASH_INSTALLED=$(sha256sum ${BIN_DIR}/rancherd)
HASH_INSTALLED=${HASH_INSTALLED%%[[:blank:]]*}
if [ "${HASH_EXPECTED}" = "${HASH_INSTALLED}" ]; then
return
fi
fi
return 1
}
# --- download binary from github url ---
download_binary() {
BIN_URL=${GITHUB_URL}/download/${VERSION_RANCHERD}/rancherd${SUFFIX}
info "Downloading binary ${BIN_URL}"
download ${TMP_BIN} ${BIN_URL}
}
# --- verify downloaded binary hash ---
verify_binary() {
info "Verifying binary download"
HASH_BIN=$(sha256sum ${TMP_BIN})
HASH_BIN=${HASH_BIN%%[[:blank:]]*}
if [ "${HASH_EXPECTED}" != "${HASH_BIN}" ]; then
fatal "Download sha256 does not match ${HASH_EXPECTED}, got ${HASH_BIN}"
fi
}
# --- setup permissions and move binary to system directory ---
setup_binary() {
chmod 755 ${TMP_BIN}
info "Installing rancherd to ${BIN_DIR}/rancherd"
$SUDO chown root:root ${TMP_BIN}
$SUDO mv -f ${TMP_BIN} ${BIN_DIR}/rancherd
}
# --- download and verify rancherd ---
download_and_verify() {
if can_skip_download; then
info 'Skipping rancherd download and verify'
verify_rancherd_is_executable
return
fi
setup_verify_arch
verify_downloader curl || verify_downloader wget || fatal 'Can not find curl or wget for downloading files'
setup_tmp
get_release_version
download_hash
if installed_hash_matches; then
info 'Skipping binary downloaded, installed rancherd matches hash'
return
fi
download_binary
verify_binary
setup_binary
}
# --- disable current service if loaded --
systemd_disable() {
$SUDO systemctl disable ${SYSTEM_NAME} >/dev/null 2>&1 || true
$SUDO rm -f /etc/systemd/system/${SERVICE_RANCHERD} || true
$SUDO rm -f /etc/systemd/system/${SERVICE_RANCHERD}.env || true
}
# --- capture current env and create file containing rancherd_ variables ---
create_env_file() {
info "env: Creating environment file ${FILE_RANCHERD_ENV}"
$SUDO touch ${FILE_RANCHERD_ENV}
$SUDO chmod 0600 ${FILE_RANCHERD_ENV}
env | grep '^RANCHERD_' | $SUDO tee ${FILE_RANCHERD_ENV} >/dev/null
env | grep -Ei '^(NO|HTTP|HTTPS)_PROXY' | $SUDO tee -a ${FILE_RANCHERD_ENV} >/dev/null
}
# --- write systemd service file ---
create_systemd_service_file() {
info "systemd: Creating service file ${FILE_RANCHERD_SERVICE}"
$SUDO tee ${FILE_RANCHERD_SERVICE} >/dev/null << EOF
[Unit]
Description=Rancher Bootstrap
Documentation=https://github.com/rancher/rancherd
Wants=network-online.target
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Type=oneshot
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-${FILE_RANCHERD_ENV}
KillMode=process
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
ExecStart=${BIN_DIR}/rancherd bootstrap
EOF
}
# --- get hashes of the current rancherd bin and service files
get_installed_hashes() {
$SUDO sha256sum ${BIN_DIR}/rancherd ${FILE_RANCHERD_SERVICE} ${FILE_RANCHERD_ENV} 2>&1 || true
}
# --- enable and start systemd service ---
systemd_enable() {
info "systemd: Enabling ${SYSTEM_NAME} unit"
$SUDO systemctl enable ${FILE_RANCHERD_SERVICE} >/dev/null
$SUDO systemctl daemon-reload >/dev/null
}
systemd_start() {
info "systemd: Starting ${SYSTEM_NAME}"
$SUDO systemctl restart --no-block ${SYSTEM_NAME}
info "Run \"journalctl -u ${SYSTEM_NAME} -f\" to watch logs"
}
# --- enable and start openrc service ---
openrc_enable() {
info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel"
$SUDO rc-update add ${SYSTEM_NAME} default >/dev/null
}
openrc_start() {
info "openrc: Starting ${SYSTEM_NAME}"
$SUDO ${FILE_RANCHERD_SERVICE} restart
}
# --- startup systemd or openrc service ---
service_enable_and_start() {
[ "${INSTALL_RANCHERD_SKIP_ENABLE}" = true ] && return
systemd_enable
[ "${INSTALL_RANCHERD_SKIP_START}" = true ] && return
POST_INSTALL_HASHES=$(get_installed_hashes)
if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ] && [ "${INSTALL_RANCHERD_FORCE_RESTART}" != true ]; then
info 'No change detected so skipping service start'
return
fi
systemd_start
return 0
}
# --- re-evaluate args to include env command ---
eval set -- $(escape "${INSTALL_RANCHERD_EXEC}") $(quote "$@")
# --- run the install process --
{
verify_system
setup_env "$@"
download_and_verify
systemd_disable
create_env_file
create_systemd_service_file
service_enable_and_start
}