summaryrefslogtreecommitdiff
path: root/util/docker-init.sh
blob: dce2339c97fb6b8cad19d46ca63936eddfc8a9a5 (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
#!/bin/sh

if ! test -f /var/vndb-docker-image; then
    echo "This script should only be run from within the VNDB docker container."
    echo "Check the README for instructions."
    exit 1
fi


mkdevuser() {
    # Create a new user with the same UID and GID as the owner of the VNDB
    # directory. This allows for convenient exchange of files without worrying
    # about permission stuff.
    USER_UID=`stat -c '%u' /var/www`
    USER_GID=`stat -c '%g' /var/www`
    groupadd -g $USER_GID devgroup
    useradd -u $USER_UID -g $USER_GID -m -s /bin/bash devuser

    # So you can easily do a 'psql -U vndb'
    echo '*:*:*:vndb:vndb'              >/home/devuser/.pgpass
    echo '*:*:*:vndb_site:vndb_site'   >>/home/devuser/.pgpass
    echo '*:*:*:vndb_multi:vndb_multi' >>/home/devuser/.pgpass
    chown devuser /home/devuser/.pgpass
    chmod 600 /home/devuser/.pgpass
}


pg_start() {
    echo 'local all postgres peer' >/etc/postgresql/9.6/main/pg_hba.conf
    echo 'local all all md5'      >>/etc/postgresql/9.6/main/pg_hba.conf
    # I'm glad Ubuntu 17.10 still has an init script for this
    /etc/init.d/postgresql start
}


pg_init() {
    if test -f /var/lib/postgresql/vndb-init-done; then
        echo "Database initialization already done."
        echo "Run the following as root to bypass this check:"
        echo "  rm /var/lib/postgresql/vndb-init-done"
        return
    fi
    su postgres -c '/var/www/util/docker-init.sh pg_load_superuser'
    su devuser -c '/var/www/util/docker-init.sh pg_load_vndb'
    touch /var/lib/postgresql/vndb-init-done
}

# Should run as the postgres user
pg_load_superuser() {
    psql -f /var/www/util/sql/superuser_init.sql
    echo "ALTER ROLE vndb       LOGIN UNENCRYPTED PASSWORD 'vndb'"       | psql -U postgres
    echo "ALTER ROLE vndb_site  LOGIN UNENCRYPTED PASSWORD 'vndb_site'"  | psql -U postgres
    echo "ALTER ROLE vndb_multi LOGIN UNENCRYPTED PASSWORD 'vndb_multi'" | psql -U postgres
}

# Should run as devuser
pg_load_vndb() {
    cd /var/www
    psql -U vndb -f util/sql/all.sql
}


# Should run as devuser
devshell() {
    cd /var/www
    make && util/vndb.pl
    bash
}


case "$1" in
    '')
        mkdevuser
        pg_start
        pg_init
        exec su devuser -c '/var/www/util/docker-init.sh devshell'
        ;;
    pg_load_superuser)
        pg_load_superuser
        ;;
    pg_load_vndb)
        pg_load_vndb
        ;;
    devshell)
        devshell
        ;;
esac