run_apache2.py revision fe33f987cb61d8a5c22f207df6d47d2346828af7
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 #Prepare the command to set ${APACHE_RUN_USER} and ${APACHE_RUN_GROUP} 57 envvars_path = os.path.join("/etc", "apache2", "envvars") 58 export_envvars_cmd = "source " + envvars_path 59 60 error_log_path = os.path.join(tmp_WebKit, "apache2-error.log") 61 custom_log_path = os.path.join(tmp_WebKit, "apache2-access.log") 62 63 #Prepare the command to (re)start/stop the server with specified settings 64 apache2_restart_cmd = "apache2 -k " + run_cmd 65 directives = " -c \"ServerRoot " + android_tree_root + "\"" 66 directives += " -c \"DocumentRoot " + os.path.join("external", "webkit") + "\"" 67 68 #This directive is commented out in apache2-debian-httpd.conf for some reason 69 #However, it is useful to browse through tests in the browser, so it's added here. 70 #One thing to note is that because of problems with mod_dir and port numbers, mod_dir 71 #is turned off. That means that there _must_ be a trailing slash at the end of URL 72 #for auto indexes to work correctly. 73 directives += " -c \"LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so\"" 74 75 directives += " -c \"ErrorLog " + error_log_path +"\"" 76 directives += " -c \"CustomLog " + custom_log_path + " combined\"" 77 directives += " -c \"SSLCertificateFile " + os.path.join ("external", "webkit", "LayoutTests", 78 "http", "conf", "webkit-httpd.pem") + "\"" 79 directives += " -c \"User ${APACHE_RUN_USER}\"" 80 directives += " -c \"Group ${APACHE_RUN_GROUP}\"" 81 directives += " -C \"TypesConfig " + os.path.join("/etc", "mime.types") + "\"" 82 conf_file_cmd = " -f " + os.path.join(android_tree_root, "external", "webkit", "LayoutTests", 83 "http", "conf", "apache2-debian-httpd.conf") 84 85 #Try to execute the commands 86 logging.info("Will " + run_cmd + " apache2 server.") 87 cmd = export_envvars_cmd + " && " + apache2_restart_cmd + directives + conf_file_cmd 88 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 89 (out, err) = p.communicate() 90 91 #Output the stdout from the command to console 92 logging.info(out) 93 94 #Report any errors 95 if p.returncode != 0: 96 logging.info("!! ERRORS:") 97 98 if err.find(envvars_path) != -1: 99 logging.info(err) 100 elif err.find('command not found') != -1: 101 logging.info("apache2 is probably not installed") 102 else: 103 logging.info(err) 104 logging.info("Try looking in " + error_log_path + " for details") 105 else: 106 logging.info("OK") 107 108if __name__ == "__main__": 109 main(); 110