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