- use preferred lowercase define() in php generation
[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.11 2006/04/03 21:50:29 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 . /etc/init.d/functions
16
17 # Verbosity
18 verbose=0
19
20 # Keep in order! All steps should be idempotent. This means that you
21 # should be able to run them multiple times without depending on
22 # anything previously being run. The idea is that when the
23 # configuration changes, "service plc restart" is called, all
24 # dependencies are fixed up, and everything just works.
25 steps=(
26 network
27 syslog
28 postgresql
29 ssl
30 gpg
31 ssh
32 apache
33 api
34 cron
35 bootcd
36 bootmanager
37 )
38 nsteps=${#steps[@]}
39
40 # Total number of errors
41 ERRORS=0
42
43 # Count the exit status of the last command
44 check ()
45 {
46     ERRORS=$(($ERRORS+$?))
47 }
48
49 # Return IP address of hostname if resolvable
50 gethostbyname ()
51 {
52     perl -MSocket -e '($a,$b,$c,$d,@addrs) = gethostbyname($ARGV[0]); print inet_ntoa($addrs[0]) . "\n";' $1 2>/dev/null
53 }
54
55 # Regenerate configuration files
56 reload ()
57 {
58     # Load configuration
59     plc-config --shell >/etc/planetlab/plc_config
60     . /etc/planetlab/plc_config
61
62     # Generate various defaults
63     if [ -z "$PLC_DB_PASSWORD" ] ; then
64         PLC_DB_PASSWORD=$(uuidgen)
65         plc-config --category=plc_db --variable=password --value="$PLC_DB_PASSWORD" --save
66     fi
67
68     if [ -z "$PLC_API_MAINTENANCE_PASSWORD" ] ; then
69         PLC_API_MAINTENANCE_PASSWORD=$(uuidgen)
70         plc-config --category=plc_api --variable=maintenance_password --value="$PLC_API_MAINTENANCE_PASSWORD" --save
71     fi
72
73     # Need to configure network before resolving hostnames
74     config_network start
75
76     PLC_API_MAINTENANCE_SOURCES=$(
77         for server in API BOOT WWW ; do
78             hostname=PLC_${server}_HOST
79             gethostbyname ${!hostname}
80         done | sort -u
81     )
82     plc-config --category=plc_api --variable=maintenance_sources --value="$PLC_API_MAINTENANCE_SOURCES" --save
83
84     # Save configuration
85     mkdir -p /etc/planetlab/php
86     plc-config --php >/etc/planetlab/php/plc_config.php
87     plc-config --shell >/etc/planetlab/plc_config
88
89     # For backward compatibility, until we can convert all code to use
90     # the now standardized variable names.
91
92     # DB constants are all named the same
93     ln -sf plc_config /etc/planetlab/plc_db
94
95     # API constants
96     cat >/etc/planetlab/plc_api <<EOF
97 PL_API_SERVER='$PLC_API_HOST'
98 PL_API_PATH='$PLC_API_PATH'
99 PL_API_PORT=$PLC_API_PORT
100 PL_API_CAPABILITY_AUTH_METHOD='capability'
101 PL_API_CAPABILITY_PASS='$PLC_API_MAINTENANCE_PASSWORD'
102 PL_API_CAPABILITY_USERNAME='$PLC_API_MAINTENANCE_USER'
103 PL_API_TICKET_KEY_FILE='$PLC_API_SSL_KEY'
104 PLANETLAB_SUPPORT_EMAIL='$PLC_MAIL_SUPPORT_ADDRESS'
105 BOOT_MESSAGES_EMAIL='$PLC_MAIL_BOOT_ADDRESS'
106 WWW_BASE='$PLC_WWW_HOST'
107 BOOT_BASE='$PLC_BOOT_HOST'
108 EOF
109
110     # API expects root SSH public key to be at /etc/planetlab/node_root_key
111     ln -sf "$PLC_ROOT_SSH_KEY_PUB" /etc/planetlab/node_root_key
112
113     # The format is
114     #
115     # ip:max_role_id:organization_id:password
116     #
117     # It is unlikely that we will let federated sites use the
118     # maintenance account to access each others' APIs, so we always
119     # set organization_id to -1.
120     (
121         echo -n "PL_API_CAPABILITY_SOURCES='"
122         first=1
123         for ip in $PLC_API_MAINTENANCE_SOURCES ; do
124             if [ $first -ne 1 ] ; then
125                 echo -n " "
126             fi
127             first=0
128             echo -n "$ip:-1:-1:$PLC_API_MAINTENANCE_PASSWORD"
129         done
130         echo "'"
131     ) >>/etc/planetlab/plc_api
132
133     cat >/etc/planetlab/php/site_constants.php <<"EOF"
134 <?php
135 include('plc_config.php');
136
137 define('PL_API_SERVER', PLC_API_HOST);
138 define('PL_API_PATH', PLC_API_PATH);
139 define('PL_API_PORT', PLC_API_PORT);
140 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
141 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
142 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
143 define('WWW_BASE', PLC_WWW_HOST);
144 define('BOOT_BASE', PLC_BOOT_HOST);
145 define('DEBUG', PLC_WWW_DEBUG);
146 define('API_CALL_DEBUG', PLC_API_DEBUG);
147 define('SENDMAIL', PLC_MAIL_ENABLED);
148 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . 'Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
149 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
150 ?>
151 EOF
152 }
153
154 config_network ()
155 {
156     case "$1" in
157         start)
158             # Minimal /etc/hosts
159             echo "127.0.0.1     localhost.localdomain localhost" >/etc/hosts
160             (
161                 for server in API BOOT WWW ; do
162                     hostname=PLC_${server}_HOST
163                     ip=$(gethostbyname ${!hostname})
164                     if [ -n "$ip" ] ; then
165                         echo "$ip       $hostname"
166                     fi
167                 done
168             ) >>/etc/hosts
169
170             # Set up nameservers
171             (
172                 [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
173                 [ -n "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
174             ) >/etc/resolv.conf
175             ;;
176     esac
177 }
178
179 config_syslog ()
180 {
181     service syslog $1
182     check
183 }
184
185 config_postgresql ()
186 {
187     # Default locations
188     PGDATA=/var/lib/pgsql/data
189     postgresql_conf=$PGDATA/postgresql.conf
190     pghba_conf=$PGDATA/pg_hba.conf
191
192     # Export so that we do not have to specify -p to psql invocations
193     export PGPORT=$PLC_DB_PORT
194
195     case "$1" in
196         start)
197             if [ "$PLC_DB_ENABLED" != "1" ] ; then
198                 return 0
199             fi
200
201             # Set data directory and redirect startup output to /var/log/pgsql
202             mkdir -p /etc/sysconfig/pgsql
203             (
204                 echo "PGDATA=$PGDATA"
205                 echo "PGLOG=/var/log/pgsql"
206                 echo "PGPORT=$PLC_DB_PORT"
207             ) >>/etc/sysconfig/pgsql/postgresql
208
209             # Fix ownership (rpm installation may have changed it)
210             chown -R -H postgres:postgres $(dirname $PGDATA)
211
212             # PostgreSQL must be started at least once to bootstrap
213             # /var/lib/pgsql/data
214             if [ ! -f $postgresql_conf ] ; then
215                 service postgresql start
216                 service postgresql stop
217             fi
218
219             # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
220             # PostgreSQL 7.x uses tcpip_socket.
221             if grep -q listen_addresses $postgresql_conf ; then
222                 sed -i -e '/^listen_addresses/d' $postgresql_conf
223                 echo "listen_addresses = '*'" >>$postgresql_conf
224             elif grep -q tcpip_socket $postgresql_conf ; then
225                 sed -i -e '/^tcpip_socket/d' $postgresql_conf
226                 echo "tcpip_socket = true" >>$postgresql_conf
227             fi
228
229             # Disable access to all DBs from all hosts
230             sed -i -e '/^\(host\|local\)/d' $pghba_conf
231
232             # Enable passwordless localhost access
233             echo "local all all trust" >>$pghba_conf
234
235             # Enable access from the API and web servers
236             PLC_API_IP=$(gethostbyname $PLC_API_HOST)
237             PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
238             (
239                 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
240                 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
241             ) >>$pghba_conf
242
243             # Fix ownership (sed -i changes it)
244             chown postgres:postgres $postgresql_conf $pghba_conf
245
246             # Start up the server
247             service postgresql start
248             # /etc/init.d/postgresql always returns 0, even on failure
249             status postmaster && [ -f /var/lock/subsys/postgresql ]
250             check
251
252             # Create/update the unprivileged database user and password
253             if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
254                 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
255             else
256                 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
257             fi
258
259             # Create the database if necessary
260             if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
261                 createdb -U postgres $PLC_DB_NAME
262                 psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
263             fi
264             ;;
265
266         stop)
267             # Drop the current user in case the username changes
268             psql -U postgres -c "DROP USER $PLC_DB_USER" template1
269
270             # WARNING: If the DB name changes, the old DB will be left
271             # intact and a new one will be created. If it changes
272             # back, the old DB will not be re-created.
273
274             # Shut down the server
275             service postgresql stop
276             check
277             ;;
278     esac
279 }
280
281 # Generate GPG keys
282 config_gpg ()
283 {
284     case "$1" in
285         start)
286             # Generate GPG keyrings
287             if [ ! -f $PLC_ROOT_GPG_KEY_PUB -o ! -f $PLC_ROOT_GPG_KEY ] ; then
288                 mkdir -p $(dirname $PLC_ROOT_GPG_KEY_PUB)
289                 mkdir -p $(dirname $PLC_ROOT_GPG_KEY)
290
291                 # Temporarily replace /dev/random with /dev/urandom to
292                 # avoid running out of entropy.
293                 rm -f /dev/random
294                 mknod /dev/random c 1 9
295                 gpg --homedir=/root --batch --gen-key <<EOF
296 Key-Type: DSA
297 Key-Length: 1024
298 Subkey-Type: ELG-E
299 Subkey-Length: 1024
300 Name-Real: $PLC_NAME Central
301 Name-Comment: http://$PLC_WWW_HOST/
302 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
303 Expire-Date: 0
304 %pubring $PLC_ROOT_GPG_KEY_PUB
305 %secring $PLC_ROOT_GPG_KEY
306 %commit
307 EOF
308                 check
309                 rm -f /dev/random
310                 mknod /dev/random c 1 8
311                 chmod 600 $PLC_ROOT_GPG_KEY_PUB $PLC_ROOT_GPG_KEY
312             fi
313             ;;
314     esac
315 }
316
317 ssl_cname ()
318 {
319     openssl x509 -noout -in $1 -subject | \
320         sed -n -e 's@.*/CN=\([^/]*\).*@\1@p'
321 }
322
323 symlink ()
324 {
325     mkdir -p $(dirname $2)
326     rm -f $2
327     ln -s $1 $2
328 }
329
330 # Generate SSL certificates
331 config_ssl ()
332 {
333     case "$1" in
334         start)
335             # Generate self-signed SSL certificate(s). These nice
336             # commands come from the mod_ssl spec file for Fedora Core
337             # 2. We generate a certificate for each enabled server
338             # with a different hostname. These self-signed
339             # certificates may be overridden later.
340             for server in WWW API BOOT ; do
341                 ssl_key=PLC_${server}_SSL_KEY
342                 ssl_crt=PLC_${server}_SSL_CRT
343                 hostname=PLC_${server}_HOST
344
345                 # Check if we have already generated a certificate for
346                 # the same hostname.
347                 for previous_server in WWW API BOOT ; do
348                     if [ "$server" = "$previous_server" ] ; then
349                         break
350                     fi
351                     previous_ssl_key=PLC_${previous_server}_SSL_KEY
352                     previous_ssl_crt=PLC_${previous_server}_SSL_CRT
353                     previous_hostname=PLC_${previous_server}_HOST
354
355                     if [ -f ${!previous_ssl_crt} ] && \
356                         [ "$(ssl_cname ${!previous_ssl_crt})" = "${!hostname}" ] ; then
357                         cp -a ${!previous_ssl_key} ${!ssl_key}
358                         cp -a ${!previous_ssl_crt} ${!ssl_crt}
359                         break
360                     fi
361                 done
362
363                 # Generate new SSL private key
364                 if [ ! -f ${!ssl_key} ] ; then
365                     mkdir -p $(dirname ${!ssl_key})
366                     openssl genrsa -rand /proc/apm:/proc/cpuinfo:/proc/dma:/proc/filesystems:/proc/interrupts:/proc/ioports:/proc/pci:/proc/rtc:/proc/uptime 1024 >${!ssl_key}
367                     check
368                     chmod 600 ${!ssl_key}
369                 fi
370
371                 # Check if self signed certificate is valid
372                 if [ -f ${!ssl_crt} ] ; then
373                     verify=$(openssl verify ${!ssl_crt})
374                     # If self signed
375                     if grep -q "self signed certificate" <<<$verify ; then
376                         # Delete if expired or hostname changed
377                         if grep -q "expired" <<<$verify || \
378                             [ "$(ssl_cname ${!ssl_crt})" != "${!hostname}" ] ; then
379                             rm -f ${!ssl_crt}
380                         fi
381                     else
382                         echo "$verify" >&2
383                     fi
384                 fi
385
386                 # Generate new self signed certificate
387                 if [ ! -f ${!ssl_crt} ] ; then
388                     mkdir -p $(dirname ${!ssl_crt})
389                     openssl req -new -x509 -days 365 -set_serial $RANDOM \
390                         -key ${!ssl_key} -out ${!ssl_crt} <<EOF
391 --
392 State
393 City
394 Organization
395 $PLC_NAME Central
396 ${!hostname}
397 $PLC_MAIL_SUPPORT_ADDRESS
398 EOF
399                     check
400                     chmod 644 ${!ssl_crt}
401                 fi
402             done
403
404             # API requires a public key for slice ticket verification
405             if [ ! -f $PLC_API_SSL_KEY_PUB ] ; then
406                 openssl rsa -pubout <$PLC_API_SSL_KEY >$PLC_API_SSL_KEY_PUB
407                 check
408             fi
409
410             # Install into both /etc/pki (Fedora Core 4) and
411             # /etc/httpd/conf (Fedora Core 2). If the API, boot, and
412             # web servers are all running on the same machine, the web
413             # server certificate takes precedence.
414             for server in API BOOT WWW ; do
415                 enabled=PLC_${server}_ENABLED
416                 if [ "${!enabled}" != "1" ] ; then
417                     continue
418                 fi
419                 ssl_key=PLC_${server}_SSL_KEY
420                 ssl_crt=PLC_${server}_SSL_CRT
421
422                 symlink ${!ssl_crt} /etc/pki/tls/certs/localhost.crt
423                 symlink ${!ssl_key} /etc/pki/tls/private/localhost.key
424                 symlink ${!ssl_crt} /etc/httpd/conf/ssl.crt/server.crt
425                 symlink ${!ssl_key} /etc/httpd/conf/ssl.key/server.key
426             done
427             ;;
428     esac
429 }
430
431 # Generate SSH keys
432 config_ssh ()
433 {
434     # XXX Could make these configurable
435     KEY_TYPE_ROOT=rsa
436     KEY_LEN_ROOT=1024
437     KEY_TYPE_DEBUG=rsa
438     KEY_LEN_DEBUG=2048  
439
440     case "$1" in
441         start)
442             tmp=$(mktemp -d /tmp/ssh.XXXXXX)
443
444             # Generate root SSH key
445             if [ ! -f $PLC_ROOT_SSH_KEY_PUB -o ! -f $PLC_ROOT_SSH_KEY ] ; then
446                 ssh-keygen -N "" -C "$PLC_NAME Central <$PLC_MAIL_SUPPORT_ADDRESS>" \
447                     -b $KEY_LEN_ROOT -t $KEY_TYPE_ROOT -f $tmp/root
448                 check
449                 install -D -m 600 $tmp/root $PLC_ROOT_SSH_KEY
450                 install -D -m 644 $tmp/root.pub $PLC_ROOT_SSH_KEY_PUB
451             fi
452
453             # Generate debug SSH key
454             if [ ! -f $PLC_DEBUG_SSH_KEY_PUB -o ! -f $PLC_DEBUG_SSH_KEY ] ; then
455                 ssh-keygen -N "" -C "$PLC_NAME Central <$PLC_MAIL_SUPPORT_ADDRESS>" \
456                     -b $KEY_LEN_DEBUG -t $KEY_TYPE_DEBUG -f $tmp/debug
457                 check
458                 install -D -m 600 $tmp/debug $PLC_DEBUG_SSH_KEY
459                 install -D -m 644 $tmp/debug.pub $PLC_DEBUG_SSH_KEY_PUB
460             fi
461
462             rm -rf $tmp
463             ;;
464     esac
465 }
466
467 # Configure Apache web server
468 config_apache ()
469 {
470     # Default locations
471     DocumentRoot=/var/www/html
472     php_ini=/etc/php.ini
473     httpd_conf=/etc/httpd/conf/httpd.conf
474     ssl_conf=/etc/httpd/conf.d/ssl.conf
475     plc_conf=/etc/httpd/conf.d/plc.conf
476
477     case "$1" in
478         start)
479             if [ "$PLC_API_ENABLED" != "1" -a \
480                  "$PLC_BOOT_ENABLED" != "1" -a \
481                  "$PLC_WWW_ENABLED" != "1" ] ; then
482                 return 0
483             fi
484
485             # Set the default include path
486             include_path=".:$DocumentRoot/includes:$DocumentRoot/generated:/etc/planetlab/php"
487             sed -i -e "s@;include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
488
489             # Disable default Listen directive
490             sed -i -e '/^Listen/d' $httpd_conf
491
492             # Set the port numbers
493             for server in WWW API BOOT ; do
494                 enabled=PLC_${server}_ENABLED
495                 if [ "${!enabled}" != "1" ] ; then
496                     continue
497                 fi
498                 hostname=PLC_${server}_HOST
499                 http_port=PLC_${server}_PORT
500                 https_port=PLC_${server}_SSL_PORT
501
502                 # API should always be accessed via SSL
503                 if [ "$server" = "API" ] ; then
504                     https_port=${!http_port}
505                     http_port=
506                 fi
507
508                 # Check if we are already listening on these ports
509                 skip_http=0
510                 skip_https=0
511                 for previous_server in WWW API BOOT ; do
512                     if [ "$server" = "$previous_server" ] ; then
513                         break
514                     fi
515                     previous_hostname=PLC_${previous_server}_HOST
516                     previous_http_port=PLC_${previous_server}_PORT
517                     previous_https_port=PLC_${previous_server}_SSL_PORT
518
519                     if [ "${!http_port}" = "${!previous_http_port}" ] ; then
520                         skip_http=1
521                     fi
522                     if [ "${!https_port}" = "${!previous_https_port}" ] ; then
523                         skip_https=1
524                     fi
525                 done
526
527                 # Listen on these ports
528                 if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
529                     cat <<EOF
530 Listen ${!http_port}
531 <VirtualHost *:${!http_port}>
532     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
533     # XXX Not yet until we can get rid of oldapi
534     # Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
535 </VirtualHost>
536 EOF
537                 fi
538                 if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
539                     # XXX Cannot support NameVirtualHost over SSL. If
540                     # the API, boot, and web servers are all running
541                     # on the same machine, the web server certificate
542                     # takes precedence.
543                     sed -i \
544                         -e "s/^Listen .*/Listen ${!https_port}/" \
545                         -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
546                         $ssl_conf
547                 fi
548             done >$plc_conf
549
550             # Set custom Apache directives
551             (
552                 if [ "$PLC_API_ENABLED" = "1" ] ; then
553                     cat <<EOF
554 <Location $PLC_API_PATH>
555     SetHandler python-program
556     PythonPath "sys.path + ['/usr/share/plc_api']"
557     PythonHandler mod_pythonXMLRPC
558 </Location>
559 EOF
560                 else
561                     cat <<EOF
562 <Location $PLC_API_PATH>
563     Deny from all
564 </Location>
565 EOF
566                 fi
567
568                 if [ "$PLC_WWW_ENABLED" != "1" ] ; then
569                     cat <<EOF
570 <Location /db>
571     Deny from all
572 </Location>
573 EOF
574                 fi
575             ) >>$plc_conf
576
577             # Make alpina-logs directory writable for bootmanager log upload
578             chown apache:apache $DocumentRoot/alpina-logs/nodes
579
580             service httpd start
581             check
582             ;;
583
584         stop)
585             service httpd stop
586             check
587             ;;
588     esac
589 }
590
591 config_api ()
592 {
593     case "$1" in
594         start)
595             if [ "$PLC_API_ENABLED" != "1" ] ; then
596                 return
597             fi
598
599             # Update the maintenance account username. This can't be
600             # done through the api-config script since it uses the
601             # maintenance account to access the API. The maintenance
602             # account should be person_id 1 since it is created by the
603             # DB schema itself.
604             psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
605
606             # Bootstrap the DB
607             api-config
608             check
609             ;;
610     esac
611 }
612
613 config_cron ()
614 {
615     case "$1" in
616         start)
617             if [ "$PLC_MAIL_ENABLED" = "1" ] ; then
618                 MAILTO=$PLC_MAIL_SUPPORT_ADDRESS
619             else
620                 MAILTO=
621             fi
622             cat >/etc/cron.d/plc.cron <<EOF
623 SHELL=/bin/bash
624 PATH=/sbin:/bin:/usr/sbin:/usr/bin
625 MAILTO=$MAILTO
626 HOME=/
627
628 # minute hour day-of-month month day-of-week user command
629 */5 * * * * root gen-slices-xml-05.py
630 */15 * * * * root gen-sites-xml.py
631 */15 * * * * root gen-static-content.py
632 EOF
633
634             # Run them once at startup
635             gen-slices-xml-05.py
636             check
637             gen-sites-xml.py
638             check
639             gen-static-content.py
640             check
641
642             service crond start
643             check
644             ;;
645
646         stop)
647             service crond stop
648             check
649             ;;
650     esac
651 }
652
653 config_bootcd ()
654 {
655     case "$1" in
656         start)
657             if [ "$PLC_BOOT_ENABLED" != "1" -a \
658                  "$PLC_WWW_ENABLED" != "1" ] ; then
659                 return 0
660             fi
661
662             # Customize the BootCD
663             pushd /var/www/html/download
664             /usr/share/bootcd/build.sh
665             check
666             popd
667             ;;
668     esac
669 }
670
671 config_bootmanager ()
672 {
673     case "$1" in
674         start)
675             if [ "$PLC_BOOT_ENABLED" != "1" -a \
676                  "$PLC_WWW_ENABLED" != "1" ] ; then
677                 return 0
678             fi
679
680             # Customize the Boot Manager
681             pushd /var/www/html/boot
682             /usr/share/bootmanager/build.sh
683             check
684             popd
685             ;;
686     esac
687 }
688
689 usage()
690 {
691     echo "Usage: $0 [OPTION]... [COMMAND]"
692     echo "      -v              Be verbose"
693     echo "      -h              This message"
694     echo
695     echo "Commands:"
696     echo "      start           Start all PLC subsystems"
697     echo "      stop            Stop all PLC subsystems"
698     echo "      reload          Regenerate configuration files"
699     echo "      restart         Restart all PLC subsystems"
700     exit 1
701 }
702
703 # Get options
704 while getopts "vh" opt ; do
705     case $opt in
706         v)
707             verbose=1
708             set -x
709             ;;
710         h|*)
711             usage
712             ;;
713     esac
714 done
715
716 shift $(($OPTIND - 1))
717 if [ -z "$1" ] ; then
718     usage
719 fi
720
721 exec 3>&1
722 exec 4>&2
723 if [ $verbose -eq 0 ] ; then
724     exec 1>>/var/log/boot.log
725     exec 2>>/var/log/boot.log
726 fi
727
728 # Generate and load configuration
729 reload
730 . /etc/planetlab/plc_config
731
732 RETVAL=0
733
734 start ()
735 {
736     for step in "${steps[@]}" ; do
737         echo -n $"PLC: Starting $step: " >&3
738         RETVAL=$ERRORS
739         config_$step start
740         if [ $RETVAL -eq $ERRORS ] ; then
741             success $"PLC: $step startup" >&3
742         else
743             failure $"PLC: $step startup" >&3
744         fi
745         echo >&3
746     done
747 }
748
749 stop ()
750 {
751     for i in $(seq 1 $nsteps) ; do
752         step=${steps[$(($nsteps - $i))]}
753         echo -n $"PLC: Shutting down $step: " >&3
754         RETVAL=$ERRORS
755         config_$step stop
756         if [ $RETVAL -eq $ERRORS ] ; then
757             success $"PLC: $step shutdown" >&3
758         else
759             failure $"PLC: $step shutdown" >&3
760         fi
761         echo >&3
762     done
763 }
764
765 case "$1" in
766     start|stop)
767         $1
768         ;;
769
770     restart)
771         stop
772         start
773         ;;
774
775     reload)
776         ;;
777
778     *)
779         usage >&3
780         ;;
781 esac
782
783 exit $RETVAL