yum.conf created from build/mirroring
[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/php/plc_config.php ] ; then
81         mkdir -p /etc/planetlab/php
82         plc-config --php >/etc/planetlab/php/plc_config.php
83     fi
84 }
85
86 usage()
87 {
88     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
89     echo "      -v              Be verbose"
90     echo "      -h              This message"
91     echo
92     echo "Commands:"
93     echo "      start           Start all PLC subsystems"
94     echo "      stop            Stop all PLC subsystems"
95     echo "      reload          Regenerate configuration files"
96     echo "      restart         Restart all PLC subsystems"
97     echo "      checkpoint filename : Checkpoint the current state of MyPLC to filename"
98     echo "      restore filename : Restore MyPLC state from filename"
99     echo "      steps           Displays ordered list of subsystems"
100     echo
101     echo "Steps:"
102     for step in "${steps[@]}" ; do
103         if [ -x /etc/plc.d/$step ] ; then
104             echo "      $(basename $step)"
105         fi
106     done
107     exit 1
108 }
109
110 # Get options
111 while getopts "vh" opt ; do
112     case $opt in
113         v)
114             verbose=1
115             ;;
116         h|*)
117             usage
118             ;;
119     esac
120 done
121
122 # Redirect stdout and stderr of each step to /var/log/boot.log
123 if [ $verbose -eq 0 ] ; then
124     touch /var/log/boot.log
125     chmod 600 /var/log/boot.log
126     exec 1>>/var/log/boot.log
127     exec 2>>/var/log/boot.log
128 fi
129
130 # Get command
131 shift $(($OPTIND - 1))
132 if [ -z "$1" ] ; then
133     usage
134 fi
135 command=$1
136
137 # Get step(s)
138 shift 1
139 if [ -z "$1" ] ; then
140     # Start or stop everything. Regenerate configuration first.
141     reload force
142 else
143     # Start or stop a particular step
144     steps=("$@")
145     nsteps=${#steps[@]}
146 fi
147
148 RETVAL=0
149
150 start ()
151 {
152     for step in "${steps[@]}" ; do
153         if [ -x /etc/plc.d/$step ] ; then
154             /etc/plc.d/$step start
155             # Steps may alter the configuration, may need to regenerate
156             reload
157         else
158             echo "PLC: $step: unrecognized step" >&4
159             exit 1
160         fi
161     done
162 }
163
164 stop ()
165 {
166     for i in $(seq 1 $nsteps) ; do
167         step=${steps[$(($nsteps - $i))]}
168         if [ -x /etc/plc.d/$step ] ; then
169             /etc/plc.d/$step stop
170             # Steps may alter the configuration, may need to regenerate
171             reload
172         else
173             echo "PLC: $step: unrecognized step" >&4
174             exit 1
175         fi
176     done
177 }
178
179 case "$command" in
180     start|stop)
181         $command
182         ;;
183
184     restart)
185         stop
186         start
187         ;;
188
189     reload)
190         reload force
191         ;;
192
193     checkpoint)
194         cpfile=$1
195         if [ -z "$cpfile" ] ; then
196             echo "PLC: checkpoint requires a filename as an argument"
197             exit 1
198         fi 
199         cpdir=$(mktemp -d tmp.XXXXXX)
200         cd $cpdir
201         mkdir -p ./etc/planetlab/
202         rsync -av /etc/planetlab/ ./etc/planetlab/
203         /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
204         tar cjf $cpfile etc
205         cd -
206         rm -rf $cpdir
207         ;;
208
209     restore)
210         cpfile=$1
211         cpdir=$(mktemp -d tmp.XXXXXX)
212         cd $cpdir
213         tar xjf $cpfile
214         /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
215         rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
216         rsync -av ./etc/planetlab/ /etc/planetlab/
217         cd -
218         rm -rf $cpdir
219         ;;
220
221     steps)
222         echo "${steps[@]}" >&4
223         ;;
224
225     # for backwards compatibility
226     mount|umount|mountstatus)
227         echo "${command} not used within native myplc environment"
228         ;;
229
230     *)
231         usage >&3
232         ;;
233 esac
234
235 exit $RETVAL