-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
143 lines (124 loc) · 2.45 KB
/
docker-entrypoint.sh
File metadata and controls
143 lines (124 loc) · 2.45 KB
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
#!/bin/bash
BUILD_NAME="$(date +%Y%m%d_%H%M%S)"
ATTEMPTS=10
test_db() {
for i in $(seq 1 $ATTEMPTS); do
echo 'SELECT 1+1' | PGPASSWORD=$POSTGRES_PASSWORD psql -h postgres -U $POSTGRES_USER
if [ $? -eq 0 ]; then
return
else
echo $i
sleep 1
fi
done
exit 1
}
check_migrations_status() {
test_db
for i in $(seq 1 $ATTEMPTS); do
last_migration_status="$(bundle exec rake db:migrate:status | tail -2 | head -1 | awk '{ print $1 }')"
if [ "$last_migration_status" == "up" ]; then
return
else
echo $i
sleep $i
fi
done
exit 1
}
copy_config_files_from_samples() {
# dev only, for prod they have been already copied onbuild
for i in $(find config -name "*.sample"); do
if [ ! -e "${i/.sample/}" ]; then
cp "$i" "${i/.sample/}"
fi
done
}
copy_assets_files() {
if [ -d /backend/public/admin/assets ]; then
cd /public
mkdir "$BUILD_NAME"
cp -r /backend/public/* "/public/$BUILD_NAME"
ln -sfn "$BUILD_NAME" current
cd -
else
echo 'ERROR! Assets not found!'
fi
}
execute_rake_tasks() {
bundle exec rake payment_plans:update
bundle exec rake payment_plans:update_lifetime
}
migrate_db() {
bundle exec rake db:migrate
}
remove_outdated_pids() {
rm -rf /backend/tmp/pids/server.pid
}
check_gems() {
bundle check || bundle install
}
create_cron_log_files() {
if [ ! -f ./log/cron.log ]; then
touch ./log/cron.log
fi
if [ ! -f ./log/error.log ]; then
touch ./log/error.log
fi
}
start_server() {
exec bundle exec puma --config config/puma.rb
}
start_dev_env() {
copy_config_files_from_samples
remove_outdated_pids
test_db
migrate_db
exec bundle exec rails s
}
run_application() {
if [ "$RAILS_ENV" = 'development' ]; then
start_dev_env
else
test_db
migrate_db
start_server
fi
}
run_cron() {
whenever --update-crontab && cron
create_cron_log_files
tail -f /backend/log/*.log
}
run_sidekiq() {
test_db
exec bundle exec sidekiq
}
run_helper() {
if [ "$RAILS_ENV" != 'development' ]; then
copy_assets_files
fi
check_migrations_status
execute_rake_tasks
}
dispatch() {
if [ "$RAILS_ENV" = 'development' ]; then
check_gems
fi
case "$SERVICE_NAME" in
"sidekiq" )
run_sidekiq
;;
"cron" )
run_cron
;;
"helper" ) # run rake tasks and copy assets file
run_helper
;;
* ) # start rails app
run_application
;;
esac
}
# MAIN
dispatch