-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative-install.sh
More file actions
executable file
·304 lines (265 loc) · 9.68 KB
/
Copy pathnative-install.sh
File metadata and controls
executable file
·304 lines (265 loc) · 9.68 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
#!/usr/bin/env bash
set -euo pipefail
script_directory="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly script_directory
environment_file="${script_directory}/.env"
install_url=''
install_setup_mode=''
install_admin_name=''
install_admin_email=''
install_admin_password_stdin=false
install_demo=false
install_database=''
environment_was_created=false
require_command() {
command -v "$1" >/dev/null 2>&1 || {
echo "Required command is unavailable: $1" >&2
exit 1
}
}
require_option_value() {
if [[ $# -lt 2 || -z "$2" ]]; then
echo "Option $1 requires a value." >&2
exit 1
fi
}
set_environment_value() {
local key="$1"
local value="$2"
local escaped_value
escaped_value="$(printf '%s' "$value" | sed 's/[&|\\]/\\&/g')"
if grep -q "^${key}=" "$environment_file"; then
sed -i "s|^${key}=.*|${key}=${escaped_value}|" "$environment_file"
else
printf '%s=%s\n' "$key" "$value" >> "$environment_file"
fi
}
environment_value() {
[[ -f "$environment_file" ]] || return 0
grep -m1 "^${1}=" "$environment_file" | cut -d= -f2- || true
}
usage() {
cat <<'EOF'
Usage:
./native-install.sh install [--url URL] [--database sqlite|mysql|mariadb|pgsql]
[--setup web|cli] [--demo]
[--admin-name NAME --admin-email EMAIL --admin-password-stdin]
./native-install.sh doctor
./native-install.sh status
The native installer expects a prepared bundle with vendor/ and public/build/.
It configures the application only; web-server and process-manager setup remains
with the operator.
EOF
}
parse_install_options() {
while [[ $# -gt 0 ]]; do
case "$1" in
--url)
require_option_value "$1" "${2:-}"
install_url="$2"
shift 2
;;
--database)
require_option_value "$1" "${2:-}"
install_database="$2"
shift 2
;;
--setup)
require_option_value "$1" "${2:-}"
install_setup_mode="$2"
shift 2
;;
--demo)
install_demo=true
shift
;;
--admin-name)
require_option_value "$1" "${2:-}"
install_admin_name="$2"
shift 2
;;
--admin-email)
require_option_value "$1" "${2:-}"
install_admin_email="$2"
shift 2
;;
--admin-password-stdin)
install_admin_password_stdin=true
shift
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown install option: $1" >&2
usage >&2
exit 1
;;
esac
done
if [[ -n "$install_setup_mode" && "$install_setup_mode" != web && "$install_setup_mode" != cli ]]; then
echo 'Setup mode must be either web or cli.' >&2
exit 1
fi
if [[ -n "$install_database" && "$install_database" != sqlite && "$install_database" != mysql && "$install_database" != mariadb && "$install_database" != pgsql ]]; then
echo 'Database must be sqlite, mysql, mariadb, or pgsql.' >&2
exit 1
fi
if [[ "$install_admin_password_stdin" == true && ( -z "$install_admin_name" || -z "$install_admin_email" ) ]]; then
echo 'Administrator name and email are required with --admin-password-stdin.' >&2
exit 1
fi
if [[ "$install_demo" == true && "$install_setup_mode" == web ]]; then
echo '--demo requires CLI setup.' >&2
exit 1
fi
}
doctor() {
require_command php
require_command grep
require_command sed
if (( BASH_VERSINFO[0] < 5 )); then
echo 'Bash 5 or newer is required.' >&2
exit 1
fi
php -r 'exit(version_compare(PHP_VERSION, "8.4.1", ">=") ? 0 : 1);' || {
echo 'PHP 8.4.1 or newer is required.' >&2
exit 1
}
for extension in mbstring openssl pdo; do
php -r "exit(extension_loaded('${extension}') ? 0 : 1);" || {
echo "PHP extension is required: ${extension}" >&2
exit 1
}
done
local database_connection
database_connection="${install_database:-$(environment_value DB_CONNECTION)}"
database_connection="${database_connection:-sqlite}"
case "$database_connection" in
sqlite)
for extension in pdo_sqlite sqlite3; do
php -r "exit(extension_loaded('${extension}') ? 0 : 1);" || {
echo "PHP extension is required for ${database_connection}: ${extension}" >&2
exit 1
}
done
;;
mysql|mariadb)
php -r "exit(extension_loaded('pdo_mysql') ? 0 : 1);" || {
echo "PHP extension is required for ${database_connection}: pdo_mysql" >&2
exit 1
}
;;
pgsql)
php -r "exit(extension_loaded('pdo_pgsql') ? 0 : 1);" || {
echo "PHP extension is required for ${database_connection}: pdo_pgsql" >&2
exit 1
}
;;
*)
echo "Unsupported database connection: ${database_connection}" >&2
exit 1
;;
esac
[[ -f "${script_directory}/artisan" ]] || { echo 'artisan is missing from the bundle.' >&2; exit 1; }
[[ -f "${script_directory}/vendor/autoload.php" ]] || { echo 'vendor/autoload.php is missing; use a prepared native bundle.' >&2; exit 1; }
[[ -d "${script_directory}/public/build" ]] || { echo 'public/build is missing; use a prepared native bundle.' >&2; exit 1; }
[[ -w "$script_directory" ]] || { echo "Bundle directory is not writable: ${script_directory}" >&2; exit 1; }
mkdir -p "${script_directory}/storage/app/public" "${script_directory}/storage/framework/cache/data" "${script_directory}/storage/framework/sessions" "${script_directory}/storage/framework/views" "${script_directory}/storage/logs" "${script_directory}/bootstrap/cache"
[[ -w "${script_directory}/storage" && -w "${script_directory}/bootstrap/cache" ]] || { echo 'Storage and bootstrap/cache must be writable.' >&2; exit 1; }
echo 'Native bundle prerequisites are valid.'
}
prepare_environment() {
if [[ ! -f "$environment_file" ]]; then
cp "${script_directory}/.env.example" "$environment_file"
environment_was_created=true
fi
chmod 600 "$environment_file"
set_environment_value APP_NAME VersionTracker
set_environment_value APP_ENV production
set_environment_value APP_DEBUG false
if [[ -n "$install_url" ]]; then
if ! [[ "$install_url" =~ ^https?://[^/?#[:space:]]+(/[^[:space:]]*)?$ ]]; then
echo 'The application URL must be an absolute http:// or https:// URL without whitespace.' >&2
exit 1
fi
set_environment_value APP_URL "$install_url"
fi
if [[ -n "$install_database" ]]; then
set_environment_value DB_CONNECTION "$install_database"
fi
if [[ "$(environment_value DB_CONNECTION)" == sqlite ]]; then
mkdir -p "${script_directory}/database"
touch "${script_directory}/database/database.sqlite"
chmod 600 "${script_directory}/database/database.sqlite"
set_environment_value DB_DATABASE "${script_directory}/database/database.sqlite"
fi
if [[ -z "$(environment_value APP_KEY)" ]]; then
php "${script_directory}/artisan" key:generate --force --no-interaction
fi
if [[ "$install_demo" == true ]]; then
install_setup_mode=cli
elif [[ -z "$install_setup_mode" ]]; then
if [[ "$install_admin_password_stdin" == true ]]; then
install_setup_mode=cli
else
install_setup_mode=web
fi
fi
if [[ "$environment_was_created" == true || -n "$install_url" ]]; then
case "$(environment_value APP_URL)" in
https://*) set_environment_value SESSION_SECURE_COOKIE true ;;
http://*) set_environment_value SESSION_SECURE_COOKIE false ;;
esac
fi
if [[ "$install_setup_mode" == web && -z "$(environment_value INSTALLER_SETUP_TOKEN)" ]]; then
set_environment_value INSTALLER_SETUP_TOKEN "$(php -r 'echo bin2hex(random_bytes(24));')"
fi
}
install() {
doctor >/dev/null
prepare_environment
php "${script_directory}/artisan" package:discover --ansi
php "${script_directory}/artisan" migrate --force
if [[ "$install_demo" == true ]]; then
php "${script_directory}/artisan" app:install --demo
elif [[ "$install_setup_mode" == cli ]]; then
if [[ "$install_admin_password_stdin" == true ]]; then
php "${script_directory}/artisan" app:install \
--no-demo \
--admin-name="$install_admin_name" \
--admin-email="$install_admin_email" \
--admin-password-stdin
else
php "${script_directory}/artisan" app:install --no-demo
fi
else
php "${script_directory}/artisan" storage:link --force
php "${script_directory}/artisan" optimize
echo "Open $(environment_value APP_URL)/install and enter this one-time setup token:"
environment_value INSTALLER_SETUP_TOKEN
fi
}
status() {
doctor
php "${script_directory}/artisan" about --only=environment
php "${script_directory}/artisan" migrate:status
}
case "${1:-}" in
install)
shift
parse_install_options "$@"
install
;;
doctor)
doctor
;;
status)
status
;;
*)
usage >&2
exit 1
;;
esac