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