run_apache2.py revision ee273fac7f223eafb7619168ec656dd4947b1e5e
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  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}
61  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")
65  custom_log_path = os.path.join(tmp_WebKit, "apache2-access.log")
66
67  # Prepare the command to (re)start/stop the server with specified settings
68  apache2_restart_cmd = "apache2 -k " + run_cmd
69  directives  = " -c \"ServerRoot " + android_tree_root + "\""
70  directives += " -c \"DocumentRoot " + webkit_path + "\""
71
72  # 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.
77  directives += " -c \"LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so\""
78
79  directives += " -c \"ErrorLog " + error_log_path +"\""
80  directives += " -c \"CustomLog " + custom_log_path + " combined\""
81  directives += " -c \"SSLCertificateFile " + os.path.join(http_conf_path, "webkit-httpd.pem") + \
82    "\""
83  directives += " -c \"User ${APACHE_RUN_USER}\""
84  directives += " -c \"Group ${APACHE_RUN_GROUP}\""
85  directives += " -C \"TypesConfig " + os.path.join("/etc", "mime.types") + "\""
86  conf_file_cmd = " -f " + \
87    os.path.join(android_tree_root, http_conf_path, "apache2-debian-httpd.conf")
88
89  # Try to execute the commands
90  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
95  # Output the stdout from the command to console
96  logging.info(out)
97
98  # Report any errors
99  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();
114