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