- fix version number, bump release number, added changelog
[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: guest.init,v 1.20 2006/08/08 23:19:52 mlhuang Exp $
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 steps=($(
24 for step in /etc/plc.d/* ; do
25     if [ -f $step -a -x $step ] ; then
26         priority=$(sed -ne 's/# priority: \(.*\)/\1/p' $step)
27         echo $priority $(basename $step)
28     fi
29 done | sort -n | cut -d' ' -f2
30 ))
31 nsteps=${#steps[@]}
32
33 # Regenerate configuration files
34 reload ()
35 {
36     force=$1
37
38     # Regenerate the main configuration file from default values
39     # overlaid with site-specific and current values.
40     files=(
41         /etc/planetlab/default_config.xml 
42         /etc/planetlab/configs/*
43         /etc/planetlab/plc_config.xml
44     )
45     for file in "${files[@]}" ; do
46         if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
47             tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
48             plc-config --xml "${files[@]}" >$tmp
49             if [ $? -eq 0 ] ; then
50                 mv $tmp /etc/planetlab/plc_config.xml
51                 chmod 644 /etc/planetlab/plc_config.xml
52             else
53                 echo "PLC: Warning: Invalid configuration file(s) detected"
54                 rm -f $tmp
55             fi
56             break
57         fi
58     done
59
60     # Convert configuration to various formats
61     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
62         plc-config --shell >/etc/planetlab/plc_config
63     fi
64     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
65         mkdir -p /etc/planetlab/php
66         plc-config --php >/etc/planetlab/php/plc_config.php
67     fi
68 }
69
70 usage()
71 {
72     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
73     echo "      -v              Be verbose"
74     echo "      -h              This message"
75     echo
76     echo "Commands:"
77     echo "      start           Start all PLC subsystems"
78     echo "      stop            Stop all PLC subsystems"
79     echo "      reload          Regenerate configuration files"
80     echo "      restart         Restart all PLC subsystems"
81     echo
82     echo "Steps:"
83     for step in "${steps[@]}" ; do
84         if [ -x /etc/plc.d/$step ] ; then
85             echo "      $(basename $step)"
86         fi
87     done
88     exit 1
89 }
90
91 # Get options
92 while getopts "vh" opt ; do
93     case $opt in
94         v)
95             verbose=1
96             ;;
97         h|*)
98             usage
99             ;;
100     esac
101 done
102
103 # Redirect stdout and stderr of each step to /var/log/boot.log
104 if [ $verbose -eq 0 ] ; then
105     touch /var/log/boot.log
106     chmod 600 /var/log/boot.log
107     exec 1>>/var/log/boot.log
108     exec 2>>/var/log/boot.log
109 fi
110
111 # Get command
112 shift $(($OPTIND - 1))
113 if [ -z "$1" ] ; then
114     usage
115 fi
116 command=$1
117
118 # Get step(s)
119 shift 1
120 if [ -z "$1" ] ; then
121     # Start or stop everything. Regenerate configuration first.
122     reload force
123 else
124     # Start or stop a particular step
125     steps=("$@")
126     nsteps=${#steps[@]}
127 fi
128
129 RETVAL=0
130
131 start ()
132 {
133     for step in "${steps[@]}" ; do
134         if [ -x /etc/plc.d/$step ] ; then
135             /etc/plc.d/$step start
136             # Steps may alter the configuration, may need to regenerate
137             reload
138         else
139             echo "PLC: $step: unrecognized step" >&4
140             exit 1
141         fi
142     done
143 }
144
145 stop ()
146 {
147     for i in $(seq 1 $nsteps) ; do
148         step=${steps[$(($nsteps - $i))]}
149         if [ -x /etc/plc.d/$step ] ; then
150             /etc/plc.d/$step stop
151             # Steps may alter the configuration, may need to regenerate
152             reload
153         else
154             echo "PLC: $step: unrecognized step" >&4
155             exit 1
156         fi
157     done
158 }
159
160 case "$command" in
161     start|stop)
162         $command
163         ;;
164
165     restart)
166         stop
167         start
168         ;;
169
170     reload)
171         reload force
172         ;;
173
174     *)
175         usage >&3
176         ;;
177 esac
178
179 exit $RETVAL