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