be verbose
[myplc.git] / plc.d / httpd
1 #!/bin/bash
2 #
3 # priority: 700
4 #
5 # Configure Apache web server
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: httpd,v 1.2 2006/04/25 21:18:19 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Default locations
21 DocumentRoot=/var/www/html
22 php_ini=/etc/php.ini
23 httpd_conf=/etc/httpd/conf/httpd.conf
24 ssl_conf=/etc/httpd/conf.d/ssl.conf
25 plc_conf=/etc/httpd/conf.d/plc.conf
26
27 case "$1" in
28     start)
29         if [ "$PLC_API_ENABLED" != "1" -a \
30              "$PLC_BOOT_ENABLED" != "1" -a \
31              "$PLC_WWW_ENABLED" != "1" ] ; then
32             exit 0
33         fi
34
35         MESSAGE=$"Starting web server"
36         dialog "$MESSAGE"
37
38         # Set the default include path
39         include_path=".:$DocumentRoot/includes:$DocumentRoot/generated:/etc/planetlab/php"
40         sed -i -e "s@;include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
41
42         # Disable default Listen directive
43         sed -i -e '/^Listen/d' $httpd_conf
44
45         # Set the port numbers
46         for server in WWW API BOOT ; do
47             enabled=PLC_${server}_ENABLED
48             if [ "${!enabled}" != "1" ] ; then
49                 continue
50             fi
51             hostname=PLC_${server}_HOST
52             http_port=PLC_${server}_PORT
53             https_port=PLC_${server}_SSL_PORT
54
55             # API should always be accessed via SSL
56             if [ "$server" = "API" ] ; then
57                 https_port=${!http_port}
58                 http_port=
59             fi
60
61             # Check if we are already listening on these ports
62             skip_http=0
63             skip_https=0
64             for previous_server in WWW API BOOT ; do
65                 if [ "$server" = "$previous_server" ] ; then
66                     break
67                 fi
68                 previous_hostname=PLC_${previous_server}_HOST
69                 previous_http_port=PLC_${previous_server}_PORT
70                 previous_https_port=PLC_${previous_server}_SSL_PORT
71
72                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
73                     skip_http=1
74                 fi
75                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
76                     skip_https=1
77                 fi
78             done
79
80             # Listen on these ports
81             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
82                 cat <<EOF
83 Listen ${!http_port}
84 <VirtualHost *:${!http_port}>
85     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
86     # XXX Not yet until we can get rid of oldapi
87     # Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
88 </VirtualHost>
89 EOF
90             fi
91             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
92                 # XXX Cannot support NameVirtualHost over SSL. If
93                 # the API, boot, and web servers are all running
94                 # on the same machine, the web server certificate
95                 # takes precedence.
96                 sed -i \
97                     -e "s/^Listen .*/Listen ${!https_port}/" \
98                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
99                     $ssl_conf
100             fi
101         done >$plc_conf
102
103         # Set custom Apache directives
104         (
105             if [ "$PLC_API_ENABLED" = "1" ] ; then
106                 cat <<EOF
107 <Location $PLC_API_PATH>
108     SetHandler python-program
109     PythonPath "sys.path + ['/usr/share/plc_api']"
110     PythonHandler mod_pythonXMLRPC
111 </Location>
112 EOF
113             else
114                 cat <<EOF
115 <Location $PLC_API_PATH>
116     Deny from all
117 </Location>
118 EOF
119             fi
120
121             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
122                 cat <<EOF
123 <Location /db>
124     Deny from all
125 </Location>
126 EOF
127             fi
128         ) >>$plc_conf
129
130         # Make alpina-logs directory writable for bootmanager log upload
131         chown apache:apache $DocumentRoot/alpina-logs/nodes
132
133         plc_daemon httpd
134         check
135
136         result "$MESSAGE"
137         ;;
138
139     stop)
140         MESSAGE=$"Stopping web server"
141         dialog "$MESSAGE"
142
143         killproc plc_httpd
144         check
145
146         result "$MESSAGE"
147         ;;
148 esac
149
150 exit $ERRORS