-
Notifications
You must be signed in to change notification settings - Fork 921
/
install_pocketbase.sh
executable file
·258 lines (219 loc) · 6.96 KB
/
install_pocketbase.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
#!/bin/bash
# 1. Check if pocketbase exists
check_pocketbase() {
if [ -f "./pb/pocketbase" ]; then
echo "Detected ./pb/pocketbase already exists, please delete it manually and try again"
exit 1
fi
# Create directory if it doesn't exist
if [ ! -d "./pb" ]; then
mkdir -p ./pb
fi
}
# 2. Get available versions
get_versions() {
echo "Fetching available versions..."
VERSIONS=($(curl -s https://api.github.com/repos/pocketbase/pocketbase/releases | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'))
LATEST_VERSION=${VERSIONS[0]}
}
# 3. Select version with arrow keys
select_version() {
# Clear screen
clear
# Array to store versions
local versions=("${VERSIONS[@]}")
local current=0
local key
local total=${#versions[@]}
while true; do
# Clear screen
clear
echo "Available versions (Use ↑↓ arrows to select, Enter to confirm):"
echo "----------------------------------------"
# Display versions
for i in "${!versions[@]}"; do
if [ $i -eq $current ]; then
echo -e "\033[32m-> ${versions[$i]}\033[0m"
else
echo " ${versions[$i]}"
fi
done
# Read a single character
read -rsn1 key
# Special key sequences
if [[ $key = $'\x1b' ]]; then
read -rsn2 key
case $key in
'[A') # Up arrow
((current--))
[ $current -lt 0 ] && current=$((total - 1))
;;
'[B') # Down arrow
((current++))
[ $current -ge $total ] && current=0
;;
esac
elif [[ $key = "" ]]; then # Enter key
SELECTED_VERSION=${versions[$current]}
break
fi
done
echo -e "\nSelected version: $SELECTED_VERSION"
}
# 4. Download corresponding system version
download_pocketbase() {
# Detect OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Remove 'v' prefix from version number
VERSION_NUM=${SELECTED_VERSION#v}
case "$OS" in
"darwin")
case "$ARCH" in
"x86_64") FILENAME="pocketbase_${VERSION_NUM}_darwin_amd64.zip" ;;
"arm64") FILENAME="pocketbase_${VERSION_NUM}_darwin_arm64.zip" ;;
esac
;;
"linux")
case "$ARCH" in
"x86_64") FILENAME="pocketbase_${VERSION_NUM}_linux_amd64.zip" ;;
"aarch64") FILENAME="pocketbase_${VERSION_NUM}_linux_arm64.zip" ;;
esac
;;
*)
echo "Unsupported operating system"
exit 1
;;
esac
# Download and extract
DOWNLOAD_URL="https://github.com/pocketbase/pocketbase/releases/download/${SELECTED_VERSION}/${FILENAME}"
echo "Downloading: $DOWNLOAD_URL"
# Download with retry mechanism
MAX_RETRIES=3
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if curl -L "$DOWNLOAD_URL" -o "./pb/${FILENAME}" --fail --silent --show-error; then
if [ -f "./pb/${FILENAME}" ] && [ -s "./pb/${FILENAME}" ]; then
echo "Download completed successfully"
break
fi
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
echo "Download failed, retrying ($RETRY_COUNT/$MAX_RETRIES)..."
sleep 2
else
echo "Download failed after $MAX_RETRIES attempts"
exit 1
fi
done
# Extract only the pocketbase executable
cd ./pb || exit 1
if ! unzip -j -o "${FILENAME}" "pocketbase" > /dev/null 2>&1; then
echo "Failed to extract pocketbase executable"
cd ..
exit 1
fi
rm "${FILENAME}" # Remove the zip file
if [ ! -f "pocketbase" ]; then
echo "pocketbase executable not found after extraction"
cd ..
exit 1
fi
chmod +x pocketbase
cd ..
echo "Successfully installed pocketbase"
}
# Validate email format
validate_email() {
local email=$1
if [[ ! "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
return 1
fi
return 0
}
# Validate password requirements
validate_password() {
local password=$1
# Check minimum length of 8 characters
if [ ${#password} -lt 8 ]; then
return 1
fi
return 0
}
# 5. Configure admin account
configure_admin() {
local valid_input=false
while [ "$valid_input" = false ]; do
# Get email
while true; do
echo "Please set superuser email:"
read EMAIL
if validate_email "$EMAIL"; then
break
else
echo "Invalid email format. Please try again."
fi
done
# Get password
while true; do
echo "Please set superuser password (minimum 8 characters):"
read -s PASSWORD
echo
if validate_password "$PASSWORD"; then
# Confirm password
echo "Please confirm password:"
read -s PASSWORD_CONFIRM
echo
if [ "$PASSWORD" = "$PASSWORD_CONFIRM" ]; then
valid_input=true
break
else
echo "Passwords do not match. Please try again."
fi
else
echo "Password must be at least 8 characters long. Please try again."
fi
done
done
cd ./pb
./pocketbase migrate up
# Try to create superuser
if ! ./pocketbase --dev superuser create "$EMAIL" "$PASSWORD"; then
echo "Failed to create superuser. Please check the error message above."
exit 1
fi
cd ..
echo "Superuser created successfully!"
}
# 6. Configure environment file
configure_env() {
# Create .env if it doesn't exist
if [ ! -f "./core/.env" ]; then
# mkdir -p ./core
cp env_sample ./core/.env
echo "Created new .env file from template"
else
echo "Found existing .env file"
fi
# Update authentication info in environment file using sed
if [ "$(uname)" = "Darwin" ]; then
# macOS version
sed -i '' 's/export PB_API_AUTH="[^"]*"/export PB_API_AUTH="'$EMAIL'|'$PASSWORD'"/' "./core/.env"
else
# Linux version
sed -i 's/export PB_API_AUTH="[^"]*"/export PB_API_AUTH="'$EMAIL'|'$PASSWORD'"/' "./core/.env"
fi
echo "Updated PB_API_AUTH in .env with new credentials"
}
main() {
echo "Starting PocketBase installation..."
check_pocketbase
get_versions
select_version
download_pocketbase
configure_admin
configure_env
echo "PocketBase installation completed!"
}
main