depot.py revision 2028
1516N/A#!/usr/bin/python2.6
20N/A#
20N/A# CDDL HEADER START
20N/A#
20N/A# The contents of this file are subject to the terms of the
20N/A# Common Development and Distribution License (the "License").
20N/A# You may not use this file except in compliance with the License.
20N/A#
20N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
20N/A# or http://www.opensolaris.org/os/licensing.
20N/A# See the License for the specific language governing permissions
20N/A# and limitations under the License.
20N/A#
20N/A# When distributing Covered Code, include this CDDL HEADER in each
20N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
20N/A# If applicable, add the following below this CDDL HEADER, with the
20N/A# fields enclosed by brackets "[]" replaced with your own identifying
20N/A# information: Portions Copyright [yyyy] [name of copyright owner]
20N/A#
20N/A# CDDL HEADER END
20N/A#
2951N/A# Copyright (c) 2007, 2010 Oracle and/or its affiliates. All rights reserved.
20N/A#
20N/A
22N/A# pkg.depotd - package repository daemon
0N/A
50N/A# XXX The prototype pkg.depotd combines both the version management server that
50N/A# answers to pkgsend(1) sessions and the HTTP file server that answers to the
50N/A# various GET operations that a pkg(1) client makes. This split is expected to
50N/A# be made more explicit, by constraining the pkg(1) operations such that they
50N/A# can be served as a typical HTTP/HTTPS session. Thus, pkg.depotd will reduce
50N/A# to a special purpose HTTP/HTTPS server explicitly for the version management
50N/A# operations, and must manipulate the various state files--catalogs, in
50N/A# particular--such that the pkg(1) pull client can operately accurately with
50N/A# only a basic HTTP/HTTPS server in place.
50N/A
50N/A# XXX Although we pushed the evaluation of next-version, etc. to the pull
50N/A# client, we should probably provide a query API to do same on the server, for
50N/A# dumb clients (like a notification service).
50N/A
589N/A# The default path for static and other web content.
589N/ACONTENT_PATH_DEFAULT = "/usr/share/lib/pkg"
965N/A# cherrypy has a max_request_body_size parameter that determines whether the
965N/A# server should abort requests with REQUEST_ENTITY_TOO_LARGE when the request
965N/A# body is larger than the specified size (in bytes). The maximum size supported
965N/A# by cherrypy is 2048 * 1024 * 1024 - 1 (just short of 2048MB), but the default
965N/A# here is purposefully conservative.
2951N/AMAX_REQUEST_BODY_SIZE = 128 * 1024 * 1024
1836N/A# The default host/port(s) to serve data from.
1836N/AHOST_DEFAULT = "0.0.0.0"
382N/APORT_DEFAULT = 80
812N/ASSL_PORT_DEFAULT = 443
382N/A# The minimum number of threads allowed.
382N/ATHREADS_MIN = 1
382N/A# The default number of threads to start.
1963N/ATHREADS_DEFAULT = 60
382N/A# The maximum number of threads that can be started.
1963N/ATHREADS_MAX = 5000
382N/A# The default server socket timeout in seconds. We want this to be longer than
382N/A# the normal default of 10 seconds to accommodate clients with poor quality
382N/A# connections.
382N/ASOCKET_TIMEOUT_DEFAULT = 60
382N/A
26N/Aimport getopt
689N/Aimport gettext
689N/Aimport locale
466N/Aimport logging
0N/Aimport os
468N/Aimport os.path
812N/Aimport OpenSSL.crypto as crypto
812N/Aimport subprocess
52N/Aimport sys
812N/Aimport tempfile
451N/Aimport urlparse
0N/A
382N/Atry:
382N/A import cherrypy
382N/A version = cherrypy.__version__.split('.')
452N/A if map(int, version) < [3, 1, 0]:
382N/A raise ImportError
452N/A elif map(int, version) >= [3, 2, 0]:
382N/A raise ImportError
382N/Aexcept ImportError:
751N/A print >> sys.stderr, """cherrypy 3.1.0 or greater (but less than """ \
751N/A """3.2.0) is required to use this program."""
382N/A sys.exit(2)
22N/A
1836N/Aimport cherrypy.process.servers
2507N/A
1836N/Afrom pkg.misc import msg, emsg, setlocale
1836N/Aimport pkg.client.api_errors as api_errors
2962N/Aimport pkg.config as cfg
2962N/Aimport pkg.portable.util as os_util
2962N/Aimport pkg.search_errors as search_errors
1431N/Aimport pkg.server.depot as ds
1968N/Aimport pkg.server.depotresponse as dr
873N/Aimport pkg.server.repository as sr
812N/A
1431N/A
873N/Aclass LogSink(object):
1431N/A """This is a dummy object that we can use to discard log entries
466N/A without relying on non-portable interfaces such as /dev/null."""
1431N/A
466N/A def write(self, *args, **kwargs):
466N/A """Discard the bits."""
466N/A pass
23N/A
466N/A def flush(self, *args, **kwargs):
466N/A """Discard the bits."""
466N/A pass
466N/A
466N/A
466N/Adef usage(text=None, retcode=2, full=False):
466N/A """Optionally emit a usage message and then exit using the specified
466N/A exit code."""
1431N/A
1633N/A if text:
1633N/A emsg(text)
1633N/A
1633N/A if not full:
466N/A # The full usage message isn't desired.
466N/A emsg(_("Try `pkg.depotd --help or -?' for more "
466N/A "information."))
1633N/A sys.exit(retcode)
1633N/A
1633N/A print """\
1633N/AUsage: /usr/lib/pkg.depotd [-d inst_root] [-p port] [-s threads]
1633N/A [-t socket_timeout] [--cfg] [--content-root]
1633N/A [--disable-ops op[/1][,...]] [--debug feature_list]
26N/A [--file-root dir] [--log-access dest] [--log-errors dest]
2230N/A [--mirror] [--nasty] [--proxy-base url] [--readonly]
1968N/A [--socket-path] [--ssl-cert-file] [--ssl-dialog] [--ssl-key-file]
1633N/A [--sort-file-max-size size] [--writable-root dir]
2515N/A
2816N/A -d inst_root The file system path at which the server should find its
2816N/A repository data. Required unless PKG_REPO has been set
1937N/A in the environment.
382N/A -p port The port number on which the instance should listen for
2230N/A incoming package requests. The default value is 80 if
2230N/A ssl certificate and key information has not been
2230N/A provided; otherwise, the default value is 443.
2230N/A -s threads The number of threads that will be started to serve
2028N/A requests. The default value is 10.
1968N/A -t timeout The maximum number of seconds the server should wait for
1968N/A a response from a client before closing a connection.
2028N/A The default value is 60.
1968N/A --cfg The pathname of the file to use when reading and writing
1968N/A depot configuration data, or a fully qualified service
1968N/A fault management resource identifier (FMRI) of the SMF
2028N/A service or instance to read configuration data from.
2028N/A --content-root The file system path to the directory containing the
2028N/A the static and other web content used by the depot's
1968N/A browser user interface. The default value is
1968N/A '/usr/share/lib/pkg'.
1968N/A --disable-ops A comma separated list of operations that the depot
1968N/A should not configure. If, for example, you wanted
1968N/A to omit loading search v1, 'search/1' should be
1968N/A provided as an argument, or to disable all search
589N/A operations, simply 'search'.
589N/A --debug The name of a debug feature to enable; or a whitespace
589N/A or comma separated list of features to enable.
589N/A Possible values are: headers.
1431N/A --file-root The path to the root of the file content for a given
1431N/A repository. This is used to override the default,
1431N/A <inst_root>/file or <inst_root>/publisher/<prefix>/file.
1431N/A --log-access The destination for any access related information
1431N/A logged by the depot process. Possible values are:
858N/A stderr, stdout, none, or an absolute pathname. The
1633N/A default value is stdout if stdout is a tty; otherwise
2962N/A the default value is none.
2962N/A --log-errors The destination for any errors or other information
2515N/A logged by the depot process. Possible values are:
2515N/A stderr, stdout, none, or an absolute pathname. The
466N/A default value is stderr.
466N/A --mirror Package mirror mode; publishing and metadata operations
466N/A disallowed. Cannot be used with --readonly or
466N/A --rebuild.
466N/A --nasty Instruct the server to misbehave. At random intervals
466N/A it will time-out, send bad responses, hang up on
466N/A clients, and generally be hostile. The option
466N/A takes a value (1 to 100) for how nasty the server
466N/A should be.
589N/A --proxy-base The url to use as the base for generating internal
589N/A redirects and content.
589N/A --readonly Read-only operation; modifying operations disallowed.
1191N/A Cannot be used with --mirror or --rebuild.
1191N/A --ssl-cert-file The absolute pathname to a PEM-encoded Certificate file.
1191N/A This option must be used with --ssl-key-file. Usage of
1191N/A this option will cause the depot to only respond to SSL
1191N/A requests on the provided port.
2816N/A --ssl-dialog Specifies what method should be used to obtain the
2816N/A passphrase needed to decrypt the file specified by
589N/A --ssl-key-file. Supported values are: builtin,
589N/A exec:/path/to/program, smf, or an SMF FMRI. The
589N/A default value is builtin. If smf is specified, an
589N/A SMF FMRI must be provided using the --cfg option.
812N/A --ssl-key-file The absolute pathname to a PEM-encoded Private Key file.
812N/A This option must be used with --ssl-cert-file. Usage of
812N/A this option will cause the depot to only respond to SSL
812N/A requests on the provided port.
812N/A --sort-file-max-size
812N/A The maximum size of the indexer sort file. Used to
812N/A limit the amount of RAM the depot uses for indexing,
1968N/A or increase it for speed.
1968N/A --writable-root The path to a directory to which the program has write
1968N/A access. Used with --readonly to allow server to
812N/A create needed files, such as search indices, without
812N/A needing write access to the package information.
812N/AOptions:
812N/A --help or -?
1475N/A
1475N/AEnvironment:
1475N/A PKG_REPO Used as default inst_root if -d not provided.
1475N/A PKG_DEPOT_CONTENT Used as default content_root if --content-root
975N/A not provided."""
975N/A sys.exit(retcode)
975N/A
975N/Aclass OptionError(Exception):
1633N/A """Option exception. """
1633N/A
1633N/A def __init__(self, *args):
1633N/A Exception.__init__(self, *args)
2028N/A
1633N/Aif __name__ == "__main__":
1633N/A
1633N/A setlocale(locale.LC_ALL, "")
14N/A gettext.install("pkg", "/usr/share/locale")
382N/A
429N/A add_content = False
14N/A exit_ready = False
404N/A mirror = False
404N/A rebuild = False
30N/A reindex = False
382N/A ll_mirror = False
30N/A nasty = False
791N/A nasty_value = 0
2728N/A
2728N/A # Track initial configuration values.
689N/A ivalues = { "pkg": {} }
1968N/A if "PKG_REPO" in os.environ:
1968N/A ivalues["pkg"]["inst_root"] = os.environ["PKG_REPO"]
1968N/A
1968N/A try:
1191N/A content_root = os.environ["PKG_DEPOT_CONTENT"]
258N/A ivalues["pkg"]["content_root"] = content_root
1968N/A except KeyError:
2816N/A try:
382N/A content_root = os.path.join(os.environ['PKG_HOME'],
1968N/A 'share/lib/pkg')
30N/A ivalues["pkg"]["content_root"] = content_root
589N/A except KeyError:
589N/A pass
1968N/A
589N/A opt = None
589N/A debug_features = []
589N/A disable_ops = []
589N/A repo_props = {}
1968N/A socket_path = ""
589N/A user_cfg = None
1968N/A try:
466N/A long_opts = ["add-content", "cfg=", "cfg-file=",
466N/A "content-root=", "debug=", "disable-ops=", "exit-ready",
2230N/A "file-root=", "help", "log-access=", "log-errors=",
1968N/A "llmirror", "mirror", "nasty=", "proxy-base=", "readonly",
1968N/A "rebuild", "refresh-index", "set-property=", "socket-path=",
1431N/A "ssl-cert-file=", "ssl-dialog=", "ssl-key-file=",
1968N/A "sort-file-max-size=", "writable-root="]
1968N/A
54N/A opts, pargs = getopt.getopt(sys.argv[1:], "d:np:s:t:?",
1968N/A long_opts)
1968N/A
2515N/A show_usage = False
2816N/A for opt, arg in opts:
2816N/A if opt == "-n":
2816N/A sys.exit(0)
2816N/A elif opt == "-d":
1475N/A ivalues["pkg"]["inst_root"] = arg
2230N/A elif opt == "-p":
466N/A ivalues["pkg"]["port"] = arg
1633N/A elif opt == "-s":
1633N/A threads = int(arg)
135N/A if threads < THREADS_MIN:
2230N/A raise OptionError, \
2230N/A "minimum value is %d" % THREADS_MIN
2230N/A if threads > THREADS_MAX:
135N/A raise OptionError, \
135N/A "maximum value is %d" % THREADS_MAX
1968N/A ivalues["pkg"]["threads"] = threads
135N/A elif opt == "-t":
1968N/A ivalues["pkg"]["socket_timeout"] = arg
382N/A elif opt == "--add-content":
382N/A add_content = True
382N/A elif opt == "--cfg":
382N/A user_cfg = arg
382N/A elif opt == "--cfg-file":
382N/A ivalues["pkg"]["cfg_file"] = arg
382N/A elif opt == "--content-root":
382N/A ivalues["pkg"]["content_root"] = arg
1968N/A elif opt == "--debug":
382N/A if arg is None or arg == "":
1968N/A continue
1542N/A
1542N/A # A list of features can be specified using a
1968N/A # "," or any whitespace character as separators.
1968N/A if "," in arg:
812N/A features = arg.split(",")
1968N/A else:
589N/A features = arg.split()
1968N/A debug_features.extend(features)
858N/A elif opt == "--disable-ops":
858N/A if arg is None or arg == "":
1968N/A raise OptionError, \
858N/A "An argument must be specified."
858N/A
858N/A disableops = arg.split(",")
858N/A for s in disableops:
858N/A if "/" in s:
858N/A op, ver = s.rsplit("/", 1)
858N/A else:
1968N/A op = s
2962N/A ver = "*"
2962N/A
2962N/A if op not in \
2962N/A ds.DepotHTTP.REPO_OPS_DEFAULT:
2962N/A raise OptionError(
2962N/A "Invalid operation "
2962N/A "'%s'." % s)
2962N/A disable_ops.append(s)
2962N/A elif opt == "--exit-ready":
2962N/A exit_ready = True
1431N/A elif opt == "--file-root":
1431N/A ivalues["pkg"]["file_root"] = arg
1431N/A elif opt.startswith("--log-"):
1431N/A prop = "log_%s" % opt.lstrip("--log-")
1431N/A ivalues["pkg"][prop] = arg
1431N/A elif opt in ("--help", "-?"):
1431N/A show_usage = True
1431N/A elif opt == "--mirror":
1431N/A ivalues["pkg"]["mirror"] = True
1431N/A elif opt == "--llmirror":
1431N/A ivalues["pkg"]["mirror"] = True
1431N/A ivalues["pkg"]["ll_mirror"] = True
1431N/A ivalues["pkg"]["readonly"] = True
1431N/A elif opt == "--nasty":
1431N/A value_err = None
1431N/A try:
1431N/A nasty_value = int(arg)
1431N/A except ValueError, e:
1968N/A value_err = e
1542N/A
1542N/A if value_err or (nasty_value > 100 or
2515N/A nasty_value < 1):
2515N/A raise OptionError, "Invalid value " \
1968N/A "for nasty option.\n Please " \
1968N/A "choose a value between 1 and 100."
1968N/A nasty = True
1633N/A elif opt == "--proxy-base":
1633N/A # Attempt to decompose the url provided into
589N/A # its base parts. This is done so we can
1968N/A # remove any scheme information since we
1902N/A # don't need it.
1968N/A scheme, netloc, path, params, query, \
1968N/A fragment = urlparse.urlparse(arg,
1968N/A "http", allow_fragments=0)
1191N/A
2816N/A if not netloc:
2816N/A raise OptionError, "Unable to " \
2816N/A "determine the hostname from " \
1191N/A "the provided URL; please use a " \
1191N/A "fully qualified URL."
1191N/A
1191N/A scheme = scheme.lower()
2816N/A if scheme not in ("http", "https"):
2816N/A raise OptionError, "Invalid URL; http " \
2816N/A "and https are the only supported " \
2816N/A "schemes."
2816N/A
589N/A # Rebuild the url with the sanitized components.
589N/A ivalues["pkg"]["proxy_base"] = \
589N/A urlparse.urlunparse((scheme, netloc, path,
589N/A params, query, fragment))
589N/A elif opt == "--readonly":
589N/A ivalues["pkg"]["readonly"] = True
589N/A elif opt == "--rebuild":
765N/A rebuild = True
765N/A elif opt == "--refresh-index":
765N/A # Note: This argument is for internal use
765N/A # only.
765N/A #
765N/A # This flag is purposefully omitted in usage.
765N/A # The supported way to forcefully reindex is to
589N/A # kill any pkg.depot using that directory,
765N/A # remove the index directory, and restart the
765N/A # pkg.depot process. The index will be rebuilt
765N/A # automatically on startup.
765N/A reindex = True
765N/A exit_ready = True
765N/A elif opt == "--set-property":
765N/A try:
1968N/A prop, p_value = arg.split("=", 1)
1968N/A p_sec, p_name = prop.split(".", 1)
1968N/A except ValueError:
135N/A usage(_("property arguments must be of "
1968N/A "the form '<section.property>="
157N/A "<value>'."))
382N/A repo_props.setdefault(p_sec, {})
429N/A repo_props[p_sec][p_name] = p_value
429N/A elif opt == "--socket-path":
2028N/A socket_path = arg
2028N/A elif opt == "--ssl-cert-file":
429N/A if arg == "none" or arg == "":
429N/A # Assume this is an override to clear
429N/A # the value.
429N/A arg = ""
429N/A elif not os.path.isabs(arg):
429N/A raise OptionError, "The path to " \
429N/A "the Certificate file must be " \
2028N/A "absolute."
1968N/A elif not os.path.exists(arg):
1968N/A raise OptionError, "The specified " \
1968N/A "file does not exist."
1968N/A elif not os.path.isfile(arg):
1968N/A raise OptionError, "The specified " \
1968N/A "pathname is not a file."
1968N/A ivalues["pkg"]["ssl_cert_file"] = arg
1968N/A elif opt == "--ssl-key-file":
1968N/A if arg == "none" or arg == "":
1968N/A # Assume this is an override to clear
812N/A # the value.
1968N/A arg = ""
1968N/A elif not os.path.isabs(arg):
1968N/A raise OptionError, "The path to " \
1968N/A "the Private Key file must be " \
1968N/A "absolute."
812N/A elif not os.path.exists(arg):
812N/A raise OptionError, "The specified " \
812N/A "file does not exist."
1968N/A elif not os.path.isfile(arg):
812N/A raise OptionError, "The specified " \
812N/A "pathname is not a file."
1968N/A ivalues["pkg"]["ssl_key_file"] = arg
812N/A elif opt == "--ssl-dialog":
812N/A if arg != "builtin" and \
1968N/A arg != "smf" and not \
812N/A arg.startswith("exec:/") and not \
1968N/A arg.startswith("svc:"):
1968N/A raise OptionError, "Invalid value " \
1968N/A "specified. Expected: builtin, " \
1968N/A "exec:/path/to/program, smf, or " \
1968N/A "an SMF FMRI."
812N/A
812N/A if arg.startswith("exec:"):
812N/A if os_util.get_canonical_os_type() != \
1968N/A "unix":
812N/A # Don't allow a somewhat
812N/A # insecure authentication method
1968N/A # on some platforms.
812N/A raise OptionError, "exec is " \
812N/A "not a supported dialog " \
1968N/A "type for this operating " \
812N/A "system."
1968N/A
1968N/A f = os.path.abspath(arg.split(
812N/A "exec:")[1])
1968N/A if not os.path.isfile(f):
812N/A raise OptionError, "Invalid " \
812N/A "file path specified for " \
1968N/A "exec."
1968N/A ivalues["pkg"]["ssl_dialog"] = arg
812N/A elif opt == "--sort-file-max-size":
1968N/A ivalues["pkg"]["sort_file_max_size"] = arg
812N/A elif opt == "--writable-root":
812N/A ivalues["pkg"]["writable_root"] = arg
873N/A
873N/A # Set accumulated values.
873N/A if debug_features:
873N/A ivalues["pkg"]["debug"] = debug_features
873N/A if disable_ops:
873N/A ivalues["pkg"]["disable_ops"] = disable_ops
873N/A
812N/A # Build configuration object.
1968N/A dconf = ds.DepotConfig(target=user_cfg, overrides=ivalues)
812N/A except getopt.GetoptError, _e:
812N/A usage("pkg.depotd: %s" % _e.msg)
812N/A except api_errors.ApiException, _e:
812N/A usage("pkg.depotd: %s" % str(_e))
812N/A except OptionError, _e:
1968N/A usage("pkg.depotd: option: %s -- %s" % (opt, _e))
1475N/A except (ArithmeticError, ValueError):
1968N/A usage("pkg.depotd: illegal option value: %s specified " \
975N/A "for option: %s" % (arg, opt))
1968N/A
1968N/A if show_usage:
1968N/A usage(retcode=0, full=True)
1968N/A
1968N/A if not dconf.get_property("pkg", "log_errors"):
1968N/A dconf.set_property("pkg", "log_errors", "stderr")
1968N/A
2230N/A # If stdout is a tty, then send access output there by default instead
2230N/A # of discarding it.
1968N/A if not dconf.get_property("pkg", "log_access"):
2962N/A if os.isatty(sys.stdout.fileno()):
2962N/A dconf.set_property("pkg", "log_access", "stdout")
2962N/A else:
1968N/A dconf.set_property("pkg", "log_access", "none")
1968N/A
873N/A # Check for invalid option combinations.
873N/A mirror = dconf.get_property("pkg", "mirror")
1968N/A readonly = dconf.get_property("pkg", "readonly")
1968N/A writable_root = dconf.get_property("pkg", "writable_root")
873N/A if rebuild and add_content:
873N/A usage("--add-content cannot be used with --rebuild")
382N/A if rebuild and reindex:
466N/A usage("--refresh-index cannot be used with --rebuild")
466N/A if (rebuild or add_content) and (readonly or mirror):
451N/A usage("--readonly and --mirror cannot be used with --rebuild "
1633N/A "or --add-content")
1633N/A if reindex and mirror:
1633N/A usage("--mirror cannot be used with --refresh-index")
1968N/A if reindex and readonly and not writable_root:
1968N/A usage("--readonly can only be used with --refresh-index if "
1968N/A "--writable-root is used")
1968N/A
1968N/A # Set any values using defaults if they weren't provided.
1968N/A inst_root = dconf.get_property("pkg", "inst_root")
1968N/A file_root = dconf.get_property("pkg", "file_root")
1968N/A if not inst_root and not file_root:
1968N/A usage("At least one of PKG_REPO, -d, or --file-root"
1968N/A " must be provided")
1968N/A
1968N/A content_root = dconf.get_property("pkg", "content_root")
2515N/A if not content_root:
2515N/A dconf.set_property("pkg", "content_root", CONTENT_PATH_DEFAULT)
1968N/A content_root = dconf.get_property("pkg", "content_root")
2230N/A
1968N/A port = dconf.get_property("pkg", "port")
1968N/A ssl_cert_file = dconf.get_property("pkg", "ssl_cert_file")
1542N/A ssl_key_file = dconf.get_property("pkg", "ssl_key_file")
1542N/A if (ssl_cert_file and not ssl_key_file) or (ssl_key_file and not
445N/A ssl_cert_file):
466N/A usage("The --ssl-cert-file and --ssl-key-file options must "
1542N/A "must both be provided when using either option.")
1633N/A elif not port:
1633N/A if ssl_cert_file and ssl_key_file:
1020N/A dconf.set_property("pkg", "port", SSL_PORT_DEFAULT)
1020N/A else:
1020N/A dconf.set_property("pkg", "port", PORT_DEFAULT)
1020N/A port = dconf.get_property("pkg", "port")
1020N/A
2515N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
2515N/A if not socket_timeout:
2515N/A dconf.set_property("pkg", "socket_timeout",
2515N/A SOCKET_TIMEOUT_DEFAULT)
2515N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
2515N/A
2515N/A threads = dconf.get_property("pkg", "threads")
2515N/A if not threads:
2515N/A dconf.set_property("pkg", "threads", THREADS_DEFAULT)
2515N/A threads = dconf.get_property("pkg", "threads")
2515N/A
451N/A # If the program is going to reindex, the port is irrelevant since
1968N/A # the program will not bind to a port.
2230N/A if not exit_ready:
2230N/A try:
2230N/A cherrypy.process.servers.check_port(HOST_DEFAULT, port)
2230N/A except Exception, e:
2230N/A emsg("pkg.depotd: unable to bind to the specified "
2230N/A "port: %d. Reason: %s" % (port, e))
2230N/A sys.exit(1)
2230N/A else:
2230N/A # Not applicable if we're not going to serve content
2230N/A dconf.set_property("pkg", "content_root", "")
2515N/A
2515N/A # Any relative paths should be made absolute using pkg_root. 'pkg_root'
1902N/A # is a special property that was added to enable internal deployment of
1968N/A # multiple disparate versions of the pkg.depotd software.
1968N/A pkg_root = dconf.get_property("pkg", "pkg_root")
1968N/A
1968N/A repo_config_file = dconf.get_property("pkg", "cfg_file")
1968N/A if repo_config_file and not os.path.isabs(repo_config_file):
1968N/A repo_config_file = os.path.join(pkg_root, repo_config_file)
1968N/A
1968N/A if content_root and not os.path.isabs(content_root):
812N/A content_root = os.path.join(pkg_root, content_root)
812N/A
812N/A if file_root and not os.path.isabs(file_root):
812N/A file_root = os.path.join(pkg_root, file_root)
1968N/A
1968N/A if inst_root and not os.path.isabs(inst_root):
1968N/A inst_root = os.path.join(pkg_root, inst_root)
1968N/A
1968N/A if ssl_cert_file:
1968N/A if ssl_cert_file == "none":
1968N/A ssl_cert_file = None
1968N/A elif not os.path.isabs(ssl_cert_file):
1968N/A ssl_cert_file = os.path.join(pkg_root, ssl_cert_file)
1968N/A
1968N/A if ssl_key_file:
1968N/A if ssl_key_file == "none":
1968N/A ssl_key_file = None
1968N/A elif not os.path.isabs(ssl_key_file):
1968N/A ssl_key_file = os.path.join(pkg_root, ssl_key_file)
1968N/A
1968N/A if writable_root and not os.path.isabs(writable_root):
812N/A writable_root = os.path.join(pkg_root, writable_root)
429N/A
429N/A # Setup SSL if requested.
2028N/A key_data = None
1836N/A ssl_dialog = dconf.get_property("pkg", "ssl_dialog")
2230N/A if not exit_ready and ssl_cert_file and ssl_key_file and \
1836N/A ssl_dialog != "builtin":
1836N/A cmdline = None
1836N/A def get_ssl_passphrase(*ignored):
429N/A p = None
612N/A try:
1542N/A p = subprocess.Popen(cmdline, shell=True,
1968N/A stdout=subprocess.PIPE,
1968N/A stderr=None)
1968N/A p.wait()
1968N/A except Exception, __e:
1968N/A emsg("pkg.depotd: an error occurred while "
1968N/A "executing [%s]; unable to obtain the "
1968N/A "passphrase needed to decrypt the SSL "
1968N/A "private key file: %s" % (cmdline, __e))
1968N/A sys.exit(1)
1968N/A return p.stdout.read().strip("\n")
1968N/A
1968N/A if ssl_dialog.startswith("exec:"):
1968N/A exec_path = ssl_dialog.split("exec:")[1]
1968N/A if not os.path.isabs(exec_path):
1968N/A exec_path = os.path.join(pkg_root, exec_path)
1968N/A cmdline = "%s %s %d" % (exec_path, "''", port)
1968N/A elif ssl_dialog == "smf" or ssl_dialog.startswith("svc:"):
1968N/A if ssl_dialog == "smf":
1968N/A # Assume the configuration target was an SMF
1968N/A # FMRI and let svcprop fail with an error if
1968N/A # it wasn't.
1968N/A svc_fmri = dconf.target
1968N/A else:
1968N/A svc_fmri = ssl_dialog
1968N/A cmdline = "/usr/bin/svcprop -p " \
1968N/A "pkg_secure/ssl_key_passphrase %s" % svc_fmri
1968N/A
1968N/A # The key file requires decryption, but the user has requested
1968N/A # exec-based authentication, so it will have to be decoded first
1968N/A # to an un-named temporary file.
1968N/A try:
1968N/A with file(ssl_key_file, "rb") as key_file:
1968N/A pkey = crypto.load_privatekey(
812N/A crypto.FILETYPE_PEM, key_file.read(),
1968N/A get_ssl_passphrase)
2028N/A
812N/A key_data = tempfile.TemporaryFile()
812N/A key_data.write(crypto.dump_privatekey(
812N/A crypto.FILETYPE_PEM, pkey))
812N/A key_data.seek(0)
812N/A except EnvironmentError, _e:
812N/A emsg("pkg.depotd: unable to read the SSL private key "
812N/A "file: %s" % _e)
812N/A sys.exit(1)
812N/A except crypto.Error, _e:
873N/A emsg("pkg.depotd: authentication or cryptography "
1836N/A "failure while attempting to decode\nthe SSL "
1836N/A "private key file: %s" % _e)
1836N/A sys.exit(1)
1836N/A else:
812N/A # Redirect the server to the decrypted key file.
812N/A ssl_key_file = "/dev/fd/%d" % key_data.fileno()
812N/A key_data.close()
812N/A
1968N/A # Setup our global configuration.
1968N/A gconf = {
1968N/A "checker.on": True,
1968N/A "environment": "production",
1968N/A "log.screen": False,
1968N/A "server.max_request_body_size": MAX_REQUEST_BODY_SIZE,
1968N/A "server.shutdown_timeout": 0,
1968N/A "server.socket_file": socket_path,
1968N/A "server.socket_host": HOST_DEFAULT,
1968N/A "server.socket_port": port,
1968N/A "server.socket_timeout": socket_timeout,
1968N/A "server.ssl_certificate": ssl_cert_file,
812N/A "server.ssl_private_key": ssl_key_file,
1968N/A "server.thread_pool": threads,
812N/A "tools.log_headers.on": True,
812N/A "tools.encode.on": True
812N/A }
812N/A
812N/A if "headers" in dconf.get_property("pkg", "debug"):
1968N/A # Despite its name, this only logs headers when there is an
1968N/A # error; it's redundant with the debug feature enabled.
1968N/A gconf["tools.log_headers.on"] = False
1968N/A
812N/A # Causes the headers of every request to be logged to the error
812N/A # log; even if an exception occurs.
812N/A gconf["tools.log_headers_always.on"] = True
812N/A cherrypy.tools.log_headers_always = cherrypy.Tool(
812N/A "on_start_resource",
873N/A cherrypy.lib.cptools.log_request_headers)
1836N/A
1836N/A log_type_map = {
812N/A "errors": {
873N/A "param": "log.error_file",
1836N/A "attr": "error_log"
1836N/A },
1836N/A "access": {
812N/A "param": "log.access_file",
812N/A "attr": "access_log"
812N/A }
812N/A }
812N/A
452N/A for log_type in log_type_map:
466N/A dest = dconf.get_property("pkg", "log_%s" % log_type)
858N/A if dest in ("stdout", "stderr", "none"):
382N/A if dest == "none":
466N/A h = logging.StreamHandler(LogSink())
965N/A else:
858N/A h = logging.StreamHandler(eval("sys.%s" % \
2230N/A dest))
382N/A
858N/A h.setLevel(logging.DEBUG)
858N/A h.setFormatter(cherrypy._cplogging.logfmt)
858N/A log_obj = eval("cherrypy.log.%s" % \
382N/A log_type_map[log_type]["attr"])
742N/A log_obj.addHandler(h)
858N/A # Since we've replaced cherrypy's log handler with our
466N/A # own, we don't want the output directed to a file.
466N/A dest = ""
1968N/A elif dest:
858N/A if not os.path.isabs(dest):
858N/A dest = os.path.join(pkg_root, dest)
858N/A gconf[log_type_map[log_type]["param"]] = dest
858N/A
858N/A cherrypy.config.update(gconf)
858N/A
858N/A # Now that our logging, etc. has been setup, it's safe to perform any
858N/A # remaining preparation.
858N/A
858N/A # Initialize repository state.
858N/A if not readonly:
2992N/A # Not readonly, so assume a new repository should be created.
2992N/A try:
2992N/A sr.repository_create(inst_root, properties=repo_props)
2992N/A except sr.RepositoryExistsError:
2992N/A # Already exists, nothing to do.
2992N/A pass
2992N/A except (api_errors.ApiException, sr.RepositoryError), _e:
2992N/A emsg("pkg.depotd: %s" % _e)
2992N/A sys.exit(1)
2992N/A
2992N/A try:
2992N/A sort_file_max_size = dconf.get_property("pkg",
2992N/A "sort_file_max_size")
2992N/A
2992N/A repo = sr.Repository(cfgpathname=repo_config_file,
2992N/A file_root=file_root, log_obj=cherrypy, mirror=mirror,
2992N/A properties=repo_props, read_only=readonly,
2992N/A root=inst_root, sort_file_max_size=sort_file_max_size,
2992N/A writable_root=writable_root)
2992N/A except (RuntimeError, sr.RepositoryError), _e:
2992N/A emsg("pkg.depotd: %s" % _e)
2992N/A sys.exit(1)
2992N/A except search_errors.IndexingException, _e:
2992N/A emsg("pkg.depotd: %s" % str(_e), "INDEX")
2992N/A sys.exit(1)
2992N/A except api_errors.ApiException, _e:
466N/A emsg("pkg.depotd: %s" % str(_e))
466N/A sys.exit(1)
466N/A
466N/A if not rebuild and not add_content and not repo.mirror and \
466N/A not (repo.read_only and not repo.writable_root):
466N/A # Automatically update search indexes on startup if not already
466N/A # told to, and not in readonly/mirror mode.
466N/A reindex = True
466N/A
466N/A if reindex:
466N/A try:
466N/A if repo.root:
2992N/A repo.refresh_index()
466N/A except (sr.RepositoryError, search_errors.IndexingException,
466N/A api_errors.ApiException), e:
466N/A emsg(str(e), "INDEX")
466N/A sys.exit(1)
466N/A elif rebuild:
466N/A try:
466N/A repo.rebuild(build_index=True)
466N/A except sr.RepositoryError, e:
466N/A emsg(str(e), "REBUILD")
466N/A sys.exit(1)
466N/A except (search_errors.IndexingException,
466N/A api_errors.UnknownErrors,
466N/A api_errors.PermissionsException), e:
466N/A emsg(str(e), "INDEX")
466N/A sys.exit(1)
1968N/A elif add_content:
1968N/A try:
1968N/A repo.add_content()
466N/A repo.refresh_index()
382N/A except sr.RepositoryError, e:
612N/A emsg(str(e), "ADD_CONTENT")
612N/A sys.exit(1)
612N/A except (search_errors.IndexingException,
612N/A api_errors.UnknownErrors,
1431N/A api_errors.PermissionsException), e:
1431N/A emsg(str(e), "INDEX")
2028N/A sys.exit(1)
2028N/A
2028N/A # Ready to start depot; exit now if requested.
2028N/A if exit_ready:
2028N/A sys.exit(0)
2028N/A
2028N/A # Next, initialize depot.
2028N/A if nasty:
2028N/A depot = ds.NastyDepotHTTP(repo, dconf)
2028N/A depot.set_nasty(nasty_value)
2028N/A else:
1431N/A depot = ds.DepotHTTP(repo, dconf)
1968N/A
1968N/A # Now build our site configuration.
1968N/A conf = {
2028N/A "/": {
2515N/A # We have to override cherrypy's default response_class so that
2515N/A # we have access to the write() callable to stream data
2515N/A # directly to the client.
1431N/A "wsgi.response_class": dr.DepotResponse,
1895N/A },
1431N/A "/robots.txt": {
1431N/A "tools.staticfile.on": True,
1672N/A "tools.staticfile.filename": os.path.join(depot.web_root,
1672N/A "robots.txt")
1672N/A },
1968N/A }
1672N/A
1431N/A proxy_base = dconf.get_property("pkg", "proxy_base")
1431N/A if proxy_base:
2028N/A # This changes the base URL for our server, and is primarily
2028N/A # intended to allow our depot process to operate behind Apache
2028N/A # or some other webserver process.
2028N/A #
2028N/A # Visit the following URL for more information:
2028N/A # http://cherrypy.org/wiki/BuiltinTools#tools.proxy
612N/A proxy_conf = {
617N/A "tools.proxy.on": True,
2065N/A "tools.proxy.local": "",
2065N/A "tools.proxy.base": proxy_base
2065N/A }
2065N/A
2028N/A # Now merge or add our proxy configuration information into the
2028N/A # existing configuration.
2028N/A for entry in proxy_conf:
2028N/A conf["/"][entry] = proxy_conf[entry]
2028N/A
2028N/A if ll_mirror:
2028N/A ds.DNSSD_Plugin(cherrypy.engine, conf, gconf).subscribe()
2028N/A
1431N/A try:
1431N/A root = cherrypy.Application(depot)
1431N/A cherrypy.quickstart(root, config=conf)
975N/A except Exception, _e:
1779N/A emsg("pkg.depotd: unknown error starting depot server, " \
1431N/A "illegal option value specified?")
1431N/A emsg(_e)
617N/A sys.exit(1)
1542N/A