-
Notifications
You must be signed in to change notification settings - Fork 70
/
development-entrypoint
executable file
·56 lines (45 loc) · 2.29 KB
/
development-entrypoint
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
#! /bin/bash
# The Docker App Container's development entrypoint.
# This is a script used by the project's Docker development environment to
# setup the app containers and databases upon runnning.
set -e
: ${APP_PATH:="/app/user"}
: ${APP_TEMP_PATH:="$APP_PATH/tmp"}
: ${APP_SETUP_LOCK:="$APP_TEMP_PATH/setup.lock"}
: ${APP_SETUP_WAIT:="5"}
# 1: Define the functions lock and unlock our app containers setup processes:
function lock_setup { mkdir -p $APP_TEMP_PATH && touch $APP_SETUP_LOCK; }
function unlock_setup { rm -rf $APP_SETUP_LOCK; }
function wait_setup { echo "Waiting for app setup to finish..."; sleep $APP_SETUP_WAIT; }
# 2: 'Unlock' the setup process if the script exits prematurely:
trap unlock_setup HUP INT QUIT KILL TERM EXIT
# 3: Specify a default command, in case it wasn't issued:
if [ -z "$1" ]; then set -- rails server -p 3000 -b 0.0.0.0 "$@"; fi
# 4: Run the app setup only if the requested command requires it:
if [[ "$1" = "rails" || "$1" = "sidekiq" ]]
then
# 5: Wait until the setup 'lock' file no longer exists:
while [ -f $APP_SETUP_LOCK ]; do wait_setup; done
# 6: 'Lock' the setup process, to prevent a race condition when the project's
# app containers will try to install gems and setup the database concurrently:
lock_setup
# 7: Check if the database exists, or setup the database if it doesn't, as it is
# the case when the project runs for the first time.
#
# We'll use a custom script `check_or_setup_db` (inside our app's `bin` folder),
# instead of running `rake db:version || rake db:setup`, as running that command
# (at least on rails 4.2.4) will leave a couple of small ruby zombie processes
# running in the container:
bundle exec check_or_setup_db
# 8: 'Unlock' the setup process:
unlock_setup
# 9: If the command to execute is 'rails server', then force it to write the pid
# file into an private directory - suddenly killing and removing app containers
# without this would leave a pidfile in the project's tmp dir, preventing containers
# from starting up on further attempts:
if [[ ("$1" = "rails" || ("$1" = "bundle" && "$2" = "exec" && "$3" = "rails")) && (("$2" = "s" || "$2" = "server") || ("$4" = "s" || "$4" = "server")) ]]; then
rm -rf "${APP_TEMP_PATH}/pids/server.pid";
fi
fi
# 10: Execute the given or default command:
exec "$@"