-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·256 lines (204 loc) · 8.49 KB
/
install.sh
File metadata and controls
executable file
·256 lines (204 loc) · 8.49 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
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
#!/bin/sh
# SentinelGate installer
# Usage: curl -sSfL https://raw.githubusercontent.com/Sentinel-Gate/Sentinelgate/main/install.sh | sh
#
# Environment variables:
# VERSION - specific version to install (e.g., v1.1.0). Default: latest
# INSTALL_DIR - installation directory. Default: /usr/local/bin
set -e
REPO="Sentinel-Gate/Sentinelgate"
BINARY_NAME="sentinel-gate"
DEFAULT_INSTALL_DIR="/usr/local/bin"
FALLBACK_INSTALL_DIR="${HOME}/.local/bin"
# ── Cleanup ──────────────────────────────────────────────────────────────────
TMPDIR_INSTALL=""
cleanup() {
if [ -n "${TMPDIR_INSTALL}" ] && [ -d "${TMPDIR_INSTALL}" ]; then
rm -rf "${TMPDIR_INSTALL}"
fi
}
trap cleanup EXIT
# ── Colors ───────────────────────────────────────────────────────────────────
setup_colors() {
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
RESET='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
BOLD=''
RESET=''
fi
}
# ── Logging ──────────────────────────────────────────────────────────────────
info() {
printf "${BLUE}=>${RESET} %s\n" "$1"
}
success() {
printf "${GREEN}=>${RESET} %s\n" "$1"
}
warn() {
printf "${YELLOW}WARNING:${RESET} %s\n" "$1" >&2
}
error() {
printf "${RED}ERROR:${RESET} %s\n" "$1" >&2
exit 1
}
# ── Dependency checks ────────────────────────────────────────────────────────
check_deps() {
if command -v curl >/dev/null 2>&1; then
HTTP_CLIENT="curl"
elif command -v wget >/dev/null 2>&1; then
HTTP_CLIENT="wget"
else
error "Either curl or wget is required. Please install one and try again."
fi
if ! command -v tar >/dev/null 2>&1; then
error "tar is required. Please install it and try again."
fi
if command -v sha256sum >/dev/null 2>&1; then
SHA_CMD="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
SHA_CMD="shasum -a 256"
else
error "sha256sum or shasum is required. Please install one and try again."
fi
}
# ── HTTP helpers ─────────────────────────────────────────────────────────────
http_get() {
url="$1"
output="$2"
if [ "${HTTP_CLIENT}" = "curl" ]; then
curl -sSfL -o "${output}" "${url}"
else
wget -q -O "${output}" "${url}"
fi
}
http_get_stdout() {
url="$1"
if [ "${HTTP_CLIENT}" = "curl" ]; then
curl -sSfL "${url}"
else
wget -q -O - "${url}"
fi
}
# ── OS / Architecture detection ──────────────────────────────────────────────
detect_os() {
os="$(uname -s)"
case "${os}" in
Linux) OS="linux" ;;
Darwin) OS="darwin" ;;
*) error "Unsupported operating system: ${os}. Only Linux and macOS are supported." ;;
esac
}
detect_arch() {
arch="$(uname -m)"
case "${arch}" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) error "Unsupported architecture: ${arch}. Only amd64 and arm64 are supported." ;;
esac
}
# ── Version resolution ───────────────────────────────────────────────────────
resolve_version() {
if [ -n "${VERSION:-}" ]; then
info "Using specified version: ${VERSION}"
return
fi
info "Fetching latest release version..."
# Try stable release first, fall back to most recent (includes pre-releases)
api_url="https://api.github.com/repos/${REPO}/releases/latest"
response="$(http_get_stdout "${api_url}" 2>/dev/null)" || {
info "No stable release found, checking pre-releases..."
api_url="https://api.github.com/repos/${REPO}/releases"
response="$(http_get_stdout "${api_url}")" || error "Failed to fetch releases from GitHub API."
}
# Extract tag_name without jq (POSIX-compatible)
VERSION="$(printf '%s' "${response}" | tr ',' '\n' | grep '"tag_name"' | head -1 | cut -d'"' -f4)"
if [ -z "${VERSION}" ]; then
error "Could not determine latest version from GitHub API response."
fi
info "Latest version: ${VERSION}"
}
# ── Download and verify ──────────────────────────────────────────────────────
download_and_verify() {
TMPDIR_INSTALL="$(mktemp -d)"
archive_name="${BINARY_NAME}_${OS}_${ARCH}.tar.gz"
archive_url="https://github.com/${REPO}/releases/download/${VERSION}/${archive_name}"
checksums_url="https://github.com/${REPO}/releases/download/${VERSION}/checksums.txt"
info "Downloading ${archive_name}..."
http_get "${archive_url}" "${TMPDIR_INSTALL}/${archive_name}" || \
error "Failed to download archive from ${archive_url}"
info "Downloading checksums..."
http_get "${checksums_url}" "${TMPDIR_INSTALL}/checksums.txt" || \
error "Failed to download checksums from ${checksums_url}"
info "Verifying SHA-256 checksum..."
expected="$(grep "${archive_name}" "${TMPDIR_INSTALL}/checksums.txt" | cut -d' ' -f1)"
if [ -z "${expected}" ]; then
error "Could not find checksum for ${archive_name} in checksums.txt"
fi
actual="$(cd "${TMPDIR_INSTALL}" && ${SHA_CMD} "${archive_name}" | cut -d' ' -f1)"
if [ "${expected}" != "${actual}" ]; then
error "Checksum mismatch for ${archive_name}\n Expected: ${expected}\n Actual: ${actual}"
fi
success "Checksum verified."
}
# ── Install ──────────────────────────────────────────────────────────────────
install_binary() {
info "Extracting ${BINARY_NAME}..."
tar -xzf "${TMPDIR_INSTALL}/${BINARY_NAME}_${OS}_${ARCH}.tar.gz" -C "${TMPDIR_INSTALL}"
if [ ! -f "${TMPDIR_INSTALL}/${BINARY_NAME}" ]; then
error "Binary '${BINARY_NAME}' not found in archive."
fi
# Determine install directory
install_dir="${INSTALL_DIR:-}"
if [ -z "${install_dir}" ]; then
if [ -d "${DEFAULT_INSTALL_DIR}" ] && [ -w "${DEFAULT_INSTALL_DIR}" ]; then
install_dir="${DEFAULT_INSTALL_DIR}"
else
install_dir="${FALLBACK_INSTALL_DIR}"
mkdir -p "${install_dir}"
warn "${DEFAULT_INSTALL_DIR} is not writable. Installing to ${install_dir}"
case ":${PATH}:" in
*":${install_dir}:"*) ;;
*) warn "Add ${install_dir} to your PATH: export PATH=\"\${PATH}:${install_dir}\"" ;;
esac
fi
else
if [ ! -d "${install_dir}" ]; then
mkdir -p "${install_dir}"
fi
fi
cp "${TMPDIR_INSTALL}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
chmod +x "${install_dir}/${BINARY_NAME}"
INSTALLED_PATH="${install_dir}/${BINARY_NAME}"
}
# ── Main ─────────────────────────────────────────────────────────────────────
main() {
setup_colors
printf '\n%sSentinelGate Installer%s\n\n' "${BOLD}" "${RESET}"
check_deps
detect_os
detect_arch
resolve_version
download_and_verify
install_binary
printf "\n"
success "SentinelGate ${VERSION} installed successfully!"
info "Binary: ${INSTALLED_PATH}"
info "Run '${BOLD}sentinel-gate start${RESET}' to get started."
CA_CERT="${HOME}/.sentinelgate/ca-cert.pem"
if [ -f "${CA_CERT}" ]; then
printf "\n"
info "Found existing CA certificate. To trust it system-wide, run:"
info " ${INSTALLED_PATH} trust-ca"
fi
printf "\n"
}
main