~loggerhead-team/loggerhead/trunk-rich

161 by Michael Hudson
don't insist on python 2.4
1
#!/usr/bin/env python
183.2.1 by John Arbash Meinel
Add Copyright information to most files.
2
# This program is free software; you can redistribute it and/or modify
3
# it under the terms of the GNU General Public License as published by
4
# the Free Software Foundation; either version 2 of the License, or
5
# (at your option) any later version.
6
#
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
# GNU General Public License for more details.
11
#
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
352.5.8 by Martin Albisetti
Add deprectation warning to start-loggerhead
16
"""
17
WARNING! This script has been deprecated and will go away soon!
18
A script for starting the loggerhead process.
19
"""
183.2.1 by John Arbash Meinel
Add Copyright information to most files.
20
89 by Robey Pointer
fix up dev.cfg so that nobody will ever have to edit it, by letting the
21
93 by Robey Pointer
slight cleanup, and add '-f' option for running in the foreground.
22
import logging
159.2.46 by Michael Hudson
add explicit import of logging.handlers
23
import logging.handlers
166.1.2 by Michael Hudson
more loggerhead.conf compatible vhosting
24
from optparse import OptionParser
89 by Robey Pointer
fix up dev.cfg so that nobody will ever have to edit it, by letting the
25
import os
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
26
import sys
166.1.2 by Michael Hudson
more loggerhead.conf compatible vhosting
27
import urlparse
89 by Robey Pointer
fix up dev.cfg so that nobody will ever have to edit it, by letting the
28
270.1.1 by Michael Hudson
defer loading of plugins until after we've set up the logging
29
from bzrlib.plugin import load_plugins
255.1.1 by Jelmer Vernooij
Fix import of ConfigObj.
30
from bzrlib.util.configobj.configobj import ConfigObj
159.2.50 by Michael Hudson
reorder imports in start-loggerhead.py
31
159.2.28 by Michael Hudson
woohoo it works
32
from paste import httpserver
33
from paste.httpexceptions import make_middleware
34
from paste.translogger import make_filter
1 by Robey Pointer
initial checkin
35
204 by Martin Albisetti
Clean up unused/inexistant import
36
from loggerhead import daemon
159.2.50 by Michael Hudson
reorder imports in start-loggerhead.py
37
from loggerhead.apps.config import Root
217.1.1 by Guillermo Gonzalez
* refactor of logging-related functions (setup_logging and make_handler) into (new) trace module
38
from loggerhead.trace import make_handler, setup_logging
217.1.4 by Guillermo Gonzalez
* new apps module: "error" and ErrorHandlerApp middleware
39
from loggerhead.apps.error import ErrorHandlerApp
93 by Robey Pointer
slight cleanup, and add '-f' option for running in the foreground.
40
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
41
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
42
def main():
352.5.8 by Martin Albisetti
Add deprectation warning to start-loggerhead
43
    sys.stderr.write('\n\n')
44
    sys.stderr.write('WARNING!!! This script has been deprecated by '
45
                     'serve-branches, and will be removed in the next '
46
                     'release. Please migrate to serve-branches and report '
47
                     'any missing features.\n')
48
    sys.stderr.write('\n\n')
49
197.1.4 by Jelmer Vernooij
Add --pidfile argument to start-loggerhead.
50
    home = os.path.realpath(os.path.dirname(__file__))
51
    default_pidfile = os.path.join(home, 'loggerhead.pid')
197.1.5 by Jelmer Vernooij
Add --configfile argument.
52
    default_configfile = os.path.join(home, 'loggerhead.conf')
197.1.6 by Jelmer Vernooij
add --log-folder option.
53
    default_log_folder = os.path.join(home, 'logs')
205 by Martin Albisetti
Further cleaning
54
    parser = OptionParser(usage='usage: %prog [options]', version='%prog')
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
55
    parser.add_option('-f', '--foreground',
56
                      action='store_true', dest='foreground', default=False,
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
57
                      help="run in the foreground; don't daemonize")
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
58
    parser.add_option('-C', '--check', action='store_true',
59
                      dest='check', default=False,
60
                      help=("only start if not already running (useful for "
61
                            "cron jobs)"))
62
    parser.add_option('-p', '--pidfile', dest="pidfile",
63
                      default=default_pidfile,
197.2.1 by Jelmer Vernooij
Fix syntax.
64
                      type=str, help="override pidfile location")
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
65
    parser.add_option('-c', '--config-file', dest="configfile",
66
                      default=default_configfile,
67
                      type=str, help="override configuration file location")
68
    parser.add_option('-L', '--log-folder', dest="log_folder",
69
                      default=default_log_folder,
197.2.1 by Jelmer Vernooij
Fix syntax.
70
                      type=str, help="override log file directory")
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
71
    options, args = parser.parse_args()
72
    if len(args) > 0:
73
        parser.error('No filename arguments are used, only options.')
159.2.27 by Michael Hudson
begin a compatibility app
74
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
75
    if options.check:
197.1.4 by Jelmer Vernooij
Add --pidfile argument to start-loggerhead.
76
        if daemon.is_running(options.pidfile):
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
77
            sys.exit(0)
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
78
        sys.stderr.write('Did not find loggerhead running in %r;' % (
79
                         options.pidfile))
80
        sys.stderr.write(' restarting...\n')
159.2.27 by Michael Hudson
begin a compatibility app
81
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
82
    # read loggerhead config
159.2.27 by Michael Hudson
begin a compatibility app
83
197.1.5 by Jelmer Vernooij
Add --configfile argument.
84
    config = ConfigObj(options.configfile, encoding='utf-8')
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
85
    extra_path = config.get('bzrpath', None)
86
    if extra_path:
87
        sys.path.insert(0, extra_path)
159.2.27 by Michael Hudson
begin a compatibility app
88
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
89
    potential_overrides = [('server.socket_port', int),
90
                           ('server.webpath', str),
91
                           ('server.thread_pool', int),
92
                           ('server.socket_host', str)]
159.2.36 by Michael Hudson
more compatibility
93
    server_port = int(config.get('server.socket_port', 8080))
94
    nworkers = int(config.get('server.thread_pool', 10))
95
    server_host = config.get('server.socket_host', '0.0.0.0')
96
    webpath = config.get('server.webpath', None)
97
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
98
    for key, keytype in potential_overrides:
99
        value = config.get(key, None)
100
        if value is not None:
101
            value = keytype(value)
159.2.28 by Michael Hudson
woohoo it works
102
159.2.29 by Michael Hudson
re-enable daemonization, remove require("TurboGears")
103
    if not options.foreground:
104
        sys.stderr.write('\n')
105
        sys.stderr.write('Launching loggerhead into the background.\n')
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
106
        sys.stderr.write('PID file: %s\n' % options.pidfile)
159.2.29 by Michael Hudson
re-enable daemonization, remove require("TurboGears")
107
        sys.stderr.write('\n')
159.2.28 by Michael Hudson
woohoo it works
108
197.1.4 by Jelmer Vernooij
Add --pidfile argument to start-loggerhead.
109
        daemon.daemonize(options.pidfile, home)
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
110
197.1.6 by Jelmer Vernooij
add --log-folder option.
111
    setup_logging(options.log_folder, config, foreground=options.foreground)
159.2.27 by Michael Hudson
begin a compatibility app
112
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
113
    log = logging.getLogger('loggerhead')
114
    log.info('Starting up...')
159.2.27 by Michael Hudson
begin a compatibility app
115
159.2.28 by Michael Hudson
woohoo it works
116
    app = Root(config)
117
118
    app = app
119
    app = make_middleware(app)
217.1.4 by Guillermo Gonzalez
* new apps module: "error" and ErrorHandlerApp middleware
120
    app = make_filter(app, None, logger_name=log.name+'.access')
121
    app = ErrorHandlerApp(app)
159.2.28 by Michael Hudson
woohoo it works
122
159.2.36 by Michael Hudson
more compatibility
123
    if webpath:
166.1.2 by Michael Hudson
more loggerhead.conf compatible vhosting
124
        scheme, netloc, path, blah, blah, blah = urlparse.urlparse(webpath)
230.1.1 by Steve 'Ashcrow' Milner
Updated to follow pep8.
125
166.1.2 by Michael Hudson
more loggerhead.conf compatible vhosting
126
        def app(environ, start_response, orig=app):
127
            environ['SCRIPT_NAME'] = path
128
            environ['HTTP_HOST'] = netloc
266 by Michael Hudson
apply neror's patch to fix bug #260547 ("start-loggerhead script doesn't properly set the wsgi.url_scheme from the server.webpath option")
129
            environ['wsgi.url_scheme'] = scheme
166.1.2 by Michael Hudson
more loggerhead.conf compatible vhosting
130
            return orig(environ, start_response)
159.2.36 by Michael Hudson
more compatibility
131
270.1.1 by Michael Hudson
defer loading of plugins until after we've set up the logging
132
    load_plugins()
133
159.2.45 by Michael Hudson
testing is grate
134
    try:
135
        httpserver.serve(
136
            app, host=server_host, port=server_port,
137
            threadpool_workers=nworkers)
138
    finally:
139
        log.info('Shutdown.')
140
        try:
197.1.4 by Jelmer Vernooij
Add --pidfile argument to start-loggerhead.
141
            os.remove(options.pidfile)
159.2.45 by Michael Hudson
testing is grate
142
        except OSError:
143
            pass
128.2.3 by Robey Pointer
add a '-C' option for ensuring loggerhead is running (from a cron job, for
144
145
146
if __name__ == '__main__':
147
    main()