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