depot.py revision 1191
1516N/A#!/usr/bin/python2.4
290N/A#
290N/A# CDDL HEADER START
290N/A#
290N/A# The contents of this file are subject to the terms of the
290N/A# Common Development and Distribution License (the "License").
290N/A# You may not use this file except in compliance with the License.
290N/A#
290N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
290N/A# or http://www.opensolaris.org/os/licensing.
290N/A# See the License for the specific language governing permissions
290N/A# and limitations under the License.
290N/A#
290N/A# When distributing Covered Code, include this CDDL HEADER in each
290N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
290N/A# If applicable, add the following below this CDDL HEADER, with the
290N/A# fields enclosed by brackets "[]" replaced with your own identifying
290N/A# information: Portions Copyright [yyyy] [name of copyright owner]
290N/A#
290N/A# CDDL HEADER END
290N/A#
2852N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
395N/A# Use is subject to license terms.
290N/A#
883N/A
454N/A# pkg.depotd - package repository daemon
290N/A
448N/A# XXX The prototype pkg.depotd combines both the version management server that
290N/A# answers to pkgsend(1) sessions and the HTTP file server that answers to the
290N/A# various GET operations that a pkg(1) client makes. This split is expected to
290N/A# be made more explicit, by constraining the pkg(1) operations such that they
383N/A# can be served as a typical HTTP/HTTPS session. Thus, pkg.depotd will reduce
290N/A# to a special purpose HTTP/HTTPS server explicitly for the version management
395N/A# operations, and must manipulate the various state files--catalogs, in
290N/A# particular--such that the pkg(1) pull client can operately accurately with
395N/A# only a basic HTTP/HTTPS server in place.
849N/A
1516N/A# XXX We should support simple "last-modified" operations via HEAD queries.
2508N/A
2826N/A# XXX Although we pushed the evaluation of next-version, etc. to the pull
290N/A# client, we should probably provide a query API to do same on the server, for
2535N/A# dumb clients (like a notification service).
2698N/A
290N/A# The default authority for the depot.
290N/AAUTH_DEFAULT = "opensolaris.org"
2535N/A# The default repository path.
2561N/AREPO_PATH_DEFAULT = "/var/pkg/repo"
290N/A# The default path for static and other web content.
2508N/ACONTENT_PATH_DEFAULT = "/usr/share/lib/pkg"
383N/A# cherrypy has a max_request_body_size parameter that determines whether the
290N/A# server should abort requests with REQUEST_ENTITY_TOO_LARGE when the request
290N/A# body is larger than the specified size (in bytes). The maximum size supported
2339N/A# by cherrypy is 2048 * 1024 * 1024 - 1 (just short of 2048MB), but the default
2535N/A# here is purposefully conservative.
290N/AMAX_REQUEST_BODY_SIZE = 128 * 1024 * 1024
290N/A# The default port(s) to serve data from.
2535N/APORT_DEFAULT = 80
2535N/ASSL_PORT_DEFAULT = 443
290N/A# The minimum number of threads allowed.
290N/ATHREADS_MIN = 1
2508N/A# The default number of threads to start.
2508N/ATHREADS_DEFAULT = 10
290N/A# The maximum number of threads that can be started.
1660N/ATHREADS_MAX = 100
1660N/A# The default server socket timeout in seconds. We want this to be longer than
1660N/A# the normal default of 10 seconds to accommodate clients with poor quality
1660N/A# connections.
1660N/ASOCKET_TIMEOUT_DEFAULT = 60
1660N/A# Whether modify operations should be allowed.
1660N/AREADONLY_DEFAULT = False
1660N/A# Whether the repository catalog should be rebuilt on startup.
1660N/AREBUILD_DEFAULT = False
1660N/A# Whether the indexes should be rebuilt
1660N/AREINDEX_DEFAULT = False
1660N/A# Not in mirror mode by default
1660N/AMIRROR_DEFAULT = False
1660N/A
1660N/Aimport getopt
1660N/Aimport gettext
1660N/Aimport locale
1660N/Aimport logging
448N/Aimport os
448N/Aimport os.path
2828N/Aimport OpenSSL.crypto as crypto
2828N/Aimport subprocess
2828N/Aimport sys
534N/Aimport tempfile
534N/Aimport urlparse
534N/A
534N/Atry:
534N/A import cherrypy
534N/A version = cherrypy.__version__.split('.')
534N/A if map(int, version) < [3, 1, 0]:
290N/A raise ImportError
290N/A elif map(int, version) >= [3, 2, 0]:
954N/A raise ImportError
954N/Aexcept ImportError:
954N/A print >> sys.stderr, """cherrypy 3.1.0 or greater (but less than """ \
954N/A """3.2.0) is required to use this program."""
534N/A sys.exit(2)
1099N/A
290N/Aimport pkg.catalog as catalog
1516N/Afrom pkg.misc import port_available, msg, emsg, setlocale
290N/Aimport pkg.portable.util as os_util
290N/Aimport pkg.search_errors as search_errors
290N/Aimport pkg.server.config as config
661N/Aimport pkg.server.depot as depot
2867N/Aimport pkg.server.depotresponse as dr
290N/Aimport pkg.server.errors as errors
2494N/Aimport pkg.server.repositoryconfig as rc
2494N/A
2494N/Aclass LogSink(object):
2516N/A """This is a dummy object that we can use to discard log entries
2516N/A without relying on non-portable interfaces such as /dev/null."""
2516N/A
2516N/A def write(self, *args, **kwargs):
2516N/A """Discard the bits."""
2516N/A pass
2516N/A
290N/A def flush(self, *args, **kwargs):
2523N/A """Discard the bits."""
2390N/A pass
1498N/A
1498N/Adef usage(text):
2867N/A if text:
2310N/A emsg(text)
2310N/A
2310N/A print """\
2852N/AUsage: /usr/lib/pkg.depotd [-d repo_dir] [-p port] [-s threads]
2852N/A [-t socket_timeout] [--cfg-file] [--content-root] [--debug]
2852N/A [--log-access dest] [--log-errors dest] [--mirror] [--nasty]
2852N/A [--proxy-base url] [--readonly] [--rebuild] [--ssl-cert-file]
2535N/A [--ssl-dialog] [--ssl-key-file] [--writable-root dir]
2535N/A
2535N/A --cfg-file The pathname of the file from which to read and to
2535N/A write configuration information.
2535N/A --content-root The file system path to the directory containing the
2535N/A the static and other web content used by the depot's
2535N/A browser user interface. The default value is
2535N/A '/usr/share/lib/pkg'.
2535N/A --debug The name of a debug feature to enable; or a whitespace
2535N/A or comma separated list of features to enable. Possible
2867N/A values are: headers.
2867N/A --log-access The destination for any access related information
2310N/A logged by the depot process. Possible values are:
290N/A stderr, stdout, none, or an absolute pathname. The
1674N/A default value is stdout if stdout is a tty; otherwise
1674N/A the default value is none.
2262N/A --log-errors The destination for any errors or other information
1674N/A logged by the depot process. Possible values are:
395N/A stderr, stdout, none, or an absolute pathname. The
430N/A default value is stderr.
395N/A --mirror Package mirror mode; publishing and metadata operations
1544N/A disallowed. Cannot be used with --readonly or
1968N/A --rebuild.
1557N/A --nasty Instruct the server to misbehave. At random intervals
1903N/A it will time-out, send bad responses, hang up on
2046N/A clients, and generally be hostile. The option
2240N/A takes a value (1 to 100) for how nasty the server
1506N/A should be.
2928N/A --proxy-base The url to use as the base for generating internal
395N/A redirects and content.
395N/A --readonly Read-only operation; modifying operations disallowed.
2026N/A Cannot be used with --mirror or --rebuild.
424N/A --rebuild Re-build the catalog from pkgs in depot. Cannot be
1024N/A used with --mirror or --readonly.
395N/A --ssl-cert-file The absolute pathname to a PEM-encoded Certificate file.
395N/A This option must be used with --ssl-key-file. Usage of
395N/A this option will cause the depot to only respond to SSL
2078N/A requests on the provided port.
578N/A --ssl-dialog Specifies what method should be used to obtain the
1172N/A passphrase needed to decrypt the file specified by
2310N/A --ssl-key-file. Supported values are: builtin,
2852N/A exec:/path/to/program, or smf:fmri. The default value
395N/A is builtin.
2535N/A --ssl-key-file The absolute pathname to a PEM-encoded Private Key file.
2535N/A This option must be used with --ssl-cert-file. Usage of
2535N/A this option will cause the depot to only respond to SSL
661N/A requests on the provided port.
2867N/A --writable-root The path to a directory to which the program has write
2867N/A access. Used with --readonly to allow server to
2867N/A create needed files, such as search indices, without
2867N/A needing write access to the package information.
2867N/A"""
2852N/A sys.exit(2)
2310N/A
2867N/Aclass OptionError(Exception):
2867N/A """Option exception. """
2867N/A
2867N/A def __init__(self, *args):
661N/A Exception.__init__(self, *args)
395N/A
849N/Aif __name__ == "__main__":
290N/A
395N/A setlocale(locale.LC_ALL, "")
395N/A gettext.install("pkg", "/usr/share/locale")
1968N/A
395N/A debug_features = {
395N/A "headers": False,
395N/A }
395N/A port = PORT_DEFAULT
395N/A port_provided = False
395N/A threads = THREADS_DEFAULT
395N/A socket_timeout = SOCKET_TIMEOUT_DEFAULT
395N/A readonly = READONLY_DEFAULT
395N/A rebuild = REBUILD_DEFAULT
395N/A reindex = REINDEX_DEFAULT
395N/A proxy_base = None
290N/A mirror = MIRROR_DEFAULT
290N/A nasty = False
395N/A nasty_value = 0
395N/A repo_config_file = None
1231N/A ssl_cert_file = None
1557N/A ssl_key_file = None
1903N/A ssl_dialog = "builtin"
1557N/A writable_root = None
395N/A
395N/A if "PKG_REPO" in os.environ:
395N/A repo_path = os.environ["PKG_REPO"]
395N/A else:
395N/A repo_path = REPO_PATH_DEFAULT
395N/A
395N/A try:
395N/A content_root = os.environ["PKG_DEPOT_CONTENT"]
395N/A except KeyError:
395N/A try:
395N/A content_root = os.path.join(os.environ['PKG_HOME'],
290N/A 'share/lib/pkg')
290N/A except KeyError:
430N/A content_root = CONTENT_PATH_DEFAULT
395N/A
395N/A # By default, if the destination for a particular log type is not
395N/A # specified, this is where we will send the output.
395N/A log_routes = {
1302N/A "access": "none",
395N/A "errors": "stderr"
395N/A }
290N/A log_opts = ["--log-%s" % log_type for log_type in log_routes]
395N/A
1024N/A # If stdout is a tty, then send access output there by default instead
413N/A # of discarding it.
1544N/A if os.isatty(sys.stdout.fileno()):
1557N/A log_routes["access"] = "stdout"
1903N/A
2046N/A opt = None
2240N/A try:
1506N/A long_opts = ["cfg-file=", "content-root=", "debug=", "mirror",
413N/A "nasty=", "proxy-base=", "readonly", "rebuild",
2026N/A "refresh-index", "ssl-cert-file=", "ssl-dialog=",
2928N/A "ssl-key-file=", "writable-root="]
413N/A for opt in log_opts:
1978N/A long_opts.append("%s=" % opt.lstrip('--'))
1024N/A opts, pargs = getopt.getopt(sys.argv[1:], "d:np:s:t:",
395N/A long_opts)
395N/A for opt, arg in opts:
2310N/A if opt == "-n":
2852N/A sys.exit(0)
2310N/A elif opt == "-d":
395N/A repo_path = arg
395N/A elif opt == "-p":
413N/A port = int(arg)
395N/A port_provided = True
2516N/A elif opt == "-s":
2516N/A threads = int(arg)
2516N/A if threads < THREADS_MIN:
2516N/A raise OptionError, \
2516N/A "minimum value is %d" % THREADS_MIN
2516N/A if threads > THREADS_MAX:
2516N/A raise OptionError, \
2516N/A "maximum value is %d" % THREADS_MAX
2516N/A elif opt == "-t":
2516N/A socket_timeout = int(arg)
2516N/A elif opt == "--cfg-file":
2516N/A repo_config_file = os.path.abspath(arg)
2516N/A elif opt == "--content-root":
2516N/A if arg == "":
2516N/A raise OptionError, "You must specify " \
2516N/A "a directory path."
2516N/A content_root = arg
2516N/A elif opt == "--debug":
2516N/A if arg is None or arg == "":
2516N/A raise OptionError, \
2516N/A "A debug feature must be specified."
2516N/A
2516N/A # A list of features can be specified using a
2516N/A # "," or any whitespace character as separators.
2516N/A if "," in arg:
2516N/A features = arg.split(",")
2516N/A else:
2516N/A features = arg.split()
2516N/A
2516N/A for f in features:
2516N/A if f not in debug_features:
2516N/A raise OptionError, \
2516N/A "Invalid debug feature: " \
2516N/A "%s." % f
2516N/A debug_features[f] = True
2516N/A elif opt in log_opts:
2516N/A if arg is None or arg == "":
2516N/A raise OptionError, \
2516N/A "You must specify a log " \
2516N/A "destination."
2516N/A log_routes[opt.lstrip("--log-")] = arg
2516N/A elif opt == "--mirror":
2516N/A mirror = True
2516N/A elif opt == "--nasty":
2516N/A value_err = None
2516N/A try:
2516N/A nasty_value = int(arg)
395N/A except ValueError, e:
395N/A value_err = e
395N/A
395N/A if value_err or (nasty_value > 100 or
395N/A nasty_value < 1):
2339N/A raise OptionError, "Invalid value " \
1191N/A "for nasty option.\n Please " \
1452N/A "choose a value between 1 and 100."
1231N/A nasty = True
2535N/A elif opt == "--proxy-base":
2046N/A # Attempt to decompose the url provided into
395N/A # its base parts. This is done so we can
395N/A # remove any scheme information since we
424N/A # don't need it.
395N/A scheme, netloc, path, params, query, \
742N/A fragment = urlparse.urlparse(arg,
2339N/A "http", allow_fragments=0)
2339N/A
2693N/A if not netloc:
2690N/A raise OptionError, "Unable to " \
2339N/A "determine the hostname from " \
2339N/A "the provided URL; please use a " \
2690N/A "fully qualified URL."
2690N/A
2693N/A scheme = scheme.lower()
2693N/A if scheme not in ("http", "https"):
2690N/A raise OptionError, "Invalid URL; http " \
2690N/A "and https are the only supported " \
2744N/A "schemes."
2339N/A
2339N/A # Rebuild the url with the sanitized components.
742N/A proxy_base = urlparse.urlunparse((scheme,
742N/A netloc, path, params, query, fragment))
742N/A elif opt == "--readonly":
742N/A readonly = True
742N/A elif opt == "--rebuild":
742N/A rebuild = True
742N/A elif opt == "--refresh-index":
742N/A # Note: This argument is for internal use
742N/A # only. It's used when pkg.depotd is reexecing
2688N/A # itself and needs to know that's the case.
2688N/A # This flag is purposefully omitted in usage.
2688N/A # The supported way to forcefully reindex is to
2688N/A # kill any pkg.depot using that directory,
2688N/A # remove the index directory, and restart the
2688N/A # pkg.depot process. The index will be rebuilt
2688N/A # automatically on startup.
2688N/A reindex = True
2688N/A elif opt == "--ssl-cert-file":
2688N/A if arg == "none":
742N/A continue
2310N/A
2852N/A ssl_cert_file = arg
1902N/A if not os.path.isabs(ssl_cert_file):
2867N/A raise OptionError, "The path to " \
2867N/A "the Certificate file must be " \
1099N/A "absolute."
2867N/A elif not os.path.exists(ssl_cert_file):
2390N/A raise OptionError, "The specified " \
2338N/A "file does not exist."
2338N/A elif not os.path.isfile(ssl_cert_file):
2310N/A raise OptionError, "The specified " \
2046N/A "pathname is not a file."
2223N/A elif opt == "--ssl-key-file":
2046N/A if arg == "none":
2046N/A continue
2523N/A
2523N/A ssl_key_file = arg
2523N/A if not os.path.isabs(ssl_key_file):
2523N/A raise OptionError, "The path to " \
2523N/A "the Private Key file must be " \
2523N/A "absolute."
2310N/A elif not os.path.exists(ssl_key_file):
2677N/A raise OptionError, "The specified " \
2310N/A "file does not exist."
2310N/A elif not os.path.isfile(ssl_key_file):
2310N/A raise OptionError, "The specified " \
2310N/A "pathname is not a file."
2310N/A elif opt == "--ssl-dialog":
2310N/A if arg != "builtin" and not \
2858N/A arg.startswith("exec:/") and not \
2310N/A arg.startswith("smf:"):
2852N/A raise OptionError, "Invalid value " \
2852N/A "specified. Expected: builtin, " \
2852N/A "exec:/path/to/program, or " \
2852N/A "smf:fmri."
2852N/A
2852N/A f = arg
2852N/A if f.startswith("exec:"):
2852N/A if os_util.get_canonical_os_type() != \
2858N/A "unix":
2852N/A # Don't allow a somewhat
2852N/A # insecure authentication method
2852N/A # on some platforms.
2852N/A raise OptionError, "exec is " \
2508N/A "not a supported dialog " \
2508N/A "type for this operating " \
2508N/A "system."
2508N/A
2508N/A f = os.path.abspath(f.split(
2867N/A "exec:")[1])
2535N/A
2535N/A if not os.path.isfile(f):
2535N/A raise OptionError, "Invalid " \
2535N/A "file path specified for " \
2535N/A "exec."
2535N/A
2535N/A f = "exec:%s" % f
2535N/A
2535N/A ssl_dialog = f
2535N/A elif opt == "--writable-root":
2535N/A if arg == "":
2535N/A raise OptionError, "You must specify " \
2535N/A "a directory path."
2535N/A writable_root = arg
2535N/A except getopt.GetoptError, _e:
2535N/A usage("pkg.depotd: %s" % _e.msg)
2535N/A except OptionError, _e:
2535N/A usage("pkg.depotd: option: %s -- %s" % (opt, _e))
2535N/A except (ArithmeticError, ValueError):
2535N/A usage("pkg.depotd: illegal option value: %s specified " \
2535N/A "for option: %s" % (arg, opt))
2535N/A
2535N/A if rebuild and reindex:
2535N/A usage("--refresh-index cannot be used with --rebuild")
2535N/A if rebuild and (readonly or mirror):
2535N/A usage("--readonly and --mirror cannot be used with --rebuild")
2535N/A if reindex and mirror:
2535N/A usage("--mirror cannot be used with --refresh-index")
2535N/A if reindex and readonly and not writable_root:
2535N/A usage("--readonly can only be used with --refresh-index if "
2535N/A "--writable-root is used")
2535N/A
2535N/A if (ssl_cert_file and not ssl_key_file) or (ssl_key_file and not
2535N/A ssl_cert_file):
2688N/A usage("The --ssl-cert-file and --ssl-key-file options must "
2535N/A "must both be provided when using either option.")
2535N/A elif ssl_cert_file and ssl_key_file and not port_provided:
2688N/A # If they didn't already specify a particular port, use the
2535N/A # default SSL port instead.
2535N/A port = SSL_PORT_DEFAULT
2535N/A
2535N/A # If the program is going to reindex, the port is irrelevant since
2535N/A # the program will not bind to a port.
2535N/A if not reindex:
2535N/A available, msg = port_available(None, port)
2535N/A if not available:
2535N/A print "pkg.depotd: unable to bind to the specified " \
2535N/A "port: %d. Reason: %s" % (port, msg)
2535N/A sys.exit(1)
2535N/A else:
2535N/A # Not applicable for reindexing operations.
2535N/A content_root = None
2535N/A
2535N/A fork_allowed = not reindex
2535N/A
2535N/A if nasty:
2535N/A scfg = config.NastySvrConfig(repo_path, content_root,
2535N/A AUTH_DEFAULT, auto_create=not readonly,
2535N/A fork_allowed=fork_allowed, writable_root=writable_root)
2339N/A scfg.set_nasty(nasty_value)
2339N/A else:
2339N/A scfg = config.SvrConfig(repo_path, content_root, AUTH_DEFAULT,
691N/A auto_create=not readonly, fork_allowed=fork_allowed,
691N/A writable_root=writable_root)
691N/A
395N/A if readonly:
395N/A scfg.set_read_only()
395N/A
395N/A if mirror:
395N/A scfg.set_mirror()
290N/A
395N/A
395N/A try:
591N/A scfg.init_dirs()
591N/A except (errors.SvrConfigError, EnvironmentError), _e:
591N/A print "pkg.depotd: an error occurred while trying to " \
2639N/A "initialize the depot repository directory " \
2639N/A "structures:\n%s" % _e
2639N/A sys.exit(1)
2639N/A
2639N/A key_data = None
2639N/A if not reindex and ssl_cert_file and ssl_key_file and \
1505N/A ssl_dialog != "builtin":
2516N/A cmdline = None
1505N/A def get_ssl_passphrase(*ignored):
1505N/A p = None
1632N/A try:
1632N/A p = subprocess.Popen(cmdline, shell=True,
1632N/A stdout=subprocess.PIPE,
1632N/A stderr=None)
2339N/A p.wait()
2339N/A except Exception, __e:
2339N/A print "pkg.depotd: an error occurred while " \
2339N/A "executing [%s]; unable to obtain the " \
2339N/A "passphrase needed to decrypt the SSL" \
2339N/A "private key file: %s" % (cmdline, __e)
2339N/A sys.exit(1)
2339N/A return p.stdout.read().strip("\n")
2339N/A
2339N/A if ssl_dialog.startswith("exec:"):
2339N/A cmdline = "%s %s %d" % (ssl_dialog.split("exec:")[1],
2339N/A "''", port)
2339N/A elif ssl_dialog.startswith("smf:"):
2339N/A cmdline = "/usr/bin/svcprop -p " \
2339N/A "pkg_secure/ssl_key_passphrase %s" % (
2339N/A ssl_dialog.split("smf:")[1])
2364N/A
2828N/A # The key file requires decryption, but the user has requested
2828N/A # exec-based authentication, so it will have to be decoded first
2828N/A # to an un-named temporary file.
2828N/A try:
2828N/A key_file = file(ssl_key_file, "rb")
2828N/A pkey = crypto.load_privatekey(crypto.FILETYPE_PEM,
2828N/A key_file.read(), get_ssl_passphrase)
2828N/A
2828N/A key_data = tempfile.TemporaryFile()
2828N/A key_data.write(crypto.dump_privatekey(
2828N/A crypto.FILETYPE_PEM, pkey))
2828N/A key_data.seek(0)
2828N/A except EnvironmentError, _e:
2828N/A print "pkg.depotd: unable to read the SSL private " \
2828N/A "key file: %s" % _e
2828N/A sys.exit(1)
2828N/A except crypto.Error, _e:
2828N/A print "pkg.depotd: authentication or cryptography " \
2828N/A "failure while attempting to decode\nthe SSL " \
2828N/A "private key file: %s" % _e
2828N/A sys.exit(1)
2828N/A else:
2828N/A # Redirect the server to the decrypted key file.
2828N/A ssl_key_file = "/dev/fd/%d" % key_data.fileno()
2828N/A
2828N/A # Setup our global configuration.
2828N/A gconf = {
2828N/A "checker.on": True,
2892N/A "environment": "production",
2892N/A "log.screen": False,
2828N/A "server.max_request_body_size": MAX_REQUEST_BODY_SIZE,
2828N/A "server.shutdown_timeout": 0,
2828N/A "server.socket_host": "0.0.0.0",
2828N/A "server.socket_port": port,
2828N/A "server.socket_timeout": socket_timeout,
2828N/A "server.ssl_certificate": ssl_cert_file,
2828N/A "server.ssl_private_key": ssl_key_file,
2828N/A "server.thread_pool": threads,
2828N/A "tools.log_headers.on": True,
2828N/A "tools.encode.on": True
2828N/A }
2828N/A
2339N/A if debug_features["headers"]:
2339N/A # Despite its name, this only logs headers when there is an
2339N/A # error; it's redundant with the debug feature enabled.
2339N/A gconf["tools.log_headers.on"] = False
2339N/A
2339N/A # Causes the headers of every request to be logged to the error
2339N/A # log; even if an exception occurs.
2339N/A gconf["tools.log_headers_always.on"] = True
2339N/A cherrypy.tools.log_headers_always = cherrypy.Tool(
2339N/A "on_start_resource",
2339N/A cherrypy.lib.cptools.log_request_headers)
2339N/A
2339N/A log_type_map = {
2339N/A "errors": {
2339N/A "param": "log.error_file",
2339N/A "attr": "error_log"
2339N/A },
2339N/A "access": {
2339N/A "param": "log.access_file",
2339N/A "attr": "access_log"
2364N/A }
2364N/A }
2364N/A
2364N/A for log_type in log_type_map:
2364N/A dest = log_routes[log_type]
2364N/A if dest in ("stdout", "stderr", "none"):
2364N/A if dest == "none":
2364N/A h = logging.StreamHandler(LogSink())
2364N/A else:
2364N/A h = logging.StreamHandler(eval("sys.%s" % \
2364N/A dest))
2364N/A
2364N/A h.setLevel(logging.DEBUG)
2339N/A h.setFormatter(cherrypy._cplogging.logfmt)
395N/A log_obj = eval("cherrypy.log.%s" % \
395N/A log_type_map[log_type]["attr"])
290N/A log_obj.addHandler(h)
290N/A # Since we've replaced cherrypy's log handler with our
2339N/A # own, we don't want the output directed to a file.
2339N/A dest = ""
290N/A
290N/A gconf[log_type_map[log_type]["param"]] = dest
290N/A
290N/A cherrypy.config.update(gconf)
290N/A
290N/A # Now that our logging, etc. has been setup, it's safe to perform any
290N/A # remaining preparation.
290N/A if reindex:
290N/A try:
290N/A scfg.acquire_catalog(rebuild=False, verbose=True)
395N/A except (search_errors.IndexingException,
395N/A catalog.CatalogPermissionsException,
290N/A errors.SvrConfigError), e:
290N/A cherrypy.log(str(e), "INDEX")
2674N/A sys.exit(1)
2674N/A sys.exit(0)
2674N/A
2674N/A # Now build our site configuration.
290N/A conf = {
2674N/A "/": {
2674N/A # We have to override cherrypy's default response_class so that
395N/A # we have access to the write() callable to stream data
395N/A # directly to the client.
395N/A "wsgi.response_class": dr.DepotResponse,
2674N/A },
395N/A "/robots.txt": {
395N/A "tools.staticfile.on": True,
395N/A "tools.staticfile.filename": os.path.join(scfg.web_root,
395N/A "robots.txt")
2674N/A },
591N/A }
591N/A
591N/A if proxy_base:
2674N/A # This changes the base URL for our server, and is primarily
2639N/A # intended to allow our depot process to operate behind Apache
2639N/A # or some other webserver process.
2639N/A #
2674N/A # Visit the following URL for more information:
2639N/A # http://cherrypy.org/wiki/BuiltinTools#tools.proxy
2639N/A proxy_conf = {
2639N/A "tools.proxy.on": True,
2674N/A "tools.proxy.local": "",
2674N/A "tools.proxy.base": proxy_base
691N/A }
691N/A
691N/A # Now merge or add our proxy configuration information into the
2674N/A # existing configuration.
2674N/A for entry in proxy_conf:
2339N/A conf["/"][entry] = proxy_conf[entry]
2339N/A
2339N/A scfg.acquire_in_flight()
290N/A try:
290N/A scfg.acquire_catalog(rebuild=rebuild, verbose=True)
290N/A except (catalog.CatalogPermissionsException, errors.SvrConfigError), _e:
290N/A emsg("pkg.depotd: %s" % _e)
290N/A sys.exit(1)
591N/A
591N/A try:
2639N/A if nasty:
2639N/A root = cherrypy.Application(depot.NastyDepotHTTP(scfg,
2639N/A repo_config_file))
2639N/A else:
691N/A root = cherrypy.Application(depot.DepotHTTP(scfg,
691N/A repo_config_file))
2339N/A except rc.InvalidAttributeValueError, _e:
2339N/A emsg("pkg.depotd: repository.conf error: %s" % _e)
290N/A sys.exit(1)
290N/A
2339N/A try:
2339N/A cherrypy.quickstart(root, config=conf)
2339N/A except Exception, _e:
2339N/A emsg("pkg.depotd: unknown error starting depot server, " \
2339N/A "illegal option value specified?")
2339N/A emsg(_e)
2339N/A sys.exit(1)
290N/A