run_apache2.py revision 9d660cb5f3202366ad8540d66018b33b3f8578df
1#!/usr/bin/python 2# 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# 17"""Start, stop, or restart apache2 server. 18 19 Apache2 must be installed with mod_php! 20 21 Usage: 22 run_apache2.py start|stop|restart 23""" 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 36 # Setup logging class 37 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) 41 logging.info("Usage: python run_apache2.py start|stop|restart") 42 return 43 44 # Create /tmp/WebKit if it doesn't exist. This is needed for various files used by apache2 45 tmp_WebKit = os.path.join("/tmp", "WebKit") 46 if not os.path.exists(tmp_WebKit): 47 os.mkdir(tmp_WebKit) 48 49 # Get the path to android tree root based on the script location. 50 # Basically we go 5 levels up 51 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 56 # Paths relative to android_tree_root 57 webkit_path = os.path.join("external", "webkit") 58 layout_tests_path = os.path.join(webkit_path, "LayoutTests") 59 http_conf_path = os.path.join(layout_tests_path, "http", "conf") 60 61 # Prepare the command to set ${APACHE_RUN_USER} and ${APACHE_RUN_GROUP} 62 envvars_path = os.path.join("/etc", "apache2", "envvars") 63 export_envvars_cmd = "source " + envvars_path 64 65 error_log_path = os.path.join(tmp_WebKit, "apache2-error.log") 66 custom_log_path = os.path.join(tmp_WebKit, "apache2-access.log") 67 68 # Prepare the command to (re)start/stop the server with specified settings 69 apache2_restart_cmd = "apache2 -k " + run_cmd 70 directives = " -c \"ServerRoot " + android_tree_root + "\"" 71 72 # We use http/tests as the document root as the HTTP tests use hardcoded 73 # resources at the server root. We then use aliases to make available the 74 # complete set of tests and the required scripts. 75 directives += " -c \"DocumentRoot " + os.path.join(layout_tests_path, "http", "tests/") + "\"" 76 directives += " -c \"Alias /LayoutTests " + layout_tests_path + "\"" 77 directives += " -c \"Alias /WebKitTools/DumpRenderTree/android " + \ 78 os.path.join(webkit_path, "WebKitTools", "DumpRenderTree", "android") + "\"" 79 80 # This directive is commented out in apache2-debian-httpd.conf for some reason 81 # However, it is useful to browse through tests in the browser, so it's added here. 82 # One thing to note is that because of problems with mod_dir and port numbers, mod_dir 83 # is turned off. That means that there _must_ be a trailing slash at the end of URL 84 # for auto indexes to work correctly. 85 directives += " -c \"LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so\"" 86 87 directives += " -c \"ErrorLog " + error_log_path +"\"" 88 directives += " -c \"CustomLog " + custom_log_path + " combined\"" 89 directives += " -c \"SSLCertificateFile " + os.path.join(http_conf_path, "webkit-httpd.pem") + \ 90 "\"" 91 directives += " -c \"User ${APACHE_RUN_USER}\"" 92 directives += " -c \"Group ${APACHE_RUN_GROUP}\"" 93 directives += " -C \"TypesConfig " + os.path.join("/etc", "mime.types") + "\"" 94 conf_file_cmd = " -f " + \ 95 os.path.join(android_tree_root, http_conf_path, "apache2-debian-httpd.conf") 96 97 # Try to execute the commands 98 logging.info("Will " + run_cmd + " apache2 server.") 99 cmd = export_envvars_cmd + " && " + apache2_restart_cmd + directives + conf_file_cmd 100 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 101 (out, err) = p.communicate() 102 103 # Output the stdout from the command to console 104 logging.info(out) 105 106 # Report any errors 107 if p.returncode != 0: 108 logging.info("!! ERRORS:") 109 110 if err.find(envvars_path) != -1: 111 logging.info(err) 112 elif err.find('command not found') != -1: 113 logging.info("apache2 is probably not installed") 114 else: 115 logging.info(err) 116 logging.info("Try looking in " + error_log_path + " for details") 117 else: 118 logging.info("OK") 119 120if __name__ == "__main__": 121 main(); 122