run_apache2.py revision 5846d4561d45c2805b3cba33995bfa08b55b980e
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  directives += " -c \"Alias /WEBKIT_MERGE_REVISION " + \
80    os.path.join(webkit_path, "WEBKIT_MERGE_REVISION") + "\""
81
82  # This directive is commented out in apache2-debian-httpd.conf for some reason
83  # However, it is useful to browse through tests in the browser, so it's added here.
84  # One thing to note is that because of problems with mod_dir and port numbers, mod_dir
85  # is turned off. That means that there _must_ be a trailing slash at the end of URL
86  # for auto indexes to work correctly.
87  directives += " -c \"LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so\""
88
89  directives += " -c \"ErrorLog " + error_log_path +"\""
90  directives += " -c \"CustomLog " + custom_log_path + " combined\""
91  directives += " -c \"SSLCertificateFile " + os.path.join(http_conf_path, "webkit-httpd.pem") + \
92    "\""
93  directives += " -c \"User ${APACHE_RUN_USER}\""
94  directives += " -c \"Group ${APACHE_RUN_GROUP}\""
95  directives += " -C \"TypesConfig " + \
96    os.path.join(android_tree_root, http_conf_path, "mime.types") + "\""
97  conf_file_cmd = " -f " + \
98    os.path.join(android_tree_root, http_conf_path, "apache2-debian-httpd.conf")
99
100  # Try to execute the commands
101  logging.info("Will " + run_cmd + " apache2 server.")
102  cmd = export_envvars_cmd + " && " + apache2_restart_cmd + directives + conf_file_cmd
103  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
104  (out, err) = p.communicate()
105
106  # Output the stdout from the command to console
107  logging.info(out)
108
109  # Report any errors
110  if p.returncode != 0:
111    logging.info("!! ERRORS:")
112
113    if err.find(envvars_path) != -1:
114      logging.info(err)
115    elif err.find('command not found') != -1:
116      logging.info("apache2 is probably not installed")
117    else:
118      logging.info(err)
119      logging.info("Try looking in " + error_log_path + " for details")
120  else:
121    logging.info("OK")
122
123if __name__ == "__main__":
124  main();
125