docker/server/entrypoint.sh
changeset 129 f6b392c37e10
equal deleted inserted replaced
128:6ec3049096dd 129:f6b392c37e10
       
     1 #!/usr/bin/env bash
       
     2 
       
     3 set -Eeuo pipefail
       
     4 
       
     5 # from https://stackoverflow.com/a/45977232
       
     6 # Following regex is based on https://www.rfc-editor.org/rfc/rfc3986#appendix-B with
       
     7 # additional sub-expressions to split authority into userinfo, host and port
       
     8 #
       
     9 readonly URI_REGEX='^(([^:/?#]+):)?(//((([^/?#]+)@)?([^:/?#]+)(:([0-9]+))?))?(/([^?#]*))(\?([^#]*))?(#(.*))?'
       
    10 #                    ↑↑            ↑  ↑↑↑            ↑         ↑ ↑            ↑ ↑        ↑  ↑        ↑ ↑
       
    11 #                    |2 scheme     |  ||6 userinfo   7 host    | 9 port       | 11 rpath |  13 query | 15 fragment
       
    12 #                    1 scheme:     |  |5 userinfo@             8 :…           10 path    12 ?…       14 #…
       
    13 #                                  |  4 authority
       
    14 #                                  3 //…
       
    15 
       
    16 parse_host () {
       
    17     [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[7]}"
       
    18 }
       
    19 
       
    20 parse_port () {
       
    21     [[ "$@" =~ $URI_REGEX ]] && echo "${BASH_REMATCH[9]}"
       
    22 }
       
    23 
       
    24 dbhost=$(parse_host "${DATABASE_URL}")
       
    25 dbport=$(parse_port "${DATABASE_URL}")
       
    26 
       
    27 cd ${BASEDIR}
       
    28 
       
    29 if [[ "$*" =~ 'uwsgi' ]]; then
       
    30     echo "Waiting for postgres on ${dbhost}:${dbport}..."
       
    31 
       
    32     while ! nc -z ${dbhost} ${dbport}; do
       
    33         sleep 0.1
       
    34     done
       
    35 
       
    36     echo "PostgreSQL started"
       
    37     
       
    38     python manage.py syncdb --migrate
       
    39     python manage.py collectstatic --noinput --clear
       
    40 
       
    41 fi
       
    42 
       
    43 exec "$@"