~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
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
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.
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
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
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
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
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
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
1948 by Canonical.com Patch Queue Manager
[r=stub] Bring trebuchet into the production/staging/development frameworks similar to Librarian
101
def start_trebuchet():
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 Trebuchet if it wasn't asked for.
107
    if not config.trebuchet.server.launch:
108
        return
109
110
    if not os.path.isdir(config.trebuchet.server.root):
111
        os.makedirs(config.trebuchet.server.root, 0700)
112
113
    pidfile = pidfile_path('trebuchet')
114
    logfile = config.trebuchet.server.logfile
115
    tacfile = os.path.abspath(os.path.join(
116
        os.path.dirname(__file__), 'daemons', 'trebuchet.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", "Trebuchet",
127
        "--logfile", logfile,
128
        ]
129
130
    if config.trebuchet.server.spew:
131
        args.append("--spew")
132
133
    trebuchet_process = subprocess.Popen(args, stdin=subprocess.PIPE)
134
    trebuchet_process.stdin.close()
135
    # I've left this off - we still check at termination and we can
136
    # avoid the startup delay. -- StuartBishop 20050525
137
    #time.sleep(1)
138
    #if trebuchet_process.poll() != None:
139
    #    raise RuntimeError(
140
    #            "Trebuchet did not start: %d" % trebuchet_process.returncode
141
    #            )
142
    def stop_trebuchet():
143
        if trebuchet_process.poll() is None:
144
            os.kill(trebuchet_process.pid, signal.SIGTERM)
145
            trebuchet_process.wait()
146
        else:
147
            print >> sys.stderr, "*** ERROR: Trebuchet died prematurely!"
148
            print >> sys.stderr, "***        Return code was %d" % (
149
                    trebuchet_process.returncode,
150
                    )
151
    atexit.register(stop_trebuchet)
152
153
2603 by Canonical.com Patch Queue Manager
Build daemon task sequencer/serialiser. r=spiv
154
def start_buildsequencer():
155
    # Imported here as path is not set fully on module load
156
    from canonical.config import config
157
    from canonical.pidfile import make_pidfile, pidfile_path
158
159
    # Don't run the sequencer if it wasn't asked for. We only want it
160
    # started up developer boxes and dogfood really, as the production
161
    # sequencer doesn't use this startup script.
162
    
163
    if not config.buildsequencer.launch:
164
        return
165
166
    pidfile = pidfile_path('buildsequencer')
167
    logfile = config.buildsequencer.logfile
168
    tacfile = os.path.abspath(os.path.join(
169
        os.path.dirname(__file__), 'daemons', 'buildd-sequencer.tac'
170
        ))
171
172
    ver = '%d.%d' % sys.version_info[:2]
173
    args = [
174
        "twistd%s" % ver,
175
        "--no_save",
176
        "--nodaemon",
177
        "--python", tacfile,
178
        "--pidfile", pidfile,
179
        "--prefix", "Librarian",
180
        "--logfile", logfile,
181
        ]
182
183
    if config.buildsequencer.spew:
184
        args.append("--spew")
185
186
    # Note that startup tracebacks and evil programmers using 'print'
187
    # will cause output to our stdout. However, we don't want to have
188
    # twisted log to stdout and redirect it ourselves because we then
189
    # lose the ability to cycle the log files by sending a signal to the
190
    # twisted process.
191
    sequencer_process = subprocess.Popen(args, stdin=subprocess.PIPE)
192
    sequencer_process.stdin.close()
193
    # I've left this off - we still check at termination and we can
194
    # avoid the startup delay. -- StuartBishop 20050525
195
    #time.sleep(1)
196
    #if sequencer_process.poll() != None:
197
    #    raise RuntimeError(
198
    #            "Sequencer did not start: %d" % sequencer_process.returncode
199
    #            )
200
    def stop_sequencer():
201
        if sequencer_process.poll() is None:
202
            os.kill(sequencer_process.pid, signal.SIGTERM)
203
            sequencer_process.wait()
204
    atexit.register(stop_sequencer)
205
206
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
207
def run(argv=list(sys.argv)):
208
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
209
    # Sort ZCML overrides for our current config
210
    generate_overrides()
211
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
212
    # setting python paths
213
    program = argv[0]
214
215
    src = 'lib'
216
    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.
217
    srcdir = os.path.join(here, src)
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
218
    sys.path = [srcdir, here] + basepath
219
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
220
    # Import canonical modules here, after path munging
221
    from canonical.pidfile import make_pidfile, pidfile_path
222
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
223
    # We really want to replace this with a generic startup harness.
224
    # However, this should last us until this is developed
225
    start_librarian()
1948 by Canonical.com Patch Queue Manager
[r=stub] Bring trebuchet into the production/staging/development frameworks similar to Librarian
226
    start_trebuchet()
2603 by Canonical.com Patch Queue Manager
Build daemon task sequencer/serialiser. r=spiv
227
    start_buildsequencer()
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
228
229
    # Store our process id somewhere
1937 by Canonical.com Patch Queue Manager
[r=jamesh] Common PID file handing module
230
    make_pidfile('launchpad')
1831 by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging
231
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
232
    main(argv[1:])
1784 by Canonical.com Patch Queue Manager
[r=spiv] Run Librarian on launchpad startup if requested
233
        
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
234
235
if __name__ == '__main__':
236
    run()