-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_olscli.sh
More file actions
executable file
·189 lines (155 loc) · 3.79 KB
/
Copy pathinstall_olscli.sh
File metadata and controls
executable file
·189 lines (155 loc) · 3.79 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
#!/usr/bin/env bash
set -euo pipefail
REPO="${REPO:-Code-Egg/ols-cli}"
BIN_NAME="ols"
INSTALL_DIR="/usr/local/bin"
TMP_FILE=""
log() {
printf '\033[1;34m[ols-installer]\033[0m %s\n' "$1"
}
warn() {
printf '\033[1;33m[ols-installer]\033[0m %s\n' "$1"
}
err() {
printf '\033[1;31m[ols-installer]\033[0m %s\n' "$1" >&2
}
cleanup() {
if [ -n "${TMP_FILE:-}" ]; then
rm -f "$TMP_FILE"
fi
}
run_as_root() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
return
fi
if command -v sudo >/dev/null 2>&1; then
sudo "$@"
return
fi
err "this step requires root privileges; re-run as root or install sudo"
exit 1
}
get_os_id_like() {
if [ ! -r /etc/os-release ]; then
echo ""
return
fi
# shellcheck disable=SC1091
. /etc/os-release
echo "${ID:-} ${ID_LIKE:-}"
}
is_debian_or_ubuntu() {
case " $(get_os_id_like) " in
*" debian "*|*" ubuntu "*)
return 0
;;
esac
return 1
}
is_rhel_family() {
case " $(get_os_id_like) " in
*" rhel "*|*" centos "*|*" rocky "*|*" alma "*|*" fedora "*)
return 0
;;
esac
return 1
}
install_packages() {
if command -v apt-get >/dev/null 2>&1 && is_debian_or_ubuntu; then
run_as_root apt-get update
run_as_root env DEBIAN_FRONTEND=noninteractive apt-get install -y "$@"
return
fi
if command -v dnf >/dev/null 2>&1 && is_rhel_family; then
run_as_root dnf install -y "$@"
return
fi
if command -v yum >/dev/null 2>&1 && is_rhel_family; then
run_as_root yum install -y "$@"
return
fi
err "unable to auto-install packages on this distro/package manager"
exit 1
}
ensure_go_installed() {
if command -v go >/dev/null 2>&1; then
log "Go detected: $(go version)"
return
fi
if is_debian_or_ubuntu; then
log "Go not found. Installing golang-go via apt..."
install_packages golang-go
log "Go installed: $(go version)"
return
fi
if is_rhel_family; then
log "Go not found. Installing golang via dnf/yum..."
install_packages golang
log "Go installed: $(go version)"
return
fi
err "Go is not installed. Automatic installation is supported for Debian/Ubuntu/CentOS-family systems."
err "Please install Go manually, then re-run this installer."
exit 1
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64)
echo "amd64"
;;
aarch64|arm64)
echo "arm64"
;;
*)
err "unsupported architecture: $(uname -m). Supported: amd64, arm64"
exit 1
;;
esac
}
ensure_downloader() {
if command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1; then
return
fi
if is_debian_or_ubuntu; then
log "Neither curl nor wget found. Installing curl via apt..."
install_packages curl
return
fi
if is_rhel_family; then
log "Neither curl nor wget found. Installing curl via dnf/yum..."
install_packages curl
return
fi
err "either curl or wget is required"
exit 1
}
main() {
if [ "$(uname -s)" != "Linux" ]; then
err "this installer supports Linux only"
exit 1
fi
ensure_go_installed
ensure_downloader
local arch url
arch="$(detect_arch)"
url="https://github.com/${REPO}/releases/latest/download/${BIN_NAME}-linux-${arch}"
TMP_FILE="$(mktemp)"
log "Downloading ${BIN_NAME} (${arch}) from latest release..."
if command -v curl >/dev/null 2>&1; then
curl -fsSL --retry 3 --retry-delay 1 "$url" -o "$TMP_FILE"
else
wget -qO "$TMP_FILE" "$url"
fi
if [ ! -s "$TMP_FILE" ]; then
err "download failed or produced an empty file"
exit 1
fi
run_as_root install -d "$INSTALL_DIR"
run_as_root install -m 0755 "$TMP_FILE" "${INSTALL_DIR}/${BIN_NAME}"
run_as_root chmod u+w "${INSTALL_DIR}/${BIN_NAME}"
log "Installed ${BIN_NAME} to ${INSTALL_DIR}/${BIN_NAME}"
log "Run '${BIN_NAME} --help' to verify"
}
trap cleanup EXIT
main "$@"