initscript knows how to initialize db
[sfa.git] / sfa / init.d / sfa
1 #!/bin/bash
2 #
3 # sfa   Wraps PLCAPI into the SFA compliant API
4 #
5 # hopefully right after plc
6 # chkconfig: 2345 61 39
7 #
8 # description:   Wraps PLCAPI into the SFA compliant API
9 #
10
11 # source function library
12 . /etc/init.d/functions
13
14 # Default locations
15 PGDATA=/var/lib/pgsql/data
16 postgresql_conf=$PGDATA/postgresql.conf
17 pghba_conf=$PGDATA/pg_hba.conf
18 postgresql_sysconfig=/etc/sysconfig/pgsql
19
20 # PLC consolidated (merged) config file
21 plc_whole_config=/etc/planetlab/plc_config.xml
22 # SFA consolidated (merged) config file
23 sfa_whole_config=/etc/sfa/sfa_config.xml
24 # SFA local (site-dependent) file
25 sfa_local_config=/etc/planetlab/configs/site.xml
26
27 # Source sfa shell config if present 
28 [ -f /etc/sfa/sfa_config ] && . /etc/sfa/sfa_config
29
30 # Export so that we do not have to specify -p to psql invocations
31 export PGPORT=$SFA_DB_PORT
32
33 ##########
34 # Total number of errors
35 ERRORS=0
36
37 # Count the exit status of the last command
38 check ()
39 {
40     ERRORS=$(($ERRORS+$?))
41 }
42
43 # can't trust the return of service postgresql start / nor status
44 function postgresql_check () {
45
46     # wait until postmaster is up and running - or 10s max
47     if status postmaster >& /dev/null && [ -f /var/lock/subsys/postgresql ] ; then
48         # The only way we can be sure is if we can access it
49         for i in $(seq 1 10) ; do
50             # Must do this as the postgres user initially (before we
51             # fix pg_hba.conf to passwordless localhost access).
52             su -c 'psql -U postgres -c "" template1' postgres && return 0
53             sleep 1
54         done
55     fi
56
57     return 1
58 }
59
60
61 # Regenerate configuration files - almost verbatim from plc.init
62 function reload () {
63     force=$1
64
65     # Regenerate the main configuration file from default values
66     # overlaid with site-specific and current values.
67     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
68     files=(
69         /etc/sfa/default_config.xml 
70         /etc/sfa/configs/site.xml
71     )
72     for file in "${files[@]}" ; do
73         if [ -n "$force" -o $file -nt sfa_whole_config ] ; then
74             tmp=$(mktemp /tmp/sfa_config.xml.XXXXXX)
75             plc-config --xml "${files[@]}" >$tmp
76             if [ $? -eq 0 ] ; then
77                 mv $tmp sfa_whole_config
78                 chmod 444 sfa_whole_config
79             else
80                 echo "SFA: Warning: Invalid configuration file(s) detected"
81                 rm -f $tmp
82             fi
83             break
84         fi
85     done
86
87     # Convert configuration to various formats
88     if [ -n "$force" -o sfa_whole_config -nt /etc/sfa/sfa_config ] ; then
89         plc-config --shell sfa_whole_config >/etc/sfa/sfa_config
90     fi
91     if [ -n "$force" -o sfa_whole_config -nt /etc/sfa/sfa_config.py ] ; then
92         plc-config --python sfa_whole_config >/etc/sfa/sfa_config.py
93     fi
94 #    if [ -n "$force" -o sfa_whole_config -nt /etc/sfa/php/sfa_config.php ] ; then
95 #       mkdir -p /etc/sfa/php
96 #       plc-config --php  sfa_whole_config >/etc/sfa/php/sfa_config.php
97 #    fi
98
99     # [re]generate the sfa_component_config
100     # this is a server-side thing but produces a file that somehow needs to be pushed
101     # on the planetlab nodes; in the case where sfa and myplc run on different boxes 
102     # (or there is no myplc at all) this should be turned off
103     # as the component manager is not operational yet we skip this for now
104     #gen-sfa-cm-config.py        
105 }
106
107 ### initialize DB (don't chkconfig postgresql on)
108 function db_start () {
109     
110     # only if enabled
111     [ "$SFA_DB_ENABLED" == 1 ] || return
112
113     if ! rpm -q myplc >& /dev/null; then
114
115         ######## standalone deployment - no colocated myplc
116
117         ######## sysconfig 
118         # Set data directory and redirect startup output to /var/log/pgsql
119         mkdir -p $(dirname $postgresql_sysconfig)
120         # remove previous definitions
121         touch $postgresql_sysconfig
122         tmp=${postgresql_sysconfig}.new
123         ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig 
124             echo "PGDATA=$PGDATA"
125             echo "PGLOG=/var/log/pgsql"
126             echo "PGPORT=$PLC_DB_PORT"
127         ) >> $tmp ; mv -f $tmp $postgresql_sysconfig
128
129         ######## /var/lib/pgsql/data 
130         # Fix ownership (rpm installation may have changed it)
131         chown -R -H postgres:postgres $(dirname $PGDATA)
132
133         # PostgreSQL must be started at least once to bootstrap
134         # /var/lib/pgsql/data
135         if [ ! -f $postgresql_conf ] ; then
136             service postgresql initdb &> /dev/null || :
137             check
138         fi
139
140         ######## /var/lib/pgsql/data/postgresql.conf
141         registry_ip=""
142         foo=$(python -c "import socket; print socket.gethostbyname(\"$SFA_REGISTRY_HOST\")") && registry_ip="$foo"
143         # Enable DB server. drop Postgresql<=7.x
144         # PostgreSQL >=8.0 defines listen_addresses
145         # listen on a specific IP + localhost, more robust when run within a vserver
146         sed -i -e '/^listen_addresses/d' $postgresql_conf
147         if [ -z "$registry_ip" ] ; then
148             echo "listen_addresses = 'localhost'" >> $postgresql_conf
149         else
150             echo "listen_addresses = '${registry_ip},localhost'" >> $postgresql_conf
151         fi
152         # tweak timezone to be 'UTC'
153         sed -i -e '/^timezone=/d' $postgresql_conf
154         echo "timezone='UTC'" >> $postgresql_conf
155
156         ######## /var/lib/pgsql/data/pg_hba.conf
157         # Disable access to all DBs from all hosts
158         sed -i -e '/^\(host\|local\)/d' $pghba_conf
159
160         # Enable passwordless localhost access
161         echo "local all all trust" >>$pghba_conf
162         # grant access
163         (
164             echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password"
165             [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password"
166         ) >>$pghba_conf
167         
168         # Fix ownership (sed -i changes it)
169         chown postgres:postgres $postgresql_conf $pghba_conf
170
171         ######## compute user and password
172         if [ -z "$SFA_DB_PASSWORD" ] ; then
173             SFA_DB_PASSWORD=$(uuidgen)
174             plc-config --category=sfa_db --variable=password --value="$SFA_DB_PASSWORD" --save=$sfa_local_config $sfa_local_config
175             reload
176         fi
177
178     else
179
180         ######## we are colocated with a myplc - re-use the pgsql setup
181         # myplc enforces the password for its user
182         PLC_DB_USER=$(plc-config --category=plc_db --variable=user)
183         PLC_DB_PASSWORD=$(plc-config --category=plc_db --variable=password)
184         # store this as the SFA user/password 
185         plc-config --category=sfa_db --variable=user --value=$PLC_DB_USER --save=$sfa_local_config $sfa_local_config
186         plc-config --category=sfa_db --variable=password --value=$PLC_DB_PASSWORD --save=$sfa_local_config $sfa_local_config
187         reload
188     fi
189
190     ######## Start up the server
191     # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
192     if ! rpm -q myplc >& /dev/null ; then
193         echo STARTING...
194         service postgresql start >& /dev/null
195     fi
196     postgresql_check
197     check
198         
199     ######## make sure we have the user and db created
200     # user
201     if ! psql -U $SFA_DB_USER -c "" template1 >/dev/null 2>&1 ; then
202         psql -U postgres -c "CREATE USER $SFA_DB_USER PASSWORD '$SFA_DB_PASSWORD'" template1
203     else
204         psql -U postgres -c "ALTER USER $SFA_DB_USER WITH PASSWORD '$SFA_DB_PASSWORD'" template1
205     fi
206     check
207     
208     # db
209     if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then
210         createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME
211         # in case we'd like to ship the db schema separately 
212         #psql -U $SFA_DB_USER -f /etc/sfa/sfa.sql $SFA_DB_NAME
213     fi
214     check
215
216     MESSAGE=$"Checking for PostgreSQL server"
217     echo -n "$MESSAGE"
218     [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
219 }
220
221 # shutdown DB
222 function db_stop () {
223
224     # only if enabled
225     [ "$SFA_DB_ENABLED" == 1 ] || return
226
227     # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
228     if ! rpm -q myplc >& /dev/null ; then
229         service postgresql stop >& /dev/null
230         check
231         MESSAGE=$"Stopping PostgreSQL server"
232         echo -n "$MESSAGE"
233         [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
234     fi
235 }
236
237 function start() {
238     
239     reload
240
241     db_start
242
243     # install peer certs
244     action $"SFA installing peer certs" daemon /usr/bin/sfa-start.py -t -d $OPTIONS 
245
246     if [ "$SFA_REGISTRY_ENABLED" -eq 1 ]; then
247         action $"SFA Registry" daemon /usr/bin/sfa-start.py -r -d $OPTIONS
248     fi
249
250     if [ "$SFA_AGGREGATE_ENABLED" -eq 1 ]; then
251         action $"SFA Aggregate" daemon /usr/bin/sfa-start.py -a -d $OPTIONS
252     fi
253         
254     if [ "$SFA_SM_ENABLED" -eq 1 ]; then
255         action "SFA SliceMgr" daemon /usr/bin/sfa-start.py -s -d $OPTIONS
256     fi
257
258     if [ "$SFA_FLASHPOLICY_ENABLED" -eq 1 ]; then
259         action "Flash Policy Server" daemon /usr/bin/sfa_flashpolicy.py --file="$SFA_FLASHPOLICY_CONFIG_FILE" --port=$SFA_FLASHPOLICY_PORT -d
260     fi
261
262     touch /var/lock/subsys/sfa-start.py
263
264 }
265
266 function stop() {
267     action $"Shutting down SFA" killproc sfa-start.py
268
269     db_stop
270
271     rm -f /var/lock/subsys/sfa-start.py
272 }
273
274
275 case "$1" in
276     start) start ;;
277     stop) stop ;;
278     reload) reload force ;;
279     restart) stop; start ;;
280     condrestart)
281         if [ -f /var/lock/subsys/sfa-start.py ]; then
282             stop
283             start
284         fi
285         ;;
286     status)
287         status sfa-start.py
288         RETVAL=$?
289         ;;
290     *)
291         echo $"Usage: $0 {start|stop|reload|restart|condrestart|status}"
292         exit 1
293         ;;
294 esac
295
296 exit $RETVAL
297