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