depot.py revision 3273
1516N/A#!/usr/bin/python2.7
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#
1672N/A# Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
20N/A#
20N/A
20N/Afrom __future__ import print_function
22N/A
0N/A# pkg.depotd - package repository daemon
50N/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).
382N/A
382N/A# The default path for static and other web content.
589N/ACONTENT_PATH_DEFAULT = "/usr/share/lib/pkg"
589N/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.
965N/AMAX_REQUEST_BODY_SIZE = 512 * 1024 * 1024
965N/A# The default host/port(s) to serve data from.
1836N/AHOST_DEFAULT = "0.0.0.0"
1836N/APORT_DEFAULT = 80
382N/ASSL_PORT_DEFAULT = 443
812N/A# The minimum number of threads allowed.
382N/ATHREADS_MIN = 1
382N/A# The default number of threads to start.
382N/ATHREADS_DEFAULT = 60
382N/A# The maximum number of threads that can be started.
382N/ATHREADS_MAX = 5000
1433N/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
382N/Aimport getopt
382N/Aimport gettext
382N/Aimport locale
382N/Aimport logging
429N/Aimport os
429N/Aimport os.path
461N/Aimport OpenSSL.crypto as crypto
461N/Aimport shlex
382N/Aimport subprocess
26N/Aimport sys
689N/Aimport tempfile
689N/A
466N/Afrom imp import reload
0N/Afrom six.moves.urllib.parse import urlparse, urlunparse
468N/A
812N/Atry:
812N/A import cherrypy
52N/A version = cherrypy.__version__.split('.')
812N/A # comparison requires same type, therefore list conversion is needed
451N/A if list(map(int, version)) < [3, 1, 0]:
0N/A raise ImportError
382N/A elif list(map(int, version)) >= [3, 2, 0]:
382N/A raise ImportError
382N/Aexcept ImportError:
452N/A print("""cherrypy 3.1.0 or greater (but less than """
382N/A """3.2.0) is required to use this program.""", file=sys.stderr)
452N/A sys.exit(2)
382N/A
382N/Aimport cherrypy.process.servers
751N/Afrom cherrypy.process.plugins import Daemonizer
751N/A
382N/Afrom pkg.misc import msg, emsg, setlocale
22N/Afrom pkg.client.debugvalues import DebugValues
1836N/A
1836N/Aimport pkg
1836N/Aimport pkg.client.api_errors as api_errors
1431N/Aimport pkg.config as cfg
1477N/Aimport pkg.portable.util as os_util
873N/Aimport pkg.search_errors as search_errors
812N/Aimport pkg.server.depot as ds
1431N/Aimport pkg.server.depotresponse as dr
873N/Aimport pkg.server.repository as sr
1431N/A
428N/A
466N/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."""
466N/A
466N/A def write(self, *args, **kwargs):
23N/A """Discard the bits."""
466N/A pass
466N/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):
1431N/A """Optionally emit a usage message and then exit using the specified
1633N/A exit code."""
1633N/A
1633N/A if text:
1633N/A emsg(text)
466N/A
466N/A if not full:
466N/A # The full usage message isn't desired.
1633N/A emsg(_("Try `pkg.depotd --help or -?' for more "
1633N/A "information."))
1633N/A sys.exit(retcode)
1633N/A
1633N/A print("""\
1633N/AUsage: /usr/lib/pkg.depotd [-a address] [-d inst_root] [-p port] [-s threads]
26N/A [-t socket_timeout] [--cfg] [--content-root]
589N/A [--disable-ops op[/1][,...]] [--debug feature_list]
1431N/A [--image-root dir] [--log-access dest] [--log-errors dest]
1633N/A [--mirror] [--nasty] [--nasty-sleep] [--proxy-base url]
1633N/A [--readonly] [--ssl-cert-file] [--ssl-dialog] [--ssl-key-file]
1431N/A [--sort-file-max-size size] [--writable-root dir]
1191N/A
1475N/A -a address The IP address on which to listen for connections. The
1475N/A default value is 0.0.0.0 (INADDR_ANY) which will listen
382N/A on all active interfaces. To listen on all active IPv6
1542N/A interfaces, use '::'.
1542N/A -d inst_root The file system path at which the server should find its
1542N/A repository data. Required unless PKG_REPO has been set
812N/A in the environment.
812N/A -p port The port number on which the instance should listen for
589N/A incoming package requests. The default value is 80 if
589N/A ssl certificate and key information has not been
589N/A provided; otherwise, the default value is 443.
589N/A -s threads The number of threads that will be started to serve
1431N/A requests. The default value is 10.
1431N/A -t timeout The maximum number of seconds the server should wait for
1431N/A a response from a client before closing a connection.
1431N/A The default value is 60.
1431N/A --cfg The pathname of the file to use when reading and writing
858N/A depot configuration data, or a fully qualified service
1633N/A fault management resource identifier (FMRI) of the SMF
1633N/A service or instance to read configuration data from.
1633N/A --content-root The file system path to the directory containing the
1633N/A the static and other web content used by the depot's
1633N/A browser user interface. The default value is
466N/A '/usr/share/lib/pkg'.
466N/A --disable-ops A comma separated list of operations that the depot
466N/A should not configure. If, for example, you wanted
466N/A to omit loading search v1, 'search/1' should be
466N/A provided as an argument, or to disable all search
466N/A operations, simply 'search'.
466N/A --debug The name of a debug feature to enable; or a whitespace
466N/A or comma separated list of features to enable.
466N/A Possible values are: headers, hash=sha1+sha256,
589N/A hash=sha256, hash=sha1+sha512_256, hash=sha512_256
589N/A --image-root The path to the image whose file information will be
589N/A used as a cache for file data.
1191N/A --log-access The destination for any access related information
1191N/A logged by the depot process. Possible values are:
1191N/A stderr, stdout, none, or an absolute pathname. The
1191N/A default value is stdout if stdout is a tty; otherwise
1191N/A the default value is none.
589N/A --log-errors The destination for any errors or other information
589N/A logged by the depot process. Possible values are:
589N/A stderr, stdout, none, or an absolute pathname. The
589N/A default value is stderr.
589N/A --mirror Package mirror mode; publishing and metadata operations
589N/A disallowed. Cannot be used with --readonly or
1431N/A --rebuild.
1431N/A --nasty Instruct the server to misbehave. At random intervals
1431N/A it will time-out, send bad responses, hang up on
1431N/A clients, and generally be hostile. The option
812N/A takes a value (1 to 100) for how nasty the server
812N/A should be.
812N/A --nasty-sleep In nasty mode (see --nasty), how many seconds to
812N/A randomly sleep when a random sleep occurs.
812N/A --proxy-base The url to use as the base for generating internal
812N/A redirects and content.
812N/A --readonly Read-only operation; modifying operations disallowed.
812N/A Cannot be used with --mirror or --rebuild.
812N/A --ssl-cert-file The absolute pathname to a PEM-encoded Certificate file.
812N/A This option must be used with --ssl-key-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 --ssl-dialog Specifies what method should be used to obtain the
1475N/A passphrase needed to decrypt the file specified by
1475N/A --ssl-key-file. Supported values are: builtin,
1475N/A exec:/path/to/program, smf, or an SMF FMRI. The
1475N/A default value is builtin. If smf is specified, an
975N/A SMF FMRI must be provided using the --cfg option.
975N/A --ssl-key-file The absolute pathname to a PEM-encoded Private Key file.
975N/A This option must be used with --ssl-cert-file. Usage of
975N/A this option will cause the depot to only respond to SSL
1633N/A requests on the provided port.
1633N/A --sort-file-max-size
1633N/A The maximum size of the indexer sort file. Used to
1633N/A limit the amount of RAM the depot uses for indexing,
1633N/A or increase it for speed.
1633N/A --writable-root The path to a directory to which the program has write
1633N/A access. Used with --readonly to allow server to
1633N/A create needed files, such as search indices, without
1633N/A needing write access to the package information.
14N/AOptions:
382N/A --help or -?
429N/A
14N/AEnvironment:
404N/A PKG_REPO Used as default inst_root if -d not provided.
404N/A PKG_DEPOT_CONTENT Used as default content_root if --content-root
30N/A not provided.""")
382N/A sys.exit(retcode)
30N/A
791N/Aclass OptionError(Exception):
689N/A """Option exception. """
689N/A
858N/A def __init__(self, *args):
858N/A Exception.__init__(self, *args)
858N/A
1431N/Aif __name__ == "__main__":
382N/A
812N/A setlocale(locale.LC_ALL, "")
382N/A gettext.install("pkg", "/usr/share/locale",
382N/A codeset=locale.getpreferredencoding())
382N/A
382N/A add_content = False
429N/A exit_ready = False
451N/A rebuild = False
461N/A reindex = False
1191N/A nasty = False
1191N/A
797N/A # Track initial configuration values.
1477N/A ivalues = { "pkg": {}, "nasty": {} }
812N/A if "PKG_REPO" in os.environ:
812N/A ivalues["pkg"]["inst_root"] = os.environ["PKG_REPO"]
812N/A
975N/A try:
1542N/A content_root = os.environ["PKG_DEPOT_CONTENT"]
1542N/A ivalues["pkg"]["content_root"] = content_root
258N/A except KeyError:
382N/A try:
382N/A content_root = os.path.join(os.environ['PKG_HOME'],
382N/A 'share/lib/pkg')
382N/A ivalues["pkg"]["content_root"] = content_root
30N/A except KeyError:
589N/A pass
589N/A
589N/A opt = None
589N/A addresses = set()
589N/A debug_features = []
589N/A disable_ops = []
589N/A repo_props = {}
589N/A socket_path = ""
589N/A user_cfg = None
466N/A try:
466N/A long_opts = ["add-content", "cfg=", "cfg-file=",
466N/A "content-root=", "debug=", "disable-ops=", "exit-ready",
466N/A "help", "image-root=", "log-access=", "log-errors=",
466N/A "llmirror", "mirror", "nasty=", "nasty-sleep=",
466N/A "proxy-base=", "readonly", "rebuild", "refresh-index",
466N/A "set-property=", "ssl-cert-file=", "ssl-dialog=",
466N/A "ssl-key-file=", "sort-file-max-size=", "writable-root="]
466N/A
466N/A opts, pargs = getopt.getopt(sys.argv[1:], "a:d:np:s:t:?",
466N/A long_opts)
466N/A
466N/A show_usage = False
466N/A for opt, arg in opts:
1431N/A if opt == "-a":
54N/A addresses.add(arg)
1633N/A elif opt == "-n":
1633N/A sys.exit(0)
1633N/A elif opt == "-d":
1633N/A ivalues["pkg"]["inst_root"] = arg
1633N/A elif opt == "-p":
1475N/A ivalues["pkg"]["port"] = arg
466N/A elif opt == "-s":
466N/A threads = int(arg)
1633N/A if threads < THREADS_MIN:
466N/A raise OptionError(
1633N/A "minimum value is {0:d}".format(
1633N/A THREADS_MIN))
135N/A if threads > THREADS_MAX:
135N/A raise OptionError(
135N/A "maximum value is {0:d}".format(
135N/A THREADS_MAX))
382N/A ivalues["pkg"]["threads"] = threads
135N/A elif opt == "-t":
135N/A ivalues["pkg"]["socket_timeout"] = arg
812N/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
382N/A elif opt == "--debug":
382N/A if arg is None or arg == "":
382N/A continue
1542N/A
1542N/A # A list of features can be specified using a
812N/A # "," or any whitespace character as separators.
812N/A if "," in arg:
589N/A features = arg.split(",")
589N/A else:
589N/A features = arg.split()
589N/A debug_features.extend(features)
589N/A
858N/A # We also allow key=value debug flags, which
858N/A # get set in pkg.client.debugvalues
858N/A for feature in features:
858N/A try:
858N/A key, val = feature.split("=", 1)
858N/A DebugValues.set_value(key, val)
858N/A except (AttributeError, ValueError):
858N/A pass
858N/A
858N/A elif opt == "--disable-ops":
858N/A if arg is None or arg == "":
858N/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)
1431N/A else:
1431N/A op = s
1431N/A ver = "*"
1431N/A
1431N/A if op not in \
1431N/A ds.DepotHTTP.REPO_OPS_DEFAULT:
1431N/A raise OptionError(
1431N/A "Invalid operation "
1431N/A "'{0}'.".format(s))
1431N/A disable_ops.append(s)
1431N/A elif opt == "--exit-ready":
1431N/A exit_ready = True
1431N/A elif opt == "--image-root":
1431N/A ivalues["pkg"]["image_root"] = arg
1431N/A elif opt.startswith("--log-"):
1431N/A prop = "log_{0}".format(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
1542N/A elif opt == "--llmirror":
1542N/A ivalues["pkg"]["mirror"] = True
466N/A ivalues["pkg"]["ll_mirror"] = True
466N/A ivalues["pkg"]["readonly"] = True
466N/A elif opt == "--nasty":
466N/A # ValueError is caught by caller.
466N/A nasty_value = int(arg)
466N/A if (nasty_value > 100 or nasty_value < 1):
1633N/A raise OptionError("Invalid value "
1633N/A "for nasty option.\n Please "
589N/A "choose a value between 1 and 100.")
589N/A nasty = True
1191N/A ivalues["nasty"]["nasty_level"] = nasty_value
1191N/A elif opt == "--nasty-sleep":
1191N/A # ValueError is caught by caller.
1191N/A sleep_value = int(arg)
1191N/A ivalues["nasty"]["nasty_sleep"] = sleep_value
1191N/A elif opt == "--proxy-base":
1191N/A # Attempt to decompose the url provided into
1191N/A # its base parts. This is done so we can
1191N/A # remove any scheme information since we
1191N/A # don't need it.
1191N/A scheme, netloc, path, params, query, \
1191N/A fragment = urlparse(arg,
1191N/A "http", allow_fragments=0)
1431N/A
1431N/A if not netloc:
1431N/A raise OptionError("Unable to "
1431N/A "determine the hostname from "
1431N/A "the provided URL; please use a "
1431N/A "fully qualified URL.")
1431N/A
1431N/A scheme = scheme.lower()
1431N/A if scheme not in ("http", "https"):
1431N/A raise OptionError("Invalid URL; http "
589N/A "and https are the only supported "
589N/A "schemes.")
589N/A
589N/A # Rebuild the url with the sanitized components.
589N/A ivalues["pkg"]["proxy_base"] = \
589N/A urlunparse((scheme, netloc, path,
589N/A params, query, fragment))
765N/A elif opt == "--readonly":
765N/A ivalues["pkg"]["readonly"] = True
765N/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.
589N/A #
765N/A # This flag is purposefully omitted in usage.
765N/A # The supported way to forcefully reindex is to
765N/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
873N/A exit_ready = True
873N/A elif opt == "--set-property":
135N/A try:
382N/A prop, p_value = arg.split("=", 1)
157N/A p_sec, p_name = prop.split(".", 1)
382N/A except ValueError:
429N/A usage(_("property arguments must be of "
429N/A "the form '<section.property>="
429N/A "<value>'."))
429N/A repo_props.setdefault(p_sec, {})
429N/A repo_props[p_sec][p_name] = p_value
429N/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):
812N/A raise OptionError("The path to "
812N/A "the Certificate file must be "
812N/A "absolute.")
812N/A elif not os.path.exists(arg):
812N/A raise OptionError("The specified "
812N/A "file does not exist.")
812N/A elif not os.path.isfile(arg):
812N/A raise OptionError("The specified "
812N/A "pathname is not a file.")
812N/A ivalues["pkg"]["ssl_cert_file"] = arg
812N/A elif opt == "--ssl-key-file":
812N/A if arg == "none" or arg == "":
812N/A # Assume this is an override to clear
812N/A # the value.
812N/A arg = ""
812N/A elif not os.path.isabs(arg):
812N/A raise OptionError("The path to "
812N/A "the Private Key file must be "
812N/A "absolute.")
812N/A elif not os.path.exists(arg):
812N/A raise OptionError("The specified "
812N/A "file does not exist.")
812N/A elif not os.path.isfile(arg):
812N/A raise OptionError("The specified "
812N/A "pathname is not a file.")
812N/A ivalues["pkg"]["ssl_key_file"] = arg
812N/A elif opt == "--ssl-dialog":
812N/A if arg != "builtin" and \
812N/A arg != "smf" and not \
812N/A arg.startswith("exec:/") and not \
812N/A arg.startswith("svc:"):
812N/A raise OptionError("Invalid value "
812N/A "specified. Expected: builtin, "
812N/A "exec:/path/to/program, smf, or "
812N/A "an SMF FMRI.")
812N/A
812N/A if arg.startswith("exec:"):
812N/A if os_util.get_canonical_os_type() != \
812N/A "unix":
812N/A # Don't allow a somewhat
812N/A # insecure authentication method
812N/A # on some platforms.
812N/A raise OptionError("exec is "
873N/A "not a supported dialog "
873N/A "type for this operating "
873N/A "system.")
873N/A
873N/A f = os.path.abspath(arg.split(
873N/A "exec:")[1])
873N/A if not os.path.isfile(f):
812N/A raise OptionError("Invalid "
812N/A "file path specified for "
812N/A "exec.")
812N/A ivalues["pkg"]["ssl_dialog"] = arg
812N/A elif opt == "--sort-file-max-size":
812N/A ivalues["pkg"]["sort_file_max_size"] = arg
812N/A elif opt == "--writable-root":
812N/A ivalues["pkg"]["writable_root"] = arg
812N/A
812N/A # Set accumulated values.
812N/A if debug_features:
812N/A ivalues["pkg"]["debug"] = debug_features
1475N/A if disable_ops:
1475N/A ivalues["pkg"]["disable_ops"] = disable_ops
1475N/A if addresses:
1475N/A ivalues["pkg"]["address"] = list(addresses)
1475N/A
975N/A if DebugValues:
975N/A reload(pkg.digest)
975N/A
975N/A # Build configuration object.
975N/A dconf = ds.DepotConfig(target=user_cfg, overrides=ivalues)
873N/A except getopt.GetoptError as _e:
873N/A usage("pkg.depotd: {0}".format(_e.msg))
873N/A except api_errors.ApiException as _e:
873N/A usage("pkg.depotd: {0}".format(str(_e)))
382N/A except OptionError as _e:
466N/A usage("pkg.depotd: option: {0} -- {1}".format(opt, _e))
466N/A except (ArithmeticError, ValueError):
451N/A usage("pkg.depotd: illegal option value: {0} specified " \
1633N/A "for option: {1}".format(arg, opt))
1633N/A
1633N/A if show_usage:
1542N/A usage(retcode=0, full=True)
1542N/A
445N/A if not dconf.get_property("pkg", "log_errors"):
466N/A dconf.set_property("pkg", "log_errors", "stderr")
1542N/A
1633N/A # If stdout is a tty, then send access output there by default instead
1633N/A # of discarding it.
1020N/A if not dconf.get_property("pkg", "log_access"):
1020N/A if os.isatty(sys.stdout.fileno()):
1020N/A dconf.set_property("pkg", "log_access", "stdout")
1020N/A else:
1020N/A dconf.set_property("pkg", "log_access", "none")
451N/A
812N/A # Check for invalid option combinations.
812N/A image_root = dconf.get_property("pkg", "image_root")
812N/A inst_root = dconf.get_property("pkg", "inst_root")
812N/A mirror = dconf.get_property("pkg", "mirror")
812N/A ll_mirror = dconf.get_property("pkg", "ll_mirror")
812N/A readonly = dconf.get_property("pkg", "readonly")
812N/A writable_root = dconf.get_property("pkg", "writable_root")
812N/A if rebuild and add_content:
812N/A usage("--add-content cannot be used with --rebuild")
429N/A if rebuild and reindex:
429N/A usage("--refresh-index cannot be used with --rebuild")
1542N/A if (rebuild or add_content) and (readonly or mirror):
1836N/A usage("--readonly and --mirror cannot be used with --rebuild "
1836N/A "or --add-content")
1836N/A if reindex and mirror:
1836N/A usage("--mirror cannot be used with --refresh-index")
1836N/A if reindex and readonly and not writable_root:
429N/A usage("--readonly can only be used with --refresh-index if "
612N/A "--writable-root is used")
1542N/A if image_root and not ll_mirror:
612N/A usage("--image-root can only be used with --llmirror.")
386N/A if image_root and writable_root:
812N/A usage("--image_root and --writable-root cannot be used "
812N/A "together.")
812N/A if image_root and inst_root:
812N/A usage("--image-root and -d cannot be used together.")
812N/A
812N/A # If the image format changes this may need to be reexamined.
812N/A if image_root:
812N/A inst_root = os.path.join(image_root, "var", "pkg")
812N/A
812N/A # Set any values using defaults if they weren't provided.
812N/A
873N/A # Only use the first value for now; multiple bind addresses may be
1836N/A # supported later.
1836N/A address = dconf.get_property("pkg", "address")
1836N/A if address:
1836N/A address = address[0]
812N/A elif not address:
812N/A dconf.set_property("pkg", "address", [HOST_DEFAULT])
812N/A address = dconf.get_property("pkg", "address")[0]
812N/A
812N/A if not inst_root:
812N/A usage("Either PKG_REPO or -d must be provided")
812N/A
812N/A content_root = dconf.get_property("pkg", "content_root")
812N/A if not content_root:
812N/A dconf.set_property("pkg", "content_root", CONTENT_PATH_DEFAULT)
812N/A content_root = dconf.get_property("pkg", "content_root")
812N/A
812N/A port = dconf.get_property("pkg", "port")
812N/A ssl_cert_file = dconf.get_property("pkg", "ssl_cert_file")
812N/A ssl_key_file = dconf.get_property("pkg", "ssl_key_file")
812N/A if (ssl_cert_file and not ssl_key_file) or (ssl_key_file and not
812N/A ssl_cert_file):
812N/A usage("The --ssl-cert-file and --ssl-key-file options must "
812N/A "must both be provided when using either option.")
812N/A elif not port:
812N/A if ssl_cert_file and ssl_key_file:
812N/A dconf.set_property("pkg", "port", SSL_PORT_DEFAULT)
812N/A else:
873N/A dconf.set_property("pkg", "port", PORT_DEFAULT)
1836N/A port = dconf.get_property("pkg", "port")
1836N/A
812N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
873N/A if not socket_timeout:
1836N/A dconf.set_property("pkg", "socket_timeout",
1836N/A SOCKET_TIMEOUT_DEFAULT)
1836N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
812N/A
812N/A threads = dconf.get_property("pkg", "threads")
812N/A if not threads:
812N/A dconf.set_property("pkg", "threads", THREADS_DEFAULT)
812N/A threads = dconf.get_property("pkg", "threads")
452N/A
466N/A # If the program is going to reindex, the port is irrelevant since
858N/A # the program will not bind to a port.
382N/A if not exit_ready:
466N/A try:
965N/A cherrypy.process.servers.check_port(address, port)
858N/A except Exception as e:
1836N/A emsg("pkg.depotd: unable to bind to the specified "
382N/A "port: {0:d}. Reason: {1}".format(port, e))
858N/A sys.exit(1)
858N/A else:
858N/A # Not applicable if we're not going to serve content
382N/A dconf.set_property("pkg", "content_root", "")
742N/A
858N/A # Any relative paths should be made absolute using pkg_root. 'pkg_root'
466N/A # is a special property that was added to enable internal deployment of
466N/A # multiple disparate versions of the pkg.depotd software.
858N/A pkg_root = dconf.get_property("pkg", "pkg_root")
858N/A
858N/A repo_config_file = dconf.get_property("pkg", "cfg_file")
858N/A if repo_config_file and not os.path.isabs(repo_config_file):
858N/A repo_config_file = os.path.join(pkg_root, repo_config_file)
858N/A
858N/A if content_root and not os.path.isabs(content_root):
858N/A content_root = os.path.join(pkg_root, content_root)
858N/A
858N/A if inst_root and not os.path.isabs(inst_root):
858N/A inst_root = os.path.join(pkg_root, inst_root)
858N/A
466N/A if ssl_cert_file:
466N/A if ssl_cert_file == "none":
466N/A ssl_cert_file = None
466N/A elif not os.path.isabs(ssl_cert_file):
466N/A ssl_cert_file = os.path.join(pkg_root, ssl_cert_file)
466N/A
466N/A if ssl_key_file:
466N/A if ssl_key_file == "none":
466N/A ssl_key_file = None
466N/A elif not os.path.isabs(ssl_key_file):
466N/A ssl_key_file = os.path.join(pkg_root, ssl_key_file)
466N/A
466N/A if writable_root and not os.path.isabs(writable_root):
466N/A writable_root = os.path.join(pkg_root, writable_root)
466N/A
466N/A # Setup SSL if requested.
466N/A key_data = None
466N/A ssl_dialog = dconf.get_property("pkg", "ssl_dialog")
466N/A if not exit_ready and ssl_cert_file and ssl_key_file and \
466N/A ssl_dialog != "builtin":
466N/A cmdline = None
466N/A def get_ssl_passphrase(*ignored):
466N/A p = None
466N/A try:
466N/A if cmdline:
466N/A cmdargs = shlex.split(cmdline)
466N/A else:
466N/A cmdargs = []
466N/A p = subprocess.Popen(cmdargs,
382N/A stdout=subprocess.PIPE,
612N/A stderr=None)
612N/A p.wait()
612N/A except Exception as __e:
612N/A emsg("pkg.depotd: an error occurred while "
1431N/A "executing [{0}]; unable to obtain the "
1431N/A "passphrase needed to decrypt the SSL "
1542N/A "private key file: {1}".format(cmdline,
1431N/A __e))
1431N/A sys.exit(1)
1431N/A return p.stdout.read().strip("\n")
1431N/A
1542N/A if ssl_dialog.startswith("exec:"):
1542N/A exec_path = ssl_dialog.split("exec:")[1]
1431N/A if not os.path.isabs(exec_path):
1431N/A exec_path = os.path.join(pkg_root, exec_path)
1431N/A cmdline = "{0} {1} {2:d}".format(exec_path, "''", port)
1431N/A elif ssl_dialog == "smf" or ssl_dialog.startswith("svc:"):
1431N/A if ssl_dialog == "smf":
1431N/A # Assume the configuration target was an SMF
1431N/A # FMRI and let svcprop fail with an error if
1431N/A # it wasn't.
1431N/A svc_fmri = dconf.target
1431N/A else:
1431N/A svc_fmri = ssl_dialog
1431N/A cmdline = "/usr/bin/svcprop -p " \
1431N/A "pkg_secure/ssl_key_passphrase {0}".format(svc_fmri)
1672N/A
1672N/A # The key file requires decryption, but the user has requested
1672N/A # exec-based authentication, so it will have to be decoded first
1779N/A # to an un-named temporary file.
1672N/A try:
1431N/A with open(ssl_key_file, "rb") as key_file:
1431N/A pkey = crypto.load_privatekey(
612N/A crypto.FILETYPE_PEM, key_file.read(),
1431N/A get_ssl_passphrase)
1431N/A
1431N/A key_data = tempfile.TemporaryFile()
1431N/A key_data.write(crypto.dump_privatekey(
1431N/A crypto.FILETYPE_PEM, pkey))
1431N/A key_data.seek(0)
1431N/A except EnvironmentError as _e:
1431N/A emsg("pkg.depotd: unable to read the SSL private key "
617N/A "file: {0}".format(_e))
1431N/A sys.exit(1)
1431N/A except crypto.Error as _e:
1431N/A emsg("pkg.depotd: authentication or cryptography "
1431N/A "failure while attempting to decode\nthe SSL "
975N/A "private key file: {0}".format(_e))
1779N/A sys.exit(1)
1431N/A else:
1431N/A # Redirect the server to the decrypted key file.
617N/A ssl_key_file = "/dev/fd/{0:d}".format(key_data.fileno())
1431N/A
1542N/A # Setup our global configuration.
1542N/A gconf = {
1542N/A "checker.on": True,
1542N/A "environment": "production",
1542N/A "log.screen": False,
1542N/A "server.max_request_body_size": MAX_REQUEST_BODY_SIZE,
1542N/A "server.shutdown_timeout": 0,
1779N/A "server.socket_host": address,
1542N/A "server.socket_port": port,
1542N/A "server.socket_timeout": socket_timeout,
1542N/A "server.ssl_certificate": ssl_cert_file,
1542N/A "server.ssl_private_key": ssl_key_file,
1542N/A "server.thread_pool": threads,
1542N/A "tools.log_headers.on": True,
1542N/A "tools.encode.on": True
1542N/A }
1431N/A
1431N/A if "headers" in dconf.get_property("pkg", "debug"):
1431N/A # Despite its name, this only logs headers when there is an
1431N/A # error; it's redundant with the debug feature enabled.
1431N/A gconf["tools.log_headers.on"] = False
1431N/A
1431N/A # Causes the headers of every request to be logged to the error
612N/A # log; even if an exception occurs.
451N/A gconf["tools.log_headers_always.on"] = True
382N/A cherrypy.tools.log_headers_always = cherrypy.Tool(
452N/A "on_start_resource",
452N/A cherrypy.lib.cptools.log_request_headers)
452N/A
452N/A log_cfg = {
873N/A "access": dconf.get_property("pkg", "log_access"),
452N/A "errors": dconf.get_property("pkg", "log_errors")
382N/A }
382N/A
1431N/A # If stdin is not a tty and the pkgdepot controller isn't being used,
382N/A # then assume process will be daemonized and redirect output.
382N/A if not os.environ.get("PKGDEPOT_CONTROLLER") and \
382N/A not os.isatty(sys.stdin.fileno()):
145N/A # Ensure log handlers are setup to use the file descriptors for
451N/A # stdout and stderr as the Daemonizer (used for test suite and
451N/A # SMF service) requires this.
451N/A if log_cfg["access"] == "stdout":
451N/A log_cfg["access"] = "/dev/fd/{0:d}".format(
451N/A sys.stdout.fileno())
451N/A elif log_cfg["access"] == "stderr":
451N/A log_cfg["access"] = "/dev/fd/{0:d}".format(
451N/A sys.stderr.fileno())
451N/A elif log_cfg["access"] == "none":
451N/A log_cfg["access"] = "/dev/null"
451N/A
451N/A if log_cfg["errors"] == "stderr":
451N/A log_cfg["errors"] = "/dev/fd/{0:d}".format(
451N/A sys.stderr.fileno())
451N/A elif log_cfg["errors"] == "stdout":
451N/A log_cfg["errors"] = "/dev/fd/{0:d}".format(
451N/A sys.stdout.fileno())
451N/A elif log_cfg["errors"] == "none":
814N/A log_cfg["errors"] = "/dev/null"
1431N/A
466N/A log_type_map = {
873N/A "errors": {
812N/A "param": "log.error_file",
812N/A "attr": "error_log"
873N/A },
812N/A "access": {
"param": "log.access_file",
"attr": "access_log"
}
}
for log_type in log_type_map:
dest = log_cfg[log_type]
if dest in ("stdout", "stderr", "none"):
if dest == "none":
h = logging.StreamHandler(LogSink())
else:
h = logging.StreamHandler(eval("sys.{0}".format(
dest)))
h.setLevel(logging.DEBUG)
h.setFormatter(cherrypy._cplogging.logfmt)
log_obj = eval("cherrypy.log.{0}".format(
log_type_map[log_type]["attr"]))
log_obj.addHandler(h)
# Since we've replaced cherrypy's log handler with our
# own, we don't want the output directed to a file.
dest = ""
elif dest:
if not os.path.isabs(dest):
dest = os.path.join(pkg_root, dest)
gconf[log_type_map[log_type]["param"]] = dest
cherrypy.config.update(gconf)
# Now that our logging, etc. has been setup, it's safe to perform any
# remaining preparation.
# Initialize repository state.
if not readonly:
# Not readonly, so assume a new repository should be created.
try:
sr.repository_create(inst_root, properties=repo_props)
except sr.RepositoryExistsError:
# Already exists, nothing to do.
pass
except (api_errors.ApiException, sr.RepositoryError) as _e:
emsg("pkg.depotd: {0}".format(_e))
sys.exit(1)
try:
sort_file_max_size = dconf.get_property("pkg",
"sort_file_max_size")
repo = sr.Repository(cfgpathname=repo_config_file,
log_obj=cherrypy, mirror=mirror, properties=repo_props,
read_only=readonly, root=inst_root,
sort_file_max_size=sort_file_max_size,
writable_root=writable_root)
except (RuntimeError, sr.RepositoryError) as _e:
emsg("pkg.depotd: {0}".format(_e))
sys.exit(1)
except search_errors.IndexingException as _e:
emsg("pkg.depotd: {0}".format(str(_e)), "INDEX")
sys.exit(1)
except api_errors.ApiException as _e:
emsg("pkg.depotd: {0}".format(str(_e)))
sys.exit(1)
if not rebuild and not add_content and not repo.mirror and \
not (repo.read_only and not repo.writable_root):
# Automatically update search indexes on startup if not already
# told to, and not in readonly/mirror mode.
reindex = True
if reindex:
try:
# Only execute a index refresh here if --exit-ready was
# requested; it will be handled later in the setup
# process for other cases.
if repo.root and exit_ready:
repo.refresh_index()
except (sr.RepositoryError, search_errors.IndexingException,
api_errors.ApiException) as e:
emsg(str(e), "INDEX")
sys.exit(1)
elif rebuild:
try:
repo.rebuild(build_index=True)
except sr.RepositoryError as e:
emsg(str(e), "REBUILD")
sys.exit(1)
except (search_errors.IndexingException,
api_errors.UnknownErrors,
api_errors.PermissionsException) as e:
emsg(str(e), "INDEX")
sys.exit(1)
elif add_content:
try:
repo.add_content()
repo.refresh_index()
except sr.RepositoryError as e:
emsg(str(e), "ADD_CONTENT")
sys.exit(1)
except (search_errors.IndexingException,
api_errors.UnknownErrors,
api_errors.PermissionsException) as e:
emsg(str(e), "INDEX")
sys.exit(1)
# Ready to start depot; exit now if requested.
if exit_ready:
sys.exit(0)
# Next, initialize depot.
if nasty:
depot = ds.NastyDepotHTTP(repo, dconf)
else:
depot = ds.DepotHTTP(repo, dconf)
# Now build our site configuration.
conf = {
"/": {
# We have to override cherrypy's default response_class so that
# we have access to the write() callable to stream data
# directly to the client.
"wsgi.response_class": dr.DepotResponse,
},
"/robots.txt": {
"tools.staticfile.on": True,
"tools.staticfile.filename": os.path.join(depot.web_root,
"robots.txt")
},
}
proxy_base = dconf.get_property("pkg", "proxy_base")
if proxy_base:
# This changes the base URL for our server, and is primarily
# intended to allow our depot process to operate behind Apache
# or some other webserver process.
#
# Visit the following URL for more information:
# http://cherrypy.org/wiki/BuiltinTools#tools.proxy
proxy_conf = {
"tools.proxy.on": True,
"tools.proxy.local": "",
"tools.proxy.base": proxy_base
}
# Now merge or add our proxy configuration information into the
# existing configuration.
for entry in proxy_conf:
conf["/"][entry] = proxy_conf[entry]
if ll_mirror:
ds.DNSSD_Plugin(cherrypy.engine, gconf).subscribe()
if reindex:
# Tell depot to update search indexes when possible;
# this is done as a background task so that packages
# can be served immediately while search indexes are
# still being updated.
depot._queue_refresh_index()
# If stdin is not a tty and the pkgdepot controller isn't being used,
# then assume process should be daemonized.
if not os.environ.get("PKGDEPOT_CONTROLLER") and \
not os.isatty(sys.stdin.fileno()):
# Translate the values in log_cfg into paths.
Daemonizer(cherrypy.engine, stderr=log_cfg["errors"],
stdout=log_cfg["access"]).subscribe()
try:
root = cherrypy.Application(depot)
cherrypy.quickstart(root, config=conf)
except Exception as _e:
emsg("pkg.depotd: unknown error starting depot server, " \
"illegal option value specified?")
emsg(_e)
sys.exit(1)