1
+ #! /bin/bash
2
+
3
+ # This script will ensure Portus' database is ready to be used. It will keep
4
+ # waiting for the db to be usable, but the script will exit with an error
5
+ # after a certain amount of failed attempts.
6
+ #
7
+ # The script will automatically import all the SSL certificates from
8
+ # `/certificates` into the final system. This is needed to talk with the
9
+ # registry API when this one is protected by TLS.
10
+ #
11
+ # Finally the script will start apache running Portus via mod_rails.
12
+
13
+ set -e
14
+
15
+ wait_for_database () {
16
+ should_setup=${1:- 0}
17
+
18
+ TIMEOUT=90
19
+ COUNT=0
20
+ RETRY=1
21
+
22
+ while [ $RETRY -ne 0 ]; do
23
+ case $( portusctl exec --vendor rails r /srv/Portus/bin/check_db.rb | grep DB) in
24
+ " DB_DOWN" )
25
+ if [ " $COUNT " -ge " $TIMEOUT " ]; then
26
+ printf " [FAIL]\n"
27
+ echo " Timeout reached, exiting with error"
28
+ exit 1
29
+ fi
30
+ echo " Waiting for mariadb to be ready in 5 seconds"
31
+ sleep 5
32
+ COUNT=$(( COUNT+ 5 ))
33
+ ;;
34
+ " DB_EMPTY" |" DB_MISSING" )
35
+ if [ $should_setup -eq 1 ]; then
36
+ # create db, apply schema and seed
37
+ echo " Initializing database"
38
+ portusctl exec --vendor rake db:setup
39
+ if [ $? -ne 0 ]; then
40
+ echo " Error at setup time"
41
+ exit 1
42
+ fi
43
+ fi
44
+ ;;
45
+ " DB_READY" )
46
+ echo " Database ready"
47
+ break
48
+ ;;
49
+ esac
50
+ done
51
+ set -e
52
+ }
53
+
54
+ setup_database () {
55
+ wait_for_database 1
56
+ }
57
+
58
+ # Usage: file_env 'XYZ_DB_PASSWORD' 'example'. This code is taken from:
59
+ # https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh
60
+ file_env () {
61
+ local var=" $1 "
62
+ local fileVar=" ${var} _FILE"
63
+ if [ -v " ${var} " ] && [ -v " ${fileVar} " ]; then
64
+ echo >&2 " error: both $var and $fileVar are set (but are exclusive)"
65
+ exit 1
66
+ fi
67
+ if [ -v " ${fileVar} " ]; then
68
+ val=" $( < " ${! fileVar} " ) "
69
+ export " $var " =" $val "
70
+ fi
71
+ unset " $fileVar "
72
+ }
73
+
74
+ # Setup environment variables from secrets.
75
+ secrets=( PORTUS_DB_PASSWORD PORTUS_PASSWORD PORTUS_SECRET_KEY_BASE
76
+ PORTUS_EMAIL_SMTP_PASSWORD PORTUS_LDAP_AUTHENTICATION_PASSWORD )
77
+ for s in " ${secrets[@]} " ; do
78
+ if [[ -z " ${! s} " ]]; then
79
+ file_env " $s "
80
+ fi
81
+ done
82
+
83
+ # Ensure additional certificates (e.g. docker registry) are known.
84
+ update-ca-certificates
85
+
86
+ # Further settings
87
+ export PORTUS_PUMA_HOST=" 0.0.0.0:3000"
88
+ export RACK_ENV=" production"
89
+ export RAILS_ENV=" production"
90
+ export CCONFIG_PREFIX=" PORTUS"
91
+
92
+ if [ -z " $PORTUS_GEM_GLOBAL " ]; then
93
+ export GEM_PATH=" /srv/Portus/vendor/bundle/ruby/2.6.0"
94
+ fi
95
+
96
+ # On debug, print the environment in which we'll call Portus.
97
+ if [ " $PORTUS_LOG_LEVEL " == " debug" ]; then
98
+ printenv
99
+ fi
100
+
101
+ # Go to the Portus directory and execute the proper command.
102
+ cd /srv/Portus
103
+ if [ ! -z " $PORTUS_BACKGROUND " ]; then
104
+ wait_for_database
105
+ portusctl exec --vendor rails r /srv/Portus/bin/background.rb
106
+ elif [ -z " $PORTUS_INIT_COMMAND " ]; then
107
+ setup_database
108
+ portusctl exec --vendor " pumactl -F /srv/Portus/config/puma.rb start"
109
+ else
110
+ wait_for_database
111
+ portusctl exec --vendor " $PORTUS_INIT_COMMAND "
112
+ fi
0 commit comments