-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint
executable file
·52 lines (41 loc) · 2.05 KB
/
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
#! /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:="/usr/src/app"}
: ${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
# 7: 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
if [[ "$1" = "rails" || "$1" = "sidekiq" || "$1" = "guard" ]]
then
# 3: Wait until the setup 'lock' file no longer exists:
while [ -f $APP_SETUP_LOCK ]; do wait_setup; done
# 4: '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
# 5: 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 `checkdb` (inside our app's `bin` folder), instead
# of running `rails db:version` to avoid loading the entire rails app for this
# simple check:
bundle exec checkdb || setup
# 6: 'Unlock' the setup process:
unlock_setup
# 8: If the command to execute is 'rails server', then force it to write the
# pid file into a non-shared container directory. Suddenly killing and removing
# app containers without this would leave a pidfile in the project's tmp dir,
# preventing the app container from starting up on further attempts:
if [[ "$2" = "s" || "$2" = "server" ]]; then rm -rf /usr/src/app/tmp/pids/server.pid; fi
fi
# 9: Execute the given or default command:
exec "$@"