3 # plc Manages all PLC services on this machine
7 # description: Manages all PLC services on this machine
9 # $Id: guest.init,v 1.9 2006/03/31 00:41:03 mlhuang Exp $
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
14 # Source function library.
15 . /etc/init.d/functions
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.
40 # Total number of errors
43 # Count the exit status of the last command
46 ERRORS=$(($ERRORS+$?))
49 # Return IP address of hostname if resolvable
52 perl -MSocket -e '($a,$b,$c,$d,@addrs) = gethostbyname($ARGV[0]); print inet_ntoa($addrs[0]) . "\n";' $1 2>/dev/null
55 # Regenerate configuration files
59 plc-config --shell >/etc/planetlab/plc_config
60 . /etc/planetlab/plc_config
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
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
73 # Need to configure network before resolving hostnames
76 PLC_API_MAINTENANCE_SOURCES=$(
77 for server in API BOOT WWW ; do
78 hostname=PLC_${server}_HOST
79 gethostbyname ${!hostname}
82 plc-config --category=plc_api --variable=maintenance_sources --value="$PLC_API_MAINTENANCE_SOURCES" --save
85 mkdir -p /etc/planetlab/php
86 plc-config --php >/etc/planetlab/php/plc_config.php
87 plc-config --shell >/etc/planetlab/plc_config
89 # For backward compatibility, until we can convert all code to use
90 # the now standardized variable names.
92 # DB constants are all named the same
93 ln -sf plc_config /etc/planetlab/plc_db
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'
112 # ip:max_role_id:organization_id:password
114 # It is unlikely that we will let federated sites use the
115 # maintenance account to access each others' APIs, so we always
116 # set organization_id to -1.
118 echo -n "PL_API_CAPABILITY_SOURCES='"
120 for ip in $PLC_API_MAINTENANCE_SOURCES ; do
121 if [ $first -ne 1 ] ; then
125 echo -n "$ip:-1:-1:$PLC_API_MAINTENANCE_PASSWORD"
128 ) >>/etc/planetlab/plc_api
130 cat >/etc/planetlab/php/site_constants.php <<"EOF"
132 include('plc_config.php');
134 DEFINE('PL_API_SERVER', PLC_API_HOST);
135 DEFINE('PL_API_PATH', PLC_API_PATH);
136 DEFINE('PL_API_PORT', PLC_API_PORT);
137 DEFINE('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
138 DEFINE('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
139 DEFINE('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
140 DEFINE('WWW_BASE', PLC_WWW_HOST);
141 DEFINE('BOOT_BASE', PLC_BOOT_HOST);
142 DEFINE('DEBUG', PLC_WWW_DEBUG);
143 DEFINE('API_CALL_DEBUG', PLC_API_DEBUG);
144 DEFINE('SENDMAIL', PLC_MAIL_ENABLED);
145 DEFINE('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . 'Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
146 DEFINE('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
156 echo "127.0.0.1 localhost.localdomain localhost" >/etc/hosts
158 for server in API BOOT WWW ; do
159 hostname=PLC_${server}_HOST
160 ip=$(gethostbyname ${!hostname})
161 if [ -n "$ip" ] ; then
169 [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
170 [ -n "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
185 PGDATA=/var/lib/pgsql/data
186 postgresql_conf=$PGDATA/postgresql.conf
187 pghba_conf=$PGDATA/pg_hba.conf
189 # Export so that we do not have to specify -p to psql invocations
190 export PGPORT=$PLC_DB_PORT
194 if [ "$PLC_DB_ENABLED" != "1" ] ; then
198 # Set data directory and redirect startup output to /var/log/pgsql
199 mkdir -p /etc/sysconfig/pgsql
201 echo "PGDATA=$PGDATA"
202 echo "PGLOG=/var/log/pgsql"
203 echo "PGPORT=$PLC_DB_PORT"
204 ) >>/etc/sysconfig/pgsql/postgresql
206 # Fix ownership (rpm installation may have changed it)
207 chown -R -H postgres:postgres $(dirname $PGDATA)
209 # PostgreSQL must be started at least once to bootstrap
210 # /var/lib/pgsql/data
211 if [ ! -f $postgresql_conf ] ; then
212 service postgresql start
213 service postgresql stop
216 # Enable DB server. PostgreSQL >=8.0 defines listen_addresses,
217 # PostgreSQL 7.x uses tcpip_socket.
218 if grep -q listen_addresses $postgresql_conf ; then
219 sed -i -e '/^listen_addresses/d' $postgresql_conf
220 echo "listen_addresses = '*'" >>$postgresql_conf
221 elif grep -q tcpip_socket $postgresql_conf ; then
222 sed -i -e '/^tcpip_socket/d' $postgresql_conf
223 echo "tcpip_socket = true" >>$postgresql_conf
226 # Disable access to all DBs from all hosts
227 sed -i -e '/^\(host\|local\)/d' $pghba_conf
229 # Enable passwordless localhost access
230 echo "local all all trust" >>$pghba_conf
232 # Enable access from the API and web servers
233 PLC_API_IP=$(gethostbyname $PLC_API_HOST)
234 PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
236 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
237 echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_WWW_IP/32 password"
240 # Fix ownership (sed -i changes it)
241 chown postgres:postgres $postgresql_conf $pghba_conf
243 # Start up the server
244 service postgresql start
245 # /etc/init.d/postgresql always returns 0, even on failure
246 status postmaster && [ -f /var/lock/subsys/postgresql ]
249 # Create/update the unprivileged database user and password
250 if ! psql -U $PLC_DB_USER -c "" template1 >/dev/null 2>&1 ; then
251 psql -U postgres -c "CREATE USER $PLC_DB_USER PASSWORD '$PLC_DB_PASSWORD'" template1
253 psql -U postgres -c "ALTER USER $PLC_DB_USER WITH PASSWORD '$PLC_DB_PASSWORD'" template1
256 # Create the database if necessary
257 if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
258 createdb -U postgres $PLC_DB_NAME
259 psql -U $PLC_DB_USER -f /usr/share/pl_db/plc_schema_3.sql $PLC_DB_NAME
264 # Drop the current user in case the username changes
265 psql -U postgres -c "DROP USER $PLC_DB_USER" template1
267 # WARNING: If the DB name changes, the old DB will be left
268 # intact and a new one will be created. If it changes
269 # back, the old DB will not be re-created.
271 # Shut down the server
272 service postgresql stop
283 # Generate GPG keyrings
284 if [ ! -f $PLC_ROOT_GPG_KEY_PUB -o ! -f $PLC_ROOT_GPG_KEY ] ; then
285 mkdir -p $(dirname $PLC_ROOT_GPG_KEY_PUB)
286 mkdir -p $(dirname $PLC_ROOT_GPG_KEY)
288 # Temporarily replace /dev/random with /dev/urandom to
289 # avoid running out of entropy.
291 mknod /dev/random c 1 9
292 gpg --homedir=/root --batch --gen-key <<EOF
297 Name-Real: $PLC_NAME Central
298 Name-Comment: http://$PLC_WWW_HOST/
299 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
301 %pubring $PLC_ROOT_GPG_KEY_PUB
302 %secring $PLC_ROOT_GPG_KEY
307 mknod /dev/random c 1 8
308 chmod 600 $PLC_ROOT_GPG_KEY_PUB $PLC_ROOT_GPG_KEY
316 openssl x509 -noout -in $1 -subject | \
317 sed -n -e 's@.*/CN=\([^/]*\).*@\1@p'
322 mkdir -p $(dirname $2)
327 # Generate SSL certificates
332 # Generate self-signed SSL certificate(s). These nice
333 # commands come from the mod_ssl spec file for Fedora Core
334 # 2. We generate a certificate for each enabled server
335 # with a different hostname. These self-signed
336 # certificates may be overridden later.
337 for server in WWW API BOOT ; do
338 ssl_key=PLC_${server}_SSL_KEY
339 ssl_crt=PLC_${server}_SSL_CRT
340 hostname=PLC_${server}_HOST
342 # Check if we have already generated a certificate for
344 for previous_server in WWW API BOOT ; do
345 if [ "$server" = "$previous_server" ] ; then
348 previous_ssl_key=PLC_${previous_server}_SSL_KEY
349 previous_ssl_crt=PLC_${previous_server}_SSL_CRT
350 previous_hostname=PLC_${previous_server}_HOST
352 if [ -f ${!previous_ssl_crt} ] && \
353 [ "$(ssl_cname ${!previous_ssl_crt})" = "${!hostname}" ] ; then
354 cp -a ${!previous_ssl_key} ${!ssl_key}
355 cp -a ${!previous_ssl_crt} ${!ssl_crt}
360 # Generate new SSL private key
361 if [ ! -f ${!ssl_key} ] ; then
362 mkdir -p $(dirname ${!ssl_key})
363 openssl genrsa -rand /proc/apm:/proc/cpuinfo:/proc/dma:/proc/filesystems:/proc/interrupts:/proc/ioports:/proc/pci:/proc/rtc:/proc/uptime 1024 >${!ssl_key}
365 chmod 600 ${!ssl_key}
368 # Check if self signed certificate is valid
369 if [ -f ${!ssl_crt} ] ; then
370 verify=$(openssl verify ${!ssl_crt})
372 if grep -q "self signed certificate" <<<$verify ; then
373 # Delete if expired or hostname changed
374 if grep -q "expired" <<<$verify || \
375 [ "$(ssl_cname ${!ssl_crt})" != "${!hostname}" ] ; then
383 # Generate new self signed certificate
384 if [ ! -f ${!ssl_crt} ] ; then
385 mkdir -p $(dirname ${!ssl_crt})
386 openssl req -new -x509 -days 365 -set_serial $RANDOM \
387 -key ${!ssl_key} -out ${!ssl_crt} <<EOF
394 $PLC_MAIL_SUPPORT_ADDRESS
397 chmod 644 ${!ssl_crt}
401 # API requires a public key for slice ticket verification
402 if [ ! -f $PLC_API_SSL_KEY_PUB ] ; then
403 openssl rsa -pubout <$PLC_API_SSL_KEY >$PLC_API_SSL_KEY_PUB
407 # Install into both /etc/pki (Fedora Core 4) and
408 # /etc/httpd/conf (Fedora Core 2). If the API, boot, and
409 # web servers are all running on the same machine, the web
410 # server certificate takes precedence.
411 for server in API BOOT WWW ; do
412 enabled=PLC_${server}_ENABLED
413 if [ "${!enabled}" != "1" ] ; then
416 ssl_key=PLC_${server}_SSL_KEY
417 ssl_crt=PLC_${server}_SSL_CRT
419 symlink ${!ssl_crt} /etc/pki/tls/certs/localhost.crt
420 symlink ${!ssl_key} /etc/pki/tls/private/localhost.key
421 symlink ${!ssl_crt} /etc/httpd/conf/ssl.crt/server.crt
422 symlink ${!ssl_key} /etc/httpd/conf/ssl.key/server.key
431 # XXX Could make these configurable
439 tmp=$(mktemp -d /tmp/ssh.XXXXXX)
441 # Generate root SSH key
442 if [ ! -f $PLC_ROOT_SSH_KEY_PUB -o ! -f $PLC_ROOT_SSH_KEY ] ; then
443 ssh-keygen -N "" -C "$PLC_NAME Central <$PLC_MAIL_SUPPORT_ADDRESS>" \
444 -b $KEY_LEN_ROOT -t $KEY_TYPE_ROOT -f $tmp/root
446 install -D -m 600 $tmp/root $PLC_ROOT_SSH_KEY
447 install -D -m 600 $tmp/root.pub $PLC_ROOT_SSH_KEY_PUB
450 # Generate debug SSH key
451 if [ ! -f $PLC_DEBUG_SSH_KEY_PUB -o ! -f $PLC_DEBUG_SSH_KEY ] ; then
452 ssh-keygen -N "" -C "$PLC_NAME Central <$PLC_MAIL_SUPPORT_ADDRESS>" \
453 -b $KEY_LEN_DEBUG -t $KEY_TYPE_DEBUG -f $tmp/debug
455 install -D -m 600 $tmp/debug $PLC_DEBUG_SSH_KEY
456 install -D -m 600 $tmp/debug.pub $PLC_DEBUG_SSH_KEY_PUB
464 # Configure Apache web server
468 DocumentRoot=/var/www/html
470 httpd_conf=/etc/httpd/conf/httpd.conf
471 ssl_conf=/etc/httpd/conf.d/ssl.conf
472 plc_conf=/etc/httpd/conf.d/plc.conf
476 if [ "$PLC_API_ENABLED" != "1" -a \
477 "$PLC_BOOT_ENABLED" != "1" -a \
478 "$PLC_WWW_ENABLED" != "1" ] ; then
482 # Set the default include path
483 include_path=".:$DocumentRoot/includes:$DocumentRoot/generated:/etc/planetlab/php"
484 sed -i -e "s@;include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
486 # Disable default Listen directive
487 sed -i -e '/^Listen/d' $httpd_conf
489 # Set the port numbers
490 for server in WWW API BOOT ; do
491 enabled=PLC_${server}_ENABLED
492 if [ "${!enabled}" != "1" ] ; then
495 hostname=PLC_${server}_HOST
496 http_port=PLC_${server}_PORT
497 https_port=PLC_${server}_SSL_PORT
499 # API should always be accessed via SSL
500 if [ "$server" = "API" ] ; then
501 https_port=${!http_port}
505 # Check if we are already listening on these ports
508 for previous_server in WWW API BOOT ; do
509 if [ "$server" = "$previous_server" ] ; then
512 previous_hostname=PLC_${previous_server}_HOST
513 previous_http_port=PLC_${previous_server}_PORT
514 previous_https_port=PLC_${previous_server}_SSL_PORT
516 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
519 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
524 # Listen on these ports
525 if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
528 <VirtualHost *:${!http_port}>
529 Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
530 # XXX Not yet until we can get rid of oldapi
531 # Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
535 if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
536 # XXX Cannot support NameVirtualHost over SSL. If
537 # the API, boot, and web servers are all running
538 # on the same machine, the web server certificate
541 -e "s/^Listen .*/Listen ${!https_port}/" \
542 -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
547 # Set custom Apache directives
549 if [ "$PLC_API_ENABLED" = "1" ] ; then
551 <Location $PLC_API_PATH>
552 SetHandler python-program
553 PythonPath "sys.path + ['/usr/share/plc_api']"
554 PythonHandler mod_pythonXMLRPC
559 <Location $PLC_API_PATH>
565 if [ "$PLC_WWW_ENABLED" != "1" ] ; then
574 # Make alpina-logs directory writable for bootmanager log upload
575 chown apache:apache $DocumentRoot/alpina-logs/nodes
592 if [ "$PLC_API_ENABLED" != "1" ] ; then
596 # Update the maintenance account username. This can't be
597 # done through the api-config script since it uses the
598 # maintenance account to access the API. The maintenance
599 # account should be person_id 1 since it is created by the
601 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
614 if [ "$PLC_MAIL_ENABLED" = "1" ] ; then
615 MAILTO=$PLC_MAIL_SUPPORT_ADDRESS
619 cat >/etc/cron.d/plc.cron <<EOF
621 PATH=/sbin:/bin:/usr/sbin:/usr/bin
625 # minute hour day-of-month month day-of-week user command
626 */5 * * * * root gen-slices-xml-05.py
627 */15 * * * * root gen-sites-xml.py
628 */15 * * * * root gen-static-content.py
631 # Run them once at startup
636 gen-static-content.py
654 if [ "$PLC_BOOT_ENABLED" != "1" -a \
655 "$PLC_WWW_ENABLED" != "1" ] ; then
659 # Customize the BootCD
660 pushd /var/www/html/download
661 /usr/share/bootcd/build.sh
668 config_bootmanager ()
672 if [ "$PLC_BOOT_ENABLED" != "1" -a \
673 "$PLC_WWW_ENABLED" != "1" ] ; then
677 # Customize the Boot Manager
678 pushd /var/www/html/boot
679 /usr/share/bootmanager/build.sh
688 echo "Usage: $0 [OPTION]... [COMMAND]"
689 echo " -v Be verbose"
690 echo " -h This message"
693 echo " start Start all PLC subsystems"
694 echo " stop Stop all PLC subsystems"
695 echo " reload Regenerate configuration files"
696 echo " restart Restart all PLC subsystems"
701 while getopts "vh" opt ; do
713 shift $(($OPTIND - 1))
714 if [ -z "$1" ] ; then
720 if [ $verbose -eq 0 ] ; then
721 exec 1>>/var/log/boot.log
722 exec 2>>/var/log/boot.log
725 # Generate and load configuration
727 . /etc/planetlab/plc_config
733 for step in "${steps[@]}" ; do
734 echo -n $"PLC: Starting $step: " >&3
737 if [ $RETVAL -eq $ERRORS ] ; then
738 success $"PLC: $step startup" >&3
740 failure $"PLC: $step startup" >&3
748 for i in $(seq 1 $nsteps) ; do
749 step=${steps[$(($nsteps - $i))]}
750 echo -n $"PLC: Shutting down $step: " >&3
753 if [ $RETVAL -eq $ERRORS ] ; then
754 success $"PLC: $step shutdown" >&3
756 failure $"PLC: $step shutdown" >&3