~launchpad-pqm/launchpad/devel

1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
1
#! /usr/bin/env python2.4
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
2
##############################################################################
3
#
4
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
5
# All Rights Reserved.
6
#
7
# This software is subject to the provisions of the Zope Public License,
8
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
9
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
10
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
12
# FOR A PARTICULAR PURPOSE.
13
#
14
##############################################################################
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
15
"""Start script for Launchpad: loads configuration and starts the server.
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
16
17
$Id: z3.py 25266 2004-06-04 21:25:45Z jim $
18
"""
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
19
import sys
20
21
if sys.version_info < (2, 4, 0):
22
    print ("ERROR: Your python version is not supported by Launchpad."
23
            "Launchpad needs Python 2.4 or greater. You are running: " 
24
            + sys.version)
25
    sys.exit(1)
26
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
27
import os
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
28
import os.path
29
import atexit
30
import signal
31
import subprocess
32
import time
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
33
from zope.app.server.main import main
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
34
from configs import generate_overrides
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
35
36
basepath = filter(None, sys.path)
37
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
38
# Disgusting hack to use our extended config file schema rather than the
39
# Z3 one. TODO: Add command line options or other to Z3 to enable overriding
40
# this -- StuartBishop 20050406
41
from zdaemon.zdoptions import ZDOptions
42
ZDOptions.schemafile = os.path.abspath(os.path.join(
43
        os.path.dirname(__file__), 'lib', 'canonical',
44
        'config', 'schema.xml'))
45
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
46
def start_librarian():
47
    # Imported here as path is not set fully on module load
48
    from canonical.config import config
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
49
    from canonical.pidfile import make_pidfile, pidfile_path
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
50
51
    # Don't run the Librarian if it wasn't asked for. We only want it
52
    # started up developer boxes really, as the production Librarian
53
    # doesn't use this startup script.
54
    if not config.librarian.server.launch:
55
        return
56
57
    if not os.path.isdir(config.librarian.server.root):
58
        os.makedirs(config.librarian.server.root, 0700)
59
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
60
    pidfile = pidfile_path('librarian')
2603 by Canonical.com Patch Queue Manager
Build daemon task sequencer/serialiser. r=spiv
61
    logfile = config.librarian.server.logfile
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
62
    tacfile = os.path.abspath(os.path.join(
63
        os.path.dirname(__file__), 'daemons', 'librarian.tac'
64
        ))
65
66
    ver = '%d.%d' % sys.version_info[:2]
67
    args = [
68
        "twistd%s" % ver,
69
        "--no_save",
70
        "--nodaemon",
71
        "--python", tacfile,
72
        "--pidfile", pidfile,
73
        "--prefix", "Librarian",
1948 by Canonical.com Patch Queue Manager
[r=stub] Bring trebuchet into the production/staging/development frameworks similar to Librarian
74
        "--logfile", logfile,
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
75
        ]
76
77
    if config.librarian.server.spew:
78
        args.append("--spew")
79
1948 by Canonical.com Patch Queue Manager
[r=stub] Bring trebuchet into the production/staging/development frameworks similar to Librarian
80
    # Note that startup tracebacks and evil programmers using 'print'
81
    # will cause output to our stdout. However, we don't want to have
82
    # twisted log to stdout and redirect it ourselves because we then
83
    # lose the ability to cycle the log files by sending a signal to the
84
    # twisted process.
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
85
    librarian_process = subprocess.Popen(args, stdin=subprocess.PIPE)
86
    librarian_process.stdin.close()
87
    # I've left this off - we still check at termination and we can
88
    # avoid the startup delay. -- StuartBishop 20050525
89
    #time.sleep(1)
90
    #if librarian_process.poll() != None:
91
    #    raise RuntimeError(
92
    #            "Librarian did not start: %d" % librarian_process.returncode
93
    #            )
94
    def stop_librarian():
95
        if librarian_process.poll() is None:
96
            os.kill(librarian_process.pid, signal.SIGTERM)
97
            librarian_process.wait()
98
    atexit.register(stop_librarian)
99
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
100
2603 by Canonical.com Patch Queue Manager
Build daemon task sequencer/serialiser. r=spiv
101
def start_buildsequencer():
102
    # Imported here as path is not set fully on module load
103
    from canonical.config import config
104
    from canonical.pidfile import make_pidfile, pidfile_path
105
106
    # Don't run the sequencer if it wasn't asked for. We only want it
107
    # started up developer boxes and dogfood really, as the production
108
    # sequencer doesn't use this startup script.
109
    
110
    if not config.buildsequencer.launch:
111
        return
112
113
    pidfile = pidfile_path('buildsequencer')
114
    logfile = config.buildsequencer.logfile
115
    tacfile = os.path.abspath(os.path.join(
116
        os.path.dirname(__file__), 'daemons', 'buildd-sequencer.tac'
117
        ))
118
119
    ver = '%d.%d' % sys.version_info[:2]
120
    args = [
121
        "twistd%s" % ver,
122
        "--no_save",
123
        "--nodaemon",
124
        "--python", tacfile,
125
        "--pidfile", pidfile,
126
        "--prefix", "Librarian",
127
        "--logfile", logfile,
128
        ]
129
130
    if config.buildsequencer.spew:
131
        args.append("--spew")
132
133
    # Note that startup tracebacks and evil programmers using 'print'
134
    # will cause output to our stdout. However, we don't want to have
135
    # twisted log to stdout and redirect it ourselves because we then
136
    # lose the ability to cycle the log files by sending a signal to the
137
    # twisted process.
138
    sequencer_process = subprocess.Popen(args, stdin=subprocess.PIPE)
139
    sequencer_process.stdin.close()
140
    # I've left this off - we still check at termination and we can
141
    # avoid the startup delay. -- StuartBishop 20050525
142
    #time.sleep(1)
143
    #if sequencer_process.poll() != None:
144
    #    raise RuntimeError(
145
    #            "Sequencer did not start: %d" % sequencer_process.returncode
146
    #            )
147
    def stop_sequencer():
148
        if sequencer_process.poll() is None:
149
            os.kill(sequencer_process.pid, signal.SIGTERM)
150
            sequencer_process.wait()
151
    atexit.register(stop_sequencer)
152
153
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
154
def run(argv=list(sys.argv)):
155
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
156
    # Sort ZCML overrides for our current config
157
    generate_overrides()
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
158
159
    # setting python paths
160
    program = argv[0]
161
162
    src = 'lib'
163
    here = os.path.dirname(os.path.abspath(program))
1627 by Canonical.com Patch Queue Manager
Initial, very very hacky, zcml-for-scripts support. Also, got rid of vestigal principals.zcml.
164
    srcdir = os.path.join(here, src)
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
165
    sys.path = [srcdir, here] + basepath
166
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
167
    # Import canonical modules here, after path munging
168
    from canonical.pidfile import make_pidfile, pidfile_path
169
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
170
    # We really want to replace this with a generic startup harness.
171
    # However, this should last us until this is developed
172
    start_librarian()
2603 by Canonical.com Patch Queue Manager
Build daemon task sequencer/serialiser. r=spiv
173
    start_buildsequencer()
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
174
175
    # Store our process id somewhere
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
176
    make_pidfile('launchpad')
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
177
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
178
    main(argv[1:])
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
179
        
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
180
181
if __name__ == '__main__':
182
    run()