a little nicer wrt pep8
[sfa.git] / systemd / sfa-setup.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 # Regenerate configuration files - almost verbatim from plc.init
22 function reload () {
23
24     # Regenerate the main configuration file from default values
25     # overlaid with site-specific and current values.
26     files=( $sfa_default_config $sfa_local_config )
27     tmp=$(mktemp /tmp/sfa_config.XXXXXX)
28     sfa-config --python "${files[@]}" > $tmp
29     if [ $? -eq 0 ] ; then
30                 mv $tmp $sfa_whole_config
31                 chmod 444 $sfa_whole_config
32     else
33                 echo "SFA: Warning: Invalid configuration file(s) detected"
34             rm -f $tmp
35         exit 1
36     fi
37
38     # Convert configuration to various formats
39     if [ -f $sfa_local_config_xml ] ; then
40         sfa-config --python $sfa_local_config_xml > $sfa_local_config
41         rm $sfa_local_config_xml
42     fi
43     if [ -n "$force" -o $sfa_local_config -nt $sfa_whole_config ] ; then
44         sfa-config --python $sfa_default_config $sfa_local_config > $sfa_whole_config
45     fi
46     if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/sfa_config.sh ] ; then
47         sfa-config --shell $sfa_default_config $sfa_local_config > /etc/sfa/sfa_config.sh
48     fi
49
50     # reload the shell version
51     source $sfa_local_config_sh
52
53 }
54
55 function postgresql_setting() {
56     param="$1"; shift
57     value="$1"; shift
58
59     # is that setting already present in file ?
60     if grep --extended-regexp -q "#?${param} *=.*" $postgresql_conf; then
61         sed --regexp-extended --in-place \
62             --expression="s|#?${param} = .*|${param} = ${value}|" \
63             $postgresql_conf
64     else
65         echo "${param} = ${value}" >> $postgresql_conf
66     fi
67 }
68
69 function start-db () {
70
71     # source shell config if present
72     # but it might not be present the very first time
73     [ ! -f $sfa_local_config_sh ] && reload
74
75     source $sfa_local_config_sh
76
77     # Export so that we do not have to specify -p to psql invocations
78     export PGPORT=$SFA_DB_PORT
79
80     # only if enabled
81     # this is because the DB can run on a separate box as well
82     [ "$SFA_DB_ENABLED" == 1 -o "$SFA_DB_ENABLED" == True ] || return
83
84     postgresql_setting port "'$SFA_DB_PORT'"
85     mkdir -p $PGLOG
86     chown postgres:postgres $PGLOG
87     postgresql_setting log_directory "'$PGLOG'"
88
89     ######## /var/lib/pgsql/data
90     # Fix ownership (rpm installation may have changed it)
91     chown -R -H postgres:postgres $(dirname $PGDATA)
92
93     # PostgreSQL must be started at least once to bootstrap
94     # /var/lib/pgsql/data
95     if [ ! -f $postgresql_conf ] ; then
96         /usr/bin/postgresql-setup --initdb --unit postgresql
97     fi
98
99     ######## /var/lib/pgsql/data/postgresql.conf
100     registry_ip=""
101     foo=$(python -c "import socket; print socket.gethostbyname('$SFA_REGISTRY_HOST')") && registry_ip="$foo"
102     db_ip=""
103     foo=$(python -c "import socket; print socket.gethostbyname('$SFA_DB_HOST')") && db_ip="$foo"
104     # Enable DB server. drop Postgresql<=7.x
105     # PostgreSQL >=8.0 defines listen_addresses
106     # listen on a specific IP + localhost, more robust when run within a vserver
107     sed -i -e '/^listen_addresses/d' $postgresql_conf
108     case "$db_ip" in
109         ""|127.0.0.1|localhost*)
110             postgresql_setting listen_addresses "'localhost'" ;;
111         *)
112             postgresql_setting listen_addresses "'${db_ip},localhost'" ;;
113     esac
114     postgresql_setting timezone "'UTC'"
115     postgresql_setting log_timezone "'UTC'"
116
117     ######## /var/lib/pgsql/data/pg_hba.conf
118     # remove/recreate passwordless localhost entry
119     sed -i -e "/^local/d" $pg_hba_conf
120     echo "local all all trust" >> $pg_hba_conf
121
122     # Disable access to our DB from all hosts
123     sed -i -e "/^host ${SFA_DB_NAME}/d" $pg_hba_conf
124     # grant access
125     {
126         echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password"
127         [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password"
128     } >> $pg_hba_conf
129
130     # Fix ownership (sed -i changes it)
131     chown postgres:postgres $postgresql_conf $pg_hba_conf
132
133     ######## compute a password if needed
134     if [ -z "$SFA_DB_PASSWORD" ] ; then
135         SFA_DB_PASSWORD=$(uuidgen)
136         sfa-config --category=sfa_db --variable=password --value="$SFA_DB_PASSWORD" --save=$sfa_local_config $sfa_local_config >& /dev/null
137         reload
138     fi
139
140     # tell postgresql that settings have changed
141     # note that changes to listen_addresses do require a full restart
142     # but it's too intrusive to do this each time, as it would in turn
143     # require a restart of the plc service
144     su - postgres bash -c "pg_ctl reload"
145
146     ######## make sure we have the user and db created
147     # user
148     if ! psql -U $SFA_DB_USER -c "" template1 >/dev/null 2>&1 ; then
149         psql -U postgres -c "CREATE USER $SFA_DB_USER PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
150     else
151         psql -U postgres -c "ALTER USER $SFA_DB_USER WITH PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
152     fi
153
154     # db
155     if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then
156         createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME
157     fi
158
159     # create schema; sfaadmin.py is safer than just sfaadmin
160     sfaadmin.py reg sync_db
161
162 }
163
164 usage="$0 start-db|reload
165   start-db: configure postgresql database and restart postgresql
166   reload: recompute miscell configuration files after changes are made in master config
167 "
168
169 func="$1"; shift
170
171 case "$func" in
172     start-db|reload) $func;;
173     *) echo "$usage"; exit 1;;
174 esac