|
17 | 17 | # limitations under the License. |
18 | 18 | # |
19 | 19 |
|
20 | | -DEFAULT_DB_NAME=$1 |
21 | | -ADMIN_USER=${2:-admin} |
22 | | -ADMIN_PASS=${3:-admin} |
23 | | -CV_DIR=$(pwd) |
24 | | - |
25 | | -if [ -z ${DEFAULT_DB_NAME} ]; |
26 | | -then |
27 | | - echo "Usage: $0 <database name> [<user> <password>]" |
28 | | - exit 1 |
| 20 | +#!/bin/bash |
| 21 | + |
| 22 | +DEFAULT_DB_NAME="$1" |
| 23 | +ADMIN_USER="${2:-admin}" |
| 24 | +ADMIN_PASS="${3:-admin}" |
| 25 | +CV_DIR="$(pwd)" |
| 26 | + |
| 27 | +if [ -z "$DEFAULT_DB_NAME" ]; then |
| 28 | + echo "Usage: $0 <database name> [<user> <password>]" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +set -e # stop on error |
| 33 | +set -o pipefail |
| 34 | + |
| 35 | +echo "Creating database: $DEFAULT_DB_NAME" |
| 36 | + |
| 37 | +# Ensure we can edit PostgreSQL config and restart the service |
| 38 | +PG_HBA_PATH=$(ls /etc/postgresql/*/main/pg_hba.conf | head -n 1) |
| 39 | +if [ -z "$PG_HBA_PATH" ]; then |
| 40 | + echo "PostgreSQL pg_hba.conf not found!" |
| 41 | + exit 1 |
29 | 42 | fi |
30 | 43 |
|
31 | | -echo "Create ${DEFAULT_DB_NAME} database" |
32 | | -sudo sed -i '/^local/c\local all all trust' /etc/postgresql/*/main/pg_hba.conf |
33 | | -sudo service postgresql restart || { echo 'Cannot restart postgresql' ; exit 1; } |
34 | | -is_exist_out=$(psql -U postgres -c "SELECT datname FROM pg_catalog.pg_database WHERE datname='${DEFAULT_DB_NAME}'") |
35 | | -if [[ $is_exist_out == *"(1 row)"* ]]; then |
36 | | - echo "Drop database ${DEFAULT_DB_NAME}" |
37 | | - dropdb -U postgres ${DEFAULT_DB_NAME} |
| 44 | +# Update pg_hba.conf to "trust" for local connections |
| 45 | +sed -i 's/^local .*/local all all trust/' "$PG_HBA_PATH" |
| 46 | +service postgresql restart || { echo "Cannot restart postgresql"; exit 1; } |
| 47 | + |
| 48 | +# Drop DB if exists |
| 49 | +DB_EXISTS=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='${DEFAULT_DB_NAME}'") |
| 50 | +if [ "$DB_EXISTS" = "1" ]; then |
| 51 | + echo "Dropping existing database: $DEFAULT_DB_NAME" |
| 52 | + dropdb -U postgres "$DEFAULT_DB_NAME" |
38 | 53 | fi |
39 | | -createdb -U postgres -T template0 -E utf8 -O postgres ${DEFAULT_DB_NAME} || { echo 'Cannot create new database' ; exit 1; } |
40 | | - |
41 | | -echo "Set up CV web-interface" |
42 | | -echo $'{\n\t\"ENGINE\": \"django.db.backends.postgresql_psycopg2\",\n\t\"NAME\": \"'${DEFAULT_DB_NAME}$'\",\n\t\"USER\": \"postgres\"\n}' > ${CV_DIR}/web/web/db.json |
43 | | -echo -e 'from web.development import *\nPERFORM_AUTO_SAVE=False' > ${CV_DIR}/web/web/settings.py |
44 | | -python3 ${CV_DIR}/web/manage.py compilemessages || { echo 'Cannot compile messages' ; exit 1; } |
45 | | -python3 ${CV_DIR}/web/manage.py makemigrations jobs marks reports service tools users || { echo 'Cannot create migrations for database' ; exit 1; } |
46 | | -python3 ${CV_DIR}/web/manage.py migrate || { echo 'Cannot update database' ; exit 1; } |
47 | | -echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('${ADMIN_USER}', '', '${ADMIN_PASS}')" | python3 ${CV_DIR}/web/manage.py shell |
48 | | -echo "from django.contrib.auth.models import User; from web.populate import Population, extend_user; user = User.objects.first(); extend_user(user, 2); Population(user=user)" | python3 ${CV_DIR}/web/manage.py shell |
49 | | - |
50 | | -echo "Launch web-interface by command: './start.sh --host <host> --port <port> &'" |
| 54 | + |
| 55 | +# Create DB |
| 56 | +createdb -U postgres -T template0 -E UTF8 -O postgres "$DEFAULT_DB_NAME" || { echo "Cannot create new database"; exit 1; } |
| 57 | + |
| 58 | +echo "Setting up CV web-interface" |
| 59 | + |
| 60 | +cat > "${CV_DIR}/web/web/db.json" <<EOF |
| 61 | +{ |
| 62 | + "ENGINE": "django.db.backends.postgresql_psycopg2", |
| 63 | + "NAME": "${DEFAULT_DB_NAME}", |
| 64 | + "USER": "postgres" |
| 65 | +} |
| 66 | +EOF |
| 67 | + |
| 68 | +cat > "${CV_DIR}/web/web/settings.py" <<EOF |
| 69 | +from web.development import * |
| 70 | +PERFORM_AUTO_SAVE = False |
| 71 | +EOF |
| 72 | + |
| 73 | +python3 "${CV_DIR}/web/manage.py" compilemessages || { echo "Cannot compile messages"; exit 1; } |
| 74 | +python3 "${CV_DIR}/web/manage.py" makemigrations jobs marks reports service tools users || { echo "Cannot create migrations"; exit 1; } |
| 75 | +python3 "${CV_DIR}/web/manage.py" migrate || { echo "Cannot update database"; exit 1; } |
| 76 | + |
| 77 | +# Create superuser if not exists |
| 78 | +python3 "${CV_DIR}/web/manage.py" shell <<EOF |
| 79 | +from django.contrib.auth import get_user_model |
| 80 | +User = get_user_model() |
| 81 | +if not User.objects.filter(username="${ADMIN_USER}").exists(): |
| 82 | + User.objects.create_superuser("${ADMIN_USER}", "", "${ADMIN_PASS}") |
| 83 | +EOF |
| 84 | + |
| 85 | +# Extend user |
| 86 | +python3 "${CV_DIR}/web/manage.py" shell <<EOF |
| 87 | +from django.contrib.auth.models import User |
| 88 | +from web.populate import Population, extend_user |
| 89 | +user = User.objects.first() |
| 90 | +extend_user(user, 2) |
| 91 | +Population(user=user) |
| 92 | +EOF |
| 93 | + |
| 94 | +echo "Launch web interface using:" |
| 95 | +echo "./start.sh --host <host> --port <port> &" |
0 commit comments