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