-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser
executable file
·439 lines (397 loc) · 14.5 KB
/
laser
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t '
function color_message() {
COLOR="\033[1;$1m"
NOCOLOR='\033[0m'
echo -e "${COLOR}$2:${NOCOLOR} $3 " >&2
}
function error_message() {
color_message 31 "ERROR" "$*"
}
function warning_message() {
color_message 33 "WARNING" "$*"
}
# thanks https://stackoverflow.com/a/5027832
function relativepath() {
source=$1
target=$2
common_part=$source
back=
# shellcheck disable=SC2295
while [ "${target#$common_part}" = "${target}" ]; do
common_part=$(dirname "$common_part")
back="../${back}"
done
# shellcheck disable=SC2295
echo "${back}${target#$common_part/}"
}
function whichproject() {
project=$(relativepath "$projects_dir" "$current_dir")
if [[ $project = */* ]]; then
# warning_message "expected a subdir of ${projects_dir}. Current dir is ${current_dir}."
project=""
fi
echo "$project"
}
function nih() {
echo "Still not implemented"
}
function dc() {
#shellcheck disable=SC2086
docker-compose $dc_params "$@"
}
function up() {
dc up -d
}
function lasercommands() {
sep="${1:-SEP}"
if [ "$sep" != "NOSEP" ]; then
echo " -----------------------------------------------------"
fi
echo " artisan Execute composer commands"
echo " composer Execute composer"
echo " dockerhelp List docker-compose and laser commands"
echo " help List laser commands"
echo " mysql Execute mysql"
echo " psql Execute psql"
echo " scan Scan and identify projects to build nginx configs"
echo " node Execute node"
echo " npm Execute npm"
echo " npx Execute npx"
echo " php Execute php commands in your app container"
echo " tinker Execute php artisan tinker"
echo " test Execute php artisan test"
echo " share Share externally your laser stack"
echo " shell Shell in the app container"
echo " appexec Execute arbitrary commands in the app container"
echo " yarn Execute yarn"
echo " stats top-like docker stats"
}
function dohelp() {
echo "Manage a local docker stack to run multiple laravel projects."
echo ""
echo "Usage:"
echo " laser [COMMAND] [ARGS...]"
echo ""
echo "Commands:"
lasercommands NOSEP
echo " * Any other command goes to docker-compose"
echo ""
echo "Some useful docker-compose commands (dockerhelp to full list)"
echo " up [-d] Create and start containers"
echo " stop Stop containers"
echo " logs View output from containers"
echo " ps List containers running"
echo ""
}
function dockerhelp() {
dc help
lasercommands
}
function list() {
cd $projects_dir
for project in *; do ls $project/public/*.php 2>/dev/null; done | sed "s|/.*$||g;s|^|http://|g;s|$|.$SUFFIX|g"
}
function stats() {
docker stats
}
function artisan() {
project=$1
shift
dc exec --workdir "/var/www/$project" app php artisan "$@"
}
function composer() {
project=$1
shift
dc exec --workdir "/var/www/$project" app composer "$@"
}
function mysql() {
if [ -z "$*" ]; then
dc exec app bash -c "mysql -h db -u root -p${DB_ROOT_PASSWORD} ${DB_DATABASE}"
else
dc exec app bash -c "mysql -h db -u root -p${DB_ROOT_PASSWORD} $*"
fi
}
function psql() {
if [ -z "$*" ]; then
dc exec app bash -c "PGPASSWORD=${POSTGRES_PASSWORD} psql -h postgres -U ${POSTGRES_USER} ${POSTGRES_DB}"
else
dc exec app bash -c "PGPASSWORD=${POSTGRES_PASSWORD} psql -h postgres -U ${POSTGRES_USER} $*"
fi
}
function stackisup() {
dc ps -q
}
function restartphp() {
dc exec app /usr/bin/supervisorctl restart php-fpm
}
function restartnginx() {
dc exec app /usr/bin/supervisorctl restart nginx
}
function restart_nginx_if_up() {
isup=$(stackisup)
if [ -n "$isup" ]; then
echo "Laser Stack is up, restarting nginx."
restartnginx
fi
}
function scan() {
fromscratch="${1:-preserve}"
local changed=0
isup=$(stackisup)
if [ -n "$isup" ]; then
if [ "$fromscratch" == "--fromscratch" ]; then
echo "SCAN: starting from scratch"
rm -f config/nginx/*
fi
# shellcheck disable=SC1091
source libs/sourcedrivers.sh
changed=$(drivers::scan_projects "${projects_dir}")
if [ "$changed" -eq "1" ]; then
echo "SCAN: configuration changed"
restartnginx
else
echo "SCAN: no configuration change"
fi
else
error_message "SCAN: stack need to be up"
fi
}
function node() {
project=$1
shift
dc exec --workdir "/var/www/$project" app node "$@"
}
function npm() {
project=$1
shift
dc exec --workdir "/var/www/$project" app npm "$@"
}
function npx() {
project=$1
shift
dc exec --workdir "/var/www/$project" app npx "$@"
}
function php() {
project=$1
shift
dc exec --workdir "/var/www/$project" app php "$@"
}
function tinker() {
project=$1
shift
dc exec --workdir "/var/www/$project" app php artisan tinker "$@"
}
function dotest() {
project=$1
shift
dc exec --workdir "/var/www/$project" app php artisan test "$@"
}
function share() {
project=$1
shift
docker run --rm -it --network "${dockerproject}_laser" --link app wernight/ngrok ngrok http -host-header="$project.test" host.docker.internal:80
}
function shell() {
project=$1
shift
dc exec --workdir "/var/www/$project" app bash "$@"
}
function appexec() {
project=$1
shift
dc exec --workdir "/var/www/$project" app "$@"
}
function yarn() {
project=$1
shift
dc exec --workdir "/var/www/$project" app yarn "$@"
}
function new() {
project=$1
shift
mkdir -p "$laser_dir/../$project/public"
export project
HELLOWORLD="
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
<title>Laserstack</title>
<!-- Fonts -->
<link href=\"https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap\" rel=\"stylesheet\">
<!-- Styles -->
<style>
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}[hidden]{display:none}html{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;line-height:1.5}*,:after,:before{box-sizing:border-box;border:0 solid #e2e8f0}a{color:inherit;text-decoration:inherit}svg,video{display:block;vertical-align:middle}video{max-width:100%;height:auto}.bg-white{--bg-opacity:1;background-color:#fff;background-color:rgba(255,255,255,var(--bg-opacity))}.bg-gray-100{--bg-opacity:1;background-color:#f7fafc;background-color:rgba(247,250,252,var(--bg-opacity))}.border-gray-200{--border-opacity:1;border-color:#edf2f7;border-color:rgba(237,242,247,var(--border-opacity))}.border-t{border-top-width:1px}.flex{display:flex}.grid{display:grid}.hidden{display:none}.items-center{align-items:center}.justify-center{justify-content:center}.font-semibold{font-weight:600}.h-5{height:1.25rem}.h-8{height:2rem}.h-16{height:4rem}.text-sm{font-size:.875rem}.text-lg{font-size:1.250rem}.leading-7{line-height:1.75rem}.mx-auto{margin-left:auto;margin-right:auto}.ml-1{margin-left:.25rem}.mt-2{margin-top:.5rem}.mr-2{margin-right:.5rem}.ml-2{margin-left:.5rem}.mt-4{margin-top:1rem}.ml-4{margin-left:1rem}.mt-8{margin-top:2rem}.ml-12{margin-left:3rem}.-mt-px{margin-top:-1px}.max-w-6xl{max-width:72rem}.min-h-screen{min-height:100vh}.overflow-hidden{overflow:hidden}.p-6{padding:1.5rem}.py-4{padding-top:1rem;padding-bottom:1rem}.px-6{padding-left:1.5rem;padding-right:1.5rem}.pt-8{padding-top:2rem}.fixed{position:fixed}.relative{position:relative}.top-0{top:0}.right-0{right:0}.shadow{box-shadow:0 1px 3px 0 rgba(0,0,0,.1),0 1px 2px 0 rgba(0,0,0,.06)}.text-center{text-align:center}.text-gray-200{--text-opacity:1;color:#edf2f7;color:rgba(237,242,247,var(--text-opacity))}.text-gray-300{--text-opacity:1;color:#e2e8f0;color:rgba(226,232,240,var(--text-opacity))}.text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}.text-gray-500{--text-opacity:1;color:#a0aec0;color:rgba(160,174,192,var(--text-opacity))}.text-gray-600{--text-opacity:1;color:#718096;color:rgba(113,128,150,var(--text-opacity))}.text-gray-700{--text-opacity:1;color:#4a5568;color:rgba(74,85,104,var(--text-opacity))}.text-gray-900{--text-opacity:1;color:#1a202c;color:rgba(26,32,44,var(--text-opacity))}.underline{text-decoration:underline}.antialiased{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.w-5{width:1.25rem}.w-8{width:2rem}.w-auto{width:auto}.grid-cols-1{grid-template-columns:repeat(1,minmax(0,1fr))}@media (min-width:640px){.sm\:rounded-lg{border-radius:.5rem}.sm\:block{display:block}.sm\:items-center{align-items:center}.sm\:justify-start{justify-content:flex-start}.sm\:justify-between{justify-content:space-between}.sm\:h-20{height:5rem}.sm\:ml-0{margin-left:0}.sm\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\:pt-0{padding-top:0}.sm\:text-left{text-align:left}.sm\:text-right{text-align:right}}@media (min-width:768px){.md\:border-t-0{border-top-width:0}.md\:border-l{border-left-width:1px}.md\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (min-width:1024px){.lg\:px-8{padding-left:2rem;padding-right:2rem}}@media (prefers-color-scheme:dark){.dark\:bg-gray-800{--bg-opacity:1;background-color:#2d3748;background-color:rgba(45,55,72,var(--bg-opacity))}.dark\:bg-gray-900{--bg-opacity:1;background-color:#1a202c;background-color:rgba(26,32,44,var(--bg-opacity))}.dark\:border-gray-700{--border-opacity:1;border-color:#4a5568;border-color:rgba(74,85,104,var(--border-opacity))}.dark\:text-white{--text-opacity:1;color:#fff;color:rgba(255,255,255,var(--text-opacity))}.dark\:text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}.dark\:text-gray-500{--tw-text-opacity:1;color:#6b7280;color:rgba(107,114,128,var(--tw-text-opacity))}}
</style>
<style>
body {font-family: 'Nunito', sans-serif;}
</style>
</head>
<body class=\"antialiased\">
<div class=\"relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0\">
<div class=\"max-w-6xl mx-auto sm:px-6 lg:px-8\">
<div class=\"mt-8 bg-white dark:bg-gray-800 overflow-hidden shadow sm:rounded-lg\">
<div class=\"grid grid-cols-1 md:grid-cols-1\">
<div class=\"p-6\">
<div class=\"flex items-center\">
<svg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" viewBox=\"0 0 24 24\" class=\"w-8 h-8 text-gray-500\"><path d=\"M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z\"></path></svg>
<div class=\"ml-4 text-lg leading-7 font-semibold text-gray-900 dark:text-white\">
$project
</div>
</div>
<div class=\"ml-12\">
<div class=\"mt-2 text-gray-600 dark:text-gray-400 text-sm\">
New laserstack project, standard static file.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
"
# echo "<h1 style=\"font-family: sans-serif; color: #777; margin: 1em; \">hello world from $project</h1>" > $laser_dir/../$project/public/index.php
echo "$HELLOWORLD" >"$laser_dir/../$project/public/index.php"
sudo bash -c "echo -e '\n127.0.0.1 $project.local $project' >> /etc/hosts"
cd "$laser_dir/../$project"
echo "$project created."
if [[ "$WSL" ]]; then
echo "WSL detected, manually add \"127.0.0.1 $project.local $project\" to %windir%\\System32\\drivers\\etc\\hosts"
fi
open "http://$project.local/"
}
# MAIN ----------------------------------------------------------------------
current_dir=$(pwd)
# thanks https://stackoverflow.com/questions/3349105/
laser_dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "$laser_dir" || exit
dockerproject=$(basename "$laser_dir")
# shellcheck source=/dev/null
source ./env.example
# source .env file to get MYSQL info and projects directory
if [ -f ./.env ]; then
# shellcheck source=/dev/null
source ./.env
else
warning_message "laser .env file not found, copying env.example as .env."
cp ./env.example ./.env
# shellcheck source=/dev/null
source ./.env
fi
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-postgres}"
POSTGRES_USER="${POSTGRES_USER:-postgres}"
POSTGRES_DB="${POSTGRES_DB:-postgres}"
export PHPV=$(echo $PHP_VERSION | sed 's/\.//g')
export PHPALPINE=$PHPV
if [ $PHPV == "74" ]; then
export PHPALPINE=7
fi
projects_dir=$(
cd "$PROJECTS_DIRECTORY"
pwd
)
dc_params="-f docker-compose.yml"
if [ -n "$PROFILES" ]; then
PROFILE="${PROFILES//,/ --profile }"
dc_params="$dc_params --profile $PROFILE"
fi
WSL=
# shellcheck disable=SC2143
if [[ $(grep -si microsoft /proc/version) ]]; then
WSL=wsl
# shellcheck disable=SC2034
windows_path=$(mount | grep drvfs | awk '{ print $3; }')
fi
set +e
subcommand="${1:-help}"
shift
set -e
case "$subcommand" in
help)
dohelp
;;
list)
list
;;
up)
up
;;
dockerhelp)
dockerhelp
;;
mysql)
mysql "$@"
;;
psql)
psql "$@"
;;
scan)
scan "$@"
;;
stats)
stats "$@"
;;
restartnginx)
restartnginx "$@"
;;
artisan | art)
project=$(whichproject)
artisan "$project" "$@"
;;
composer)
project=$(whichproject)
composer "$project" "$@"
;;
node)
project=$(whichproject)
node "$project" "$@"
;;
npm)
project=$(whichproject)
npm "$project" "$@"
;;
npx)
project=$(whichproject)
npx "$project" "$@"
;;
php)
project=$(whichproject)
php "$project" "$@"
;;
tinker)
project=$(whichproject)
tinker "$project" "$@"
;;
test)
project=$(whichproject)
dotest "$project" "$@"
;;
share)
project=$(whichproject)
share "$project" "$@"
;;
shell)
project=$(whichproject)
shell "$project" "$@"
;;
appexec)
project=$(whichproject)
appexec "$project" "$@"
;;
yarn)
project=$(whichproject)
yarn "$project" "$@"
;;
new)
new "$@"
;;
*)
dc "$subcommand" "$@"
;;
esac