PlanetLab software license and copyright
[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.17 2006/04/27 21:50:00 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     tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
39     plc-config --xml \
40         /etc/planetlab/default_config.xml \
41         /etc/planetlab/configs/* \
42         /etc/planetlab/plc_config.xml \
43         >$tmp
44     if [ $? -eq 0 ] ; then
45         mv $tmp /etc/planetlab/plc_config.xml
46         chmod 644 /etc/planetlab/plc_config.xml
47     else
48         echo "PLC: Warning: Invalid configuration file(s) detected"
49         rm -f $tmp
50     fi
51
52     # Shell constants
53     plc-config --shell >/etc/planetlab/plc_config
54     . /etc/planetlab/plc_config
55
56     # Generate various defaults
57     if [ -z "$PLC_DB_PASSWORD" ] ; then
58         PLC_DB_PASSWORD=$(uuidgen)
59         plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
60     fi
61
62     if [ -z "$PLC_API_MAINTENANCE_PASSWORD" ] ; then
63         PLC_API_MAINTENANCE_PASSWORD=$(uuidgen)
64         plc-config --category=plc_api --variable=maintenance_password --value="$PLC_API_MAINTENANCE_PASSWORD" --save
65     fi
66
67     # Need to configure network before resolving hostnames
68     /etc/plc.d/network start 3>/dev/null 4>/dev/null
69
70     PLC_API_MAINTENANCE_SOURCES=$(
71         for server in API BOOT WWW ; do
72             hostname=PLC_${server}_HOST
73             gethostbyname ${!hostname}
74         done | sort -u
75     )
76     plc-config --category=plc_api --variable=maintenance_sources --value="$PLC_API_MAINTENANCE_SOURCES" --save
77
78     # Save configuration
79     mkdir -p /etc/planetlab/php
80     plc-config --php >/etc/planetlab/php/plc_config.php
81     plc-config --shell >/etc/planetlab/plc_config
82
83     # For backward compatibility, until we can convert all code to use
84     # the now standardized variable names.
85
86     # DB constants are all named the same
87     ln -sf plc_config /etc/planetlab/plc_db
88
89     # PHP constants
90     cat >/etc/planetlab/php/site_constants.php <<"EOF"
91 <?php
92 include('plc_config.php');
93
94 define('PL_API_SERVER', PLC_API_HOST);
95 define('PL_API_PATH', PLC_API_PATH);
96 define('PL_API_PORT', PLC_API_PORT);
97 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
98 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
99 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
100 define('WWW_BASE', PLC_WWW_HOST);
101 define('BOOT_BASE', PLC_BOOT_HOST);
102 define('DEBUG', PLC_WWW_DEBUG);
103 define('API_CALL_DEBUG', PLC_API_DEBUG);
104 define('SENDMAIL', PLC_MAIL_ENABLED);
105 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
106 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
107 ?>
108 EOF
109
110     # API constants are written in plc.d/api
111 }
112
113 usage()
114 {
115     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
116     echo "      -v              Be verbose"
117     echo "      -h              This message"
118     echo
119     echo "Commands:"
120     echo "      start           Start all PLC subsystems"
121     echo "      stop            Stop all PLC subsystems"
122     echo "      reload          Regenerate configuration files"
123     echo "      restart         Restart all PLC subsystems"
124     echo
125     echo "Steps:"
126     for step in "${steps[@]}" ; do
127         if [ -x /etc/plc.d/$step ] ; then
128             echo "      $(basename $step)"
129         fi
130     done
131     exit 1
132 }
133
134 # Get options
135 while getopts "vh" opt ; do
136     case $opt in
137         v)
138             verbose=1
139             ;;
140         h|*)
141             usage
142             ;;
143     esac
144 done
145
146 # Redirect stdout and stderr of each step to /var/log/boot.log
147 if [ $verbose -eq 0 ] ; then
148     touch /var/log/boot.log
149     chmod 600 /var/log/boot.log
150     exec 1>>/var/log/boot.log
151     exec 2>>/var/log/boot.log
152 fi
153
154 # Get command
155 shift $(($OPTIND - 1))
156 if [ -z "$1" ] ; then
157     usage
158 fi
159 command=$1
160
161 # Get step(s)
162 shift 1
163 if [ -z "$1" ] ; then
164     # Start or stop everything. Regenerate configuration first.
165     reload
166 else
167     # Start or stop a particular step
168     steps=("$@")
169     nsteps=${#steps[@]}
170 fi
171
172 RETVAL=0
173
174 start ()
175 {
176     for step in "${steps[@]}" ; do
177         if [ -x /etc/plc.d/$step ] ; then
178             /etc/plc.d/$step start
179         else
180             echo "PLC: $step: unrecognized step" >&4
181             exit 1
182         fi
183     done
184 }
185
186 stop ()
187 {
188     for i in $(seq 1 $nsteps) ; do
189         step=${steps[$(($nsteps - $i))]}
190         if [ -x /etc/plc.d/$step ] ; then
191             /etc/plc.d/$step stop
192         else
193             echo "PLC: $step: unrecognized step" >&4
194             exit 1
195         fi
196     done
197 }
198
199 case "$command" in
200     start|stop)
201         $command
202         ;;
203
204     restart)
205         stop
206         start
207         ;;
208
209     reload)
210         ;;
211
212     *)
213         usage >&3
214         ;;
215 esac
216
217 exit $RETVAL