use the FQDN for PLC_WWW_HOST rather than localhost to get cron.php
[myplc.git] / guest.init
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 5 99
6 #
7 # description:  Manages all PLC services on this machine
8 #
9 # $Id$
10 #
11
12 # Source function library and configuration
13 . /etc/plc.d/functions
14
15 # Verbosity
16 verbose=0
17
18 # All steps should be idempotent. This means that you should be able
19 # to run them multiple times without depending on anything previously
20 # being run. The idea is that when the configuration changes, "service
21 # plc restart" is called, all dependencies are fixed up, and
22 # everything just works.
23
24 ### NOTE.
25 # we want the resulting myplc to be able to easily skip
26 # some steps. e.g. the packages step takes ages if you install
27 # all rpms under the repository.
28 # We skip steps whose name contains a dot (.) or a tilde (~)
29 # this way the operations would just rename a step name e.g.
30 # cd /etc/plc.d
31 # mv packages packages.hide
32 #
33 # The drawback is, this stuff does not survive an rpm update
34 # but that's maybe a good thing, that all is done at first start
35 ###
36
37 steps=($(
38 for step in /etc/plc.d/* ; do
39     stepname=$(basename $step)
40     plainstepname=$(echo $stepname | sed -e 's,\.,,' -e 's,~,,')
41     if [ -f $step -a -x $step -a "$stepname" = "$plainstepname" ] ; then
42         priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step)
43         echo $priority $stepname
44     fi
45 done | sort -n | cut -d' ' -f2
46 ))
47 nsteps=${#steps[@]}
48
49 # Regenerate configuration files
50 reload ()
51 {
52     force=$1
53
54     # Regenerate the main configuration file from default values
55     # overlaid with site-specific and current values.
56     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
57     files=(
58         /etc/planetlab/default_config.xml 
59         /etc/planetlab/configs/site.xml
60     )
61     for file in "${files[@]}" ; do
62         if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
63             tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
64             plc-config --xml "${files[@]}" >$tmp
65             if [ $? -eq 0 ] ; then
66                 mv $tmp /etc/planetlab/plc_config.xml
67                 chmod 444 /etc/planetlab/plc_config.xml
68             else
69                 echo "PLC: Warning: Invalid configuration file(s) detected"
70                 rm -f $tmp
71             fi
72             break
73         fi
74     done
75
76     # Convert configuration to various formats
77     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
78         plc-config --shell >/etc/planetlab/plc_config
79     fi
80     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
81         plc-config --python >/etc/planetlab/plc_config.py
82     fi
83     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
84         mkdir -p /etc/planetlab/php
85         plc-config --php >/etc/planetlab/php/plc_config.php
86     fi
87 }
88
89 usage()
90 {
91     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
92     echo "      -v              Be verbose"
93     echo "      -h              This message"
94     echo
95     echo "Commands:"
96     echo "      start           Start all PLC subsystems"
97     echo "      stop            Stop all PLC subsystems"
98     echo "      reload          Regenerate configuration files"
99     echo "      restart         Restart all PLC subsystems"
100     echo "      checkpoint filename : Checkpoint the current state of MyPLC to filename"
101     echo "      restore filename : Restore MyPLC state from filename"
102     echo "      steps           Displays ordered list of subsystems"
103     echo
104     echo "Steps:"
105     for step in "${steps[@]}" ; do
106         if [ -x /etc/plc.d/$step ] ; then
107             echo "      $(basename $step)"
108         fi
109     done
110     exit 1
111 }
112
113 # Get options
114 while getopts "vh" opt ; do
115     case $opt in
116         v)
117             verbose=1
118             ;;
119         h|*)
120             usage
121             ;;
122     esac
123 done
124
125 # Redirect stdout and stderr of each step to /var/log/boot.log
126 if [ $verbose -eq 0 ] ; then
127     touch /var/log/boot.log
128     chmod 600 /var/log/boot.log
129     exec 1>>/var/log/boot.log
130     exec 2>>/var/log/boot.log
131 fi
132
133 # Get command
134 shift $(($OPTIND - 1))
135 if [ -z "$1" ] ; then
136     usage
137 fi
138 command=$1
139
140 # Get step(s)
141 shift 1
142 if [ -z "$1" ] ; then
143     # Start or stop everything. Regenerate configuration first.
144     reload force
145 else
146     # Start or stop a particular step
147     steps=("$@")
148     nsteps=${#steps[@]}
149 fi
150
151 RETVAL=0
152
153 start ()
154 {
155     for step in "${steps[@]}" ; do
156         if [ -x /etc/plc.d/$step ] ; then
157             /etc/plc.d/$step start
158             # Steps may alter the configuration, may need to regenerate
159             reload
160         else
161             echo "PLC: $step: unrecognized step" >&4
162             exit 1
163         fi
164     done
165 }
166
167 stop ()
168 {
169     for i in $(seq 1 $nsteps) ; do
170         step=${steps[$(($nsteps - $i))]}
171         if [ -x /etc/plc.d/$step ] ; then
172             /etc/plc.d/$step stop
173             # Steps may alter the configuration, may need to regenerate
174             reload
175         else
176             echo "PLC: $step: unrecognized step" >&4
177             exit 1
178         fi
179     done
180 }
181
182 case "$command" in
183     start|stop)
184         $command
185         ;;
186
187     restart)
188         stop
189         start
190         ;;
191
192     reload)
193         reload force
194         ;;
195
196     checkpoint)
197         cpfile=$1
198         if [ -z "$cpfile" ] ; then
199             echo "PLC: checkpoint requires a filename as an argument"
200             exit 1
201         fi 
202         cpdir=$(mktemp -d tmp.XXXXXX)
203         cd $cpdir
204         mkdir -p ./etc/planetlab/
205         rsync -av /etc/planetlab/ ./etc/planetlab/
206         /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
207         tar cjf $cpfile etc
208         cd -
209         rm -rf $cpdir
210         ;;
211
212     restore)
213         cpfile=$1
214         cpdir=$(mktemp -d tmp.XXXXXX)
215         cd $cpdir
216         tar xjf $cpfile
217         /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
218         rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
219         rsync -av ./etc/planetlab/ /etc/planetlab/
220         cd -
221         rm -rf $cpdir
222         ;;
223
224     steps)
225         echo "${steps[@]}" >&4
226         ;;
227
228     # for backwards compatibility
229     mount|umount|mountstatus)
230         echo "${command} not used within native myplc environment"
231         ;;
232
233     *)
234         usage >&3
235         ;;
236 esac
237
238 exit $RETVAL