-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdev
executable file
·498 lines (429 loc) · 12.6 KB
/
dev
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
#!/usr/bin/env bash
# vim: et:sw=2:ts=2:ai
# Load environment variables
load_env() {
# We use "tr" to translate the uppercase "uname" output into lowercase
UNAME=$(uname -s | tr '[:upper:]' '[:lower:]')
# Then we map the output to the names used on the Github releases page
case "$UNAME" in
linux*) MACHINE=linux;;
darwin*) MACHINE=macos;;
mingw*) MACHINE=windows;;
esac
export MACHINE
# OSX requires coreutils
if [[ "${MACHINE}" == "macos" ]] && ! which gtouch > /dev/null; then
echo "Required tools are missing, please run: brew install coreutils"
exit 1
fi
if [[ "${MACHINE}" == "macos" ]]; then
touch() {
gtouch "$@"
}
fi
# Set the default environment
export SPECIFIED_ENV=dev
# Ensure the docker host is accessible
if [[ "${MACHINE}" == "windows" ]]; then
HOST_IP=host.docker.internal
elif [[ "${MACHINE}" == "macos" ]]; then
HOST_IP=host.docker.internal
else
HOST_IP=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
fi
export HOST_IP
# Ensure an SSH socket is available
if [[ "$SSH_AUTH_SOCK" == "" ]]; then
SSH_AUTH_SOCK="/tmp/.ssh-sock"
ssh-agent -a "${SSH_AUTH_SOCK}"
fi
export SSH_AUTH_SOCK
source_env "${APP_PROJECT_PATH}"
PROJECT="${APP_PROJECT}"
export PROJECT
CACHE_DIR="${HOME}/.cache/development-manager-docker-compose"
export CACHE_DIR
GID=$(id -g)
export GID
export UID
if [[ "${MACHINE}" == "windows" ]]; then
DC="winpty docker-compose"
CUID="1000"
CGID="1000"
CHOME="/home/app"
else
DC="docker-compose"
CUID="${UID}"
CGID="${GID}"
CHOME="${HOME}"
fi
export CUID
export CGID
export CHOME
}
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_\-]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
sub(/-/, "_", vn)
sub(/-/, "_", $2)
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
source_env() {
# Configure .env files
local env="${1}/.env"
local env_local="${1}/.env.local"
# Load all variables as if they are exported
set -a
# Check and load if environment .env file exists
# shellcheck source=.env
if [ -e "$env" ]; then
source "$env"
fi
# Check and load if environment local .env file exists
# shellcheck source=.env.local
if [ -e "$env_local" ]; then
source "$env_local"
fi
# Configure .env files for specified SPECIFIED_ENV
local specified_env="${1}/.env.${SPECIFIED_ENV}"
local specified_env_local="${1}/.env.${SPECIFIED_ENV}.local"
# Check and load if environment specific .env file exists
# shellcheck source=.env.dev
if [ -e "$specified_env" ]; then
source "$specified_env"
fi
# Check and load if environment specific local .env file exists
# shellcheck source=.env.dev.local
if [ -e "$specified_env_local" ]; then
source "$specified_env_local"
fi
# Don't automatically export set variables
set +a
}
# Container for all supported commands
run() {
# Initialize primary variables
APP_SELF_PATH=$(dirname "$(realpath "$0")")
APP_PROJECT_PATH=$(pwd)
APP_PROJECT=$(basename "${APP_SELF_PATH}")
# Turn on and attach to specified container
attach() {
$DC up "$1"
return $?
}
# Check system and update if marked old
autoupdate() {
local cache_limit
local cache_project
local cache_tool
cache_limit="${CACHE_DIR}/limit"
cache_project="${CACHE_DIR}/$(pwd | base64)"
cache_tool="${CACHE_DIR}/dev"
# Ensure cache directory exists
test ! -d "${CACHE_DIR}" && mkdir -p "${CACHE_DIR}"
# Remove old cache files
find "${CACHE_DIR}" -type f -mtime +1 -delete
# Set limit of last check
touch -d '-16 hours' "${cache_limit}"
# Ensure a last check file exists
test ! -f "${cache_project}" && touch -d '-17 hours' "${cache_project}"
# Ensure a last check file exists
test ! -f "${cache_tool}" && touch -d '-17 hours' "${cache_tool}"
# Check if the last check exceeds the given limit
if [ "${cache_limit}" -nt "${cache_project}" ]; then
touch "${cache_project}"
echo "Checking if new images are available ..."
# Run the pull command and download images if available
pull
fi
# Check if the last check exceeds the given limit
if [ "${cache_limit}" -nt "${cache_tool}" ]; then
touch "${cache_tool}"
# Run the self update command
selfupdate
fi
check
}
# Open the shell of a container
console() {
autoupdate
local shell="console"
[ "$1" != "" ] && shell=$1
$DC run \
--rm \
-u "$(id -u):$(id -g)" \
--no-deps \
"$shell"
return $?
}
# Checks whether the current template version of the project is still the latest
check() {
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
if [ -e "docker-compose.yml" ]; then
eval "$(parse_yaml docker-compose.yml "COMPOSE_VAR_")" &> /dev/null
elif [ -e "docker-compose.yaml" ]; then
eval "$(parse_yaml docker-compose.yaml "COMPOSE_VAR_")" &> /dev/null
fi
if [ -n "${COMPOSE_VAR_x_custom_type}" ]; then
local latestFilePath
latestFilePath="${APP_SELF_PATH}/templates/${COMPOSE_VAR_x_custom_type}/docker-compose.yml"
eval "$(parse_yaml $latestFilePath "COMPOSE_LATEST_VAR_")" &> /dev/null
if [ -n "${COMPOSE_VAR_x_custom_version}" ] || [ -n "${COMPOSE_LATEST_VAR_x_custom_version}" ] && [ "${COMPOSE_LATEST_VAR_x_custom_version}" != "${COMPOSE_VAR_x_custom_version}" ]; then
echo "The current docker template is outdated, please run \"dev init ${COMPOSE_VAR_x_custom_type} -f\" to update it."
else
echo "The current docker template is up-to-date."
fi
else
echo "The current docker template is outdated, please run \"dev init TEMPLATE_NAME -f\" to update it."
fi
fi
}
# Run a Docker Compose command
dc() {
$DC "$@"
return $?
}
# Turn off all containers or a specific container
down() {
# If there is a docker-compose file present in the current directory, use the default.
if [ -e "docker-compose.yml" ] || [ -e "docker-compose.yaml" ]; then
$DC down "$@"
else
if [ -e "${CACHE_DIR}/known_locations" ]; then
while read -r location
do
if [ -e "${location}/docker-compose.yml" ]; then
source_env "${location}"
$DC -f "${location}/docker-compose.yml" down "$@"
fi
done < "${CACHE_DIR}/known_locations"
fi
fi
return $?
}
# Execute a command on a specified container
exec() {
$DC exec "$@"
return $?
}
# Get help for commands
help() {
attach() {
echo "Attach to a container."
echo "Usage: $0 attach <container-name>"
return 0
}
console() {
echo "Open up a console on a service."
echo "Usage: $0 console [service]"
echo "Options:"
echo " service: Defaults to console"
echo "Available services:"
load_env
$DC ps --services | xargs echo " "
return 0
}
check() {
echo "Check whether the currently used template is up-to-date."
return 0
}
dc() {
echo "Execute a Docker Compose command directly, using configured .env* files."
echo "Usage: $0 dc [options...]"
return 0
}
down() {
echo "Turn off development environment."
echo "Usage: $0 down [options...]"
echo "For more info, see: docker-compose help down"
return 0
}
exec() {
echo "Execute a command on a specified container"
echo "Usage: $0 exec <container> <command> [options...]"
echo "Example: $0 exec redis redis-cli flushall"
echo "For more info, see: docker-compose help exec"
return 0
}
init() {
echo "Initialize a project type for development."
echo "Usage: $0 init [-f|--force] <project-type>"
echo "Available project types: "
for type in "${APP_SELF_PATH}"/templates/*; do
echo "- $(basename "${type}")"
done
return 0
}
logs() {
echo "Display container output logs."
echo "Usage: $0 logs [-f] [service]"
echo "For more info, see: docker-compose help logs"
return 0
}
ps() {
echo "Display container status for current project"
echo "Usage: $0 ps"
echo "For more info, see: docker-compose help ps"
return 0
}
pull() {
echo "Pull latest development environment images."
echo "Usage: $0 pull"
echo "For more info, see: docker-compose help pull"
return 0
}
run() {
echo "Run a command on the development environment."
echo "Usage: $0 run <command>"
echo "For more info, see: docker-compose help run"
return 0
}
selfupdate() {
echo "Run self update."
echo "Usage: $0 selfupdate"
return 0
}
up() {
echo "Turn on development environment."
echo "Usage: $0 up [options...]"
echo "For more info, see: docker-compose help up"
return 0
}
# Attempt to run a command or output the help
if [ "$(type -t "$1")" == "function" ]; then
"$@"
return $?
else
echo "Usage: $0 <option>"
echo "The following options are available"
echo " console: Open the console"
echo " dc: Execute a Docker Compose command"
echo " down: Stop the environment"
echo " exec: Execute a command on a container"
echo " init: Initialize a project based on a template"
echo " logs: Display container output logs"
echo " ps: Display container status"
echo " pull: Pull latest images"
echo " run: Run a command"
echo " selfupdate: Run self update"
echo " up: Start the environment"
fi
return $?
}
# Initialize a project type for development
# shellcheck disable=SC2120
init() {
local force=0
local type=""
for option in "$@"; do
case "$option" in
-f|--force)
force=1
;;
*)
if [ "$type" == "" ]; then
type="$option"
fi
;;
esac
done
if [ "${type}" == "" ]; then
echo "Error: Type not given"
help init
exit 1
fi
if [ ! -e "${APP_SELF_PATH}"/templates/"${type}" ]; then
echo "Error: Type '$type' not found."
exit 1
fi
if [ ! -e "${APP_PROJECT_PATH}/docker-compose.yml" ] && [ ! -e "${APP_PROJECT_PATH}/docker-compose.yaml" ] || [ "$force" == "1" ]; then
cp -prf "${APP_SELF_PATH}"/templates/"${type}"/* "${APP_PROJECT_PATH}"
cp -prf "${APP_SELF_PATH}"/templates/"${type}"/.[!.]* "${APP_PROJECT_PATH}"
echo "Initialized $type project."
return $?
elif [ -e "${APP_PROJECT_PATH}/docker-compose.yml" ] || [ -e "${APP_PROJECT_PATH}/docker-compose.yaml" ]; then
echo "Warning: Project already initialized. Use --force to re-initialize the project"
exit 1
fi
run help init
exit 1
}
# Show container output logs
logs() {
$DC logs "$@"
return $?
}
# Show container status
ps() {
$DC ps "$@"
return $?
}
# Pull latest images
pull() {
$DC pull "$@"
return $?
}
# Run a command
run() {
autoupdate
$DC run console "$@"
return $?
}
# Try to self update
selfupdate() {
# shellcheck disable=SC2164
pushd "${APP_SELF_PATH}" > /dev/null
git pull origin master
chmod +x "$0"
# shellcheck disable=SC2164
popd > /dev/null
}
# Turn on all containers or a specific container
up() {
autoupdate
if ! grep -qxF $(pwd) "${CACHE_DIR}/known_locations" &> /dev/null; then
pwd >> "${CACHE_DIR}/known_locations"
fi
$DC up -d "$@"
return $?
}
# Attempt to run a command or output the help
if [ "$(type -t "$1")" == "function" ]; then
if [ "$1" == "init" ] || [ "$1" == "help" ] || load_env; then
"$@"
fi
return $?
else
help "$@"
return 1
fi
}
# Main process execution
main() {
# Ensure root does not use this
if [[ "$(id -u)" == "0" ]]; then
echo "This script should not be run as root."
exit 1
fi
# Ensure this is not executed from a docker container
if [[ -e /proc/1/cgroup ]] && grep -c docker /proc/1/cgroup > /dev/null; then
echo "This script should not be run from a docker container."
exit 1
fi
# Run the tool
run "$@"
exit $?
}
# Run the manager
main "$@"