Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-dbs / postgresql / postgresql / postgresql-init
1 #!/bin/sh
2 # set -x
3
4 PN=postgresql
5 CONTROLLER_IP=%CONTROLLER_IP%
6 COMPUTE_IP=%COMPUTE_IP%
7 DB_USER=%DB_USER%
8 DB_PASSWORD=%DB_PASSWORD%
9 DATA_DIR=%DB_DATADIR%
10
11 initdb(){
12     if [ ! -e $DATA_DIR ]; then
13         mkdir -p $DATA_DIR
14         chown postgres $DATA_DIR
15     fi
16
17     if [ -e $DATA_DIR/PG_VERSION ]; then
18         # the database has already been initialized, return
19         exit 0
20     fi
21
22     # Create the DB
23     sudo -u postgres initdb -D $DATA_DIR
24
25     # Allow readers/writers by IP
26     echo "listen_addresses = '*'" >> $DATA_DIR/postgresql.conf
27     echo "host   all   all   ${CONTROLLER_IP}/32   trust" >> $DATA_DIR/pg_hba.conf
28     echo "host   all   all   ${COMPUTE_IP}/32   trust" >> $DATA_DIR/pg_hba.conf
29     }
30
31 if [ "$1" == "initdb" ]; then
32     initdb
33     exit 0
34 fi
35
36 count=0
37 done=0
38 while [ $count -le 10 ] && [ $done -eq 0 ]; do
39     sudo -u postgres psql -c "CREATE ROLE ${DB_USER} WITH SUPERUSER LOGIN PASSWORD '${DB_PASSWORD}'" 2> /dev/null
40     if [ $? -ne 0 ]; then
41         echo "[INFO] postgres: failed to create account for ${DB_USER}, trying again"
42         systemctl stop postresql
43         sleep 3
44         systemctl start postgresql
45         sleep 3
46     else
47         echo "[INFO] postgres: created account for ${DB_USER}, continuing .. "
48         done=1
49     fi
50     count=`expr $count + 1`
51 done
52
53 if [ $done -eq 0 ]; then
54     echo "[ERROR] postgres: unable to create admin account"
55     exit 1
56 fi
57
58 ln -s /usr/share/zoneinfo /usr/share/postgresql/timezone || true