runzeo.py 12.3 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2001, 2002, 2003 Zope Corporation and Contributors.
4 5 6
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
Jim Fulton's avatar
Jim Fulton committed
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8 9 10 11 12 13 14 15
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Start the ZEO storage server.

16
Usage: %s [-C URL] [-a ADDRESS] [-f FILENAME] [-h]
17 18

Options:
19
-C/--configuration URL -- configuration file or URL
20 21 22
-a/--address ADDRESS -- server address of the form PORT, HOST:PORT, or PATH
                        (a PATH must contain at least one "/")
-f/--filename FILENAME -- filename for FileStorage
Tim Peters's avatar
Tim Peters committed
23
-t/--timeout TIMEOUT -- transaction timeout in seconds (default no timeout)
24
-h/--help -- print this usage message and exit
25
-m/--monitor ADDRESS -- address of monitor server ([HOST:]PORT or PATH)
Tim Peters's avatar
Tim Peters committed
26 27 28
--pid-file PATH -- relative path to output file containing this process's pid;
                   default $(INSTANCE_HOME)/var/ZEO.pid but only if envar
                   INSTANCE_HOME is defined
29

30
Unless -C is specified, -a and -f are required.
31 32 33 34 35 36 37 38 39 40
"""

# The code here is designed to be reused by other, similar servers.
# For the forseeable future, it must work under Python 2.1 as well as
# 2.2 and above.

import os
import sys
import signal
import socket
41
import logging
42

43
import ZConfig, ZConfig.datatypes
44
import ZEO
45
from zdaemon.zdoptions import ZDOptions
46

47 48 49 50 51 52 53 54
logger = logging.getLogger('ZEO.runzeo')
_pid = str(os.getpid())

def log(msg, level=logging.INFO, exc_info=False):
    """Internal: generic logging function."""
    message = "(%s) %s" % (_pid, msg)
    logger.log(level, message, exc_info=exc_info)

Tim Peters's avatar
Tim Peters committed
55
def parse_binding_address(arg):
Tim Peters's avatar
Tim Peters committed
56
    # Caution:  Not part of the official ZConfig API.
Tim Peters's avatar
Tim Peters committed
57
    obj = ZConfig.datatypes.SocketBindingAddress(arg)
58 59
    return obj.family, obj.address

Tim Peters's avatar
Tim Peters committed
60 61 62 63
def windows_shutdown_handler():
    # Called by the signal mechanism on Windows to perform shutdown.
    import asyncore
    asyncore.close_all()
64

65
class ZEOOptionsMixin:
66 67 68 69

    storages = None

    def handle_address(self, arg):
Tim Peters's avatar
Tim Peters committed
70
        self.family, self.address = parse_binding_address(arg)
71 72

    def handle_monitor_address(self, arg):
Tim Peters's avatar
Tim Peters committed
73
        self.monitor_family, self.monitor_address = parse_binding_address(arg)
74 75 76 77 78 79 80 81 82 83 84 85 86

    def handle_filename(self, arg):
        from ZODB.config import FileStorage # That's a FileStorage *opener*!
        class FSConfig:
            def __init__(self, name, path):
                self._name = name
                self.path = path
                self.create = 0
                self.read_only = 0
                self.stop = None
                self.quota = None
            def getSectionName(self):
                return self._name
87
        if not self.storages:
88 89 90 91 92
            self.storages = []
        name = str(1 + len(self.storages))
        conf = FileStorage(FSConfig(name, arg))
        self.storages.append(conf)

93
    def add_zeo_options(self):
94 95 96 97 98 99 100 101
        self.add(None, None, "a:", "address=", self.handle_address)
        self.add(None, None, "f:", "filename=", self.handle_filename)
        self.add("family", "zeo.address.family")
        self.add("address", "zeo.address.address",
                 required="no server address specified; use -a or -C")
        self.add("read_only", "zeo.read_only", default=0)
        self.add("invalidation_queue_size", "zeo.invalidation_queue_size",
                 default=100)
102 103
        self.add("transaction_timeout", "zeo.transaction_timeout",
                 "t:", "timeout=", float)
104 105
        self.add("monitor_address", "zeo.monitor_address.address",
                 "m:", "monitor=", self.handle_monitor_address)
106 107 108 109 110 111
        self.add('auth_protocol', 'zeo.authentication_protocol',
                 None, 'auth-protocol=', default=None)
        self.add('auth_database', 'zeo.authentication_database',
                 None, 'auth-database=')
        self.add('auth_realm', 'zeo.authentication_realm',
                 None, 'auth-realm=')
Tim Peters's avatar
Tim Peters committed
112 113
        self.add('pid_file', 'zeo.pid_filename',
                 None, 'pid-file=')
114 115 116

class ZEOOptions(ZDOptions, ZEOOptionsMixin):

117
    logsectionname = "eventlog"
118
    schemadir = os.path.dirname(ZEO.__file__)
119

120 121 122 123 124 125
    def __init__(self):
        ZDOptions.__init__(self)
        self.add_zeo_options()
        self.add("storages", "storages",
                 required="no storages specified; use -f or -C")

126

Guido van Rossum's avatar
Guido van Rossum committed
127
class ZEOServer:
128

129
    def __init__(self, options):
Guido van Rossum's avatar
Guido van Rossum committed
130
        self.options = options
131 132

    def main(self):
133
        self.setup_default_logging()
134 135
        self.check_socket()
        self.clear_socket()
Tim Peters's avatar
Tim Peters committed
136
        self.make_pidfile()
137
        try:
138 139
            self.open_storages()
            self.setup_signals()
140 141 142
            self.create_server()
            self.loop_forever()
        finally:
143
            self.close_storages()
144
            self.clear_socket()
Tim Peters's avatar
Tim Peters committed
145
            self.remove_pidfile()
146

147 148 149
    def setup_default_logging(self):
        if self.options.config_logger is not None:
            return
150
        # No log file is configured; default to stderr.
Tim Peters's avatar
Tim Peters committed
151 152 153 154 155
        root = logging.getLogger()
        root.setLevel(logging.INFO)
        fmt = logging.Formatter(
            "------\n%(asctime)s %(levelname)s %(name)s %(message)s",
            "%Y-%m-%dT%H:%M:%S")
156
        handler = logging.StreamHandler()
Tim Peters's avatar
Tim Peters committed
157 158
        handler.setFormatter(fmt)
        root.addHandler(handler)
159

160
    def check_socket(self):
Guido van Rossum's avatar
Guido van Rossum committed
161 162 163
        if self.can_connect(self.options.family, self.options.address):
            self.options.usage("address %s already in use" %
                               repr(self.options.address))
164 165 166

    def can_connect(self, family, address):
        s = socket.socket(family, socket.SOCK_STREAM)
167
        try:
168
            s.connect(address)
169
        except socket.error:
170
            return 0
171 172
        else:
            s.close()
173
            return 1
174 175

    def clear_socket(self):
Guido van Rossum's avatar
Guido van Rossum committed
176
        if isinstance(self.options.address, type("")):
177
            try:
Guido van Rossum's avatar
Guido van Rossum committed
178
                os.unlink(self.options.address)
179 180 181 182 183
            except os.error:
                pass

    def open_storages(self):
        self.storages = {}
184
        for opener in self.options.storages:
185 186
            log("opening storage %r using %s"
                % (opener.name, opener.__class__.__name__))
187
            self.storages[opener.name] = opener.open()
188 189 190 191 192 193 194 195 196 197

    def setup_signals(self):
        """Set up signal handlers.

        The signal handler for SIGFOO is a method handle_sigfoo().
        If no handler method is defined for a signal, the signal
        action is not changed from its initial value.  The handler
        method is called without additional arguments.
        """
        if os.name != "posix":
Tim Peters's avatar
Tim Peters committed
198 199
            if os.name == "nt":
                self.setup_win32_signals()
200 201 202 203 204 205 206 207 208 209 210
            return
        if hasattr(signal, 'SIGXFSZ'):
            signal.signal(signal.SIGXFSZ, signal.SIG_IGN) # Special case
        init_signames()
        for sig, name in signames.items():
            method = getattr(self, "handle_" + name.lower(), None)
            if method is not None:
                def wrapper(sig_dummy, frame_dummy, method=method):
                    method()
                signal.signal(sig, wrapper)

Tim Peters's avatar
Tim Peters committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    def setup_win32_signals(self):
        # Borrow the Zope Signals package win32 support, if available.
        # Signals does a check/log for the availability of pywin32.
        try:
            import Signals.Signals
        except ImportError:
            logger.debug("Signals package not found. "
                         "Windows-specific signal handler "
                         "will *not* be installed.")
            return
        SignalHandler = Signals.Signals.SignalHandler
        if SignalHandler is not None: # may be None if no pywin32.
            SignalHandler.registerHandler(signal.SIGTERM,
                                          windows_shutdown_handler)
            SignalHandler.registerHandler(signal.SIGINT,
                                          windows_shutdown_handler)
            SIGUSR2 = 12 # not in signal module on Windows.
Tim Peters's avatar
Tim Peters committed
228
            SignalHandler.registerHandler(SIGUSR2, self.handle_sigusr2)
Tim Peters's avatar
Tim Peters committed
229

230 231
    def create_server(self):
        from ZEO.StorageServer import StorageServer
232 233 234 235 236
        self.server = StorageServer(
            self.options.address,
            self.storages,
            read_only=self.options.read_only,
            invalidation_queue_size=self.options.invalidation_queue_size,
237
            transaction_timeout=self.options.transaction_timeout,
238 239
            monitor_address=self.options.monitor_address,
            auth_protocol=self.options.auth_protocol,
Tim Peters's avatar
Tim Peters committed
240
            auth_database=self.options.auth_database,
241
            auth_realm=self.options.auth_realm)
242 243

    def loop_forever(self):
244 245
        import ThreadedAsync.LoopCallback
        ThreadedAsync.LoopCallback.loop()
246 247

    def handle_sigterm(self):
248
        log("terminated by SIGTERM")
249 250 251
        sys.exit(0)

    def handle_sigint(self):
252
        log("terminated by SIGINT")
253 254
        sys.exit(0)

255
    def handle_sighup(self):
256
        log("restarted by SIGHUP")
257 258
        sys.exit(1)

259
    def handle_sigusr2(self):
Tim Peters's avatar
Tim Peters committed
260 261 262
        # TODO: this used to reinitialize zLOG. How do I achieve
        # the same effect with Python's logging package?
        # Should we restart as with SIGHUP?
263
        log("received SIGUSR2, but it was not handled!", level=logging.WARNING)
264 265 266

    def close_storages(self):
        for name, storage in self.storages.items():
267
            log("closing storage %r" % name)
268 269 270
            try:
                storage.close()
            except: # Keep going
271
                log("failed to close storage %r" % name,
Tim Peters's avatar
Tim Peters committed
272
                    level=logging.ERROR, exc_info=True)
273

Tim Peters's avatar
Tim Peters committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    def _get_pidfile(self):
        pidfile = self.options.pid_file
        # 'pidfile' is marked as not required.
        if not pidfile:
            # Try to find a reasonable location if the pidfile is not
            # set. If we are running in a Zope environment, we can
            # safely assume INSTANCE_HOME.
            instance_home = os.environ.get("INSTANCE_HOME")
            if not instance_home:
                # If all our attempts failed, just log a message and
                # proceed.
                logger.debug("'pidfile' option not set, and 'INSTANCE_HOME' "
                             "environment variable could not be found. "
                             "Cannot guess pidfile location.")
                return
            self.options.pid_file = os.path.join(instance_home,
                                                 "var", "ZEO.pid")

    def make_pidfile(self):
        if not self.options.read_only:
            self._get_pidfile()
            pidfile = self.options.pid_file
            if pidfile is None:
                return
            pid = os.getpid()
            try:
                if os.path.exists(pidfile):
                    os.unlink(pidfile)
                f = open(pidfile, 'w')
                print >> f, pid
                f.close()
                log("created PID file '%s'" % pidfile)
            except IOError:
                logger.error("PID file '%s' cannot be opened" % pidfile)

    def remove_pidfile(self):
        if not self.options.read_only:
            pidfile = self.options.pid_file
            if pidfile is None:
                return
            try:
                if os.path.exists(pidfile):
                    os.unlink(pidfile)
                    log("removed PID file '%s'" % pidfile)
            except IOError:
                logger.error("PID file '%s' could not be removed" % pidfile)
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

# Signal names

signames = None

def signame(sig):
    """Return a symbolic name for a signal.

    Return "signal NNN" if there is no corresponding SIG name in the
    signal module.
    """

    if signames is None:
        init_signames()
    return signames.get(sig) or "signal %d" % sig

def init_signames():
    global signames
    signames = {}
    for name, sig in signal.__dict__.items():
        k_startswith = getattr(name, "startswith", None)
        if k_startswith is None:
            continue
        if k_startswith("SIG") and not k_startswith("SIG_"):
            signames[sig] = name


# Main program

def main(args=None):
350 351
    options = ZEOOptions()
    options.realize(args)
Guido van Rossum's avatar
Guido van Rossum committed
352
    s = ZEOServer(options)
353
    s.main()
354 355 356

if __name__ == "__main__":
    main()