forked from BRACKETS-by-TRIAD/craftable-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harbor
executable file
·547 lines (447 loc) · 16.7 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
#!/usr/bin/env bash
TYPE=php
VERSION=1.0.0
# check system if supported
UNAMEOUT="$(uname -s)"
case "${UNAMEOUT}" in
Linux*) MACHINE=linux;;
Darwin*) MACHINE=mac;;
*) MACHINE="UNKNOWN"
esac
if [[ "$MACHINE" == "UNKNOWN" ]]; then
echo "Unsupported system type"
echo "System must be a Macintosh or Linux"
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)"
fi
# set xdebug host for developing
if [[ $(docker version --format '{{.Server.Version}}') > 18.03.0 ]]; then
export XDEBUG_HOST=host.docker.internal
else
if [[ "$MACHINE" == "linux" ]]; then
export XDEBUG_HOST=$(/sbin/ifconfig docker0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1)
elif [[ "$MACHINE" == "mac" ]]; then
export XDEBUG_HOST=$(ipconfig getifaddr en0) # Ethernet
if [[ -z "$XDEBUG_HOST" ]]; then
export XDEBUG_HOST=$(ipconfig getifaddr en1) # Wifi
fi
fi
fi
# set sed command
if [[ "$MACHINE" == "linux" ]]; then
SEDCMD="sed -i"
elif [[ "$MACHINE" == "mac" ]]; then
SEDCMD="sed -i .bak"
fi
# making harbor command available
chmod +x ./harbor
# prepare default env variables
export DOCKER_APP_PORT=${DOCKER_APP_PORT:-80}
export DOCKER_PGSQL_PORT=${DOCKER_PGSQL_PORT:-5432}
export DOCKER_PGSQL_TEST_PORT=${DOCKER_PGSQL_TEST_PORT:-5433}
export DOCKER_PGSQL_TEST_DIR=${DOCKER_PGSQL_TEST_DIR:-./docker/testing/db}
export DOCKER_PHP_VERSION=${DOCKER_PHP_VERSION:-7.2}
export DOCKER_POSTGRES_VERSION=${DOCKER_POSTGRES_VERSION:-10}
export DOCKER_NODE_VERSION=${DOCKER_NODE_VERSION:-8}
export DOCKER_PHP_XDEBUG=${DOCKER_PHP_XDEBUG:-off}
export DB_DATABASE=${DB_DATABASE:-homestead}
export DB_USERNAME=${DB_USERNAME:-homestead}
export DB_PASSWORD=${DB_PASSWORD:-secret}
# 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"
function testRunningContainers {
if [[ ! -z $(docker ps -q) ]]; then
echo "You have some running containers. Are you sure that they are not blocking required ports: $DOCKER_APP_PORT, $DOCKER_PGSQL_PORT, $DOCKER_PGSQL_TEST_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
}
function modifyEnv {
if [[ -f .env ]]; then
# source actual env values
source .env
# modify values
${SEDCMD} "s/APP_URL=.*/APP_URL=http:\/\/localhost:"${DOCKER_APP_PORT}"/" .env
${SEDCMD} "s/DB_CONNECTION=.*/DB_CONNECTION=pgsql/" .env
${SEDCMD} "s/DB_HOST=.*/DB_HOST=pgsql/" .env
${SEDCMD} "s/DB_PORT=.*/DB_PORT=5432/" .env
${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=redis/" .env
${SEDCMD} "s/SESSION_DRIVER=.*/SESSION_DRIVER=redis/" .env
${SEDCMD} "s/REDIS_HOST=.*/REDIS_HOST=redis/" .env
# removed bak file
if [[ -f .env.bak ]]; then
rm .env.bak
fi
fi
}
function setHarborInEnv {
if [[ ! ".env" == "$1" ]] && [[ ! ".env.example" == "$1" ]]; then
return
fi
# source actual env values
if [[ ".env" == "$1" ]]; then
source .env
fi
echo "Adding values to $1 ..."
# add or modify values in .env/.env.example
if grep -Fxq "#docker-config" $1; then
echo "Docker config already in .env.example"
else
echo -e "" >> $1
echo -e "#docker-config" >> $1
fi
if grep -Fxq "DOCKER_APP_PORT" $1; then
${SEDCMD} "s/DOCKER_APP_PORT=.*/DOCKER_APP_PORT="${DOCKER_APP_PORT}"/" $1
else
echo -e "DOCKER_APP_PORT="${DOCKER_APP_PORT}"" >> $1
fi
if grep -Fxq "DOCKER_PGSQL_PORT" $1; then
${SEDCMD} "s/DOCKER_PGSQL_PORT=.*/DOCKER_PGSQL_PORT="${DOCKER_PGSQL_PORT}"/" $1
else
echo -e "DOCKER_PGSQL_PORT="${DOCKER_PGSQL_PORT}"" >> $1
fi
if grep -Fxq "DOCKER_PGSQL_TEST_PORT" $1; then
${SEDCMD} "s/DOCKER_PGSQL_TEST_PORT=.*/DOCKER_PGSQL_TEST_PORT="${DOCKER_PGSQL_TEST_PORT}"/" $1
else
echo -e "DOCKER_PGSQL_TEST_PORT="${DOCKER_PGSQL_TEST_PORT}"" >> $1
fi
if grep -Fxq "DOCKER_PGSQL_TEST_DIR" $1; then
${SEDCMD} "s/DOCKER_PGSQL_TEST_DIR=.*/DOCKER_PGSQL_TEST_DIR="${DOCKER_PGSQL_TEST_DIR}"/" $1
else
echo -e "DOCKER_PGSQL_TEST_DIR="${DOCKER_PGSQL_TEST_DIR}"" >> $1
fi
if grep -Fxq "DOCKER_PHP_VERSION" $1; then
${SEDCMD} "s/DOCKER_PHP_VERSION=.*/DOCKER_PHP_VERSION="${DOCKER_PHP_VERSION}"/" $1
else
echo -e "DOCKER_PHP_VERSION="${DOCKER_PHP_VERSION}"" >> $1
fi
if grep -Fxq "DOCKER_POSTGRES_VERSION" $1; then
${SEDCMD} "s/DOCKER_POSTGRES_VERSION=.*/DOCKER_POSTGRES_VERSION="${DOCKER_POSTGRES_VERSION}"/" $1
else
echo -e "DOCKER_POSTGRES_VERSION="${DOCKER_POSTGRES_VERSION}"" >> $1
fi
if grep -Fxq "DOCKER_NODE_VERSION" $1; then
${SEDCMD} "s/DOCKER_NODE_VERSION=.*/DOCKER_NODE_VERSION="${DOCKER_NODE_VERSION}"/" $1
else
echo -e "DOCKER_NODE_VERSION="${DOCKER_NODE_VERSION}"" >> $1
fi
if grep -Fxq "DOCKER_PHP_XDEBUG" $1; then
${SEDCMD} "s/DOCKER_PHP_XDEBUG=.*/DOCKER_PHP_XDEBUG="${DOCKER_PHP_XDEBUG}"/" $1
else
echo -e "DOCKER_PHP_XDEBUG="${DOCKER_PHP_XDEBUG}"" >> $1
fi
}
# main script
# If we pass any arguments...
if [[ $# -gt 0 ]]; then
# Source .env, which can over-ride env vars
if [[ -f .env ]]; then
source .env
fi
if [[ ! "$(docker volume ls -q | grep composer-cache)" ]]; then
docker volume create --driver local composer-cache
fi
case "$1" in
# Start up containers
start)
# creating dir for testing database if not exists already
if [[ ! -d ${DOCKER_PGSQL_TEST_DIR} ]]; then
mkdir -p ${DOCKER_PGSQL_TEST_DIR}
fi
${COMPOSE} up -d
;;
# Stop the containers
stop)
shift 1
${COMPOSE} down "$@"
;;
# Restart the containers
restart)
if [[ "$EXEC" == "yes" ]]; then
./harbor stop
fi
./harbor start
;;
# Rebuild the containers
rebuild)
./harbor start
if [[ "$EXEC" == "yes" ]]; then
shift 1
REMOVE_IMAGES=""
REMOVE_VOLUMES=""
for i in "$@"
do
case ${i} in
-d|--database)
DATABASE=true
;;
-i|--images)
REMOVE_IMAGES="--rmi all"
;;
-v|--volumes)
REMOVE_VOLUMES="-v"
;;
*)
;;
esac
done
${COMPOSE} down ${REMOVE_VOLUMES} ${REMOVE_IMAGES}
if [[ "$DATABASE" == "true" ]]; then
rm -rf ${DOCKER_PGSQL_TEST_DIR}/*
fi
fi
${COMPOSE} build
;;
# If "artisan" or "art" is used, pass to "artisan" inside a container
artisan|art)
shift 1
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
php \
php artisan "$@"
else
${COMPOSE} run --rm \
php \
php artisan "$@"
fi
;;
# If "composer" or "comp" is used, pass to "composer" inside a container
composer|comp)
shift 1
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
php \
composer "$@"
else
${COMPOSE} run --rm \
php \
composer "$@"
fi
;;
# 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 "$@"
;;
# If "npm" is used, run npm from our node container
npm)
shift 1
${COMPOSE} run --rm \
node \
npm "$@"
;;
# If "yarn" is used, run yarn from our node container
yarn)
shift 1
${COMPOSE} run --rm \
node \
yarn "$@"
;;
# If "gulp" is used, run gulp from our node container
gulp)
shift 1
${COMPOSE} run --rm \
node \
./node_modules/.bin/gulp "$@"
;;
# If "psql" is used, run psql from our existing pgsql container
psql)
shift 1
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
pgsql \
psql -U ${DB_USERNAME} -h localhost ${DB_DATABASE} "$@"
fi
;;
# If "pg_dump" is used, run pg_dump from our existing pgsql container
pg_dump)
shift 1
if [[ "$EXEC" == "yes" ]]; then
${COMPOSE} exec \
pgsql \
pg_dump -U ${DB_USERNAME} -h localhost ${DB_DATABASE} "$@"
fi
;;
# Initialize harbor for existing project
init)
echo "Setting up your docker environment for this laravel based project."
testRunningContainers
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 0
fi
# cp .env.example .env if .env is missing
if [[ ! -f .env ]] && [[ -f .env.example ]]; then
cp .env.example .env
fi
echo "Changing .env ..."
# change values in .env file
modifyEnv
echo "Adding predis/predis ..."
# composer require predis and install - in a composer container
# TODO somehow get response if finished ok - (exit \$?) not working
${COMPOSE} run --rm php composer require predis/predis
${COMPOSE} run --rm php composer install
# source actual env values
source .env
# art key:generate
if [[ -z "${APP_KEY}" ]]; then
${COMPOSE} run --rm php php artisan key:generate
# source actual env values
source .env
fi
echo "Restarting containers ..."
# restart docker containers
./harbor restart
echo "Migrating ..."
# migrate and seed
./harbor art migrate --seed
echo "Npm ..."
# npm install
./harbor npm install
# npm run dev
./harbor npm run dev
# stop docker containers
./harbor stop
;;
# Installing new laravel or craftable project
new)
case "$2" in
# Installing new laravel project
laravel)
echo "Installing new laravel application to current folder."
# remove all containers and volumes
${COMPOSE} down -v
testRunningContainers
# laravel new
${COMPOSE} run --rm php laravel new ./application
# move application content to this folder
mv application/* application/.[!.]* .
rm -rf ./application
echo "Laravel installed, initializing ..."
# setting harbor in .env and .env.example
echo "Setting harbor in .env and .env.example..."
setHarborInEnv .env.example
setHarborInEnv .env
echo "Changing .env ..."
# change values in .env file
modifyEnv
echo "Adding predis/predis ..."
# composer require predis and install - in a composer container
# TODO somehow get response if finished ok - (exit \$?) not working
${COMPOSE} run --rm php composer require predis/predis
${COMPOSE} run --rm php composer install
# source actual env values
source .env
# art key:generate
if [[ -z "${APP_KEY}" ]]; then
${COMPOSE} run --rm php php artisan key:generate
# source actual env values
source .env
fi
echo "Restarting containers ..."
# restart docker containers
./harbor restart
echo "Migrating ..."
# migrate and seed
./harbor art migrate --seed
echo "Npm ..."
# npm install
./harbor npm install
# npm run dev
./harbor npm run dev
# stop docker containers
./harbor stop
;;
# Installing new craftable project
craftable)
echo "Installing new craftable application to current folder."
# remove all containers and volumes
${COMPOSE} down -v
testRunningContainers
# craftable new
# use php container to create craftable new without install
if [[ "$3" == "--dev" ]]; then
${COMPOSE} run --rm php craftable new --dev --no-install ./application
else
${COMPOSE} run --rm php craftable new --no-install ./application
fi
# move application content to this folder
mv application/* application/.[!.]* .
rm -rf ./application
echo "Craftable installed, initializing ..."
# setting harbor in .env and .env.example
echo "Setting harbor in .env and .env.example..."
setHarborInEnv .env.example
setHarborInEnv .env
echo "Changing .env ..."
# change values in .env file
modifyEnv
echo "Adding predis/predis ..."
# composer require predis and install - in a composer container
# TODO somehow get response if finished ok - (exit \$?) not working
${COMPOSE} run --rm php composer require predis/predis
${COMPOSE} run --rm php composer install
# source actual env values
source .env
echo "Restarting containers ..."
# restart docker containers
./harbor restart
echo "Installing craftable ..."
# install craftable
./harbor art craftable:install
echo "Npm ..."
# npm install
./harbor npm install
# npm run dev
./harbor npm run dev
# stop docker containers
./harbor stop
echo "All set."
;;
# If not provided, show what to use
*)
echo $"Usage: $0 $1 {laravel|craftable}"
exit 1
;;
esac
;;
# Get version
-v|--version)
echo ${VERSION}
;;
# Get type
-t|--type)
echo ${TYPE}
;;
# Else, pass args to docker-compose
*)
${COMPOSE} "$@"
;;
esac
else
# Use the docker-compose ps command if nothing else passed through
${COMPOSE} ps
fi