depot.py revision 429
3177N/A#!/usr/bin/python2.4
290N/A#
290N/A# CDDL HEADER START
290N/A#
290N/A# The contents of this file are subject to the terms of the
290N/A# Common Development and Distribution License (the "License").
290N/A# You may not use this file except in compliance with the License.
290N/A#
290N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
290N/A# or http://www.opensolaris.org/os/licensing.
290N/A# See the License for the specific language governing permissions
290N/A# and limitations under the License.
290N/A#
290N/A# When distributing Covered Code, include this CDDL HEADER in each
290N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
290N/A# If applicable, add the following below this CDDL HEADER, with the
290N/A# fields enclosed by brackets "[]" replaced with your own identifying
290N/A# information: Portions Copyright [yyyy] [name of copyright owner]
290N/A#
290N/A# CDDL HEADER END
290N/A#
3313N/A# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
395N/A# Use is subject to license terms.
290N/A#
3143N/A
883N/A# pkg.depotd - package repository daemon
454N/A
290N/A# XXX The prototype pkg.depotd combines both the version management server that
448N/A# answers to pkgsend(1) sessions and the HTTP file server that answers to the
3234N/A# various GET operations that a pkg(1) client makes. This split is expected to
290N/A# be made more explicit, by constraining the pkg(1) operations such that they
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
383N/A# operations, and must manipulate the various state files--catalogs, in
290N/A# particular--such that the pkg(1) pull client can operately accurately with
395N/A# only a basic HTTP/HTTPS server in place.
290N/A
849N/A# XXX We should support simple "last-modified" operations via HEAD queries.
1516N/A
2508N/A# XXX Although we pushed the evaluation of next-version, etc. to the pull
290N/A# client, we should probably provide a query API to do same on the server, for
2535N/A# dumb clients (like a notification service).
2698N/A
290N/A# The default authority for the depot.
290N/AAUTH_DEFAULT = "opensolaris.org"
2535N/A# The default repository path.
2561N/AREPO_PATH_DEFAULT = "/var/pkg/repo"
290N/A# The default port to serve data from.
2508N/APORT_DEFAULT = 80
383N/A# The minimum number of threads allowed.
290N/ATHREADS_MIN = 1
290N/A# The default number of threads to start.
2339N/ATHREADS_DEFAULT = 10
2535N/A# The maximum number of threads that can be started.
290N/ATHREADS_MAX = 100
290N/A# The default server socket timeout in seconds. We want this to be longer than
2535N/A# the normal default of 10 seconds to accommodate clients with poor quality
2535N/A# connections.
290N/ASOCKET_TIMEOUT_DEFAULT = 60
290N/A# Whether modify operations should be allowed.
2508N/AREADONLY_DEFAULT = False
2508N/A# Whether the repository catalog should be rebuilt on startup.
290N/AREBUILD_DEFAULT = False
1660N/A# Whether the indexes should be rebuilt
1660N/AREINDEX_DEFAULT = False
1660N/A
1660N/Aimport getopt
1660N/Aimport os
1660N/Aimport sys
1660N/A
1660N/Atry:
1660N/A import cherrypy
1660N/A version = cherrypy.__version__.split('.')
1660N/A if map(int, version) < [3, 0, 3]:
1660N/A raise ImportError
1660N/A elif map(int, version) >= [3, 1, 0]:
1660N/A raise ImportError
1660N/Aexcept ImportError:
1660N/A print """cherrypy 3.0.3 or greater (but less than 3.1.0) is """ \
1660N/A """required to use this program."""
1660N/A sys.exit(2)
448N/A
448N/Aimport pkg.server.face as face
2828N/Aimport pkg.server.config as config
3253N/Aimport pkg.server.depot as depot
2828N/Aimport pkg.server.repository as repo
534N/Aimport pkg.server.repositoryconfig as rc
534N/Afrom pkg.misc import port_available, emsg
534N/A
534N/Adef usage():
3339N/A print """\
3339N/AUsage: /usr/lib/pkg.depotd [--readonly] [--rebuild] [-d repo_dir] [-p port]
3339N/A [-s threads] [-t socket_timeout]
3339N/A
534N/A --readonly Read-only operation; modifying operations disallowed
290N/A --rebuild Re-build the catalog from pkgs in depot
290N/A"""
954N/A sys.exit(2)
954N/A
954N/Aclass OptionError(Exception):
954N/A """Option exception. """
534N/A
1099N/A def __init__(self, *args):
3261N/A Exception.__init__(self, *args)
290N/A
3117N/Aif __name__ == "__main__":
3117N/A
3203N/A port = PORT_DEFAULT
3117N/A threads = THREADS_DEFAULT
290N/A socket_timeout = SOCKET_TIMEOUT_DEFAULT
3246N/A readonly = READONLY_DEFAULT
3246N/A rebuild = REBUILD_DEFAULT
3246N/A reindex = REINDEX_DEFAULT
3246N/A
3246N/A if "PKG_REPO" in os.environ:
3246N/A repo_path = os.environ["PKG_REPO"]
3246N/A else:
3246N/A repo_path = REPO_PATH_DEFAULT
290N/A
290N/A try:
661N/A parsed = set()
2867N/A opts, pargs = getopt.getopt(sys.argv[1:], "d:np:s:t:",
290N/A ["readonly", "rebuild", "refresh-index"])
2494N/A for opt, arg in opts:
2494N/A if opt in parsed:
2494N/A raise OptionError, "Each option may only be " \
2516N/A "specified once."
2516N/A else:
2516N/A parsed.add(opt)
2516N/A
2516N/A if opt == "-n":
2516N/A sys.exit(0)
2516N/A elif opt == "-d":
290N/A repo_path = arg
3185N/A elif opt == "-p":
2523N/A port = int(arg)
3138N/A elif opt == "-s":
2390N/A threads = int(arg)
1498N/A if threads < THREADS_MIN:
1498N/A raise OptionError, \
2867N/A "minimum value is %d" % THREADS_MIN
2310N/A if threads > THREADS_MAX:
3237N/A raise OptionError, \
2310N/A "maximum value is %d" % THREADS_MAX
2310N/A elif opt == "-t":
2852N/A socket_timeout = int(arg)
3237N/A elif opt == "--readonly":
2852N/A readonly = True
2852N/A elif opt == "--rebuild":
2852N/A rebuild = True
2535N/A elif opt == "--refresh-index":
2867N/A # Note: This argument is for internal use
2867N/A # only. It's used when pkg.depotd is reexecing
2310N/A # itself and needs to know that's the case.
290N/A # This flag is purposefully omitted in usage.
1674N/A # The supported way to forcefully reindex is to
1674N/A # kill any pkg.depot using that directory,
2262N/A # remove the index directory, and restart the
1674N/A # pkg.depot process. The index will be rebuilt
395N/A # automatically on startup.
430N/A reindex = True
395N/A except getopt.GetoptError, e:
1544N/A print "pkg.depotd: %s" % e.msg
1968N/A usage()
1557N/A except OptionError, e:
1903N/A print "pkg.depotd: option: %s -- %s" % (opt, e)
2046N/A usage()
2240N/A except (ArithmeticError, ValueError):
1506N/A print "pkg.depotd: illegal option value: %s specified " \
2928N/A "for option: %s" % (arg, opt)
395N/A usage()
395N/A # If the program is going to reindex, the port is irrelevant since
2026N/A # the program will not bind to a port.
395N/A if not reindex:
395N/A available, msg = port_available(None, port)
395N/A if not available:
2310N/A print "pkg.depotd: unable to bind to the specified " \
2852N/A "port: %d. Reason: %s" % (port, msg)
395N/A sys.exit(1)
661N/A
2867N/A try:
2867N/A face.set_content_root(os.environ["PKG_DEPOT_CONTENT"])
2867N/A except KeyError:
2867N/A pass
2867N/A
2852N/A scfg = config.SvrConfig(repo_path, AUTH_DEFAULT)
2310N/A
3216N/A if rebuild:
3216N/A scfg.destroy_catalog()
2867N/A
2867N/A if readonly:
2867N/A scfg.set_read_only()
661N/A
3185N/A try:
3185N/A scfg.init_dirs()
3185N/A except EnvironmentError, e:
395N/A print "pkg.depotd: an error occurred while trying to " \
849N/A "initialize the depot repository directory " \
290N/A "structures:\n%s" % e
395N/A sys.exit(1)
395N/A
1968N/A if reindex:
395N/A scfg.acquire_catalog(rebuild = False)
395N/A scfg.catalog.run_update_index()
395N/A sys.exit(0)
395N/A
395N/A scfg.acquire_in_flight()
395N/A scfg.acquire_catalog()
395N/A
395N/A try:
395N/A root = cherrypy.Application(repo.Repository(scfg))
395N/A except rc.InvalidAttributeValueError, e:
395N/A emsg("pkg.depotd: repository.conf error: %s" % e)
290N/A sys.exit(1)
290N/A
395N/A # We have to override cherrypy's default response_class so that we
395N/A # have access to the write() callable to stream data directly to the
1231N/A # client.
1557N/A root.wsgiapp.response_class = depot.DepotResponse
1903N/A
1557N/A cherrypy.config.update({
395N/A "environment": "production",
395N/A "checker.on": True,
395N/A "log.screen": True,
395N/A "server.socket_port": port,
395N/A "server.thread_pool": threads,
395N/A "server.socket_timeout": socket_timeout
395N/A })
395N/A
395N/A conf = {
395N/A "/robots.txt": {
3185N/A "tools.staticfile.on": True,
3185N/A "tools.staticfile.filename": os.path.join(face.content_root,
3185N/A "robots.txt")
395N/A },
290N/A "/static": {
290N/A "tools.staticdir.on": True,
430N/A "tools.staticdir.root": face.content_root,
395N/A "tools.staticdir.dir": ""
395N/A }
395N/A }
395N/A
1302N/A try:
395N/A cherrypy.quickstart(root, config = conf)
395N/A except:
290N/A print "pkg.depotd: unknown error starting depot, illegal " \
3313N/A "option value specified?"
3313N/A usage()
3139N/A
395N/A