~loggerhead-team/loggerhead/trunk-rich

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
WARNING! This script has been deprecated and will go away soon!
A script for starting the loggerhead process.
"""


import logging
import logging.handlers
from optparse import OptionParser
import os
import sys
import urlparse

from bzrlib.plugin import load_plugins
from bzrlib.util.configobj.configobj import ConfigObj

from paste import httpserver
from paste.httpexceptions import make_middleware
from paste.translogger import make_filter

from loggerhead import daemon
from loggerhead.apps.config import Root
from loggerhead.trace import make_handler, setup_logging
from loggerhead.apps.error import ErrorHandlerApp


def main():
    sys.stderr.write('\n\n')
    sys.stderr.write('WARNING!!! This script has been deprecated by '
                     'serve-branches, and will be removed in the next '
                     'release. Please migrate to serve-branches and report '
                     'any missing features.\n')
    sys.stderr.write('\n\n')

    home = os.path.realpath(os.path.dirname(__file__))
    default_pidfile = os.path.join(home, 'loggerhead.pid')
    default_configfile = os.path.join(home, 'loggerhead.conf')
    default_log_folder = os.path.join(home, 'logs')
    parser = OptionParser(usage='usage: %prog [options]', version='%prog')
    parser.add_option('-f', '--foreground',
                      action='store_true', dest='foreground', default=False,
                      help="run in the foreground; don't daemonize")
    parser.add_option('-C', '--check', action='store_true',
                      dest='check', default=False,
                      help=("only start if not already running (useful for "
                            "cron jobs)"))
    parser.add_option('-p', '--pidfile', dest="pidfile",
                      default=default_pidfile,
                      type=str, help="override pidfile location")
    parser.add_option('-c', '--config-file', dest="configfile",
                      default=default_configfile,
                      type=str, help="override configuration file location")
    parser.add_option('-L', '--log-folder', dest="log_folder",
                      default=default_log_folder,
                      type=str, help="override log file directory")
    options, args = parser.parse_args()
    if len(args) > 0:
        parser.error('No filename arguments are used, only options.')

    if options.check:
        if daemon.is_running(options.pidfile):
            sys.exit(0)
        sys.stderr.write('Did not find loggerhead running in %r;' % (
                         options.pidfile))
        sys.stderr.write(' restarting...\n')

    # read loggerhead config

    config = ConfigObj(options.configfile, encoding='utf-8')
    extra_path = config.get('bzrpath', None)
    if extra_path:
        sys.path.insert(0, extra_path)

    potential_overrides = [('server.socket_port', int),
                           ('server.webpath', str),
                           ('server.thread_pool', int),
                           ('server.socket_host', str)]
    server_port = int(config.get('server.socket_port', 8080))
    nworkers = int(config.get('server.thread_pool', 10))
    server_host = config.get('server.socket_host', '0.0.0.0')
    webpath = config.get('server.webpath', None)

    for key, keytype in potential_overrides:
        value = config.get(key, None)
        if value is not None:
            value = keytype(value)

    if not options.foreground:
        sys.stderr.write('\n')
        sys.stderr.write('Launching loggerhead into the background.\n')
        sys.stderr.write('PID file: %s\n' % options.pidfile)
        sys.stderr.write('\n')

        daemon.daemonize(options.pidfile, home)

    setup_logging(options.log_folder, config, foreground=options.foreground)

    log = logging.getLogger('loggerhead')
    log.info('Starting up...')

    app = Root(config)

    app = app
    app = make_middleware(app)
    app = make_filter(app, None, logger_name=log.name+'.access')
    app = ErrorHandlerApp(app)

    if webpath:
        scheme, netloc, path, blah, blah, blah = urlparse.urlparse(webpath)

        def app(environ, start_response, orig=app):
            environ['SCRIPT_NAME'] = path
            environ['HTTP_HOST'] = netloc
            environ['wsgi.url_scheme'] = scheme
            return orig(environ, start_response)

    load_plugins()

    try:
        httpserver.serve(
            app, host=server_host, port=server_port,
            threadpool_workers=nworkers)
    finally:
        log.info('Shutdown.')
        try:
            os.remove(options.pidfile)
        except OSError:
            pass


if __name__ == '__main__':
    main()