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