- serve PlanetLabConf if not running web server
[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.11 2007/02/06 16:24:13 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 document root to /data/var/www/html (static files
39         # and PlanetLabConf only, no Drupal or admin pages) if the web
40         # server should not run on this machine.
41         if [ "$PLC_WWW_ENABLED" != "1" ] ; then
42             sed -i -e "s@^DocumentRoot.*@DocumentRoot \"/data$DocumentRoot\"@" $httpd_conf
43             ln -nsf $DocumentRoot/PlanetLabConf /data$DocumentRoot/PlanetLabConf
44         else
45             sed -i -e "s@^DocumentRoot.*@DocumentRoot \"$DocumentRoot\"@" $httpd_conf
46             rm -f /data$DocumentRoot/PlanetLabConf
47         fi
48
49         # Set the default include path
50         include_path=".:$DocumentRoot/planetlab/includes:$DocumentRoot/generated:/etc/planetlab/php:/usr/share/plc_api/php"
51         sed -i -e "s@[;]*include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
52
53         # Disable default Listen directive
54         sed -i -e '/^Listen/d' $httpd_conf
55
56         # Set the port numbers
57         for server in WWW API BOOT ; do
58             enabled=PLC_${server}_ENABLED
59             if [ "${!enabled}" != "1" ] ; then
60                 continue
61             fi
62             hostname=PLC_${server}_HOST
63             http_port=PLC_${server}_PORT
64             https_port=PLC_${server}_SSL_PORT
65
66             # API should always be accessed via SSL
67             if [ "$server" = "API" ] ; then
68                 https_port=${!http_port}
69                 http_port=
70             fi
71
72             # Check if we are already listening on these ports
73             skip_http=0
74             skip_https=0
75             for previous_server in WWW API BOOT ; do
76                 if [ "$server" = "$previous_server" ] ; then
77                     break
78                 fi
79                 previous_enabled=PLC_${previous_server}_ENABLED
80                 if [ "${!previous_enabled}" != "1" ] ; then
81                     continue
82                 fi
83                 previous_http_port=PLC_${previous_server}_PORT
84                 previous_https_port=PLC_${previous_server}_SSL_PORT
85
86                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
87                     skip_http=1
88                 fi
89                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
90                     skip_https=1
91                 fi
92             done
93
94             # HTTP configuration
95             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
96                 cat <<EOF
97 Listen ${!http_port}
98 # Make sure that the admin web pages and API are always accessed via SSL
99 <VirtualHost *:${!http_port}>
100     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
101     Redirect /planetlab https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/planetlab
102     Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
103 </VirtualHost>
104 EOF
105             fi
106
107             # HTTPS configuration
108             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
109                 # XXX Cannot support NameVirtualHost over SSL. If
110                 # the API, boot, and web servers are all running
111                 # on the same machine, the web server certificate
112                 # takes precedence.
113                 sed -i \
114                     -e "s/^Listen .*/Listen ${!https_port}/" \
115                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
116                     $ssl_conf
117             fi
118         done >$plc_conf
119
120         # Set custom Apache directives
121         (
122             if [ "$PLC_API_ENABLED" = "1" ] ; then
123                 cat <<EOF
124 <Location $PLC_API_PATH>
125     SetHandler mod_python
126     PythonPath "sys.path + ['/usr/share/plc_api']"
127     PythonHandler ModPython
128 </Location>
129 EOF
130             else
131                 cat <<EOF
132 <Location $PLC_API_PATH>
133     Deny from all
134 </Location>
135 EOF
136             fi
137
138             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
139                 cat <<EOF
140 Redirect /index.html http://$PLC_WWW_HOST:$PLC_WWW_PORT/
141 EOF
142             fi
143         ) >>$plc_conf
144
145         # Make alpina-logs directory writable for bootmanager log upload
146         chown apache:apache $DocumentRoot/alpina-logs/nodes
147
148         # Make the Drupal files upload directory owned by Apache
149         mkdir -p $DocumentRoot/files
150         chown apache:apache $DocumentRoot/files
151
152         # Symlink any (real) files or directories in
153         # /data/var/www/html/* to /var/www/html/. We could descend
154         # into subdirectories, but the code to do so properly would be
155         # madness.
156         for file in /data/$DocumentRoot/* ; do
157             if [ -e "$file" -a ! -h "$file" ] ; then
158                 base=$(basename "$file")
159                 if [ ! -e "$DocumentRoot/$base" ] ; then
160                     ln -nsf "$file" "$DocumentRoot/$base"
161                 fi
162             fi
163         done
164
165         # Cleanup broken symlinks
166         for file in $DocumentRoot/* ; do
167             if [ -h "$file" -a ! -e "$file" ] ; then
168                 rm -f "$file"
169             fi
170         done
171
172         # Old style PHP constants
173         mkdir -p /etc/planetlab/php
174         cat >/etc/planetlab/php/site_constants.php <<"EOF"
175 <?php
176 include('plc_config.php');
177
178 define('PL_API_SERVER', PLC_API_HOST);
179 define('PL_API_PATH', PLC_API_PATH);
180 define('PL_API_PORT', PLC_API_PORT);
181 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
182 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
183 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
184 define('WWW_BASE', PLC_WWW_HOST);
185 define('BOOT_BASE', PLC_BOOT_HOST);
186 define('DEBUG', PLC_WWW_DEBUG);
187 define('API_CALL_DEBUG', PLC_API_DEBUG);
188 define('SENDMAIL', PLC_MAIL_ENABLED);
189 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
190 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
191 ?>
192 EOF
193
194         plc_daemon httpd
195         check
196
197         result "$MESSAGE"
198         ;;
199
200     stop)
201         MESSAGE=$"Stopping web server"
202         dialog "$MESSAGE"
203
204         killproc plc_httpd
205         check
206
207         result "$MESSAGE"
208         ;;
209 esac
210
211 exit $ERRORS