- make gpg public key readable
[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.16 2006/04/17 17:04:37 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     # API constants
90     cat >/etc/planetlab/plc_api <<EOF
91 PL_API_SERVER='$PLC_API_HOST'
92 PL_API_PATH='$PLC_API_PATH'
93 PL_API_PORT=$PLC_API_PORT
94 PL_API_CAPABILITY_AUTH_METHOD='capability'
95 PL_API_CAPABILITY_PASS='$PLC_API_MAINTENANCE_PASSWORD'
96 PL_API_CAPABILITY_USERNAME='$PLC_API_MAINTENANCE_USER'
97 PL_API_TICKET_KEY_FILE='$PLC_API_SSL_KEY'
98 PLANETLAB_SUPPORT_EMAIL='$PLC_MAIL_SUPPORT_ADDRESS'
99 BOOT_MESSAGES_EMAIL='$PLC_MAIL_BOOT_ADDRESS'
100 WWW_BASE='$PLC_WWW_HOST'
101 BOOT_BASE='$PLC_BOOT_HOST'
102 EOF
103
104     # API expects root SSH public key to be at /etc/planetlab/node_root_key
105     ln -sf "$PLC_ROOT_SSH_KEY_PUB" /etc/planetlab/node_root_key
106
107     # The format is
108     #
109     # ip:max_role_id:organization_id:password
110     #
111     # It is unlikely that we will let federated sites use the
112     # maintenance account to access each others' APIs, so we always
113     # set organization_id to -1.
114     (
115         echo -n "PL_API_CAPABILITY_SOURCES='"
116         first=1
117         for ip in $PLC_API_MAINTENANCE_SOURCES ; do
118             if [ $first -ne 1 ] ; then
119                 echo -n " "
120             fi
121             first=0
122             echo -n "$ip:-1:-1:$PLC_API_MAINTENANCE_PASSWORD"
123         done
124         echo "'"
125     ) >>/etc/planetlab/plc_api
126
127     cat >/etc/planetlab/php/site_constants.php <<"EOF"
128 <?php
129 include('plc_config.php');
130
131 define('PL_API_SERVER', PLC_API_HOST);
132 define('PL_API_PATH', PLC_API_PATH);
133 define('PL_API_PORT', PLC_API_PORT);
134 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
135 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
136 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
137 define('WWW_BASE', PLC_WWW_HOST);
138 define('BOOT_BASE', PLC_BOOT_HOST);
139 define('DEBUG', PLC_WWW_DEBUG);
140 define('API_CALL_DEBUG', PLC_API_DEBUG);
141 define('SENDMAIL', PLC_MAIL_ENABLED);
142 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
143 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
144 ?>
145 EOF
146 }
147
148 usage()
149 {
150     echo "Usage: $0 [OPTION]... [COMMAND] [STEP]..."
151     echo "      -v              Be verbose"
152     echo "      -h              This message"
153     echo
154     echo "Commands:"
155     echo "      start           Start all PLC subsystems"
156     echo "      stop            Stop all PLC subsystems"
157     echo "      reload          Regenerate configuration files"
158     echo "      restart         Restart all PLC subsystems"
159     echo
160     echo "Steps:"
161     for step in "${steps[@]}" ; do
162         if [ -x /etc/plc.d/$step ] ; then
163             echo "      $(basename $step)"
164         fi
165     done
166     exit 1
167 }
168
169 # Get options
170 while getopts "vh" opt ; do
171     case $opt in
172         v)
173             verbose=1
174             ;;
175         h|*)
176             usage
177             ;;
178     esac
179 done
180
181 # Redirect stdout and stderr of each step to /var/log/boot.log
182 if [ $verbose -eq 0 ] ; then
183     exec 1>>/var/log/boot.log
184     exec 2>>/var/log/boot.log
185 fi
186
187 # Get command
188 shift $(($OPTIND - 1))
189 if [ -z "$1" ] ; then
190     usage
191 fi
192 command=$1
193
194 # Get step(s)
195 shift 1
196 if [ -z "$1" ] ; then
197     # Start or stop everything. Regenerate configuration first.
198     reload
199 else
200     # Start or stop a particular step
201     steps=("$@")
202     nsteps=${#steps[@]}
203 fi
204
205 RETVAL=0
206
207 start ()
208 {
209     for step in "${steps[@]}" ; do
210         if [ -x /etc/plc.d/$step ] ; then
211             /etc/plc.d/$step start
212         else
213             echo "PLC: $step: unrecognized step" >&4
214             exit 1
215         fi
216     done
217 }
218
219 stop ()
220 {
221     for i in $(seq 1 $nsteps) ; do
222         step=${steps[$(($nsteps - $i))]}
223         if [ -x /etc/plc.d/$step ] ; then
224             /etc/plc.d/$step stop
225         else
226             echo "PLC: $step: unrecognized step" >&4
227             exit 1
228         fi
229     done
230 }
231
232 case "$command" in
233     start|stop)
234         $command
235         ;;
236
237     restart)
238         stop
239         start
240         ;;
241
242     reload)
243         ;;
244
245     *)
246         usage >&3
247         ;;
248 esac
249
250 exit $RETVAL