3 # sfa Wraps PLCAPI into the SFA compliant API
5 # hopefully right after plc
6 # chkconfig: 2345 61 39
8 # description: Wraps PLCAPI into the SFA compliant API
11 # source function library
12 . /etc/init.d/functions
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
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
29 # Source sfa shell config if present
30 [ -f /etc/sfa/sfa_config ] && . /etc/sfa/sfa_config
32 # Export so that we do not have to specify -p to psql invocations
33 export PGPORT=$SFA_DB_PORT
36 # Total number of errors
39 # Count the exit status of the last command
42 ERRORS=$(($ERRORS+$?))
45 # can't trust the return of service postgresql start / nor status
46 function postgresql_check () {
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
63 # Regenerate configuration files - almost verbatim from plc.init
67 # Regenerate the main configuration file from default values
68 # overlaid with site-specific and current values.
69 # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
70 files=( $sfa_default_config $sfa_local_config )
71 for file in "${files[@]}" ; do
72 if [ -n "$force" -o $file -nt $sfa_whole_config ] ; then
73 tmp=$(mktemp /tmp/sfa_config.xml.XXXXXX)
74 plc-config --xml "${files[@]}" >$tmp
75 if [ $? -eq 0 ] ; then
76 mv $tmp $sfa_whole_config
77 chmod 444 $sfa_whole_config
79 echo "SFA: Warning: Invalid configuration file(s) detected"
86 # Convert configuration to various formats
87 if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/sfa_config ] ; then
88 plc-config --shell $sfa_whole_config >/etc/sfa/sfa_config
90 if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/sfa_config.py ] ; then
91 plc-config --python $sfa_whole_config >/etc/sfa/sfa_config.py
93 # if [ -n "$force" -o $sfa_whole_config -nt /etc/sfa/php/sfa_config.php ] ; then
94 # mkdir -p /etc/sfa/php
95 # plc-config --php $sfa_whole_config >/etc/sfa/php/sfa_config.php
98 # [re]generate the sfa_component_config
99 # this is a server-side thing but produces a file that somehow needs to be pushed
100 # on the planetlab nodes; in the case where sfa and myplc run on different boxes
101 # (or there is no myplc at all) this should be turned off
102 # as the component manager is not operational yet we skip this for now
103 #gen-sfa-cm-config.py
106 ### initialize DB (don't chkconfig postgresql on)
107 function db_start () {
110 [ "$SFA_DB_ENABLED" == 1 ] || return
112 if ! rpm -q myplc >& /dev/null; then
114 ######## standalone deployment - no colocated myplc
117 # Set data directory and redirect startup output to /var/log/pgsql
118 mkdir -p $(dirname $postgresql_sysconfig)
119 # remove previous definitions
120 touch $postgresql_sysconfig
121 tmp=${postgresql_sysconfig}.new
122 ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig
123 echo "PGDATA=$PGDATA"
124 echo "PGLOG=/var/log/pgsql"
125 echo "PGPORT=$PLC_DB_PORT"
126 ) >> $tmp ; mv -f $tmp $postgresql_sysconfig
128 ######## /var/lib/pgsql/data
129 # Fix ownership (rpm installation may have changed it)
130 chown -R -H postgres:postgres $(dirname $PGDATA)
132 # PostgreSQL must be started at least once to bootstrap
133 # /var/lib/pgsql/data
134 if [ ! -f $postgresql_conf ] ; then
135 service postgresql initdb &> /dev/null || :
139 ######## /var/lib/pgsql/data/postgresql.conf
141 foo=$(python -c "import socket; print socket.gethostbyname(\"$SFA_REGISTRY_HOST\")") && registry_ip="$foo"
142 # Enable DB server. drop Postgresql<=7.x
143 # PostgreSQL >=8.0 defines listen_addresses
144 # listen on a specific IP + localhost, more robust when run within a vserver
145 sed -i -e '/^listen_addresses/d' $postgresql_conf
146 if [ -z "$registry_ip" ] ; then
147 echo "listen_addresses = 'localhost'" >> $postgresql_conf
149 echo "listen_addresses = '${registry_ip},localhost'" >> $postgresql_conf
151 # tweak timezone to be 'UTC'
152 sed -i -e '/^timezone=/d' $postgresql_conf
153 echo "timezone='UTC'" >> $postgresql_conf
155 ######## /var/lib/pgsql/data/pg_hba.conf
156 # Disable access to all DBs from all hosts
157 sed -i -e '/^\(host\|local\)/d' $pghba_conf
159 # Enable passwordless localhost access
160 echo "local all all trust" >>$pghba_conf
163 echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password"
164 [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password"
167 # Fix ownership (sed -i changes it)
168 chown postgres:postgres $postgresql_conf $pghba_conf
170 ######## compute a password if needed
171 if [ -z "$SFA_DB_PASSWORD" ] ; then
172 SFA_DB_PASSWORD=$(uuidgen)
173 plc-config --category=sfa_db --variable=password --value="$SFA_DB_PASSWORD" --save=$sfa_local_config $sfa_local_config >& /dev/null
179 ######## we are colocated with a myplc
180 # no need to worry about the pgsql setup (see /etc/plc.d/postgresql)
181 # myplc enforces the password for its user
182 PLC_DB_USER=$(plc-config --category=plc_db --variable=user)
183 PLC_DB_PASSWORD=$(plc-config --category=plc_db --variable=password)
184 # store this as the SFA user/password
185 plc-config --category=sfa_db --variable=user --value=$PLC_DB_USER --save=$sfa_local_config $sfa_local_config >& /dev/null
186 plc-config --category=sfa_db --variable=password --value=$PLC_DB_PASSWORD --save=$sfa_local_config $sfa_local_config >& /dev/null
190 ######## Start up the server
191 # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
192 if ! rpm -q myplc >& /dev/null ; then
194 service postgresql start >& /dev/null
199 ######## make sure we have the user and db created
201 if ! psql -U $SFA_DB_USER -c "" template1 >/dev/null 2>&1 ; then
202 psql -U postgres -c "CREATE USER $SFA_DB_USER PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
204 psql -U postgres -c "ALTER USER $SFA_DB_USER WITH PASSWORD '$SFA_DB_PASSWORD'" template1 >& /dev/null
209 if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then
210 createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME
211 # xxx in case we'd like to ship the db schema separately
212 #psql -U $SFA_DB_USER -f /etc/sfa/or/someplace/else/sfa.sql $SFA_DB_NAME
216 MESSAGE=$"Checking for PostgreSQL server"
218 [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
222 function db_stop () {
225 [ "$SFA_DB_ENABLED" == 1 ] || return
227 # not too nice, but.. when co-located with myplc we'll let it start/stop postgresql
228 if ! rpm -q myplc >& /dev/null ; then
229 service postgresql stop >& /dev/null
231 MESSAGE=$"Stopping PostgreSQL server"
233 [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo
244 action $"SFA installing peer certs" daemon /usr/bin/sfa-start.py -t -d $OPTIONS
246 if [ "$SFA_REGISTRY_ENABLED" -eq 1 ]; then
247 action $"SFA Registry" daemon /usr/bin/sfa-start.py -r -d $OPTIONS
250 if [ "$SFA_AGGREGATE_ENABLED" -eq 1 ]; then
251 action $"SFA Aggregate" daemon /usr/bin/sfa-start.py -a -d $OPTIONS
254 if [ "$SFA_SM_ENABLED" -eq 1 ]; then
255 action "SFA SliceMgr" daemon /usr/bin/sfa-start.py -s -d $OPTIONS
258 if [ "$SFA_FLASHPOLICY_ENABLED" -eq 1 ]; then
259 action "Flash Policy Server" daemon /usr/bin/sfa_flashpolicy.py --file="$SFA_FLASHPOLICY_CONFIG_FILE" --port=$SFA_FLASHPOLICY_PORT -d
262 touch /var/lock/subsys/sfa-start.py
267 action $"Shutting down SFA" killproc sfa-start.py
271 rm -f /var/lock/subsys/sfa-start.py
278 reload) reload force ;;
279 restart) stop; start ;;
281 if [ -f /var/lock/subsys/sfa-start.py ]; then
291 echo $"Usage: $0 {start|stop|reload|restart|condrestart|status}"