1a030fd95692fc65dd71c743fc7c475c82e73f65
[sfa.git] / systemd / sfa-db-init.sh
1 #!/bin/bash
2
3 set -e
4
5 ####################
6 PGDATA=/var/lib/pgsql/data/
7 PGLOG=/var/log/pgsql
8
9 postgresql_conf=$PGDATA/postgresql.conf
10 pg_hba_conf=$PGDATA/pg_hba.conf
11
12 # SFA consolidated (merged) config file
13 sfa_whole_config=/etc/sfa/sfa_config
14 # SFA default config (read-only template)
15 sfa_default_config=/etc/sfa/default_config.xml
16 # SFA local (site-dependent) file
17 sfa_local_config=/etc/sfa/configs/site_config
18 sfa_local_config_xml=/etc/sfa/configs/site_config.xml
19 sfa_local_config_sh=/etc/sfa/sfa_config.sh
20
21 # source shell config if present; might not be the very first time
22 [ -f $sfa_local_config_sh ] && source $sfa_local_config_sh
23
24 # Export so that we do not have to specify -p to psql invocations
25 export PGPORT=$SFA_DB_PORT
26
27 # Regenerate configuration files - almost verbatim from plc.init
28 function reconfigure () {
29
30     # Regenerate the main configuration file from default values
31     # overlaid with site-specific and current values.
32     files=( $sfa_default_config $sfa_local_config )
33     tmp=$(mktemp /tmp/sfa_config.XXXXXX)
34     sfa-config --python "${files[@]}" > $tmp
35     if [ $? -eq 0 ] ; then
36                 mv $tmp $sfa_whole_config
37                 chmod 444 $sfa_whole_config
38     else
39                 echo "SFA: Warning: Invalid configuration file(s) detected"
40             rm -f $tmp
41         exit 1
42     fi
43
44     # Convert configuration to various formats
45     if [ -f $sfa_local_config_xml ] ; then
46         sfa-config --python $sfa_local_config_xml > $sfa_local_config
47         rm $sfa_local_config_xml
48     fi
49     if [ -n "$force" -o $sfa_local_config -nt $sfa_whole_config ] ; then
50         sfa-config --python $sfa_default_config $sfa_local_config > $sfa_whole_config
51     fi
52     if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/sfa_config.sh ] ; then
53         sfa-config --shell $sfa_default_config $sfa_local_config > /etc/sfa/sfa_config.sh
54     fi
55
56     # reload the shell version
57     source $sfa_local_config_sh
58
59 }
60
61 function postgresql_setting() {
62     param="$1"; shift
63     value="$1"; shift
64
65     sed --regexp-extended --in-place \
66       --expression="s|#?${param} = .*|${param} = ${value}|" \
67       $postgresql_conf
68 }
69
70 function start () {
71
72     # only if enabled
73     [ "$SFA_DB_ENABLED" == 1 -o "$SFA_DB_ENABLED" == True ] || return
74
75     postgresql_setting port "'$SFA_DB_PORT'"
76     mkdir -p $PGLOG
77     chown postgres:postgres $PGLOG
78     postgresql_setting log_directory "'$PGLOG'"
79
80     ######## /var/lib/pgsql/data
81     # Fix ownership (rpm installation may have changed it)
82     chown -R -H postgres:postgres $(dirname $PGDATA)
83
84     # PostgreSQL must be started at least once to bootstrap
85     # /var/lib/pgsql/data
86     if [ ! -f $postgresql_conf ] ; then
87         /usr/bin/postgresql-setup --initdb --unit postgresql
88     fi
89
90     ######## /var/lib/pgsql/data/postgresql.conf
91     registry_ip=""
92     foo=$(python -c "import socket; print socket.gethostbyname('$SFA_REGISTRY_HOST')") && registry_ip="$foo"
93     # Enable DB server. drop Postgresql<=7.x
94     # PostgreSQL >=8.0 defines listen_addresses
95     # listen on a specific IP + localhost, more robust when run within a vserver
96     sed -i -e '/^listen_addresses/d' $postgresql_conf
97     if [ -z "$registry_ip" ] ; then
98         postgresql_setting listen_addresses "'localhost'"
99     else
100         postgresql_setting listen_addresses "'${registry_ip},localhost'"
101     fi
102     postgresql_setting timezone "'UTC'"
103     postgresql_setting log_timezone "'UTC'"
104
105     ######## /var/lib/pgsql/data/pg_hba.conf
106     # remove/recreate passwordless localhost entry
107     sed -i -e "/^local/d" $pg_hba_conf
108     echo "local all all trust" >> $pg_hba_conf
109
110     # Disable access to our DB from all hosts
111     sed -i -e "/^host ${SFA_DB_NAME}/d' $pg_hba_conf
112     # grant access
113     {
114         echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password"
115         [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password"
116     } >> $pg_hba_conf
117
118     # Fix ownership (sed -i changes it)
119     chown postgres:postgres $postgresql_conf $pg_hba_conf
120
121     ######## compute a password if needed
122     if [ -z "$SFA_DB_PASSWORD" ] ; then
123         SFA_DB_PASSWORD=$(uuidgen)
124         sfa-config --category=sfa_db --variable=password --value="$SFA_DB_PASSWORD" --save=$sfa_local_config $sfa_local_config >& /dev/null
125         reconfigure
126     fi
127
128     systemctl restart postgresql
129
130     ######## make sure we have the user and db created
131     # user
132     if ! psql -U $SFA_DB_USER -c "" template1 >/dev/null 2>&1 ; then
133         psql -U postgres -c "CREATE USER $SFA_DB_USER PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
134     else
135         psql -U postgres -c "ALTER USER $SFA_DB_USER WITH PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
136     fi
137
138     # db
139     if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then
140         createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME
141     fi
142
143     # create schema; sfaadmin.py is safer than just sfaadmin
144     sfaadmin.py reg sync_db
145
146 }
147
148 start