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