cosmetic
[sfa.git] / init.d / sfa
1 #!/bin/bash
2 #
3 # sfa
4 # Provides a generic SFA wrapper based on the initial PlanetLab Implementation
5 #
6 # hopefully right after plc
7 # chkconfig: 2345 61 39
8 #
9 # description:   An implementation  of the SFA Architecture
10 #
11
12 ####################
13 # borrowed from postgresql
14 function debian_get_postgresql_versions () {
15     versions=()
16     for v in `ls /usr/lib/postgresql/ 2>/dev/null`; do
17         if [ -x /usr/lib/postgresql/$v/bin/pg_ctl ] && [ ! -x /etc/init.d/postgresql-$v ]; then
18             versions+=($v)
19         fi
20     done
21     if [[ ${#versions[*]} == "0" ]]; then 
22         echo "E: Missing postgresql installation. Aborting."
23         exit
24     fi
25     if [[ ${#versions[*]} != "1" ]]; then 
26         echo "E: Too many postgresql versions installed. Aborting."
27         exit
28     fi
29     pgver=${versions[0]}
30 }
31
32 ####################
33 if [ -f /etc/redhat-release ] ; then
34     # source function library
35     . /etc/init.d/functions
36     PGDATA=/var/lib/pgsql/data/
37     PGWATCH=postmaster
38     PGLOCK=/var/lock/subsys/postgresql
39     SFALOCK=/var/lock/subsys/sfa-start.pid
40 elif [ -f /etc/debian_version ] ; then
41     . /etc/init.d/functions.sfa
42     debian_get_postgresql_versions
43     PGDATA=/etc/postgresql/$pgver/main/
44     PGWATCH=postgres
45     PGLOCK=/var/run/postgresql/$pgver-main.pid
46     SFALOCK=/var/run/sfa-start.pid
47 else
48     echo "initscript can only handle redhat/fedora or debian/ubuntu systems"
49     exit 1
50 fi
51
52
53 postgresql_conf=$PGDATA/postgresql.conf
54 pg_hba_conf=$PGDATA/pg_hba.conf
55 postgresql_sysconfig=/etc/sysconfig/pgsql
56
57 # SFA consolidated (merged) config file
58 sfa_whole_config=/etc/sfa/sfa_config
59 # SFA default config (read-only template)
60 sfa_default_config=/etc/sfa/default_config.xml
61 # SFA local (site-dependent) file
62 sfa_local_config=/etc/sfa/configs/site_config
63 sfa_local_config_xml=/etc/sfa/configs/site_config.xml
64
65 # Source sfa shell config if present 
66 [ -f /etc/sfa/sfa_config.sh ] && . /etc/sfa/sfa_config.sh
67
68 # Export so that we do not have to specify -p to psql invocations
69 export PGPORT=$SFA_DB_PORT
70
71 ##########
72 # Total number of errors
73 ERRORS=0
74
75 # Count the exit status of the last command
76 check ()
77 {
78     ERRORS=$(($ERRORS+$?))
79 }
80
81 # can't trust the return of service postgresql start / nor status
82 function postgresql_check () {
83
84     # wait until postmaster is up and running - or 10s max
85     if status $PGWATCH >& /dev/null && [ -f $PGLOCK ] ; then
86         # The only way we can be sure is if we can access it
87         for i in $(seq 1 10) ; do
88             # Must do this as the postgres user initially (before we
89             # fix pg_hba.conf to passwordless localhost access).
90             su -c 'psql -U postgres -c "" template1' postgres && return 0
91             sleep 1
92         done
93     fi
94
95     return 1
96 }
97
98 # use a single date of this script invocation for the dump_*_db functions.
99 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
100
101 # Dumps the database - optional argument to specify filename suffix
102 function dump_sfa_db() {
103     if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
104     mkdir -p /usr/share/sfa/backups
105     dumpfile=/usr/share/sfa/backups/$(date +"${SFA_DB_NAME}.${DATE}${suffix}.sql")
106     pg_dump -U $SFA_DB_USER $SFA_DB_NAME > $dumpfile
107     echo "Saved sfa database in $dumpfile"
108     check
109 }
110
111 # Regenerate configuration files - almost verbatim from plc.init
112 function reload () {
113     force=$1
114
115     # Regenerate the main configuration file from default values
116     # overlaid with site-specific and current values.
117     files=( $sfa_default_config $sfa_local_config )
118     for file in "${files[@]}" ; do
119         if [ -n "$force" -o $file -nt $sfa_whole_config ] ; then
120             tmp=$(mktemp /tmp/sfa_config.XXXXXX)
121             sfa-config --python "${files[@]}" >$tmp
122             if [ $? -eq 0 ] ; then
123                 mv $tmp $sfa_whole_config
124                 chmod 444 $sfa_whole_config
125             else
126                 echo "SFA: Warning: Invalid configuration file(s) detected"
127                 rm -f $tmp
128             fi
129             break
130         fi
131     done
132
133     # Convert configuration to various formats
134     if [ -f $sfa_local_config_xml ] ; then
135     sfa-config --python $sfa_local_config_xml > $sfa_local_config 
136     rm $sfa_local_config_xml 
137     fi
138     if [ -n "$force" -o $sfa_local_config -nt $sfa_whole_config ] ; then
139         sfa-config --python $sfa_default_config $sfa_local_config > $sfa_whole_config
140     fi
141     if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/sfa_config.sh ] ; then
142         sfa-config --shell $sfa_default_config $sfa_local_config > /etc/sfa/sfa_config.sh
143     fi
144
145     # [re]generate the sfa_component_config
146     # this is a server-side thing but produces a file that somehow needs to be pushed
147     # on the planetlab nodes; in the case where sfa and myplc run on different boxes 
148     # (or there is no myplc at all) this should be turned off
149     # as the component manager is not operational yet we skip this for now
150     #gen-sfa-cm-config.py        
151
152     # reload the shell version
153     [ -f /etc/sfa/sfa_config.sh ] && . /etc/sfa/sfa_config.sh
154
155 }
156
157 ### initialize DB (don't chkconfig postgresql on)
158 function db_start () {
159     
160     # only if enabled
161     [ "$SFA_DB_ENABLED" == 1 -o "$SFA_DB_ENABLED" == True ] || return
162
163     if [ ! -f /etc/myplc-release ] ; then
164
165         ######## standalone deployment - no colocated myplc
166
167         ######## sysconfig 
168         # Set data directory and redirect startup output to /var/log/pgsql
169         mkdir -p $(dirname $postgresql_sysconfig)
170         # remove previous definitions
171         touch $postgresql_sysconfig
172         tmp=${postgresql_sysconfig}.new
173         ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig 
174             echo "PGDATA=$PGDATA"
175             echo "PGLOG=/var/log/pgsql"
176             echo "PGPORT=$SFA_DB_PORT"
177         ) >> $tmp ; mv -f $tmp $postgresql_sysconfig
178
179         ######## /var/lib/pgsql/data 
180         # Fix ownership (rpm installation may have changed it)
181         chown -R -H postgres:postgres $(dirname $PGDATA)
182
183         # PostgreSQL must be started at least once to bootstrap
184         # /var/lib/pgsql/data
185         if [ ! -f $postgresql_conf ] ; then
186             service postgresql initdb &> /dev/null || :
187             check
188         fi
189
190         ######## /var/lib/pgsql/data/postgresql.conf
191         registry_ip=""
192         foo=$(python -c "import socket; print socket.gethostbyname(\"$SFA_REGISTRY_HOST\")") && registry_ip="$foo"
193         # Enable DB server. drop Postgresql<=7.x
194         # PostgreSQL >=8.0 defines listen_addresses
195         # listen on a specific IP + localhost, more robust when run within a vserver
196         sed -i -e '/^listen_addresses/d' $postgresql_conf
197         if [ -z "$registry_ip" ] ; then
198             echo "listen_addresses = 'localhost'" >> $postgresql_conf
199         else
200             echo "listen_addresses = '${registry_ip},localhost'" >> $postgresql_conf
201         fi
202         # tweak timezone to be 'UTC'
203         sed -i -e '/^timezone=/d' $postgresql_conf
204         echo "timezone='UTC'" >> $postgresql_conf
205
206         ######## /var/lib/pgsql/data/pg_hba.conf
207         # Disable access to all DBs from all hosts
208         sed -i -e '/^\(host\|local\)/d' $pg_hba_conf
209
210         # Enable passwordless localhost access
211         echo "local all all trust" >>$pg_hba_conf
212         # grant access
213         (
214             echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password"
215             [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password"
216         ) >>$pg_hba_conf
217
218     if [ "$SFA_GENERIC_FLAVOUR" == "openstack" ] ; then
219         [ -n "$registry_ip" ] && echo "host nova nova ${registry_ip}/32 password" >> $pg_hba_conf
220     fi   
221         
222         # Fix ownership (sed -i changes it)
223         chown postgres:postgres $postgresql_conf $pg_hba_conf
224
225         ######## compute a password if needed
226         if [ -z "$SFA_DB_PASSWORD" ] ; then
227             SFA_DB_PASSWORD=$(uuidgen)
228             sfa-config --category=sfa_db --variable=password --value="$SFA_DB_PASSWORD" --save=$sfa_local_config $sfa_local_config >& /dev/null
229             reload force
230         fi
231
232     else
233
234         ######## we are colocated with a myplc
235         # no need to worry about the pgsql setup (see /etc/plc.d/postgresql)
236         # myplc enforces the password for its user
237         PLC_DB_USER=$(plc-config --category=plc_db --variable=user)
238         PLC_DB_PASSWORD=$(plc-config --category=plc_db --variable=password)
239         # store this as the SFA user/password 
240         sfa-config --category=sfa_db --variable=user --value=$PLC_DB_USER --save=$sfa_local_config $sfa_local_config >& /dev/null
241         sfa-config --category=sfa_db --variable=password --value=$PLC_DB_PASSWORD --save=$sfa_local_config $sfa_local_config >& /dev/null
242         reload force
243     fi
244
245     ######## Start up the server
246     # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
247     if [ ! -f /etc/myplc-release ] ; then
248         echo STARTING...
249         service postgresql start >& /dev/null
250     fi
251     postgresql_check
252     check
253         
254     ######## make sure we have the user and db created
255     # user
256     if ! psql -U $SFA_DB_USER -c "" template1 >/dev/null 2>&1 ; then
257         psql -U postgres -c "CREATE USER $SFA_DB_USER PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
258     else
259         psql -U postgres -c "ALTER USER $SFA_DB_USER WITH PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
260     fi
261     check
262     
263     # db
264     if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then
265         createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME
266         check
267     fi
268     check
269     # mention sfaadmin.py instead of just sfaadmin for safety
270     sfaadmin.py reg sync_db
271
272     MESSAGE=$"SFA: Checking for PostgreSQL server"
273     echo -n "$MESSAGE"
274     [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
275 }
276
277 # shutdown DB
278 function db_stop () {
279
280     # only if enabled
281     [ "$SFA_DB_ENABLED" == 1 -o "$SFA_DB_ENABLED" == True ] || return
282
283     # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
284     if [ ! -f /etc/myplc-release ] ; then
285         service postgresql stop >& /dev/null
286         check
287         MESSAGE=$"Stopping PostgreSQL server"
288         echo -n "$MESSAGE"
289         [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
290     fi
291 }
292
293 function start() {
294     
295     reload
296
297     db_start
298     # migrations are now handled in the code by sfa.storage.dbschema
299
300     # install peer certs
301     action $"SFA: installing peer certs" daemon /usr/bin/sfa-start.py -t -d $OPTIONS 
302
303     [ "$SFA_REGISTRY_ENABLED" == 1 -o "$SFA_REGISTRY_ENABLED" == True ] && action $"SFA: Registry" daemon /usr/bin/sfa-start.py -r -d $OPTIONS
304     
305     [ "$SFA_AGGREGATE_ENABLED" == 1  -o "$SFA_AGGREGATE_ENABLED" == True ] && action $"SFA: Aggregate" daemon /usr/bin/sfa-start.py -a -d $OPTIONS
306         
307     [ "$SFA_SM_ENABLED" == 1 -o "$SFA_SM_ENABLED" == True ] && action "SFA: SliceMgr" daemon /usr/bin/sfa-start.py -s -d $OPTIONS
308
309     [ "$SFA_FLASHPOLICY_ENABLED" == 1 ] && \
310         action "Flash Policy Server" daemon /usr/bin/sfa_flashpolicy.py --file="$SFA_FLASHPOLICY_CONFIG_FILE" --port=$SFA_FLASHPOLICY_PORT -d
311
312     touch $SFALOCK
313
314 }
315
316 function stop() {
317     action $"Shutting down SFA" killproc sfa-start.py
318 # a possible alternative reads; esp. as we remove lock manually below
319 #    echo $"Shutting down SFA" ; pkill '^sfa-start'
320
321     db_stop
322
323     rm -f $SFALOCK
324 }
325
326
327 case "$1" in
328     start) start ;;
329     stop) stop ;;
330     reload) reload force ;;
331     restart) stop; start ;;
332     condrestart)
333         if [ -f $SFALOCK ]; then
334             stop
335             start
336         fi
337         ;;
338     status)
339         status sfa-start.py
340 # possible alternative for debian
341 #       pids=$(pgrep '^sfa-start'); [ -n "$pids" ] && ps $pids
342
343         RETVAL=$?
344         ;;
345     dbdump)
346         dump_sfa_db
347         ;;
348     *)
349         echo $"Usage: $0 {start|stop|reload|restart|condrestart|status|dbdump}"
350         exit 1
351         ;;
352 esac
353
354 exit $RETVAL
355