-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
390 lines (338 loc) · 12.4 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
#!/bin/bash
# Set strict error handling
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
TICK="✓"
CROSS="✗"
# Print colored messages
log_success() { printf "${GREEN}${TICK} %s${NC}\n" "$1"; }
log_error() { printf "${RED}${CROSS} %s${NC}\n" "$1" >&2; }
log_warn() { printf "${YELLOW}! %s${NC}\n" "$1"; }
log_info() { printf "• %s\n" "$1"; }
# Global cleanup function for the entire script
cleanup_script() {
log_info "Performing final cleanup..."
# Clean any temporary npm configurations that might exist
find /tmp -name 'npm.*.rc' -type f -mmin -5 -exec rm -f {} \;
log_success "Cleanup completed"
}
# Set up script-level cleanup trap
trap cleanup_script EXIT SIGINT SIGTERM ERR
# Check if running with sudo/root
check_root() {
if [ "$(id -u)" != "0" ]; then
log_error "This script must be run as root or with sudo"
exit 1
fi
}
# Function to check GitHub token
check_github_token() {
local token=$1
log_info "Verifying GitHub token..."
# Check token with GitHub API
log_info "Checking user access..."
local response=$(curl -s -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/user)
if echo "$response" | grep -q '"login"'; then
# Verify package access specifically for npm packages
log_info "Checking package access..."
local pkg_response=$(curl -s -H "Authorization: Bearer $token" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/orgs/flxbl-io/packages?package_type=npm")
if echo "$pkg_response" | grep -q "sfp"; then
log_success "GitHub token verified - Has package access"
return 0
else
log_error "GitHub token lacks package access permissions"
log_error "Make sure your token has read:packages scope"
return 1
fi
else
log_error "Invalid GitHub token"
return 1
fi
}
# Install Node.js 20
install_node() {
log_info "Installing Node.js 20..."
if ! command -v node &> /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
log_success "Node.js $(node --version) installed"
else
local version=$(node --version)
if [[ ${version:1:2} -ge 20 ]]; then
log_success "Node.js $version already installed"
else
log_warn "Updating Node.js to version 20..."
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
log_success "Node.js $(node --version) installed"
fi
fi
}
# Install Docker
install_docker() {
log_info "Installing Docker..."
if ! command -v docker &> /dev/null; then
# Install Docker's prerequisites
apt-get update
apt-get install -y ca-certificates curl gnupg
# Add Docker's official GPG key
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Start and enable Docker
systemctl start docker
systemctl enable docker
log_success "Docker installed"
else
log_success "Docker already installed"
fi
}
# Install Infisical CLI
install_infisical() {
log_info "Installing Infisical CLI..."
if ! command -v infisical &> /dev/null; then
curl -1sLf 'https://dl.cloudsmith.io/public/infisical/infisical-cli/setup.deb.sh' | bash
apt-get update
apt-get install -y infisical
log_success "Infisical CLI installed"
else
log_success "Infisical CLI already installed"
fi
}
# Install Supabase CLI
install_supabase() {
log_info "Installing Supabase CLI..."
if ! command -v supabase &> /dev/null; then
local version="2.0.0" # Update this version as needed
wget -O /tmp/supabase.deb \
"https://github.com/supabase/cli/releases/download/v${version}/supabase_${version}_linux_amd64.deb"
dpkg -i /tmp/supabase.deb || apt-get install -f -y
rm /tmp/supabase.deb
log_success "Supabase CLI installed"
else
log_success "Supabase CLI already installed"
fi
}
# Function to show usage/help
show_usage() {
echo "
Usage: $0 [options]
Options:
-u, --update Update only the SFP CLI, skip other installations
-v, --version VERSION Install/update to a specific version of SFP CLI
(e.g., --version 1.2.3 or @web for latest)
-h, --help Show this help message
Environment variables:
FLXBL_NPM_REGISTRY_KEY GitHub token for npm registry authentication
Examples:
$0 # Full installation with latest version
$0 --update # Update SFP CLI to latest version
$0 --version 1.2.3 # Full installation with specific version
$0 -u -v 1.2.3 # Update only SFP CLI to version 1.2.3
"
}
# Function to run npm commands with authentication
npm_install_authenticated() {
local package=$1
local version=$2
local token=${FLXBL_NPM_REGISTRY_KEY:-$3}
local temp_npmrc=""
local exit_code=0
if [ -z "$token" ]; then
log_error "No npm registry token provided. Please set FLXBL_NPM_REGISTRY_KEY"
return 1
fi
local full_package
if [ -z "$version" ] || [ "$version" = "@web" ]; then
full_package="${package}@web"
else
full_package="${package}@${version}"
fi
log_info "Installing ${full_package}..."
# Function to cleanup temp files
cleanup() {
local temp_file=$1
if [ ! -z "$temp_file" ] && [ -f "$temp_file" ]; then
log_info "Cleaning up temporary npm configuration..."
rm -f "$temp_file"
fi
}
# Create temporary .npmrc
temp_npmrc=$(mktemp)
# Set up cleanup trap for various signals
trap "cleanup '$temp_npmrc'" EXIT SIGINT SIGTERM ERR
# Configure npm
{
echo "@flxbl-io:registry=https://npm.pkg.github.com/"
echo "//npm.pkg.github.com/:_authToken=${token}"
echo "registry=https://registry.npmjs.org/"
} > "$temp_npmrc"
# Use temporary .npmrc file
if NPM_CONFIG_USERCONFIG="$temp_npmrc" npm install -g "${full_package}"; then
log_success "Successfully installed ${full_package}"
exit_code=0
else
log_error "Failed to install ${full_package}"
exit_code=1
fi
# Cleanup will happen automatically via trap
return $exit_code
}
# Install SFP function
install_sfp() {
local version=$1
log_info "Installing/Updating SFP CLI..."
# Check if sfp is already installed
local current_version=""
if command -v sfp &> /dev/null; then
current_version=$(sfp --version 2>/dev/null || echo "unknown")
log_info "Current SFP CLI version: ${current_version}"
fi
# Show target version
if [ -z "$version" ] || [ "$version" = "@web" ]; then
log_info "Target: latest version (@web)"
else
log_info "Target: version ${version}"
fi
if ! npm_install_authenticated "@flxbl-io/sfp" "$version" "$FLXBL_NPM_REGISTRY_KEY"; then
log_error "Failed to install/update SFP CLI"
return 1
fi
# Show new version after update
if command -v sfp &> /dev/null; then
local new_version=$(sfp --version 2>/dev/null || echo "unknown")
if [ "$current_version" != "$new_version" ]; then
log_success "SFP CLI updated from ${current_version} to ${new_version}"
else
log_success "SFP CLI version ${new_version} is current"
fi
fi
}
# Main function
main() {
local update_only=false
local target_version=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-u|--update)
update_only=true
shift
;;
-v|--version)
if [ -z "$2" ]; then
log_error "Version argument is required for -v|--version"
show_usage
exit 1
fi
target_version="$2"
shift 2
;;
-h|--help)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
if [ "$update_only" = true ]; then
echo "
╔═══════════════════════════════════════╗
║ SFP CLI Updater ║
╚═══════════════════════════════════════╝
"
if [ ! -z "$target_version" ]; then
echo "Target Version: $target_version"
fi
else
echo "
╔═══════════════════════════════════════╗
║ SFP Prerequisites ║
║ Installation Script ║
╚═══════════════════════════════════════╝
"
if [ ! -z "$target_version" ]; then
echo "Target SFP Version: $target_version"
fi
fi
# Check if running as root
check_root
# Check if FLXBL_NPM_REGISTRY_KEY is provided in environment
local github_token=${FLXBL_NPM_REGISTRY_KEY:-""}
# If no token in environment, prompt for it
if [ -z "$github_token" ]; then
log_warn "FLXBL_NPM_REGISTRY_KEY not set"
while true; do
read -p "Enter your GitHub Personal Access Token: " github_token
export FLXBL_NPM_REGISTRY_KEY="$github_token"
if check_github_token "$github_token"; then
break
else
log_error "Please ensure your token has package read access"
read -p "Would you like to try another token? (y/n) " retry
if [[ $retry != "y" ]]; then
exit 1
fi
fi
done
else
if ! check_github_token "$github_token"; then
log_error "Provided FLXBL_NPM_REGISTRY_KEY is invalid or lacks required permissions"
exit 1
fi
fi
if [ "$update_only" = true ]; then
# Only update SFP CLI
install_sfp "$target_version"
echo "
╔═══════════════════════════════════════╗
║ Update Complete! 🎉 ║
╚═══════════════════════════════════════╝
"
exit 0
fi
# Full installation
apt-get update
apt-get install -y curl wget jq git
# Install all prerequisites
install_node
install_docker
install_infisical
install_supabase
install_sfp "$target_version"
# Final verification
echo "
Verifying installations:
"
node --version && log_success "Node.js"
docker --version && log_success "Docker"
docker compose version && log_success "Docker Compose"
infisical --version && log_success "Infisical CLI"
supabase --version && log_success "Supabase CLI"
sfp --version && log_success "SFP CLI"
echo "
╔═══════════════════════════════════════╗
║ Installation Complete! 🎉 ║
╚═══════════════════════════════════════╝
You can now use the 'sfp' command to manage your SFP installation.
Get started with: sfp server init --help
"
}
# Execute main with all arguments
main "$@"