forked from flipt-io/flipt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·327 lines (282 loc) · 8.19 KB
/
install.sh
File metadata and controls
executable file
·327 lines (282 loc) · 8.19 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
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
#!/bin/sh
#
# _________ __
# / ____/ (_)___ / /_
# / /_ / / / __ \/ __/
# / __/ / / / /_/ / /_
# /_/ /_/_/ .___/\__/
# /_/
#
# Flipt installer
#
# Usage:
# curl -fsSL https://github.com/flipt-io/flipt/raw/main/install.sh | sh
# curl -fsSL https://github.com/flipt-io/flipt/raw/main/install.sh | sh -s -- v1.45.0
set -e
# Configuration
REPO="flipt-io/flipt"
RELEASE_URL_BASE="https://api.github.com/repos/${REPO}/releases"
DOWNLOAD_URL_BASE="https://download.flipt.io/flipt"
# Color and formatting
if command -v tput >/dev/null 2>&1 && [ -t 1 ]; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)
else
RED=""
GREEN=""
YELLOW=""
RESET=""
fi
# Logging functions
log_info() {
printf "%s[INFO]%s %s\n" "${GREEN}" "${RESET}" "$1" >&2
}
log_warn() {
printf "%s[WARN]%s %s\n" "${YELLOW}" "${RESET}" "$1" >&2
}
log_error() {
printf "%s[ERROR]%s %s\n" "${RED}" "${RESET}" "$1" >&2
}
# Detect OS
uname_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
msys_nt*) os="windows" ;;
mingw*) os="windows" ;;
cygwin*) os="windows" ;;
win*) os="windows" ;;
esac
echo "$os"
}
# Detect architecture
uname_arch() {
arch=$(uname -m)
case $arch in
x86_64) arch="x86_64" ;;
x64) arch="x86_64" ;;
i686) arch="386" ;;
i386) arch="386" ;;
aarch64) arch="arm64" ;;
armv5*) arch="armv5" ;;
armv6*) arch="armv6" ;;
armv7*) arch="armv7" ;;
esac
echo "${arch}"
}
# Check if OS is supported
uname_os_check() {
os=$(uname_os)
case "$os" in
darwin) return 0 ;;
linux) return 0 ;;
windows) return 0 ;;
esac
log_error "OS $os is not supported"
log_error "Please open an issue: https://github.com/flipt-io/flipt/issues/new"
return 1
}
# Check if architecture is supported
uname_arch_check() {
arch=$(uname_arch)
case "$arch" in
x86_64) return 0 ;;
arm64) return 0 ;;
esac
log_error "Architecture $arch is not supported"
log_error "Please open an issue: https://github.com/flipt-io/flipt/issues/new"
return 1
}
# Get latest v1 version from GitHub
get_latest_version() {
tmpfile=$(mktemp)
log_info "Fetching latest v1 release information..."
# Use /releases endpoint to get all releases and filter for v1
url="${RELEASE_URL_BASE}"
if [ -n "$GITHUB_TOKEN" ]; then
if ! curl -fsSL -H "Authorization: Bearer $GITHUB_TOKEN" "$url" > "$tmpfile" 2>/dev/null; then
rm -f "$tmpfile"
log_error "Failed to fetch releases from GitHub API"
log_error "You can specify a version manually: curl -fsSL https://github.com/flipt-io/flipt/raw/main/install.sh | sh -s -- v1.45.0"
return 1
fi
else
if ! curl -fsSL "$url" > "$tmpfile" 2>/dev/null; then
rm -f "$tmpfile"
log_error "Failed to fetch releases from GitHub API"
log_error "You can specify a version manually: curl -fsSL https://github.com/flipt-io/flipt/raw/main/install.sh | sh -s -- v1.45.0"
return 1
fi
fi
# Extract v1.x.x stable releases (excluding prereleases and drafts)
# Process each release object and filter for stable v1 releases
version=$(awk '
BEGIN {
in_release = 0
tag_name = ""
is_prerelease = "true"
is_draft = "true"
}
/{/ { in_release = 1 }
/"tag_name":/ {
gsub(/.*"tag_name": *"/, "", $0)
gsub(/".*/, "", $0)
tag_name = $0
}
/"prerelease":/ {
gsub(/.*"prerelease": */, "", $0)
gsub(/,.*/, "", $0)
is_prerelease = $0
}
/"draft":/ {
gsub(/.*"draft": */, "", $0)
gsub(/,.*/, "", $0)
is_draft = $0
}
/}/ {
if (in_release && tag_name ~ /^v1\./ && is_prerelease == "false" && is_draft == "false") {
print tag_name
exit
}
in_release = 0
tag_name = ""
is_prerelease = "true"
is_draft = "true"
}
' "$tmpfile")
rm -f "$tmpfile"
# Validate that we found a version
if [ -z "$version" ]; then
log_error "No stable v1.x.x releases found"
log_error "You can specify a version manually: curl -fsSL https://github.com/flipt-io/flipt/raw/main/install.sh | sh -s -- v1.45.0"
return 1
fi
log_info "Found latest v1 version: $version"
echo "$version"
return 0
}
# Normalize version (ensure it starts with v)
normalize_version() {
version="$1"
case "$version" in
v*) echo "$version" ;;
*) echo "v$version" ;;
esac
}
# Validate that version is a v1.x.x release
validate_v1_version() {
version="$1"
case "$version" in
v1.*)
return 0
;;
*)
log_error "Version $version is not a v1.x.x release"
log_error "This installer only supports v1.x.x releases"
return 1
;;
esac
}
# Get version to install
get_version() {
version=""
# Priority order: 1. Environment variable, 2. Command line argument, 3. Latest
if [ -n "$VERSION" ]; then
version="$VERSION"
log_info "Using version from environment: $version"
elif [ -n "$1" ]; then
version="$1"
log_info "Using version from argument: $version"
else
version=$(get_latest_version) || return 1
fi
version=$(normalize_version "$version")
validate_v1_version "$version" || return 1
echo "$version"
}
# Install binary to appropriate location
install_binary() {
bin_dir=""
flipt_bin="/tmp/flipt"
# Determine installation directory
if [ -n "$BIN_DIR" ]; then
bin_dir="$BIN_DIR"
elif echo ":$PATH:" | grep -q ":$HOME/.local/bin:"; then
bin_dir="$HOME/.local/bin"
else
bin_dir="/usr/local/bin"
fi
log_info "Installing flipt to $bin_dir"
# Create directory if it doesn't exist
if [ ! -d "$bin_dir" ]; then
if ! mkdir -p "$bin_dir" 2>/dev/null; then
log_warn "Cannot create $bin_dir as current user, trying with sudo"
sudo mkdir -p "$bin_dir"
fi
fi
# Install binary
if cp "$flipt_bin" "$bin_dir/flipt" 2>/dev/null && chmod +x "$bin_dir/flipt" 2>/dev/null; then
: # Success
else
log_warn "Cannot install to $bin_dir as current user, trying with sudo"
if ! sudo install "$flipt_bin" "$bin_dir/flipt"; then
log_error "Failed to install binary"
return 1
fi
fi
# Clean up
rm -f "$flipt_bin"
log_info "Successfully installed flipt to $bin_dir/flipt"
}
# Download and install
install_flipt() {
version=""
os=""
arch=""
archive_url=""
archive_name=""
# Get version
version=$(get_version "$1") || return 1
# Check platform support
uname_os_check || return 1
uname_arch_check || return 1
# Get platform details
os=$(uname_os)
arch=$(uname_arch)
log_info "Installing Flipt $version for $os/$arch"
# Construct download URL
archive_name="${os}_${arch}.tar.gz"
archive_url="${DOWNLOAD_URL_BASE}/${version}/${archive_name}"
log_info "Downloading from: $archive_url"
# Download archive
if ! curl -fsSL -o "/tmp/flipt.tar.gz" "$archive_url"; then
log_error "Failed to download $archive_url"
return 1
fi
# Extract archive
log_info "Extracting archive..."
if ! tar -xzf "/tmp/flipt.tar.gz" -C "/tmp"; then
log_error "Failed to extract archive"
return 1
fi
# Clean up archive
rm -f "/tmp/flipt.tar.gz"
# Make binary executable
chmod +x "/tmp/flipt"
# Install binary
install_binary
# Success message
echo ""
log_info "${GREEN}Flipt $version installed successfully!${RESET}"
echo ""
echo "Get started with: flipt --help"
echo ""
echo "For feedback and support:"
echo " Discord: https://flipt.io/discord"
echo " GitHub: https://github.com/flipt-io/flipt"
echo " Email: dev@flipt.io"
echo ""
}
# Main execution
install_flipt "$1"