depot.py revision 2962
3177N/A#!/usr/bin/python2.6
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#
3313N/A# Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
395N/A#
290N/A
3143N/A# pkg.depotd - package repository daemon
883N/A
454N/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
448N/A# various GET operations that a pkg(1) client makes. This split is expected to
3234N/A# be made more explicit, by constraining the pkg(1) operations such that they
290N/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
290N/A# operations, and must manipulate the various state files--catalogs, in
383N/A# particular--such that the pkg(1) pull client can operately accurately with
290N/A# only a basic HTTP/HTTPS server in place.
395N/A
290N/A# XXX Although we pushed the evaluation of next-version, etc. to the pull
849N/A# client, we should probably provide a query API to do same on the server, for
1516N/A# dumb clients (like a notification service).
2508N/A
290N/A# The default path for static and other web content.
2535N/ACONTENT_PATH_DEFAULT = "/usr/share/lib/pkg"
2698N/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
2535N/A# by cherrypy is 2048 * 1024 * 1024 - 1 (just short of 2048MB), but the default
2561N/A# here is purposefully conservative.
290N/AMAX_REQUEST_BODY_SIZE = 512 * 1024 * 1024
2508N/A# The default host/port(s) to serve data from.
383N/AHOST_DEFAULT = "0.0.0.0"
290N/APORT_DEFAULT = 80
290N/ASSL_PORT_DEFAULT = 443
2339N/A# The minimum number of threads allowed.
2535N/ATHREADS_MIN = 1
290N/A# The default number of threads to start.
290N/ATHREADS_DEFAULT = 60
2535N/A# The maximum number of threads that can be started.
2535N/ATHREADS_MAX = 5000
290N/A# The default server socket timeout in seconds. We want this to be longer than
290N/A# the normal default of 10 seconds to accommodate clients with poor quality
2508N/A# connections.
2508N/ASOCKET_TIMEOUT_DEFAULT = 60
290N/A
1660N/Aimport getopt
1660N/Aimport gettext
1660N/Aimport locale
1660N/Aimport logging
1660N/Aimport os
1660N/Aimport os.path
1660N/Aimport OpenSSL.crypto as crypto
1660N/Aimport subprocess
1660N/Aimport sys
1660N/Aimport tempfile
1660N/Aimport urlparse
1660N/A
1660N/Atry:
1660N/A import cherrypy
1660N/A version = cherrypy.__version__.split('.')
1660N/A if map(int, version) < [3, 1, 0]:
1660N/A raise ImportError
1660N/A elif map(int, version) >= [3, 2, 0]:
448N/A raise ImportError
448N/Aexcept ImportError:
2828N/A print >> sys.stderr, """cherrypy 3.1.0 or greater (but less than """ \
3253N/A """3.2.0) is required to use this program."""
2828N/A sys.exit(2)
534N/A
534N/Aimport cherrypy.process.servers
534N/Afrom cherrypy.process.plugins import Daemonizer
534N/A
534N/Afrom pkg.misc import msg, emsg, setlocale
534N/Afrom pkg.client.debugvalues import DebugValues
534N/A
290N/Aimport pkg
290N/Aimport pkg.client.api_errors as api_errors
954N/Aimport pkg.config as cfg
954N/Aimport pkg.portable.util as os_util
954N/Aimport pkg.search_errors as search_errors
954N/Aimport pkg.server.depot as ds
534N/Aimport pkg.server.depotresponse as dr
1099N/Aimport pkg.server.repository as sr
3261N/A
290N/A
3117N/Aclass LogSink(object):
3117N/A """This is a dummy object that we can use to discard log entries
3203N/A without relying on non-portable interfaces such as /dev/null."""
3117N/A
290N/A def write(self, *args, **kwargs):
3246N/A """Discard the bits."""
3246N/A pass
3246N/A
3246N/A def flush(self, *args, **kwargs):
3246N/A """Discard the bits."""
3246N/A pass
3246N/A
3246N/A
290N/Adef usage(text=None, retcode=2, full=False):
290N/A """Optionally emit a usage message and then exit using the specified
661N/A exit code."""
2867N/A
290N/A if text:
2494N/A emsg(text)
2494N/A
2494N/A if not full:
2516N/A # The full usage message isn't desired.
2516N/A emsg(_("Try `pkg.depotd --help or -?' for more "
2516N/A "information."))
2516N/A sys.exit(retcode)
2516N/A
2516N/A print """\
2516N/AUsage: /usr/lib/pkg.depotd [-a address] [-d inst_root] [-p port] [-s threads]
290N/A [-t socket_timeout] [--cfg] [--content-root]
3185N/A [--disable-ops op[/1][,...]] [--debug feature_list]
2523N/A [--image-root dir] [--log-access dest] [--log-errors dest]
3138N/A [--mirror] [--nasty] [--nasty-sleep] [--proxy-base url]
2390N/A [--readonly] [--ssl-cert-file] [--ssl-dialog] [--ssl-key-file]
1498N/A [--sort-file-max-size size] [--writable-root dir]
1498N/A
2867N/A -a address The IP address on which to listen for connections. The
2310N/A default value is 0.0.0.0 (INADDR_ANY) which will listen
3237N/A on all active interfaces. To listen on all active IPv6
2310N/A interfaces, use '::'.
2310N/A -d inst_root The file system path at which the server should find its
2852N/A repository data. Required unless PKG_REPO has been set
3237N/A in the environment.
2852N/A -p port The port number on which the instance should listen for
2852N/A incoming package requests. The default value is 80 if
2852N/A ssl certificate and key information has not been
2535N/A provided; otherwise, the default value is 443.
2867N/A -s threads The number of threads that will be started to serve
2867N/A requests. The default value is 10.
2310N/A -t timeout The maximum number of seconds the server should wait for
290N/A a response from a client before closing a connection.
1674N/A The default value is 60.
1674N/A --cfg The pathname of the file to use when reading and writing
2262N/A depot configuration data, or a fully qualified service
1674N/A fault management resource identifier (FMRI) of the SMF
395N/A service or instance to read configuration data from.
430N/A --content-root The file system path to the directory containing the
395N/A the static and other web content used by the depot's
1544N/A browser user interface. The default value is
1968N/A '/usr/share/lib/pkg'.
1557N/A --disable-ops A comma separated list of operations that the depot
1903N/A should not configure. If, for example, you wanted
2046N/A to omit loading search v1, 'search/1' should be
2240N/A provided as an argument, or to disable all search
1506N/A operations, simply 'search'.
2928N/A --debug The name of a debug feature to enable; or a whitespace
395N/A or comma separated list of features to enable.
395N/A Possible values are: headers, hash=sha1+sha256,
2026N/A hash=sha256
395N/A --image-root The path to the image whose file information will be
395N/A used as a cache for file data.
395N/A --log-access The destination for any access related information
2310N/A logged by the depot process. Possible values are:
2852N/A stderr, stdout, none, or an absolute pathname. The
395N/A default value is stdout if stdout is a tty; otherwise
661N/A the default value is none.
2867N/A --log-errors The destination for any errors or other information
2867N/A logged by the depot process. Possible values are:
2867N/A stderr, stdout, none, or an absolute pathname. The
2867N/A default value is stderr.
2867N/A --mirror Package mirror mode; publishing and metadata operations
2852N/A disallowed. Cannot be used with --readonly or
2310N/A --rebuild.
3216N/A --nasty Instruct the server to misbehave. At random intervals
3216N/A it will time-out, send bad responses, hang up on
2867N/A clients, and generally be hostile. The option
2867N/A takes a value (1 to 100) for how nasty the server
2867N/A should be.
661N/A --nasty-sleep In nasty mode (see --nasty), how many seconds to
3185N/A randomly sleep when a random sleep occurs.
3185N/A --proxy-base The url to use as the base for generating internal
3185N/A redirects and content.
395N/A --readonly Read-only operation; modifying operations disallowed.
849N/A Cannot be used with --mirror or --rebuild.
290N/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
1968N/A requests on the provided port.
395N/A --ssl-dialog Specifies what method should be used to obtain the
395N/A passphrase needed to decrypt the file specified by
395N/A --ssl-key-file. Supported values are: builtin,
395N/A exec:/path/to/program, smf, or an SMF FMRI. The
395N/A default value is builtin. If smf is specified, an
395N/A SMF FMRI must be provided using the --cfg option.
395N/A --ssl-key-file The absolute pathname to a PEM-encoded Private Key file.
395N/A This option must be used with --ssl-cert-file. Usage of
395N/A this option will cause the depot to only respond to SSL
395N/A requests on the provided port.
395N/A --sort-file-max-size
290N/A The maximum size of the indexer sort file. Used to
290N/A limit the amount of RAM the depot uses for indexing,
395N/A or increase it for speed.
395N/A --writable-root The path to a directory to which the program has write
1231N/A access. Used with --readonly to allow server to
1557N/A create needed files, such as search indices, without
1903N/A needing write access to the package information.
1557N/AOptions:
395N/A --help or -?
395N/A
395N/AEnvironment:
395N/A PKG_REPO Used as default inst_root if -d not provided.
395N/A PKG_DEPOT_CONTENT Used as default content_root if --content-root
395N/A not provided."""
395N/A sys.exit(retcode)
395N/A
395N/Aclass OptionError(Exception):
395N/A """Option exception. """
3185N/A
3185N/A def __init__(self, *args):
3185N/A Exception.__init__(self, *args)
395N/A
290N/Aif __name__ == "__main__":
290N/A
430N/A setlocale(locale.LC_ALL, "")
395N/A gettext.install("pkg", "/usr/share/locale",
395N/A codeset=locale.getpreferredencoding())
395N/A
395N/A add_content = False
1302N/A exit_ready = False
395N/A rebuild = False
395N/A reindex = False
290N/A nasty = False
3313N/A
3313N/A # Track initial configuration values.
3139N/A ivalues = { "pkg": {}, "nasty": {} }
395N/A if "PKG_REPO" in os.environ:
3139N/A ivalues["pkg"]["inst_root"] = os.environ["PKG_REPO"]
3139N/A
3139N/A try:
3139N/A content_root = os.environ["PKG_DEPOT_CONTENT"]
3139N/A ivalues["pkg"]["content_root"] = content_root
3139N/A except KeyError:
3139N/A try:
3139N/A content_root = os.path.join(os.environ['PKG_HOME'],
3139N/A 'share/lib/pkg')
3139N/A ivalues["pkg"]["content_root"] = content_root
3139N/A except KeyError:
3139N/A pass
3139N/A
3139N/A opt = None
3139N/A addresses = set()
3139N/A debug_features = []
395N/A disable_ops = []
3139N/A repo_props = {}
3139N/A socket_path = ""
3139N/A user_cfg = None
3139N/A try:
3139N/A long_opts = ["add-content", "cfg=", "cfg-file=",
3139N/A "content-root=", "debug=", "disable-ops=", "exit-ready",
3139N/A "help", "image-root=", "log-access=", "log-errors=",
395N/A "llmirror", "mirror", "nasty=", "nasty-sleep=",
3139N/A "proxy-base=", "readonly", "rebuild", "refresh-index",
3139N/A "set-property=", "ssl-cert-file=", "ssl-dialog=",
3139N/A "ssl-key-file=", "sort-file-max-size=", "writable-root="]
3139N/A
3139N/A opts, pargs = getopt.getopt(sys.argv[1:], "a:d:np:s:t:?",
2516N/A long_opts)
2516N/A
3139N/A show_usage = False
3139N/A for opt, arg in opts:
3139N/A if opt == "-a":
3139N/A addresses.add(arg)
3139N/A elif opt == "-n":
3139N/A sys.exit(0)
3139N/A elif opt == "-d":
3139N/A ivalues["pkg"]["inst_root"] = arg
3139N/A elif opt == "-p":
3139N/A ivalues["pkg"]["port"] = arg
3139N/A elif opt == "-s":
3139N/A threads = int(arg)
3139N/A if threads < THREADS_MIN:
3139N/A raise OptionError, \
3139N/A "minimum value is %d" % THREADS_MIN
2516N/A if threads > THREADS_MAX:
3139N/A raise OptionError, \
3139N/A "maximum value is %d" % THREADS_MAX
3139N/A ivalues["pkg"]["threads"] = threads
3139N/A elif opt == "-t":
3139N/A ivalues["pkg"]["socket_timeout"] = arg
3139N/A elif opt == "--add-content":
2516N/A add_content = True
3139N/A elif opt == "--cfg":
3139N/A user_cfg = arg
3139N/A elif opt == "--cfg-file":
3139N/A ivalues["pkg"]["cfg_file"] = arg
3139N/A elif opt == "--content-root":
2516N/A ivalues["pkg"]["content_root"] = arg
2516N/A elif opt == "--debug":
3139N/A if arg is None or arg == "":
3139N/A continue
3139N/A
3139N/A # A list of features can be specified using a
3139N/A # "," or any whitespace character as separators.
3139N/A if "," in arg:
3139N/A features = arg.split(",")
3139N/A else:
3139N/A features = arg.split()
3139N/A debug_features.extend(features)
3139N/A
3139N/A # We also allow key=value debug flags, which
3139N/A # get set in pkg.client.debugvalues
3139N/A for feature in features:
3139N/A try:
2516N/A key, val = feature.split("=", 1)
3139N/A DebugValues.set_value(key, val)
3139N/A except (AttributeError, ValueError):
3139N/A pass
3139N/A
3139N/A elif opt == "--disable-ops":
3139N/A if arg is None or arg == "":
2516N/A raise OptionError, \
3139N/A "An argument must be specified."
3139N/A
3139N/A disableops = arg.split(",")
3139N/A for s in disableops:
3139N/A if "/" in s:
2516N/A op, ver = s.rsplit("/", 1)
395N/A else:
395N/A op = s
395N/A ver = "*"
395N/A
395N/A if op not in \
2339N/A ds.DepotHTTP.REPO_OPS_DEFAULT:
1191N/A raise OptionError(
1452N/A "Invalid operation "
1231N/A "'%s'." % s)
2046N/A disable_ops.append(s)
395N/A elif opt == "--exit-ready":
395N/A exit_ready = True
424N/A elif opt == "--image-root":
395N/A ivalues["pkg"]["image_root"] = arg
742N/A elif opt.startswith("--log-"):
2339N/A prop = "log_%s" % opt.lstrip("--log-")
2339N/A ivalues["pkg"][prop] = arg
2693N/A elif opt in ("--help", "-?"):
2690N/A show_usage = True
2339N/A elif opt == "--mirror":
3094N/A ivalues["pkg"]["mirror"] = True
2339N/A elif opt == "--llmirror":
2690N/A ivalues["pkg"]["mirror"] = True
2690N/A ivalues["pkg"]["ll_mirror"] = True
2693N/A ivalues["pkg"]["readonly"] = True
2693N/A elif opt == "--nasty":
2690N/A # ValueError is caught by caller.
2690N/A nasty_value = int(arg)
2339N/A if (nasty_value > 100 or nasty_value < 1):
2339N/A raise OptionError, "Invalid value " \
742N/A "for nasty option.\n Please " \
742N/A "choose a value between 1 and 100."
742N/A nasty = True
742N/A ivalues["nasty"]["nasty_level"] = nasty_value
742N/A elif opt == "--nasty-sleep":
742N/A # ValueError is caught by caller.
742N/A sleep_value = int(arg)
742N/A ivalues["nasty"]["nasty_sleep"] = sleep_value
742N/A elif opt == "--proxy-base":
3136N/A # Attempt to decompose the url provided into
2688N/A # its base parts. This is done so we can
2688N/A # remove any scheme information since we
2688N/A # don't need it.
2688N/A scheme, netloc, path, params, query, \
2688N/A fragment = urlparse.urlparse(arg,
2688N/A "http", allow_fragments=0)
2688N/A
2688N/A if not netloc:
2688N/A raise OptionError, "Unable to " \
742N/A "determine the hostname from " \
2310N/A "the provided URL; please use a " \
2852N/A "fully qualified URL."
1902N/A
2867N/A scheme = scheme.lower()
2867N/A if scheme not in ("http", "https"):
1099N/A raise OptionError, "Invalid URL; http " \
2867N/A "and https are the only supported " \
3216N/A "schemes."
2338N/A
2338N/A # Rebuild the url with the sanitized components.
2310N/A ivalues["pkg"]["proxy_base"] = \
2046N/A urlparse.urlunparse((scheme, netloc, path,
2223N/A params, query, fragment))
2046N/A elif opt == "--readonly":
2046N/A ivalues["pkg"]["readonly"] = True
2523N/A elif opt == "--rebuild":
2523N/A rebuild = True
2523N/A elif opt == "--refresh-index":
2523N/A # Note: This argument is for internal use
2523N/A # only.
2523N/A #
2310N/A # This flag is purposefully omitted in usage.
2677N/A # The supported way to forcefully reindex is to
2310N/A # kill any pkg.depot using that directory,
2310N/A # remove the index directory, and restart the
2310N/A # pkg.depot process. The index will be rebuilt
3237N/A # automatically on startup.
3237N/A reindex = True
3237N/A exit_ready = True
3237N/A elif opt == "--set-property":
2310N/A try:
2310N/A prop, p_value = arg.split("=", 1)
2310N/A p_sec, p_name = prop.split(".", 1)
2858N/A except ValueError:
2310N/A usage(_("property arguments must be of "
2852N/A "the form '<section.property>="
2852N/A "<value>'."))
2852N/A repo_props.setdefault(p_sec, {})
2852N/A repo_props[p_sec][p_name] = p_value
3136N/A elif opt == "--ssl-cert-file":
2852N/A if arg == "none" or arg == "":
3237N/A # Assume this is an override to clear
3237N/A # the value.
3237N/A arg = ""
3237N/A elif not os.path.isabs(arg):
3237N/A raise OptionError, "The path to " \
2852N/A "the Certificate file must be " \
2852N/A "absolute."
2852N/A elif not os.path.exists(arg):
2858N/A raise OptionError, "The specified " \
2852N/A "file does not exist."
3138N/A elif not os.path.isfile(arg):
3138N/A raise OptionError, "The specified " \
2852N/A "pathname is not a file."
2852N/A ivalues["pkg"]["ssl_cert_file"] = arg
2852N/A elif opt == "--ssl-key-file":
2508N/A if arg == "none" or arg == "":
2508N/A # Assume this is an override to clear
2508N/A # the value.
2508N/A arg = ""
2867N/A elif not os.path.isabs(arg):
2535N/A raise OptionError, "The path to " \
2535N/A "the Private Key file must be " \
2535N/A "absolute."
3053N/A elif not os.path.exists(arg):
3261N/A raise OptionError, "The specified " \
3053N/A "file does not exist."
3026N/A elif not os.path.isfile(arg):
3261N/A raise OptionError, "The specified " \
3026N/A "pathname is not a file."
2339N/A ivalues["pkg"]["ssl_key_file"] = arg
3261N/A elif opt == "--ssl-dialog":
2339N/A if arg != "builtin" and \
691N/A arg != "smf" and not \
3261N/A arg.startswith("exec:/") and not \
691N/A arg.startswith("svc:"):
395N/A raise OptionError, "Invalid value " \
395N/A "specified. Expected: builtin, " \
395N/A "exec:/path/to/program, smf, or " \
395N/A "an SMF FMRI."
395N/A
290N/A if arg.startswith("exec:"):
3261N/A if os_util.get_canonical_os_type() != \
395N/A "unix":
591N/A # Don't allow a somewhat
591N/A # insecure authentication method
591N/A # on some platforms.
2639N/A raise OptionError, "exec is " \
2639N/A "not a supported dialog " \
2639N/A "type for this operating " \
2639N/A "system."
2639N/A
2639N/A f = os.path.abspath(arg.split(
1505N/A "exec:")[1])
2516N/A if not os.path.isfile(f):
1505N/A raise OptionError, "Invalid " \
1505N/A "file path specified for " \
1632N/A "exec."
1632N/A ivalues["pkg"]["ssl_dialog"] = arg
1632N/A elif opt == "--sort-file-max-size":
1632N/A ivalues["pkg"]["sort_file_max_size"] = arg
2339N/A elif opt == "--writable-root":
2339N/A ivalues["pkg"]["writable_root"] = arg
2339N/A
2339N/A # Set accumulated values.
2339N/A if debug_features:
2339N/A ivalues["pkg"]["debug"] = debug_features
2339N/A if disable_ops:
2339N/A ivalues["pkg"]["disable_ops"] = disable_ops
2339N/A if addresses:
2339N/A ivalues["pkg"]["address"] = list(addresses)
2339N/A
2339N/A if DebugValues:
2339N/A reload(pkg.digest)
2339N/A
2339N/A # Build configuration object.
2339N/A dconf = ds.DepotConfig(target=user_cfg, overrides=ivalues)
3253N/A except getopt.GetoptError, _e:
2828N/A usage("pkg.depotd: %s" % _e.msg)
2828N/A except api_errors.ApiException, _e:
2828N/A usage("pkg.depotd: %s" % str(_e))
2828N/A except OptionError, _e:
2828N/A usage("pkg.depotd: option: %s -- %s" % (opt, _e))
2828N/A except (ArithmeticError, ValueError):
2828N/A usage("pkg.depotd: illegal option value: %s specified " \
2828N/A "for option: %s" % (arg, opt))
2828N/A
2828N/A if show_usage:
2828N/A usage(retcode=0, full=True)
2828N/A
2828N/A if not dconf.get_property("pkg", "log_errors"):
2828N/A dconf.set_property("pkg", "log_errors", "stderr")
2828N/A
2828N/A # If stdout is a tty, then send access output there by default instead
2828N/A # of discarding it.
3253N/A if not dconf.get_property("pkg", "log_access"):
3253N/A if os.isatty(sys.stdout.fileno()):
2828N/A dconf.set_property("pkg", "log_access", "stdout")
2828N/A else:
2828N/A dconf.set_property("pkg", "log_access", "none")
2828N/A
2828N/A # Check for invalid option combinations.
2828N/A image_root = dconf.get_property("pkg", "image_root")
2828N/A inst_root = dconf.get_property("pkg", "inst_root")
2828N/A mirror = dconf.get_property("pkg", "mirror")
2828N/A ll_mirror = dconf.get_property("pkg", "ll_mirror")
2828N/A readonly = dconf.get_property("pkg", "readonly")
2828N/A writable_root = dconf.get_property("pkg", "writable_root")
2892N/A if rebuild and add_content:
2892N/A usage("--add-content cannot be used with --rebuild")
2828N/A if rebuild and reindex:
2828N/A usage("--refresh-index cannot be used with --rebuild")
2828N/A if (rebuild or add_content) and (readonly or mirror):
2828N/A usage("--readonly and --mirror cannot be used with --rebuild "
2828N/A "or --add-content")
2828N/A if reindex and mirror:
2828N/A usage("--mirror cannot be used with --refresh-index")
2828N/A if reindex and readonly and not writable_root:
3158N/A usage("--readonly can only be used with --refresh-index if "
3158N/A "--writable-root is used")
2828N/A if image_root and not ll_mirror:
2828N/A usage("--image-root can only be used with --llmirror.")
2339N/A if image_root and writable_root:
2339N/A usage("--image_root and --writable-root cannot be used "
2339N/A "together.")
3253N/A if image_root and inst_root:
2339N/A usage("--image-root and -d cannot be used together.")
2339N/A
2339N/A # If the image format changes this may need to be reexamined.
2339N/A if image_root:
2339N/A inst_root = os.path.join(image_root, "var", "pkg")
2339N/A
2339N/A # Set any values using defaults if they weren't provided.
2339N/A
2339N/A # Only use the first value for now; multiple bind addresses may be
2339N/A # supported later.
2339N/A address = dconf.get_property("pkg", "address")
2339N/A if address:
2339N/A address = address[0]
3253N/A elif not address:
3253N/A dconf.set_property("pkg", "address", [HOST_DEFAULT])
3253N/A address = dconf.get_property("pkg", "address")[0]
3253N/A
3253N/A if not inst_root:
3253N/A usage("Either PKG_REPO or -d must be provided")
3253N/A
3253N/A content_root = dconf.get_property("pkg", "content_root")
3253N/A if not content_root:
3253N/A dconf.set_property("pkg", "content_root", CONTENT_PATH_DEFAULT)
3253N/A content_root = dconf.get_property("pkg", "content_root")
3253N/A
3253N/A port = dconf.get_property("pkg", "port")
3253N/A ssl_cert_file = dconf.get_property("pkg", "ssl_cert_file")
3253N/A ssl_key_file = dconf.get_property("pkg", "ssl_key_file")
3253N/A if (ssl_cert_file and not ssl_key_file) or (ssl_key_file and not
3253N/A ssl_cert_file):
3253N/A usage("The --ssl-cert-file and --ssl-key-file options must "
3253N/A "must both be provided when using either option.")
3253N/A elif not port:
3253N/A if ssl_cert_file and ssl_key_file:
3253N/A dconf.set_property("pkg", "port", SSL_PORT_DEFAULT)
3253N/A else:
3253N/A dconf.set_property("pkg", "port", PORT_DEFAULT)
3253N/A port = dconf.get_property("pkg", "port")
3253N/A
2364N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
2364N/A if not socket_timeout:
2364N/A dconf.set_property("pkg", "socket_timeout",
2364N/A SOCKET_TIMEOUT_DEFAULT)
2364N/A socket_timeout = dconf.get_property("pkg", "socket_timeout")
2364N/A
2364N/A threads = dconf.get_property("pkg", "threads")
3253N/A if not threads:
3253N/A dconf.set_property("pkg", "threads", THREADS_DEFAULT)
3253N/A threads = dconf.get_property("pkg", "threads")
2339N/A
395N/A # If the program is going to reindex, the port is irrelevant since
395N/A # the program will not bind to a port.
290N/A if not exit_ready:
290N/A try:
2339N/A cherrypy.process.servers.check_port(address, port)
2339N/A except Exception, e:
290N/A emsg("pkg.depotd: unable to bind to the specified "
290N/A "port: %d. Reason: %s" % (port, e))
290N/A sys.exit(1)
290N/A else:
290N/A # Not applicable if we're not going to serve content
290N/A dconf.set_property("pkg", "content_root", "")
290N/A
290N/A # Any relative paths should be made absolute using pkg_root. 'pkg_root'
290N/A # is a special property that was added to enable internal deployment of
290N/A # multiple disparate versions of the pkg.depotd software.
395N/A pkg_root = dconf.get_property("pkg", "pkg_root")
395N/A
290N/A repo_config_file = dconf.get_property("pkg", "cfg_file")
290N/A if repo_config_file and not os.path.isabs(repo_config_file):
2674N/A repo_config_file = os.path.join(pkg_root, repo_config_file)
2674N/A
2674N/A if content_root and not os.path.isabs(content_root):
2674N/A content_root = os.path.join(pkg_root, content_root)
290N/A
2674N/A if inst_root and not os.path.isabs(inst_root):
2674N/A inst_root = os.path.join(pkg_root, inst_root)
3158N/A
395N/A if ssl_cert_file:
395N/A if ssl_cert_file == "none":
2674N/A ssl_cert_file = None
3158N/A elif not os.path.isabs(ssl_cert_file):
395N/A ssl_cert_file = os.path.join(pkg_root, ssl_cert_file)
3158N/A
395N/A if ssl_key_file:
2674N/A if ssl_key_file == "none":
3158N/A ssl_key_file = None
591N/A elif not os.path.isabs(ssl_key_file):
591N/A ssl_key_file = os.path.join(pkg_root, ssl_key_file)
2674N/A
3158N/A if writable_root and not os.path.isabs(writable_root):
2639N/A writable_root = os.path.join(pkg_root, writable_root)
2639N/A
2674N/A # Setup SSL if requested.
3158N/A key_data = None
2639N/A ssl_dialog = dconf.get_property("pkg", "ssl_dialog")
2639N/A if not exit_ready and ssl_cert_file and ssl_key_file and \
2674N/A ssl_dialog != "builtin":
2674N/A cmdline = None
3158N/A def get_ssl_passphrase(*ignored):
691N/A p = None
691N/A try:
2674N/A p = subprocess.Popen(cmdline, shell=True,
2674N/A stdout=subprocess.PIPE,
3158N/A stderr=None)
2339N/A p.wait()
2339N/A except Exception, __e:
3026N/A emsg("pkg.depotd: an error occurred while "
3026N/A "executing [%s]; unable to obtain the "
3158N/A "passphrase needed to decrypt the SSL "
3026N/A "private key file: %s" % (cmdline, __e))
3158N/A sys.exit(1)
3026N/A return p.stdout.read().strip("\n")
3053N/A
3053N/A if ssl_dialog.startswith("exec:"):
3158N/A exec_path = ssl_dialog.split("exec:")[1]
3053N/A if not os.path.isabs(exec_path):
3158N/A exec_path = os.path.join(pkg_root, exec_path)
3053N/A cmdline = "%s %s %d" % (exec_path, "''", port)
290N/A elif ssl_dialog == "smf" or ssl_dialog.startswith("svc:"):
290N/A if ssl_dialog == "smf":
290N/A # Assume the configuration target was an SMF
290N/A # FMRI and let svcprop fail with an error if
290N/A # it wasn't.
591N/A svc_fmri = dconf.target
591N/A else:
2639N/A svc_fmri = ssl_dialog
2639N/A cmdline = "/usr/bin/svcprop -p " \
2639N/A "pkg_secure/ssl_key_passphrase %s" % svc_fmri
2639N/A
691N/A # The key file requires decryption, but the user has requested
691N/A # exec-based authentication, so it will have to be decoded first
2339N/A # to an un-named temporary file.
2339N/A try:
3026N/A with file(ssl_key_file, "rb") as key_file:
3026N/A pkey = crypto.load_privatekey(
3053N/A crypto.FILETYPE_PEM, key_file.read(),
3053N/A get_ssl_passphrase)
290N/A
290N/A key_data = tempfile.TemporaryFile()
2339N/A key_data.write(crypto.dump_privatekey(
2339N/A crypto.FILETYPE_PEM, pkey))
2339N/A key_data.seek(0)
2339N/A except EnvironmentError, _e:
2339N/A emsg("pkg.depotd: unable to read the SSL private key "
2339N/A "file: %s" % _e)
2339N/A sys.exit(1)
290N/A except crypto.Error, _e:
2339N/A emsg("pkg.depotd: authentication or cryptography "
2339N/A "failure while attempting to decode\nthe SSL "
290N/A "private key file: %s" % _e)
2339N/A sys.exit(1)
2339N/A else:
2339N/A # Redirect the server to the decrypted key file.
2339N/A ssl_key_file = "/dev/fd/%d" % key_data.fileno()
2339N/A
2339N/A # Setup our global configuration.
2339N/A gconf = {
2339N/A "checker.on": True,
290N/A "environment": "production",
395N/A "log.screen": False,
290N/A "server.max_request_body_size": MAX_REQUEST_BODY_SIZE,
395N/A "server.shutdown_timeout": 0,
506N/A "server.socket_host": address,
506N/A "server.socket_port": port,
506N/A "server.socket_timeout": socket_timeout,
506N/A "server.ssl_certificate": ssl_cert_file,
506N/A "server.ssl_private_key": ssl_key_file,
506N/A "server.thread_pool": threads,
506N/A "tools.log_headers.on": True,
506N/A "tools.encode.on": True
834N/A }
506N/A
506N/A if "headers" in dconf.get_property("pkg", "debug"):
506N/A # Despite its name, this only logs headers when there is an
513N/A # error; it's redundant with the debug feature enabled.
506N/A gconf["tools.log_headers.on"] = False
506N/A
506N/A # Causes the headers of every request to be logged to the error
506N/A # log; even if an exception occurs.
290N/A gconf["tools.log_headers_always.on"] = True
290N/A cherrypy.tools.log_headers_always = cherrypy.Tool(
2535N/A "on_start_resource",
2535N/A cherrypy.lib.cptools.log_request_headers)
2535N/A
395N/A log_type_map = {
413N/A "errors": {
395N/A "param": "log.error_file",
290N/A "attr": "error_log"
1674N/A },
1674N/A "access": {
1674N/A "param": "log.access_file",
1674N/A "attr": "access_log"
1674N/A }
1674N/A }
1674N/A
1674N/A for log_type in log_type_map:
1674N/A dest = dconf.get_property("pkg", "log_%s" % log_type)
1674N/A if dest in ("stdout", "stderr", "none"):
1674N/A if dest == "none":
1674N/A h = logging.StreamHandler(LogSink())
1674N/A else:
1674N/A h = logging.StreamHandler(eval("sys.%s" % \
1674N/A dest))
3203N/A
3203N/A h.setLevel(logging.DEBUG)
3203N/A h.setFormatter(cherrypy._cplogging.logfmt)
3203N/A log_obj = eval("cherrypy.log.%s" % \
3203N/A log_type_map[log_type]["attr"])
3234N/A log_obj.addHandler(h)
395N/A # Since we've replaced cherrypy's log handler with our
506N/A # own, we don't want the output directed to a file.
506N/A dest = ""
395N/A elif dest:
2535N/A if not os.path.isabs(dest):
3246N/A dest = os.path.join(pkg_root, dest)
3246N/A gconf[log_type_map[log_type]["param"]] = dest
395N/A
430N/A cherrypy.config.update(gconf)
849N/A
834N/A # Now that our logging, etc. has been setup, it's safe to perform any
290N/A # remaining preparation.
2561N/A
2561N/A # Initialize repository state.
2561N/A if not readonly:
2561N/A # Not readonly, so assume a new repository should be created.
2561N/A try:
2561N/A sr.repository_create(inst_root, properties=repo_props)
2561N/A except sr.RepositoryExistsError:
2561N/A # Already exists, nothing to do.
2561N/A pass
2561N/A except (api_errors.ApiException, sr.RepositoryError), _e:
2561N/A emsg("pkg.depotd: %s" % _e)
2561N/A sys.exit(1)
2561N/A
2561N/A try:
2561N/A sort_file_max_size = dconf.get_property("pkg",
2561N/A "sort_file_max_size")
2561N/A
2561N/A repo = sr.Repository(cfgpathname=repo_config_file,
2561N/A log_obj=cherrypy, mirror=mirror, properties=repo_props,
2561N/A read_only=readonly, root=inst_root,
2535N/A sort_file_max_size=sort_file_max_size,
2535N/A writable_root=writable_root)
2535N/A except (RuntimeError, sr.RepositoryError), _e:
2535N/A emsg("pkg.depotd: %s" % _e)
2535N/A sys.exit(1)
1099N/A except search_errors.IndexingException, _e:
2535N/A emsg("pkg.depotd: %s" % str(_e), "INDEX")
2535N/A sys.exit(1)
2535N/A except api_errors.ApiException, _e:
2535N/A emsg("pkg.depotd: %s" % str(_e))
2535N/A sys.exit(1)
2535N/A
2535N/A if not rebuild and not add_content and not repo.mirror and \
2535N/A not (repo.read_only and not repo.writable_root):
2535N/A # Automatically update search indexes on startup if not already
2535N/A # told to, and not in readonly/mirror mode.
1099N/A reindex = True
2535N/A
2535N/A if reindex:
2535N/A try:
2535N/A # Only execute a index refresh here if --exit-ready was
3234N/A # requested; it will be handled later in the setup
2535N/A # process for other cases.
2535N/A if repo.root and exit_ready:
2535N/A repo.refresh_index()
2535N/A except (sr.RepositoryError, search_errors.IndexingException,
2535N/A api_errors.ApiException), e:
2535N/A emsg(str(e), "INDEX")
2535N/A sys.exit(1)
2535N/A elif rebuild:
2535N/A try:
2535N/A repo.rebuild(build_index=True)
2535N/A except sr.RepositoryError, e:
2535N/A emsg(str(e), "REBUILD")
1099N/A sys.exit(1)
2826N/A except (search_errors.IndexingException,
2688N/A api_errors.UnknownErrors,
2688N/A api_errors.PermissionsException), e:
2688N/A emsg(str(e), "INDEX")
2688N/A sys.exit(1)
2688N/A elif add_content:
2688N/A try:
1660N/A repo.add_content()
2688N/A repo.refresh_index()
2688N/A except sr.RepositoryError, e:
2688N/A emsg(str(e), "ADD_CONTENT")
2826N/A sys.exit(1)
2826N/A except (search_errors.IndexingException,
2688N/A api_errors.UnknownErrors,
2688N/A api_errors.PermissionsException), e:
2688N/A emsg(str(e), "INDEX")
2688N/A sys.exit(1)
2688N/A
849N/A # Ready to start depot; exit now if requested.
2688N/A if exit_ready:
2688N/A sys.exit(0)
3158N/A
3143N/A # Next, initialize depot.
3177N/A if nasty:
3143N/A depot = ds.NastyDepotHTTP(repo, dconf)
3158N/A else:
849N/A depot = ds.DepotHTTP(repo, dconf)
2688N/A
2688N/A # Now build our site configuration.
290N/A conf = {
2535N/A "/": {
2535N/A # We have to override cherrypy's default response_class so that
2597N/A # we have access to the write() callable to stream data
2597N/A # directly to the client.
2597N/A "wsgi.response_class": dr.DepotResponse,
2535N/A },
2535N/A "/robots.txt": {
2535N/A "tools.staticfile.on": True,
2535N/A "tools.staticfile.filename": os.path.join(depot.web_root,
2535N/A "robots.txt")
2535N/A },
3246N/A }
3246N/A
3246N/A proxy_base = dconf.get_property("pkg", "proxy_base")
3246N/A if proxy_base:
3234N/A # This changes the base URL for our server, and is primarily
2535N/A # intended to allow our depot process to operate behind Apache
2535N/A # or some other webserver process.
3171N/A #
2535N/A # Visit the following URL for more information:
2535N/A # http://cherrypy.org/wiki/BuiltinTools#tools.proxy
3158N/A proxy_conf = {
2535N/A "tools.proxy.on": True,
3234N/A "tools.proxy.local": "",
2535N/A "tools.proxy.base": proxy_base
2535N/A }
2535N/A
2535N/A # Now merge or add our proxy configuration information into the
2597N/A # existing configuration.
2597N/A for entry in proxy_conf:
2597N/A conf["/"][entry] = proxy_conf[entry]
2597N/A
2597N/A if ll_mirror:
2597N/A ds.DNSSD_Plugin(cherrypy.engine, gconf).subscribe()
2597N/A
2597N/A if reindex:
2597N/A # Tell depot to update search indexes when possible;
2597N/A # this is done as a background task so that packages
2597N/A # can be served immediately while search indexes are
2597N/A # still being updated.
2597N/A depot._queue_refresh_index()
2597N/A
3246N/A # If stdin is not a tty and the pkgdepot controller isn't being used,
3246N/A # then assume process should be daemonized.
3246N/A if not os.environ.get("PKGDEPOT_CONTROLLER") and \
3246N/A not os.isatty(sys.stdin.fileno()):
3246N/A Daemonizer(cherrypy.engine).subscribe()
3246N/A
3246N/A try:
3246N/A root = cherrypy.Application(depot)
3246N/A cherrypy.quickstart(root, config=conf)
3246N/A except Exception, _e:
3246N/A emsg("pkg.depotd: unknown error starting depot server, " \
2597N/A "illegal option value specified?")
2597N/A emsg(_e)
2535N/A sys.exit(1)
3246N/A