~launchpad-pqm/launchpad/devel

8687.15.8 by Karl Fogel
Add the copyright header block to more files.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
3
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
4
# Twisted Application Configuration file.
2237 by Canonical.com Patch Queue Manager
[trivial] Libraraian upstream tweaks, which demonstrate the the feature is broken (?)
5
# Use with "twistd2.4 -y <file.tac>", e.g. "twistd -noy server.tac"
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
6
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
7
import os
10739.3.1 by Guilherme Salgado
Make the librarian dump its memory (using meliae) upon a 'kill -44'
8
import signal
9
10
from meliae import scanner
11
6998.3.2 by Guilherme Salgado
fix the bug.
12
from twisted.application import service, strports
11784.2.1 by Gary Poster
add diagnostics for why the librarian rejects requests, for diagnosing bug 662912.
13
from twisted.internet import reactor
14
from twisted.python import log
14446.1.8 by Julian Edwards
fix some logging; TestBranchPuller still failing
15
from twisted.scripts.twistd import ServerOptions
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
16
from twisted.web import server
17
4450.7.1 by James Henstridge
move librarian over to use the webapp database adapter
18
from canonical.config import config, dbconfig
11765.1.1 by Robert Collins
Split out the launchpad-buildd needed component from tachandler.py.
19
from canonical.launchpad.daemons import readyservice
14565.2.15 by Curtis Hovey
Moved canonical.launchpad.scripts __init__ to lp.services.scripts.
20
from lp.services.scripts import execute_zcml_for_scripts
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
21
10739.3.1 by Guilherme Salgado
Make the librarian dump its memory (using meliae) upon a 'kill -44'
22
from canonical.librarian.interfaces import DUMP_FILE, SIGDUMPMEM
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
23
from canonical.librarian.libraryprotocol import FileUploadFactory
24
from canonical.librarian import storage, db
25
from canonical.librarian import web as fatweb
11221.1.1 by Robert Collins
Start logging oops from the librarian. Fixes lp:86185
26
from lp.services.twistedsupport.loggingsupport import set_up_oops_reporting
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
27
28
# Connect to database
13993.1.2 by William Grant
librarian no longer uses setConfigSection.
29
dbconfig.override(
30
    dbuser=config.librarian.dbuser,
31
    isolation_level=config.librarian.isolation_level)
4450.7.1 by James Henstridge
move librarian over to use the webapp database adapter
32
execute_zcml_for_scripts()
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
33
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
34
if os.environ.get('LP_TEST_INSTANCE'):
35
    # Running in ephemeral mode: get the root dir from the environment and
36
    # dynamically allocate ports.
37
    path = os.environ['LP_LIBRARIAN_ROOT']
38
else:
39
    path = config.librarian_server.root
5863.9.2 by Curtis Hovey
Updated code to use the libraran_server section.
40
if config.librarian_server.upstream_host:
41
    upstreamHost = config.librarian_server.upstream_host
42
    upstreamPort = config.librarian_server.upstream_port
11784.2.2 by Gary Poster
switch callWhenRunning to addSystemEventTrigger because it is clearer about intent.
43
    reactor.addSystemEventTrigger(
44
        'before', 'startup', log.msg,
11784.2.1 by Gary Poster
add diagnostics for why the librarian rejects requests, for diagnosing bug 662912.
45
        'Using upstream librarian http://%s:%d' %
46
        (upstreamHost, upstreamPort))
2109 by Canonical.com Patch Queue Manager
[trivial] Add upstream librarian support for the staging environment
47
else:
48
    upstreamHost = upstreamPort = None
11784.2.2 by Gary Poster
switch callWhenRunning to addSystemEventTrigger because it is clearer about intent.
49
    reactor.addSystemEventTrigger(
50
        'before', 'startup', log.msg, 'Not using upstream librarian')
6998.3.2 by Guilherme Salgado
fix the bug.
51
52
application = service.Application('Librarian')
53
librarianService = service.IServiceCollection(application)
54
55
# Service that announces when the daemon is ready
11765.1.1 by Robert Collins
Split out the launchpad-buildd needed component from tachandler.py.
56
readyservice.ReadyService().setServiceParent(librarianService)
6998.3.2 by Guilherme Salgado
fix the bug.
57
6998.3.3 by Guilherme Salgado
A couple changes after Francis' review
58
def setUpListener(uploadPort, webPort, restricted):
59
    """Set up a librarian listener on the given ports.
60
61
    :param restricted: Should this be a restricted listener?  A restricted
62
        listener will serve only files with the 'restricted' file set and all
63
        files uploaded through the restricted listener will have that flag
64
        set.
65
    """
66
    librarian_storage = storage.LibrarianStorage(
67
        path, db.Library(restricted=restricted))
68
    upload_factory = FileUploadFactory(librarian_storage)
12581.2.1 by Gavin Panella
Do as the warnings suggest.
69
    strports.service("tcp:%d" % uploadPort, upload_factory).setServiceParent(
6998.3.3 by Guilherme Salgado
A couple changes after Francis' review
70
        librarianService)
71
    root = fatweb.LibraryFileResource(
72
        librarian_storage, upstreamHost, upstreamPort)
73
    root.putChild('search', fatweb.DigestSearchResource(librarian_storage))
74
    root.putChild('robots.txt', fatweb.robotsTxt)
75
    site = server.Site(root)
76
    site.displayTracebacks = False
12581.2.1 by Gavin Panella
Do as the warnings suggest.
77
    strports.service("tcp:%d" % webPort, site).setServiceParent(
78
        librarianService)
6998.3.3 by Guilherme Salgado
A couple changes after Francis' review
79
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
80
if os.environ.get('LP_TEST_INSTANCE'):
81
    # Running in ephemeral mode: allocate ports on demand.
82
    setUpListener(0, 0, restricted=False)
83
    setUpListener(0, 0, restricted=True)
84
else:
85
    # Set up the public librarian.
86
    uploadPort = config.librarian.upload_port
87
    webPort = config.librarian.download_port
88
    setUpListener(uploadPort, webPort, restricted=False)
89
    # Set up the restricted librarian.
90
    webPort = config.librarian.restricted_download_port
91
    uploadPort = config.librarian.restricted_upload_port
92
    setUpListener(uploadPort, webPort, restricted=True)
10739.3.1 by Guilherme Salgado
Make the librarian dump its memory (using meliae) upon a 'kill -44'
93
11221.1.1 by Robert Collins
Start logging oops from the librarian. Fixes lp:86185
94
# Log OOPS reports
14446.1.8 by Julian Edwards
fix some logging; TestBranchPuller still failing
95
options = ServerOptions()
96
options.parseOptions()
97
logfile = options.get("logfile")
98
set_up_oops_reporting('librarian', 'librarian', logfile)
11221.1.1 by Robert Collins
Start logging oops from the librarian. Fixes lp:86185
99
10739.3.1 by Guilherme Salgado
Make the librarian dump its memory (using meliae) upon a 'kill -44'
100
# Setup a signal handler to dump the process' memory upon 'kill -44'.
101
def sigdumpmem_handler(signum, frame):
102
    scanner.dump_all_objects(DUMP_FILE)
103
104
signal.signal(SIGDUMPMEM, sigdumpmem_handler)