fix resolv.conf issue on plc
[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 steps=($(
39 for step in /etc/plc.d/* ; do
40     stepname=$(basename $step)
41     plainstepname=$(echo $stepname | sed -e 's,\.,,' -e 's,~,,')
42     if [ -f $step -a -x $step -a "$stepname" = "$plainstepname" ] ; then
43         priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step)
44         echo $priority $stepname
45     fi
46 done | sort -n | cut -d' ' -f2
47 ))
48 nsteps=${#steps[@]}
49
50 # Regenerate configuration files
51 reload ()
52 {
53     force=$1
54
55     # Regenerate the main configuration file from default values
56     # overlaid with site-specific and current values.
57     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
58     files=(
59         /etc/planetlab/default_config.xml 
60         /etc/planetlab/configs/site.xml
61     )
62     for file in "${files[@]}" ; do
63         if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
64             tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
65             plc-config --xml "${files[@]}" >$tmp
66             if [ $? -eq 0 ] ; then
67                 mv $tmp /etc/planetlab/plc_config.xml
68                 chmod 444 /etc/planetlab/plc_config.xml
69             else
70                 echo "PLC: Warning: Invalid configuration file(s) detected"
71                 rm -f $tmp
72             fi
73             break
74         fi
75     done
76
77     # Convert configuration to various formats
78     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
79         plc-config --shell >/etc/planetlab/plc_config
80     fi
81     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
82         plc-config --python >/etc/planetlab/plc_config.py
83     fi
84     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.rb ] ; then
85         plc-config --ruby >/etc/planetlab/plc_config.rb
86     fi
87     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
88         mkdir -p /etc/planetlab/php
89         plc-config --php >/etc/planetlab/php/plc_config.php
90     fi
91 }
92
93 usage()
94 {
95     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
96     echo "      -v              Be verbose"
97     echo "      -h              This message"
98     echo
99     echo "Commands:"
100     echo "      start           Start all PLC subsystems"
101     echo "      stop            Stop all PLC subsystems"
102     echo "      reload          Regenerate configuration files"
103     echo "      restart         Restart all PLC subsystems"
104     echo "      checkpoint filename : Checkpoint the current state of MyPLC to filename"
105     echo "      restore filename : Restore MyPLC state from filename"
106     echo "      steps           Displays ordered list of subsystems"
107     echo
108     echo "Steps:"
109     for step in "${steps[@]}" ; do
110         if [ -x /etc/plc.d/$step ] ; then
111             echo "      $(basename $step)"
112         fi
113     done
114     exit 1
115 }
116
117 # Get options
118 while getopts "vh" opt ; do
119     case $opt in
120         v)
121             verbose=1
122             ;;
123         h|*)
124             usage
125             ;;
126     esac
127 done
128
129 # Redirect stdout and stderr of each step to /var/log/boot.log
130 if [ $verbose -eq 0 ] ; then
131     touch /var/log/boot.log
132     chmod 600 /var/log/boot.log
133     exec 1>>/var/log/boot.log
134     exec 2>>/var/log/boot.log
135 fi
136
137 # Get command
138 shift $(($OPTIND - 1))
139 if [ -z "$1" ] ; then
140     usage >&3
141 fi
142 command=$1
143
144 # Get step(s)
145 shift 1
146 if [ -z "$1" ] ; then
147     # Start or stop everything. Regenerate configuration first.
148     reload force
149 else
150     # Start or stop a particular step
151     steps=("$@")
152     nsteps=${#steps[@]}
153 fi
154
155 RETVAL=0
156
157 start ()
158 {
159     for step in "${steps[@]}" ; do
160         if [ -x /etc/plc.d/$step ] ; then
161             /etc/plc.d/$step start
162             # Steps may alter the configuration, may need to regenerate
163             reload
164         else
165             echo "PLC: $step: unrecognized step" >&4
166             exit 1
167         fi
168     done
169 }
170
171 stop ()
172 {
173     for i in $(seq 1 $nsteps) ; do
174         step=${steps[$(($nsteps - $i))]}
175         if [ -x /etc/plc.d/$step ] ; then
176             /etc/plc.d/$step stop
177             # Steps may alter the configuration, may need to regenerate
178             reload
179         else
180             echo "PLC: $step: unrecognized step" >&4
181             exit 1
182         fi
183     done
184 }
185
186 case "$command" in
187     start|stop)
188         $command
189         ;;
190
191     restart)
192         stop
193         start
194         ;;
195
196     reload)
197         reload force
198         ;;
199
200     checkpoint)
201         cpfile=$1
202         if [ -z "$cpfile" ] ; then
203             echo "PLC: checkpoint requires a filename as an argument"
204             exit 1
205         fi 
206         cpdir=$(mktemp -d tmp.XXXXXX)
207         cd $cpdir
208         mkdir -p ./etc/planetlab/
209         rsync -av /etc/planetlab/ ./etc/planetlab/
210         /etc/plc.d/db checkpoint ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
211         tar cjf $cpfile etc
212         cd -
213         rm -rf $cpdir
214         ;;
215
216     restore)
217         cpfile=$1
218         cpdir=$(mktemp -d tmp.XXXXXX)
219         cd $cpdir
220         tar xjf $cpfile
221         /etc/plc.d/db restore ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
222         rm -f ./etc/planetlab/plc_db.checkpoint ./etc/planetlab/plc_drupal.checkpoint
223         rsync -av ./etc/planetlab/ /etc/planetlab/
224         cd -
225         rm -rf $cpdir
226         ;;
227
228     steps)
229         echo "${steps[@]}" >&4
230         ;;
231
232     # for backwards compatibility
233     mount|umount|mountstatus)
234         echo "${command} not used within native myplc environment"
235         ;;
236
237     *)
238         usage >&3
239         ;;
240 esac
241
242 exit $RETVAL