blob: bbb007c76fa1ee74270cf7f0991a5f42a081aedf [file] [log] [blame]
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +01001#!/usr/bin/python
Maksymilian Osowski073d2c92010-08-13 17:12:51 +01002#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010017"""Start, stop, or restart apache2 server.
18
19 Apache2 must be installed with mod_php!
20
21 Usage:
Steve Block0e4d86f2010-08-18 15:25:34 +010022 run_apache2.py start|stop|restart
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010023"""
24
25import sys
26import os
27import subprocess
28import logging
29
30def main():
31 if len(sys.argv) < 2:
32 run_cmd = ""
33 else:
34 run_cmd = sys.argv[1]
35
Steve Blockee273fa2010-08-26 15:09:27 +010036 # Setup logging class
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010037 logging.basicConfig(level=logging.INFO, format='%(message)s')
38
39 if not run_cmd in ("start", "stop", "restart"):
40 logging.info("illegal argument: " + run_cmd)
Steve Block0e4d86f2010-08-18 15:25:34 +010041 logging.info("Usage: python run_apache2.py start|stop|restart")
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010042 return
43
Steve Blockee273fa2010-08-26 15:09:27 +010044 # Create /tmp/WebKit if it doesn't exist. This is needed for various files used by apache2
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010045 tmp_WebKit = os.path.join("/tmp", "WebKit")
46 if not os.path.exists(tmp_WebKit):
47 os.mkdir(tmp_WebKit)
48
Steve Blockee273fa2010-08-26 15:09:27 +010049 # Get the path to android tree root based on the script location.
50 # Basically we go 5 levels up
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010051 parent = os.pardir
52 script_location = os.path.abspath(os.path.dirname(sys.argv[0]))
53 android_tree_root = os.path.join(script_location, parent, parent, parent, parent, parent)
54 android_tree_root = os.path.normpath(android_tree_root)
55
Steve Blockee273fa2010-08-26 15:09:27 +010056 # Paths relative to android_tree_root
57 webkit_path = os.path.join("external", "webkit")
58 http_conf_path = os.path.join(webkit_path, "LayoutTests", "http", "conf")
59
60 # Prepare the command to set ${APACHE_RUN_USER} and ${APACHE_RUN_GROUP}
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010061 envvars_path = os.path.join("/etc", "apache2", "envvars")
62 export_envvars_cmd = "source " + envvars_path
63
64 error_log_path = os.path.join(tmp_WebKit, "apache2-error.log")
Maksymilian Osowskife33f982010-08-26 15:25:02 +010065 custom_log_path = os.path.join(tmp_WebKit, "apache2-access.log")
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010066
Steve Blockee273fa2010-08-26 15:09:27 +010067 # Prepare the command to (re)start/stop the server with specified settings
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010068 apache2_restart_cmd = "apache2 -k " + run_cmd
69 directives = " -c \"ServerRoot " + android_tree_root + "\""
Steve Blockee273fa2010-08-26 15:09:27 +010070 directives += " -c \"DocumentRoot " + webkit_path + "\""
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010071
Steve Blockee273fa2010-08-26 15:09:27 +010072 # This directive is commented out in apache2-debian-httpd.conf for some reason
73 # However, it is useful to browse through tests in the browser, so it's added here.
74 # One thing to note is that because of problems with mod_dir and port numbers, mod_dir
75 # is turned off. That means that there _must_ be a trailing slash at the end of URL
76 # for auto indexes to work correctly.
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010077 directives += " -c \"LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so\""
78
79 directives += " -c \"ErrorLog " + error_log_path +"\""
Maksymilian Osowskife33f982010-08-26 15:25:02 +010080 directives += " -c \"CustomLog " + custom_log_path + " combined\""
Steve Blockee273fa2010-08-26 15:09:27 +010081 directives += " -c \"SSLCertificateFile " + os.path.join(http_conf_path, "webkit-httpd.pem") + \
82 "\""
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010083 directives += " -c \"User ${APACHE_RUN_USER}\""
84 directives += " -c \"Group ${APACHE_RUN_GROUP}\""
85 directives += " -C \"TypesConfig " + os.path.join("/etc", "mime.types") + "\""
Steve Blockee273fa2010-08-26 15:09:27 +010086 conf_file_cmd = " -f " + \
87 os.path.join(android_tree_root, http_conf_path, "apache2-debian-httpd.conf")
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010088
Steve Blockee273fa2010-08-26 15:09:27 +010089 # Try to execute the commands
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010090 logging.info("Will " + run_cmd + " apache2 server.")
91 cmd = export_envvars_cmd + " && " + apache2_restart_cmd + directives + conf_file_cmd
92 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
93 (out, err) = p.communicate()
94
Steve Blockee273fa2010-08-26 15:09:27 +010095 # Output the stdout from the command to console
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010096 logging.info(out)
97
Steve Blockee273fa2010-08-26 15:09:27 +010098 # Report any errors
Maksymilian Osowski74b13ae2010-08-12 14:35:09 +010099 if p.returncode != 0:
100 logging.info("!! ERRORS:")
101
102 if err.find(envvars_path) != -1:
103 logging.info(err)
104 elif err.find('command not found') != -1:
105 logging.info("apache2 is probably not installed")
106 else:
107 logging.info(err)
108 logging.info("Try looking in " + error_log_path + " for details")
109 else:
110 logging.info("OK")
111
112if __name__ == "__main__":
113 main();