-
Notifications
You must be signed in to change notification settings - Fork 45
/
init
executable file
·112 lines (99 loc) · 3.02 KB
/
init
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
#!/bin/bash
# This script will ensure Portus' database is ready to be used. It will keep
# waiting for the db to be usable, but the script will exit with an error
# after a certain amount of failed attempts.
#
# The script will automatically import all the SSL certificates from
# `/certificates` into the final system. This is needed to talk with the
# registry API when this one is protected by TLS.
#
# Finally the script will start apache running Portus via mod_rails.
set -e
wait_for_database() {
should_setup=${1:-0}
TIMEOUT=90
COUNT=0
RETRY=1
while [ $RETRY -ne 0 ]; do
case $(portusctl exec --vendor rails r /srv/Portus/bin/check_db.rb | grep DB) in
"DB_DOWN")
if [ "$COUNT" -ge "$TIMEOUT" ]; then
printf " [FAIL]\n"
echo "Timeout reached, exiting with error"
exit 1
fi
echo "Waiting for mariadb to be ready in 5 seconds"
sleep 5
COUNT=$((COUNT+5))
;;
"DB_EMPTY"|"DB_MISSING")
if [ $should_setup -eq 1 ]; then
# create db, apply schema and seed
echo "Initializing database"
portusctl exec --vendor rake db:setup
if [ $? -ne 0 ]; then
echo "Error at setup time"
exit 1
fi
fi
;;
"DB_READY")
echo "Database ready"
break
;;
esac
done
set -e
}
setup_database() {
wait_for_database 1
}
# Usage: file_env 'XYZ_DB_PASSWORD' 'example'. This code is taken from:
# https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh
file_env() {
local var="$1"
local fileVar="${var}_FILE"
if [ -v "${var}" ] && [ -v "${fileVar}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
if [ -v "${fileVar}" ]; then
val="$(< "${!fileVar}")"
export "$var"="$val"
fi
unset "$fileVar"
}
# Setup environment variables from secrets.
secrets=( PORTUS_DB_PASSWORD PORTUS_PASSWORD PORTUS_SECRET_KEY_BASE
PORTUS_EMAIL_SMTP_PASSWORD PORTUS_LDAP_AUTHENTICATION_PASSWORD )
for s in "${secrets[@]}"; do
if [[ -z "${!s}" ]]; then
file_env "$s"
fi
done
# Ensure additional certificates (e.g. docker registry) are known.
update-ca-certificates
# Further settings
export PORTUS_PUMA_HOST="0.0.0.0:3000"
export RACK_ENV="production"
export RAILS_ENV="production"
export CCONFIG_PREFIX="PORTUS"
if [ -z "$PORTUS_GEM_GLOBAL" ]; then
export GEM_PATH="/srv/Portus/vendor/bundle/ruby/2.6.0"
fi
# On debug, print the environment in which we'll call Portus.
if [ "$PORTUS_LOG_LEVEL" == "debug" ]; then
printenv
fi
# Go to the Portus directory and execute the proper command.
cd /srv/Portus
if [ ! -z "$PORTUS_BACKGROUND" ]; then
wait_for_database
portusctl exec --vendor rails r /srv/Portus/bin/background.rb
elif [ -z "$PORTUS_INIT_COMMAND" ]; then
setup_database
portusctl exec --vendor "pumactl -F /srv/Portus/config/puma.rb start"
else
wait_for_database
portusctl exec --vendor "$PORTUS_INIT_COMMAND"
fi