summaryrefslogtreecommitdiff
path: root/tools/docker/entrypoint.sh
blob: 32f7132a89237bba164990d70f5be7842eaab8c3 (plain)
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
#!/bin/bash
set -euo pipefail

PW_TEST_DB_TYPE=${PW_TEST_DB_TYPE:-mysql}

# functions

test_db_connection() {
    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
        echo ';' | psql -h $PW_TEST_DB_HOST -U postgres 2> /dev/null > /dev/null
    else
        mysqladmin -h $PW_TEST_DB_HOST -u patchwork --password=password ping > /dev/null 2> /dev/null
    fi
}

test_database() {
    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
        echo ';' | psql -h $PW_TEST_DB_HOST -U postgres patchwork 2> /dev/null
    else
        echo ';' | mysql -h $PW_TEST_DB_HOST -u patchwork -ppassword patchwork 2> /dev/null
    fi
}

reset_data_mysql() {
    mysql -u$db_user -p$db_pass -h $PW_TEST_DB_HOST << EOF
DROP DATABASE IF EXISTS patchwork;
CREATE DATABASE patchwork CHARACTER SET utf8;
GRANT ALL ON patchwork.* TO 'patchwork' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON test_patchwork.* TO 'patchwork'@'%';
FLUSH PRIVILEGES;
EOF
}

reset_data_postgres() {
    psql -h $PW_TEST_DB_HOST -U postgres <<EOF
DROP DATABASE IF EXISTS patchwork;
CREATE DATABASE patchwork WITH ENCODING = 'UTF8';
EOF
}

reset_data() {
    if [ x${PW_TEST_DB_TYPE} = x"postgres" ]; then
        reset_data_postgres
    else
        reset_data_mysql
    fi

    # load initial data
    python3 $PROJECT_HOME/manage.py migrate #> /dev/null
    python3 $PROJECT_HOME/manage.py loaddata default_tags #> /dev/null
    python3 $PROJECT_HOME/manage.py loaddata default_states #> /dev/null
    python3 $PROJECT_HOME/manage.py loaddata default_projects #> /dev/null
}

# the script begins!

# check if patchwork is mounted. Checking if we exist is a
# very good start!
if [ ! -f ~patchwork/patchwork/tools/docker/entrypoint.sh ]; then
    cat << EOF
The patchwork directory doesn't seem to be mounted!

Are you using docker-compose? If so, you may need to create an SELinux rule.
Refer to the development installation documentation for more information.
If not, you need -v PATH_TO_PATCHWORK:/home/patchwork/patchwork
EOF
    exit 1
fi

# check if we need to rebuild because requirements changed
for x in /tmp/requirements-*.txt; do
    if ! cmp $x ~/patchwork/$(basename $x); then
        cat << EOF
A requirements file has changed.

You may need to rebuild the patchwork image:

    docker-compose build web
EOF
        diff -u $x ~/patchwork/$(basename $x)
    fi
done

# check if db is connected
if ! test_db_connection; then
    echo "The database seems not to be connected, or the patchwork user is broken"
    echo "MySQL/Postgres may still be starting. Waiting 5 seconds."
    sleep 5
    if ! test_db_connection; then
        echo "Still cannot connect to database."
        echo "Maybe you are starting the db for the first time. Waiting up to 60 seconds."
        for i in {0..9}; do
            sleep 5
            if test_db_connection; then
                break
            fi
        done
        if ! test_db_connection; then
            echo "Still cannot connect to database. Giving up."
            echo "Are you using docker-compose? If not, have you set up the link correctly?"
            exit 1
        fi
    fi
fi

# rebuild db
# do this on --reset or if the db doesn't exist
if [[ "$1" == "--reset" ]]; then
    shift
    reset_data
elif ! test_database; then
    reset_data
fi

# TODO(stephenfin): Deprecated the --test, --tox, --quick-test and --quick-tox
# flags in a future release
if [ $# -eq 0 ]; then
    # we probably ran with --reset and nothing else
    # just exit cleanly
    exit 0
elif [ "$1" == "--shell" ]; then
    exec bash
elif [ "$1" == "--test" ] || [ "$1" == "--quick-test" ]; then
    shift
    python3 manage.py test $@
elif [ "$1" == "--tox" ] || [ "$1" == "--quick-tox" ]; then
    shift
    tox $@
else # run whatever CMD is set to
    $@
fi