-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathharbor
executable file
·634 lines (522 loc) · 15.1 KB
/
harbor
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env bash
TYPE=php
VERSION=3.0.3
# making harbor command available
chmod +x ./harbor
checkSystemSupport() {
# check system if supported
UNAMEOUT="$(uname -s)"
case "${UNAMEOUT}" in
Linux*)
MACHINE=linux
;;
Darwin*)
MACHINE=mac
;;
MINGW64_NT-10.0*)
MACHINE=mingw64
;;
*)
MACHINE="UNKNOWN"
;;
esac
if [[ "$MACHINE" == "UNKNOWN" ]]; then
echo "Unsupported system type"
echo "System must be a Macintosh/Linux/Windows"
echo ""
echo "System detection determined via uname command"
echo "If the following is empty, could not find uname command: $(which uname)"
echo "Your reported uname is: $(uname -s)"
exit 1
fi
}
setupEnvVariables() {
# prepare default env variables
# source .env.harbor
if [[ -f .env.harbor ]]; then
set -o allexport
source .env.harbor
set +o allexport
else
echo "Harbor .env.harbor file is missing, cannot continue."
exit 1
fi
# set xdebug host for developing
if [[ $(docker version --format '{{.Server.Version}}') > 18.03.0 ]]; then
HARBOR_XDEBUG_HOST=host.docker.internal
elif [[ "$MACHINE" == "linux" ]]; then
HARBOR_XDEBUG_HOST=$(/sbin/ifconfig docker0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1)
elif [[ "$MACHINE" == "mac" ]]; then
HARBOR_XDEBUG_HOST=$(ipconfig getifaddr en0) # Ethernet
if [[ -z "$HARBOR_XDEBUG_HOST" ]]; then
HARBOR_XDEBUG_HOST=$(ipconfig getifaddr en1) # Wifi
fi
elif [[ "$MACHINE" == "mingw64" ]]; then # Git Bash
HARBOR_XDEBUG_HOST=10.0.75.1
fi
export HARBOR_XDEBUG_HOST
# set sed command
if [[ "$MACHINE" == "linux" ]] || [[ "$MACHINE" == "mingw64" ]]; then
SEDCMD="sed -i"
elif [[ "$MACHINE" == "mac" ]]; then
SEDCMD="sed -i .bak"
fi
# is the environment running
PSRESULT="$(docker-compose ps -q)"
if [[ ! -z "$PSRESULT" ]]; then
EXEC="yes"
else
EXEC="no"
fi
# create base docker-compose command to run
COMPOSE="docker-compose -f docker-compose.yml"
if [[ -f "docker-compose.override.yml" ]]; then
COMPOSE="$COMPOSE -f docker-compose.override.yml"
fi
}
touchHistory() {
if [[ ! -f ./.harbor/history/harbor_bash_history ]]; then
touch ./.harbor/history/harbor_bash_history
fi
if [[ ! -f ./.harbor/history/root_bash_history ]]; then
touch ./.harbor/history/root_bash_history
fi
}
prepareExternalVolumes() {
if [[ ! "$(docker volume ls -q | grep empty-node-modules)" ]]; then
# echo "empty-node-modules volume does not exist, will be created now."
docker volume create --driver local empty-node-modules
fi
if [[ ! "$(docker volume ls -q | grep empty-vendor)" ]]; then
# echo "empty-vendor volume does not exist, will be created now."
docker volume create --driver local empty-vendor
fi
if [[ ! "$(docker volume ls -q | grep composer-cache)" ]]; then
# echo "composer-cache volume does not exist, will be created now."
docker volume create --driver local composer-cache
fi
}
testRunningContainers() {
if [[ ! -z $(docker ps -q) ]]; then
echo "You have some running containers. Are you sure that they are not blocking required ports: $HARBOR_WEB_PORT, $HARBOR_DB_PORT, $HARBOR_DB_TESTING_PORT ?"
echo "Do you wish to continue (y/n)?"
old_stty_cfg=$(stty -g)
stty raw -echo
answer=$(head -c 1)
stty ${old_stty_cfg} # Careful playing with stty
if echo "$answer" | grep -iq "^y"; then
echo "Continue ..."
${COMPOSE} down
else
exit 0
fi
fi
}
rebuild() {
REMOVE_IMAGES=""
REMOVE_VOLUMES=""
for i in "$@"; do
case ${i} in
-i | --images)
REMOVE_IMAGES="--rmi all"
;;
-v | --volumes)
REMOVE_VOLUMES="-v"
;;
*) ;;
esac
done
./harbor start
${COMPOSE} down ${REMOVE_VOLUMES} ${REMOVE_IMAGES}
${COMPOSE} pull
${COMPOSE} build
}
artisan() {
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
-u harbor \
php \
php artisan "$@"
else
${COMPOSE} run --rm \
php \
php artisan "$@"
fi
}
composer() {
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
-u harbor \
php \
composer "$@"
else
${COMPOSE} run --rm \
php \
composer "$@"
fi
}
db() {
if [[ ${HARBOR_DB_CONNECTION} == "pgsql" ]]; then
DB_COMMAND="psql -U ${DB_USERNAME} -h localhost ${DB_DATABASE}"
elif [[ ${HARBOR_DB_CONNECTION} == "mysql" ]]; then
DB_COMMAND="mysql -u ${DB_USERNAME} -h localhost ${DB_DATABASE}"
else
echo "Database ${HARBOR_DB_CONNECTION} not supported in this command."
fi
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
db \
${DB_COMMAND} "$@"
else
${COMPOSE} run --rm \
db \
${DB_COMMAND} "$@"
fi
}
dump() {
if [[ ${HARBOR_DB_CONNECTION} == "pgsql" ]]; then
DB_COMMAND="pg_dump -U ${DB_USERNAME} -h localhost ${DB_DATABASE}"
elif [[ ${HARBOR_DB_CONNECTION} == "mysql" ]]; then
DB_COMMAND="mysqldump -u ${DB_USERNAME} -h localhost ${DB_DATABASE}"
else
echo "Database ${HARBOR_DB_CONNECTION} not supported in this command."
fi
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
db \
${DB_COMMAND} "$@"
else
${COMPOSE} run --rm \
db \
${DB_COMMAND} "$@"
fi
}
ssh() {
if [[ "$EXEC" == "yes" ]] && [[ "$1" != "node" ]]; then
if [[ "$1" == "php" ]] && [[ "$2" != "root" ]]; then
USER_ARGUMENT="-u harbor"
else
USER_ARGUMENT=""
fi
${COMPOSE} exec \
${USER_ARGUMENT} \
$1 \
bash
else
${COMPOSE} run --rm \
$1 \
bash
fi
}
initLaravelCheckAndCopyEnv() {
echo "Checking .env and folders ..."
if [[ ! -f .env.example ]]; then
echo "No .env.example file found within current working directory $(pwd), this is not a laravel project, so please run harbor install"
exit 1
fi
# cp .env.example .env if .env is missing
if [[ ! -f .env ]] && [[ -f .env.example ]]; then
cp .env.example .env
fi
}
initNodeModulesDir() {
# make dir for node_modules, before docker-compose does
mkdir -p ./node_modules
}
initNpm() {
echo "Npm ..."
# npm install
./harbor npm install
# npm run dev
./harbor npm run build
}
initNpmCraftable() {
initNpm
./harbor npm run craftable-dev
}
prepareEnv() {
echo "Changing .env ..."
# change values in .env file
if [[ -f .env ]]; then
# source actual env values
source .env
if [[ ${DB_PASSWORD} == "" ]]; then
export DB_PASSWORD="bestsecret"
fi
# modify values
${SEDCMD} "s/APP_URL=.*/APP_URL=http:\/\/localhost:${HARBOR_WEB_PORT}/" .env
${SEDCMD} "s/DB_CONNECTION=.*/DB_CONNECTION=${HARBOR_DB_CONNECTION}/" .env
${SEDCMD} "s/DB_HOST=.*/DB_HOST=${HARBOR_DB_HOST}/" .env
${SEDCMD} "s/DB_PORT=.*/DB_PORT=${HARBOR_DB_PORT}/" .env
if [[ -f .env.harbor ]]; then
source .env.harbor
fi
${SEDCMD} "s/DB_DATABASE=.*/DB_DATABASE=${DB_DATABASE}/" .env
${SEDCMD} "s/DB_USERNAME=.*/DB_USERNAME=${DB_USERNAME}/" .env
${SEDCMD} "s/DB_PASSWORD=.*/DB_PASSWORD=${DB_PASSWORD}/" .env
${SEDCMD} "s/CACHE_DRIVER=.*/CACHE_DRIVER=${HARBOR_CACHE_DRIVER}/" .env
${SEDCMD} "s/SESSION_DRIVER=.*/SESSION_DRIVER=${HARBOR_SESSION_DRIVER}/" .env
${SEDCMD} "s/REDIS_HOST=.*/REDIS_HOST=${HARBOR_REDIS_HOST}/" .env
# removed bak file
if [[ -f .env.bak ]]; then
rm .env.bak
fi
fi
# source actual env values
source .env
}
prepareContainers() {
# need to start first, because it takes time to setup database
echo "Starting db ..."
${COMPOSE} run --rm db wait-for-active-database.sh
echo "Restarting containers ..."
# restart docker containers
./harbor restart
}
initLaravel() {
echo "Setting up your docker environment for this laravel based project."
prepareEnv
# art key:generate
if [[ -z "${APP_KEY}" ]] || [[ ${APP_KEY} == "SomeRandomString" ]]; then
${COMPOSE} run --rm php php artisan key:generate
# source actual env values
source .env
fi
prepareContainers
echo "Migrating ..."
# migrate and seed
./harbor art migrate --seed
initNpm
# stop docker containers
./harbor stop
echo "All set."
}
installLaravel() {
echo "Installing new laravel application to current folder."
# remove all containers and volumes
${COMPOSE} down -v
testRunningContainers
initNodeModulesDir
# use php container to create laravel application
${COMPOSE} run --rm php composer create-project laravel/laravel ./application
echo "Laravel installed"
# move sail docker-compose.yml to docker-compose-sail.yml
if [[ -f application/docker-compose.yml ]]; then
echo "Move sail docker-compose.yml to docker-compose-sail.yml"
mv application/docker-compose.yml application/docker-compose-sail.yml
fi
# move application content to this folder
echo "Move application and remove dir"
mv application/* application/.[!.]* .
rm -rf ./application
echo "Laravel installed"
}
laravel() {
installLaravel
echo "Laravel initializing ..."
initLaravel
}
setCraftableStability() {
if [[ -f composer.json ]]; then
# modify values
${COMPOSE} run --rm php composer config minimum-stability dev
fi
}
craftable() {
installLaravel
# TODO remove later
echo "Stability"
setCraftableStability
echo "Require craftable"
# ${COMPOSE} run --rm php composer require "brackets/craftable"
${COMPOSE} run --rm php composer require psr/http-message:1.1
${COMPOSE} run --rm php composer require dejwcake/craftable
echo "Require admin-generator"
# ${COMPOSE} run --rm php composer require --dev "brackets/admin-generator"
${COMPOSE} run --rm php composer require --dev dejwcake/admin-generator
echo "Craftable installed, initializing ..."
prepareEnv
prepareContainers
echo "Installing craftable ..."
# install craftable
./harbor art craftable:test-db-connection
./harbor art craftable:install
initNpmCraftable
# stop docker containers
./harbor stop
echo "All set."
}
main() {
# If we pass any arguments...
if [[ $# -gt 0 ]]; then
case "$1" in
#
# Type and version
#
# Get version
-v | --version)
echo ${VERSION}
exit 0
;;
# Get type
-t | --type)
echo ${TYPE}
exit 0
;;
esac
checkSystemSupport "$@"
setupEnvVariables "$@"
touchHistory
# Source .env, which can over-ride env vars
if [[ -f .env ]]; then
source .env
fi
prepareExternalVolumes
case "$1" in
#
# Docker commands
#
# Start up containers
start)
shift 1
${COMPOSE} up -d "$@"
;;
# Start up containers not as daemon
up)
shift 1
${COMPOSE} up "$@"
;;
# Stop the containers
stop)
shift 1
${COMPOSE} down "$@"
;;
# Restart the containers
restart)
if [[ "$EXEC" == "yes" ]]; then
./harbor stop
fi
./harbor start
;;
# Rebuild the containers
pull)
shift 1
${COMPOSE} pull
;;
# Rebuild the containers
rebuild)
shift 1
rebuild "$@"
;;
#
# PHP container commands
#
# If "artisan" or "art" is used, pass to "artisan" inside a container
artisan | art)
shift 1
artisan "$@"
;;
# If "composer" or "comp" is used, pass to "composer" inside a container
composer | comp)
shift 1
composer "$@"
;;
# If "test" is used, run unit tests, pass any extra arguments to phpunit
test)
shift 1
export APP_ENV="testing"
${COMPOSE} run --rm \
php \
./vendor/bin/phpunit "$@"
;;
#
# NPM container commands
#
# If "npm" is used, run npm from our node container
npm)
shift 1
${COMPOSE} run --rm \
node \
npm "$@"
;;
#
# DB container commands
#
# If "db" is used, run psql/mysql from our existing db container
db)
shift 1
db "$@"
;;
# If "pg_dump" is used, run pg_dump from our existing pgsql container
dump)
shift 1
dump "$@"
;;
#
# Global commands
#
# If "ssh" is used, connect to given container with bash
ssh)
shift 1
ssh "$@"
;;
# Sent exec command to docker-compose
exec)
shift 1
${COMPOSE} exec "$@"
;;
# Sent run command to docker-compose
run)
shift 1
${COMPOSE} run "$@"
;;
#
# Init and installation commands
#
# Initialize harbor for existing project
init)
case "$2" in
# Installing laravel project
laravel | craftable)
testRunningContainers
initLaravelCheckAndCopyEnv
initNodeModulesDir
initLaravel
;;
# If not provided, init just standard
*)
testRunningContainers
initNodeModulesDir
prepareContainers
;;
esac
;;
# Installing new laravel or craftable project
create-project | new)
case "$2" in
# Installing new laravel project
laravel)
laravel
;;
# Installing new craftable project
craftable)
craftable "$@"
;;
# If not provided, show what to use
*)
echo $"Usage: $0 $1 {laravel|craftable}"
exit 0
;;
esac
;;
# Else, pass args to docker-compose
*)
${COMPOSE} "$@"
;;
esac
else
# Use the docker-compose ps command if nothing else passed through
${COMPOSE} ps
fi
}
main "$@"