This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
executable file
·443 lines (396 loc) · 12.6 KB
/
install.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
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
#!/bin/bash
# filename: install.sh
# author: othyn
# description: Installation script for the docker-compose-laravel repo.
# link: https://github.com/othyn/docker-compose-laravel
##
# Colours.
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
##
RED="\033[0;31m"
GREEN="\033[0;32m"
BLUE="\033[0;34m"
RESET="\033[0m"
##
# Let's begin!
##
clear
printf "[${BLUE}Docker Compose Laravel${RESET}: Project installation]\n"
##
# Helper function for displaying hanging log lines.
##
log() {
# OPTIONAL: This nicely formats numbers passed as the second param
if [ "${2}" = "" ]; then
PRINT_STRING="${1}..."
else
PRINT_NUMBER=$(echo "${2}" | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
PRINT_STRING="${1}: ${PRINT_NUMBER}..."
fi
printf "> %-50s" "${PRINT_STRING}"
# ^^-- Needs to be set to the length of the longest log message, it's
# purely aesthetic, but it aligns all the log termination messages.
}
##
# Flag that the script is processing arguments.
##
log "Processing provided arguments"
##
# Helper functions for displaying termination log lines.
##
logDone() {
printf "${GREEN}done!${RESET}\n"
}
logError() {
printf "${RED}error!${RESET}\n"
printf "${RED}[Error]${RESET} ${1}\n"
exit $2
}
##
# Helper function for displaying usage instructions.
##
showHelp() {
cat << EOF
Usage: $0 -l <new-local-repo> [options]
[required]
-l The local directory of the project to Docker-ise.
E.g. ~/git/new-docker-laravel-project
[options]
-r New, empty, remote repo to setup a new project against.
E.g. git@github.com:othyn/new-docker-laravel-project.git
-p Use HTTPS clone method instead of SSH.
-f Force the local directory, if it exists, it will be removed.
-h Brings up this help screen.
EOF
exit 0
}
##
# Set default args.
##
REPO_DOCKER="git@github.com:othyn/docker-compose-laravel.git"
NEW_PROJECT=0
REPO_REMOTE=""
REPO_LOCAL=""
USE_FORCE=0
##
# Capture provided command args.
# https://stackoverflow.com/a/24868071/4494375
##
while getopts ":r:l:pfh" OPT
do
case $OPT in
r)
NEW_PROJECT=1
REPO_REMOTE="${OPTARG}"
;;
l)
REPO_LOCAL="${OPTARG}"
;;
p)
REPO_DOCKER="https://github.com/othyn/docker-compose-laravel.git"
;;
f)
USE_FORCE=1
;;
h)
logDone
showHelp
;;
\?)
logError "Invalid option: ${OPTARG}" 1
;;
esac
done
# Validate the local repo option is provided
if [ -z "$REPO_LOCAL" ]; then
logError "A local repo is required." 1
fi
##
# Complete: Processing arguments.
##
logDone
##
# Check the supplied local directory for existence if a new project has been instructed to be setup.
# We don't want it to exist ideally, but we can handle it if the user has instructed us to do so.
#
# https://superuser.com/questions/363444/how-do-i-get-the-output-and-exit-value-of-a-subshell-when-using-bash-e
##
if [[ -d "${REPO_LOCAL}" ]] && [[ "${NEW_PROJECT}" == "1" ]] ; then
log "Checking local directory"
if [[ "${USE_FORCE}" == "0" ]] ; then
logError "There is already a local directory at '${REPO_LOCAL}' and the force option was not provided. Please provide the '-f' option or remove the directroy first and try again." 1
else
# https://superuser.com/a/363454/483484
# https://stackoverflow.com/a/48734832/4494375
if ! RESULT=$(rm -rf "${REPO_LOCAL}" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
logDone
fi
##
# Check the supplied local directory for existence if an existing project integration has been instructed to be setup.
# We need the directory to exist to proceed.
#
# https://superuser.com/questions/363444/how-do-i-get-the-output-and-exit-value-of-a-subshell-when-using-bash-e
##
if [[ ! -d "${REPO_LOCAL}" ]] && [[ "${NEW_PROJECT}" == "0" ]] ; then
log "Checking local directory"
logError "The local directory was not found at '${REPO_LOCAL}', the installation cannot proceed." 1
logDone
fi
##
# Clone the repo.
##
log "Cloning remote to local"
if [[ "${NEW_PROJECT}" == "1" ]] ; then
if ! RESULT=$(git clone "${REPO_DOCKER}" "${REPO_LOCAL}" 2>&1) ; then
logError "${RESULT}" $?
fi
else
# As we are working with an existing project, we need to stick it somewhere temporarily
REPO_LOCAL_TEMP="${REPO_LOCAL}_temp"
if ! RESULT=$(git clone "${REPO_DOCKER}" "${REPO_LOCAL_TEMP}" 2>&1) ; then
logError "${RESULT}" $?
fi
# Copy the docker folder to the actual location from the temp clone
if [[ -d "${REPO_LOCAL}/docker" ]] ; then
if ! RESULT=$(rm -rf "${REPO_LOCAL}/docker" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
if ! RESULT=$(cp -R "${REPO_LOCAL_TEMP}/docker" "${REPO_LOCAL}/docker" 2>&1) ; then
logError "${RESULT}" $?
fi
# Copy the default env file to the actual location from the temp clone
if [[ -d "${REPO_LOCAL}/default.env" ]] ; then
if ! RESULT=$(rm -rf "${REPO_LOCAL}/default.env" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
if ! RESULT=$(cp -R "${REPO_LOCAL_TEMP}/default.env" "${REPO_LOCAL}/default.env" 2>&1) ; then
logError "${RESULT}" $?
fi
# Copy the docker compose file to the actual location from the temp clone
if [[ -d "${REPO_LOCAL}/docker-compose.yml" ]] ; then
if ! RESULT=$(rm -rf "${REPO_LOCAL}/docker-compose.yml" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
if ! RESULT=$(cp -R "${REPO_LOCAL_TEMP}/docker-compose.yml" "${REPO_LOCAL}/docker-compose.yml" 2>&1) ; then
logError "${RESULT}" $?
fi
# Clean up the temp clone
if ! RESULT=$(rm -rf "${REPO_LOCAL_TEMP}" 2>&1) ; then
logError "${RESULT}" $?
fi
REPO_LOCAL_TEMP=""
fi
logDone
##
# Enter the repo.
##
log "Entering local repo"
if [[ -d "${REPO_LOCAL}" ]] ; then
cd "${REPO_LOCAL}"
else
logError "Local directory '${REPO_LOCAL}' does not exist, the clone must have failed. Please try again." 1
fi
logDone
##
# The following steps are new project initialisation, so only need to occur during
# new project setup.
##
if [[ "${NEW_PROJECT}" == "1" ]] ; then
##
# Clean the project of git and a few unnecessary files.
##
log "Cleaning project"
if ! RESULT=$(rm -rf "${REPO_LOCAL}/.git" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(rm "${REPO_LOCAL}/install.sh" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(rm "${REPO_LOCAL}/LICENSE" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(rm "${REPO_LOCAL}/README.md" 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Init a new repo.
##
log "Creating a new local repo"
if ! RESULT=$(git init 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Set the remote.
##
log "Setting remote"
if ! RESULT=$(git remote add origin "${REPO_REMOTE}" 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Create a new master branch.
##
log "Creating new master branch with initial commit"
if ! RESULT=$(git add . 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(git commit -m "[AUTO] Implement base docker project from ${REPO_DOCKER}." 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Push the new master to remote setting the new origin so we have something to work with from now on.
##
log "Pushing new remote"
if ! RESULT=$(git push --set-upstream origin master 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
else
##
# Commit the base docker project.
##
log "Committing base docker project"
if ! RESULT=$(git add . 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(git commit -m "[AUTO] Updated base docker project to latest version from ${REPO_DOCKER}." 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
fi
##
# Laravel installation steps only required on a new installation.
##
if [[ "${NEW_PROJECT}" == "1" ]] ; then
##
# Create a new Laravel project.
##
log "Creating a new Laravel project"
if ! RESULT=$(laravel new ./ --force --quiet 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Commit the the new Laravel project.
##
log "Committing the new Laravel project"
if ! RESULT=$(git add . 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(git commit -m "[AUTO] Created a new Laravel installation." 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
fi
##
# Get a hold of the default.env values to be merged into the local installation
# .env and env.example files.
#
# https://gist.github.com/judy2k/7656bfe3b322d669ef75364a46327836
##
export $(egrep -v '^#' ${REPO_LOCAL}/default.env | xargs)
##
# Configure the Laravel project.
##
log "Configure the Laravel project"
if ! RESULT=$(sed -i "" -e "/DB_HOST/s/.*/DB_HOST=${DB_HOST}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_HOST/s/.*/DB_HOST=${DB_HOST}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_PORT/s/.*/DB_PORT=${DB_PORT}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_PORT/s/.*/DB_PORT=${DB_PORT}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_DATABASE/s/.*/DB_DATABASE=${DB_DATABASE}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_DATABASE/s/.*/DB_DATABASE=${DB_DATABASE}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_USERNAME/s/.*/DB_USERNAME=${DB_USERNAME}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_USERNAME/s/.*/DB_USERNAME=${DB_USERNAME}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_PASSWORD/s/.*/DB_PASSWORD=${DB_PASSWORD}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/DB_PASSWORD/s/.*/DB_PASSWORD=${DB_PASSWORD}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! grep -q "# Docker config" "${REPO_LOCAL}/.env"; then
if ! RESULT=$(echo -e "\n# Docker config\nDB_EXT_PORT=${DB_EXT_PORT}\nWEBSERVER_PORT=${WEBSERVER_PORT}\nWEBSERVER_EXT_PORT=${WEBSERVER_EXT_PORT}" >> "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
else
if ! RESULT=$(sed -i "" -e "/nDB_EXT_PORT/s/.*/nDB_EXT_PORT=${nDB_EXT_PORT}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/nWEBSERVER_PORT/s/.*/nWEBSERVER_PORT=${nWEBSERVER_PORT}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/nWEBSERVER_EXT_PORT/s/.*/nWEBSERVER_EXT_PORT=${nWEBSERVER_EXT_PORT}/" "${REPO_LOCAL}/.env" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
if ! grep -q "# Docker config" "${REPO_LOCAL}/.env.example"; then
if ! RESULT=$(echo -e "\n# Docker config\nDB_EXT_PORT=${DB_EXT_PORT}\nWEBSERVER_PORT=${WEBSERVER_PORT}\nWEBSERVER_EXT_PORT=${WEBSERVER_EXT_PORT}" >> "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
else
if ! RESULT=$(sed -i "" -e "/nDB_EXT_PORT/s/.*/nDB_EXT_PORT=${nDB_EXT_PORT}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/nWEBSERVER_PORT/s/.*/nWEBSERVER_PORT=${nWEBSERVER_PORT}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(sed -i "" -e "/nWEBSERVER_EXT_PORT/s/.*/nWEBSERVER_EXT_PORT=${nWEBSERVER_EXT_PORT}/" "${REPO_LOCAL}/.env.example" 2>&1) ; then
logError "${RESULT}" $?
fi
fi
logDone
##
# Clean up the temporary default.env file.
##
log "Clean up the temporary default.env file"
if ! RESULT=$(rm "${REPO_LOCAL}/default.env" 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Commit the the Laravel project.
##
log "Committing the Laravel project"
if ! RESULT=$(git add . 2>&1) ; then
logError "${RESULT}" $?
fi
if ! RESULT=$(git commit -m "[AUTO] Configured Laravel installation after installing docker configuration." 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# Git push.
##
log "Pushing to remote"
if ! RESULT=$(git push 2>&1) ; then
logError "${RESULT}" $?
fi
logDone
##
# That's it! Magic. 🎉
##
printf "${GREEN}[Installation complete!]${RESET}\n"