-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage
executable file
·338 lines (287 loc) · 6.93 KB
/
manage
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
#!/bin/bash
# -- INTERNAL CALLS --
internal_header() {
# Nice header
echo
cat build/header.txt
echo
}
internal_check_valid() {
# Check that internal calls don't get run
if [[ $1 == internal_* ]]; then
echo "[!] Internal calls can only be run internally!"
echo
exit 1
fi
}
internal_loadenv() {
# Test if .env is present and load it (or exit)
if [[ -f .env ]]; then
if [[ -z $ENVLOADED ]]; then
source .env
ENVLOADED=true
fi
else
echo "[!] .env file missing - run ${0} init to create"
echo
exit 1
fi
}
internal_resolve_docker() {
# Test for docker/docker-compose executables and save correct calls
which docker &> /dev/null
if [[ $? -ne 0 ]]; then
echo "[!] docker executable not found"
echo
exit 1
fi
docker compose --help &> /dev/null
if [[ $? -eq 0 ]]; then
DOCKERCOMPOSE="docker compose"
else
which docker-compose &> /dev/null
if [[ $? -eq 0 ]]; then
DOCKERCOMPOSE="docker-compose"
else
echo "[!] docker compose not available"
echo
exit 1
fi
fi
}
internal_check_openssl() {
openssl rand 1 &> /dev/null
if [[ $? -ne 0 ]]; then
echo "[!] openssl not installed/available"
fi
}
internal_dcfiles() {
# Build docker compose files string for current configuration
DCFILES="-f build/docker-compose.yml"
if [[ -z $DOCKER_INGRESS ]] || [[ "${DOCKER_INGRESS}" == "local" ]]; then
DCFILES="${DCFILES} -f build/docker-compose.local.yml"
echo "[.] Using local ingress configuration"
elif [[ "${DOCKER_INGRESS}" == "traefik" ]]; then
DCFILES="${DCFILES} -f build/docker-compose.traefik.yml"
echo "[.] Using traefik ingress configuration"
fi
}
internal_init() {
# Initialise new .env file
internal_resolve_docker
internal_check_openssl
if [[ ! -f ".env" ]]; then
touch .env
echo "DOCKER_UID=$(id -u)" >> .env
echo "DOCKER_GID=$(id -g)" >> .env
cat build/.env.example >> .env
echo "[.] .env file created"
fi
## TODO: - move this somewhere else
if [[ ! -d "redis" ]]; then
mkdir redis
echo "[.] redis dump directory created"
fi
}
internal_build_env() {
# Build full .env file
echo "[.] Creating application .env file"
if [[ -f "src/.env" ]]; then
rm src/.env
fi
echo "# AUTO GENERATED - CHANGES NOT PERSISTED" > src/.env
cat .env >> src/.env
cat build/.env.stub >> src/.env
}
internal_build_image() {
# Build core app docker image
echo "[.] Building docker image"
docker build ./build -t ${DOCKER_TAG}
if [[ $? -ne 0 ]]; then
echo "[!] image failed to build"
echo
exit 1
fi
}
internal_run_setup() {
# Run setup script (composer install) on core app container
echo "[.] Running setup script"
internal_isup
docker exec ${DOCKER_NAMESPACE}_app /bin/bash setup.sh
internal_exec_postcheck
}
internal_run_cache() {
# Run lavarel caching commands on core app container
echo "[.] Running cache script"
internal_isup
docker exec ${DOCKER_NAMESPACE}_app /bin/bash cache.sh
internal_exec_postcheck
}
internal_run_postbuild() {
internal_compose_up
internal_run_setup
internal_run_cache
internal_compose_down
}
internal_compose_up() {
# Start docker containers with relevant configurations
internal_dcfiles
${DOCKERCOMPOSE} --env-file ./src/.env -p ${DOCKER_NAMESPACE} ${DCFILES} up -d
if [[ $? -ne 0 ]]; then
echo "[!] containers failed to start"
echo
exit 1
fi
}
internal_compose_down() {
# Stop docker containers
internal_dcfiles
${DOCKERCOMPOSE} --env-file ./src/.env -p ${DOCKER_NAMESPACE} ${DCFILES} down
}
internal_isup() {
# Check if docker containers are up before continuing
internal_loadenv
docker ps | grep ${DOCKER_NAMESPACE}_app &> /dev/null
if [[ $? -ne 0 ]]; then
echo "[!] The app is not currently running - run '${0} start' to bring it up first"
echo
exit 1
fi
}
internal_exec_postcheck() {
# Test if docker exec commands failed (normally indicates app isn't running)
if [[ $? -ne 0 ]]; then
echo "[!] failed - have you started the app?"
echo
exit 1
fi
}
internal_migrate() {
# Run laravel migrations
internal_isup
docker exec -it ${DOCKER_NAMESPACE}_app php artisan migrate
internal_exec_postcheck
}
internal_migrate_force() {
# Run laravel migrations forced (so will run in production without questioning)
internal_isup
docker exec -it ${DOCKER_NAMESPACE}_app php artisan migrate --force
internal_exec_postcheck
}
internal_migrate_rollback() {
# Rollback laravel migrations
internal_isup
docker exec -it ${DOCKER_NAMESPACE}_app php artisan migrate:rollback
internal_exec_postcheck
}
internal_exit() {
echo
exit 0
}
internal_help_message() {
# If we get here then first argument wasn't a valid call -> show help message
# TODO: write this
echo "HELP"
}
# -- EXTERNAL CALLS --
init() {
# Initialise new app (create .env if it doesn't already exist)
echo "[*] Initialising..."
internal_init
echo "[*] done."
internal_exit
}
build() {
# Compile env files and place in app directory; build core app docker image; run setup and cache scripts
internal_loadenv
internal_resolve_docker
echo "[*] Building app..."
internal_build_env
internal_build_image
internal_run_postbuild
echo "[*] done."
internal_exit
}
start() {
# Start the app
internal_loadenv
internal_resolve_docker
echo "[*] Starting app..."
internal_compose_up
internal_run_cache
echo "[*] done."
internal_exit
}
stop() {
# Stop the app
internal_loadenv
internal_resolve_docker
echo "[*] Stopping app..."
internal_compose_down
echo "[*] done."
internal_exit
}
update() {
# Pull lastest version of repo
echo "[*] Pulling latest..."
git pull
echo "[*] done."
internal_exit
}
migrate() {
# Run database migrations in core app container
internal_loadenv
internal_resolve_docker
OPTION=$1
if [[ -z $OPTION ]]; then
echo "[*] Running migrations..."
internal_migrate
elif [[ "${OPTION}" == "force" ]]; then
echo "[*] Force running migrations..."
internal_migrate_force
elif [[ "${OPTION}" == "rollback" ]]; then
echo "[*] Rolling back migrations..."
internal_migrate_rollback
fi
echo "[*] done."
internal_exit
}
add-user() {
internal_loadenv
internal_resolve_docker
echo "[*] Adding new user..."
internal_isup
internal_add_user $1 $2
echo "[*] done - login at ${APP_URL}/login"
internal_exit
}
exec() {
internal_loadenv
internal_resolve_docker
internal_isup
docker exec -it ${DOCKER_NAMESPACE}_app $@
internal_exit
}
tinker() {
internal_loadenv
internal_resolve_docker
internal_isup
docker exec -it --env XDG_CONFIG_HOME=storage/psysh ${DOCKER_NAMESPACE}_app php artisan tinker
internal_exit
}
test() {
internal_loadenv
internal_resolve_docker
internal_isup
docker exec -it ${DOCKER_NAMESPACE}_app php artisan test
internal_exit
}
help () {
internal_help_message
internal_exit
}
# -- RUN --
internal_header
internal_check_valid $1
$@
internal_help_message