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