Example #1
0
def refresh_conf(repo_info, log_dir, host, port, runtime_dir,
            template_dir, cache_dir, cache_size, sroot, fragment=False,
            allow_refresh=False):
        """Creates a new configuration for the depot."""
        try:
                ret = EXIT_OK
                cleanup_conf(runtime_dir=runtime_dir)
                if not repo_info:
                        raise DepotException(_("no repositories found"))

                htdocs_path = os.path.join(runtime_dir, DEPOT_HTDOCS_DIRNAME,
                    sroot)
                misc.makedirs(htdocs_path)

                # pubs and default_pubs are lists of tuples of the form:
                # (publisher prefix, repository root dir, repository prefix)
                pubs = []
                default_pubs = []

                repo_prefixes = [prefix for root, prefix in repo_info]
                errors = []

                # Query each repository for its publisher information.
                for (repo_root, repo_prefix) in repo_info:
                        try:
                                publishers, default_pub = \
                                    _get_publishers(repo_root)
                                for pub in publishers:
                                        pubs.append(
                                            (pub, repo_root,
                                            repo_prefix))
                                default_pubs.append((default_pub,
                                    repo_root, repo_prefix))

                        except DepotException, err:
                                errors.append(str(err))
                if errors:
                        raise DepotException(_("Unable to get publisher "
                            "information: %s") % "\n".join(errors))

                # Write the publisher/0 response for each repository
                pubs_by_repo = {}
                for pub_prefix, repo_root, repo_prefix in pubs:
                        pubs_by_repo.setdefault(repo_prefix, []).append(
                            pub_prefix)
                for repo_prefix in pubs_by_repo:
                        _write_publisher_response(
                            pubs_by_repo[repo_prefix], htdocs_path, repo_prefix)

                _write_httpd_conf(pubs, default_pubs, runtime_dir, log_dir,
                    template_dir, cache_dir, cache_size, host, port, sroot,
                    fragment=fragment, allow_refresh=allow_refresh)
                _write_versions_response(htdocs_path, fragment=fragment)
                # If we're writing a configuration fragment, then the web server
                # is probably not running as DEPOT_USER:DEPOT_GROUP
                if not fragment:
                        _chown_dir(runtime_dir)
                        _chown_dir(cache_dir)
                else:
                        msg(_("Created %s/depot.conf") % runtime_dir)
Example #2
0
def _write_publisher_response(pubs, htdocs_path, repo_prefix):
        """Writes a static publisher/0 response for the depot."""
        try:
                # convert our list of strings to a list of Publishers
                pub_objs = [pkg.client.publisher.Publisher(pub) for pub in pubs]

                # write individual reponses for the publishers
                for pub in pub_objs:
                        pub_path = os.path.join(htdocs_path,
                            os.path.sep.join(
                               [repo_prefix, pub.prefix] + DEPOT_PUB_DIRNAME))
                        misc.makedirs(pub_path)
                        with file(os.path.join(pub_path, "index.html"), "w") as\
                            pub_file:
                                p5i.write(pub_file, [pub])

                # write a response that contains all publishers
                pub_path = os.path.join(htdocs_path,
                    os.path.sep.join([repo_prefix] + DEPOT_PUB_DIRNAME))
                os.makedirs(pub_path)
                with file(os.path.join(pub_path, "index.html"), "w") as \
                    pub_file:
                        p5i.write(pub_file, pub_objs)

        except (OSError, apx.ApiException), err:
                raise DepotException(
                    _("Unable to write publisher response: %s") % err)
Example #3
0
def _write_publisher_response(pubs, htdocs_path, repo_prefix):
    """Writes a static publisher/0 response for the depot."""
    try:
        # convert our list of strings to a list of Publishers
        pub_objs = [pkg.client.publisher.Publisher(pub) for pub in pubs]

        # write individual reponses for the publishers
        for pub in pub_objs:
            pub_path = os.path.join(
                htdocs_path,
                os.path.sep.join([repo_prefix, pub.prefix] +
                                 DEPOT_PUB_DIRNAME))
            misc.makedirs(pub_path)
            with file(os.path.join(pub_path, "index.html"), "w") as\
                pub_file:
                p5i.write(pub_file, [pub])

        # write a response that contains all publishers
        pub_path = os.path.join(
            htdocs_path, os.path.sep.join([repo_prefix] + DEPOT_PUB_DIRNAME))
        os.makedirs(pub_path)
        with file(os.path.join(pub_path, "index.html"), "w") as \
            pub_file:
            p5i.write(pub_file, pub_objs)

    except (OSError, apx.ApiException) as err:
        raise DepotException(
            _("Unable to write publisher response: {0}").format(err))
Example #4
0
def _generate_server_cert_key(host,
                              port,
                              ca_cert_file="",
                              ca_key_file="",
                              output_dir="/tmp"):
    """ Generate certificate and key files for https service."""
    if os.path.exists(output_dir):
        if not os.path.isdir(output_dir):
            raise DepotException(
                _("{0} is not a directory").format(output_dir))
    else:
        misc.makedirs(output_dir)
    server_id = "{0}_{1}".format(host, port)

    cs_prefix = "server_{0}".format(server_id)
    server_cert_file = os.path.join(output_dir,
                                    "{0}_cert.pem".format(cs_prefix))
    server_key_file = os.path.join(output_dir, "{0}_key.pem".format(cs_prefix))

    # If the cert and key files do not exist, then generate one.
    if not os.path.exists(server_cert_file) or not os.path.exists(
            server_key_file):
        # Used as a factor to easily specify a year.
        year_factor = 60 * 60 * 24 * 365

        # If user specifies ca_cert_file and ca_key_file, just load
        # the files. Otherwise, generate new ca_cert and ca_key.
        if not ca_cert_file or not ca_key_file:
            ca_cert_file = os.path.join(output_dir,
                                        "ca_{0}_cert.pem".format(server_id))
            ca_key_file = os.path.join(output_dir,
                                       "ca_{0}_key.pem".format(server_id))
            ca_cert, ca_key = _createCertificateKey(1, host, 0,
                                                    year_factor * 10,
                                                    ca_cert_file, ca_key_file)
        else:
            if not os.path.exists(ca_cert_file):
                raise DepotException(
                    _("Cannot find user "
                      "provided CA certificate file: {0}").format(
                          ca_cert_file))
            if not os.path.exists(ca_key_file):
                raise DepotException(
                    _("Cannot find user "
                      "provided CA key file: {0}").format(ca_key_file))
            with open(ca_cert_file, "r") as fr:
                ca_cert = load_certificate(FILETYPE_PEM, fr.read())
            with open(ca_key_file, "r") as fr:
                ca_key = load_privatekey(FILETYPE_PEM, fr.read())

        _createCertificateKey(2,
                              host,
                              0,
                              year_factor * 10,
                              server_cert_file,
                              server_key_file,
                              issuerCert=ca_cert,
                              issuerKey=ca_key)

    return (ca_cert_file, ca_key_file, server_cert_file, server_key_file)
Example #5
0
File: feed.py Project: aszeszo/test
def handle(depot, request, response, pub):
        """If there have been package updates since we last generated the feed,
        update the feed and send it to the client.  Otherwise, send them the
        cached copy if it is available.
        """

        cfpath = __get_cache_pathname(depot, pub)

        # First check to see if we already have a valid cache of the feed.
        need_update, last = __cache_needs_update(depot, pub)

        if need_update:
                # Update always looks at feed.window seconds before the last
                # update until "now."  If last is none, we want it to use "now"
                # as its starting point.
                if last is None:
                        last = datetime.datetime.utcnow()

                # Generate and cache the feed.
                misc.makedirs(os.path.dirname(cfpath))
                cf = file(cfpath, "w")
                update(request, depot, last, cf, pub)
                cf.close()

        return serve_file(cfpath, MIME_TYPE)
Example #6
0
def handle(depot, request, response, pub):
        """If there have been package updates since we last generated the feed,
        update the feed and send it to the client.  Otherwise, send them the
        cached copy if it is available.
        """

        cfpath = __get_cache_pathname(depot, pub)

        # First check to see if we already have a valid cache of the feed.
        need_update, last = __cache_needs_update(depot, pub)

        if need_update:
                # Update always looks at feed.window seconds before the last
                # update until "now."  If last is none, we want it to use "now"
                # as its starting point.
                if last is None:
                        last = datetime.datetime.utcnow()

                # Generate and cache the feed.
                misc.makedirs(os.path.dirname(cfpath))
                cf = open(cfpath, "w")
                update(request, depot, last, cf, pub)
                cf.close()

        return serve_file(cfpath, MIME_TYPE)
Example #7
0
 def link_manifests():
     for pub in os.listdir(pub_path):
         pkg_root = os.path.join(pub_path, pub, "pkg")
         for stem in os.listdir(pkg_root):
             stem_root = os.path.join(pkg_root, stem)
             for ver in os.listdir(stem_root):
                 src = os.path.join(stem_root, ver)
                 dest = os.path.join(img_root, "pkg", stem, ver,
                                     "manifest")
                 misc.makedirs(os.path.dirname(dest))
                 os.link(src, dest)
Example #8
0
def _write_status_response(status, htdocs_path, repo_prefix):
        """Writes a status status/0 response for the depot."""
        try:
                status_path = os.path.join(htdocs_path, repo_prefix,
                    os.path.sep.join(DEPOT_STATUS_DIRNAME), "index.html")
                misc.makedirs(os.path.dirname(status_path))
                with open(status_path, "w") as status_file:
                        status_file.write(json.dumps(status, ensure_ascii=False,
                            indent=2, sort_keys=True))
        except OSError as err:
                raise DepotException(
                    _("Unable to write status response: {0}").format(err))
Example #9
0
 def link_manifests():
         for pub in os.listdir(pub_path):
                 pkg_root = os.path.join(pub_path, pub, "pkg")
                 for stem in os.listdir(pkg_root):
                         stem_root = os.path.join(pkg_root, stem)
                         for ver in os.listdir(stem_root):
                                 src = os.path.join(stem_root,
                                     ver)
                                 dest = os.path.join(img_root,
                                     "pkg", stem, ver,
                                     "manifest")
                                 misc.makedirs(
                                     os.path.dirname(dest))
                                 os.link(src, dest)
Example #10
0
def _write_versions_response(htdocs_path, fragment=False):
    """Writes a static versions/0 response for the Apache depot."""

    try:
        versions_path = os.path.join(htdocs_path, *DEPOT_VERSIONS_DIRNAME)
        misc.makedirs(versions_path)

        with file(os.path.join(versions_path, "index.html"), "w") as \
            versions_file:
            versions_file.write(fragment and DEPOT_FRAGMENT_VERSIONS_STR
                                or DEPOT_VERSIONS_STR)

        versions_file.close()
    except (OSError, apx.ApiException) as err:
        raise DepotException(
            _("Unable to write versions response: {0}").format(err))
Example #11
0
        def test_force(self):
                """Ensure that image creation works as expected when an image
                already exists."""

                # These tests are interdependent.
                #
                # Bug 3588: Make sure we can't create an image where one
                # already exists
                self.pkg("image-create -p test1={0} {1}/3588_image".format(
                    self.rurl1, self.get_img_path()))
                self.pkg("image-create -p test1={0} {1}/3588_image".format(
                    self.rurl1, self.get_img_path()), exit=1)

                # Make sure we can create an image where one
                # already exists with the -f (force) flag
                self.pkg("image-create -p test1={0} {1}/3588_image_1".format(
                    self.rurl1, self.get_img_path()))
                self.pkg("image-create -f -p test1={0} {1}/3588_image_1".format(
                         self.rurl1, self.get_img_path()))

                # Bug 3588: Make sure we can't create an image where a
                # non-empty directory exists
                p = os.path.join(self.get_img_path(), "3588_2_image")
                os.mkdir(p)
                self.cmdline_run("touch {0}/{1}".format(p, "somefile"),
                    coverage=False)
                self.pkg("image-create -p test1={0} {1}".format(self.rurl1, p),
                    exit=1)
                self.pkg("image-create -f -p test1={0} {1}".format(self.rurl1, p))

                # Bug 17680: Ensure ssl directory is preserved if it
                # already exists when creating an image where one
                # might already exist with the -f (force) flag.
                shutil.rmtree(self.get_img_path())
                self.pkg("image-create -p test1={0} {1}".format(self.rurl1,
                    self.get_img_path()))

                img = self.get_img_api_obj().img
                cert_path = os.path.join(img.imgdir, "ssl", "cert.file")
                misc.makedirs(os.path.dirname(cert_path))
                open(cert_path, "wb").close()
                assert os.path.exists(cert_path)
                self.pkg("image-create -f -p test1={0} {1}".format(self.rurl1,
                    self.get_img_path()))
                assert os.path.exists(cert_path)
Example #12
0
        def test_force(self):
                """Ensure that image creation works as expected when an image
                already exists."""

                # These tests are interdependent.
                #
                # Bug 3588: Make sure we can't create an image where one
                # already exists
                self.pkg("image-create -p test1=%s %s/3588_image" % (
                    self.rurl1, self.get_img_path()))
                self.pkg("image-create -p test1=%s %s/3588_image" % (
                    self.rurl1, self.get_img_path()), exit=1)

                # Make sure we can create an image where one
                # already exists with the -f (force) flag
                self.pkg("image-create -p test1=%s %s/3588_image_1" % (
                    self.rurl1, self.get_img_path()))
                self.pkg("image-create -f -p test1=%s %s/3588_image_1" %
                         (self.rurl1, self.get_img_path()))

                # Bug 3588: Make sure we can't create an image where a
                # non-empty directory exists
                p = os.path.join(self.get_img_path(), "3588_2_image")
                os.mkdir(p)
                self.cmdline_run("touch %s/%s" % (p, "somefile"),
                    coverage=False)
                self.pkg("image-create -p test1=%s %s" % (self.rurl1, p),
                    exit=1)
                self.pkg("image-create -f -p test1=%s %s" % (self.rurl1, p))

                # Bug 17680: Ensure ssl directory is preserved if it
                # already exists when creating an image where one
                # might already exist with the -f (force) flag.
                shutil.rmtree(self.get_img_path())
                self.pkg("image-create -p test1=%s %s" % (self.rurl1,
                    self.get_img_path()))

                img = self.get_img_api_obj().img
                cert_path = os.path.join(img.imgdir, "ssl", "cert.file")
                misc.makedirs(os.path.dirname(cert_path))
                open(cert_path, "wb").close()
                assert os.path.exists(cert_path)
                self.pkg("image-create -f -p test1=%s %s" % (self.rurl1,
                    self.get_img_path()))
                assert os.path.exists(cert_path)
Example #13
0
def _write_versions_response(htdocs_path, fragment=False):
        """Writes a static versions/0 response for the Apache depot."""

        try:
                versions_path = os.path.join(htdocs_path,
                    *DEPOT_VERSIONS_DIRNAME)
                misc.makedirs(versions_path)

                with file(os.path.join(versions_path, "index.html"), "w") as \
                    versions_file:
                        versions_file.write(
                            fragment and DEPOT_FRAGMENT_VERSIONS_STR or
                            DEPOT_VERSIONS_STR)

                versions_file.close()
        except (OSError, apx.ApiException), err:
                raise DepotException(
                    _("Unable to write versions response: %s") % err)
Example #14
0
        def publish_package(self):
                """This method is called by the server to publish a package.

                It moves the files associated with the transaction into the
                appropriate position in the server repository.  Callers
                shall supply a fmri, repo store, and transaction in fmri,
                rstore, and trans, respectively."""

                pkg_name = self.fmri.pkg_name

                # mv manifest to pkg_name / version
                src_mpath = os.path.join(self.dir, "manifest")
                dest_mpath = self.rstore.manifest(self.fmri)
                misc.makedirs(os.path.dirname(dest_mpath))
                portable.rename(src_mpath, dest_mpath)

                # Move each file to file_root, with appropriate directory
                # structure.
                for f in os.listdir(self.dir):
                        if f == "append":
                                continue
                        src_path = os.path.join(self.dir, f)
                        self.rstore.cache_store.insert(f, src_path)
Example #15
0
        def publish_package(self):
                """This method is called by the server to publish a package.

                It moves the files associated with the transaction into the
                appropriate position in the server repository.  Callers
                shall supply a fmri, repo store, and transaction in fmri,
                rstore, and trans, respectively."""

                pkg_name = self.fmri.pkg_name

                # mv manifest to pkg_name / version
                src_mpath = os.path.join(self.dir, "manifest")
                dest_mpath = self.rstore.manifest(self.fmri)
                misc.makedirs(os.path.dirname(dest_mpath))
                portable.rename(src_mpath, dest_mpath)

                # Move each file to file_root, with appropriate directory
                # structure.
                for f in os.listdir(self.dir):
                        if f == "append":
                                continue
                        src_path = os.path.join(self.dir, f)
                        self.rstore.cache_store.insert(f, src_path)
Example #16
0
def refresh_conf(repo_info,
                 log_dir,
                 host,
                 port,
                 runtime_dir,
                 template_dir,
                 cache_dir,
                 cache_size,
                 sroot,
                 fragment=False,
                 allow_refresh=False,
                 ssl_cert_file="",
                 ssl_key_file="",
                 ssl_cert_chain_file=""):
    """Creates a new configuration for the depot."""
    try:
        ret = EXIT_OK
        if not repo_info:
            raise DepotException(_("no repositories found"))

        htdocs_path = os.path.join(runtime_dir, DEPOT_HTDOCS_DIRNAME, sroot)
        cleanup_htdocs(htdocs_path)
        misc.makedirs(htdocs_path)

        # pubs and default_pubs are lists of tuples of the form:
        # (publisher prefix, repository root dir, repository prefix,
        #     writable_root)
        pubs = []
        default_pubs = []
        errors = []

        # Query each repository for its publisher information.
        for (repo_root, repo_prefix, writable_root) in repo_info:
            try:
                publishers, default_pub, status = \
                    _get_publishers(repo_root)
                for pub in publishers:
                    pubs.append((pub, repo_root, repo_prefix, writable_root))
                default_pubs.append((default_pub, repo_root, repo_prefix))
                _write_status_response(status, htdocs_path, repo_prefix)
                # The writable root must exist and must be
                # owned by pkg5srv:pkg5srv
                if writable_root:
                    misc.makedirs(writable_root)
                    _chown_dir(writable_root)

            except DepotException as err:
                errors.append(str(err))
        if errors:
            raise DepotException(
                _("Unable to write configuration: "
                  "{0}").format("\n".join(errors)))

        # Write the publisher/0 response for each repository
        pubs_by_repo = {}
        for pub_prefix, repo_root, repo_prefix, writable_root in pubs:
            pubs_by_repo.setdefault(repo_prefix, []).append(pub_prefix)
        for repo_prefix in pubs_by_repo:
            _write_publisher_response(pubs_by_repo[repo_prefix], htdocs_path,
                                      repo_prefix)

        _write_httpd_conf(pubs,
                          default_pubs,
                          runtime_dir,
                          log_dir,
                          template_dir,
                          cache_dir,
                          cache_size,
                          host,
                          port,
                          sroot,
                          fragment=fragment,
                          allow_refresh=allow_refresh,
                          ssl_cert_file=ssl_cert_file,
                          ssl_key_file=ssl_key_file,
                          ssl_cert_chain_file=ssl_cert_chain_file)
        _write_versions_response(htdocs_path, fragment=fragment)
        # If we're writing a configuration fragment, then the web server
        # is probably not running as DEPOT_USER:DEPOT_GROUP
        if not fragment:
            _chown_dir(runtime_dir)
            _chown_dir(cache_dir)
        else:
            msg(_("Created {0}/depot.conf").format(runtime_dir))
    except (DepotException, OSError, apx.ApiException) as err:
        error(err)
        ret = EXIT_OOPS
    return ret
Example #17
0
def _write_httpd_conf(pubs, default_pubs, runtime_dir, log_dir, template_dir,
        cache_dir, cache_size, host, port, sroot,
        fragment=False, allow_refresh=False):
        """Writes the webserver configuration for the depot.

        pubs            repository and publisher information, a list in the form
                        [(publisher_prefix, repo_dir, repo_prefix), ... ]
        default_pubs    default publishers, per repository, a list in the form
                        [(default_publisher_prefix, repo_dir, repo_prefix) ... ]

        runtime_dir     where we write httpd.conf files
        log_dir         where Apache should write its log files
        template_dir    where we find our Mako templates
        cache_dir       where Apache should write its cache and wsgi search idx
        cache_size      how large our cache can grow
        host            our hostname, needed to set ServerName properly
        port            the port on which Apache should listen
        sroot           the prefix into the server namespace,
                        ignored if fragment==False
        fragment        True if we should only write a file to drop into conf.d/
                        (i.e. a partial server configuration)

        allow_refresh   True if we allow the 'refresh' or 'refresh-indexes'
                        admin/0 operations

        The URI namespace we create on the web server looks like this:

        <sroot>/<repo_prefix>/<publisher>/<file, catalog etc.>/<version>/
        <sroot>/<repo_prefix>/<file, catalog etc.>/<version>/

        'sroot' is only used when the Apache server is serving other content
        and we want to separate pkg(5) resources from the other resources
        provided.

        'repo_prefix' exists so that we can disambiguate between multiple
        repositories that provide the same publisher.
        """

        try:
                # check our hostname
                socket.getaddrinfo(host, None)

                # Apache needs IPv6 addresses wrapped in square brackets
                if ":" in host:
                        host = "[%s]" % host

                # check our directories
                dirs = [runtime_dir]
                if not fragment:
                        dirs.append(log_dir)
                if cache_dir:
                        dirs.append(cache_dir)
                for dir in dirs + [template_dir]:
                        if os.path.exists(dir) and not os.path.isdir(dir):
                                raise DepotException(
                                    _("%s is not a directory") % dir)

                for dir in dirs:
                        misc.makedirs(dir)

                # check our port
                if not fragment:
                        try:
                                num = int(port)
                                if num <= 0 or num >= 65535:
                                        raise DepotException(
                                            _("invalid port: %s") % port)
                        except ValueError:
                                raise DepotException(_("invalid port: %s") %
                                    port)

                # check our cache size
                try:
                        num = int(cache_size)
                        if num < 0:
                                raise DepotException(_("invalid cache size: "
                                   "%s") % num)
                except ValueError:
                        raise DepotException(_("invalid cache size: %s") %
                            cache_size)

                httpd_conf_template_path = os.path.join(template_dir,
                    DEPOT_HTTP_TEMPLATE)
                fragment_conf_template_path = os.path.join(template_dir,
                    DEPOT_FRAGMENT_TEMPLATE)

                # we're disabling unicode here because we want Mako to
                # passthrough any filesystem path names, whatever the
                # original encoding.
                conf_lookup = TemplateLookup(directories=[template_dir])
                if fragment:
                        conf_template = Template(
                            filename=fragment_conf_template_path,
                            disable_unicode=True, lookup=conf_lookup)
                        conf_path = os.path.join(runtime_dir,
                            DEPOT_FRAGMENT_FILENAME)
                else:
                        conf_template = Template(
                            filename=httpd_conf_template_path,
                            disable_unicode=True, lookup=conf_lookup)
                        conf_path = os.path.join(runtime_dir,
                            DEPOT_HTTP_FILENAME)

                conf_text = conf_template.render(
                    pubs=pubs,
                    default_pubs=default_pubs,
                    log_dir=log_dir,
                    cache_dir=cache_dir,
                    cache_size=cache_size,
                    runtime_dir=runtime_dir,
                    template_dir=template_dir,
                    ipv6_addr="::1",
                    host=host,
                    port=port,
                    sroot=sroot,
                    allow_refresh=allow_refresh
                )

                with file(conf_path, "wb") as conf_file:
                        conf_file.write(conf_text)

        except socket.gaierror, err:
                raise DepotException(
                    _("Unable to write Apache configuration: %(host)s: "
                    "%(err)s") % locals())
Example #18
0
def _write_httpd_conf(pubs,
                      default_pubs,
                      runtime_dir,
                      log_dir,
                      template_dir,
                      cache_dir,
                      cache_size,
                      host,
                      port,
                      sroot,
                      fragment=False,
                      allow_refresh=False,
                      ssl_cert_file="",
                      ssl_key_file="",
                      ssl_cert_chain_file=""):
    """Writes the webserver configuration for the depot.

        pubs            repository and publisher information, a list in the form
                        [(publisher_prefix, repo_dir, repo_prefix,
                            writable_root), ... ]
        default_pubs    default publishers, per repository, a list in the form
                        [(default_publisher_prefix, repo_dir, repo_prefix) ... ]

        runtime_dir     where we write httpd.conf files
        log_dir         where Apache should write its log files
        template_dir    where we find our Mako templates
        cache_dir       where Apache should write its cache and wsgi search idx
        cache_size      how large our cache can grow
        host            our hostname, needed to set ServerName properly
        port            the port on which Apache should listen
        sroot           the prefix into the server namespace,
                        ignored if fragment==False
        fragment        True if we should only write a file to drop into conf.d/
                        (i.e. a partial server configuration)

        allow_refresh   True if we allow the 'refresh' or 'refresh-indexes'
                        admin/0 operations

        The URI namespace we create on the web server looks like this:

        <sroot>/<repo_prefix>/<publisher>/<file, catalog etc.>/<version>/
        <sroot>/<repo_prefix>/<file, catalog etc.>/<version>/

        'sroot' is only used when the Apache server is serving other content
        and we want to separate pkg(5) resources from the other resources
        provided.

        'repo_prefix' exists so that we can disambiguate between multiple
        repositories that provide the same publisher.

        'ssl_cert_file' the location of the server certificate file.

        'ssl_key_file' the location of the server key file.

        'ssl_cert_chain_file' the location of the certificate chain file if the
            the server certificate is not signed by the top level CA.
        """

    try:
        # check our hostname
        socket.getaddrinfo(host, None)

        # Apache needs IPv6 addresses wrapped in square brackets
        if ":" in host:
            host = "[{0}]".format(host)

        # check our directories
        dirs = [runtime_dir]
        if not fragment:
            dirs.append(log_dir)
        if cache_dir:
            dirs.append(cache_dir)
        for dir in dirs + [template_dir]:
            if os.path.exists(dir) and not os.path.isdir(dir):
                raise DepotException(_("{0} is not a directory").format(dir))

        for dir in dirs:
            misc.makedirs(dir)

        # check our port
        if not fragment:
            try:
                num = int(port)
                if num <= 0 or num >= 65535:
                    raise DepotException(_("invalid port: {0}").format(port))
            except ValueError:
                raise DepotException(_("invalid port: {0}").format(port))

        # check our cache size
        try:
            num = int(cache_size)
            if num < 0:
                raise DepotException(
                    _("invalid cache size: "
                      "{0}").format(num))
        except ValueError:
            raise DepotException(
                _("invalid cache size: {0}").format(cache_size))

        httpd_conf_template_path = os.path.join(template_dir,
                                                DEPOT_HTTP_TEMPLATE)
        fragment_conf_template_path = os.path.join(template_dir,
                                                   DEPOT_FRAGMENT_TEMPLATE)

        # we're disabling unicode here because we want Mako to
        # passthrough any filesystem path names, whatever the
        # original encoding.
        conf_lookup = TemplateLookup(directories=[template_dir])
        if fragment:
            conf_template = Template(filename=fragment_conf_template_path,
                                     disable_unicode=True,
                                     lookup=conf_lookup)
            conf_path = os.path.join(runtime_dir, DEPOT_FRAGMENT_FILENAME)
        else:
            conf_template = Template(filename=httpd_conf_template_path,
                                     disable_unicode=True,
                                     lookup=conf_lookup)
            conf_path = os.path.join(runtime_dir, DEPOT_HTTP_FILENAME)

        conf_text = conf_template.render(
            pubs=pubs,
            default_pubs=default_pubs,
            log_dir=log_dir,
            cache_dir=cache_dir,
            cache_size=cache_size,
            runtime_dir=runtime_dir,
            template_dir=template_dir,
            ipv6_addr="::1",
            host=host,
            port=port,
            sroot=sroot,
            allow_refresh=allow_refresh,
            ssl_cert_file=ssl_cert_file,
            ssl_key_file=ssl_key_file,
            ssl_cert_chain_file=ssl_cert_chain_file)

        with file(conf_path, "wb") as conf_file:
            conf_file.write(conf_text)

    except socket.gaierror as err:
        raise DepotException(
            _("Unable to write Apache configuration: {host}: "
              "{err}").format(**locals()))
    except (OSError, IOError, EnvironmentError, apx.ApiException) as err:
        traceback.print_exc(err)
        raise DepotException(
            _("Unable to write depot_httpd.conf: {0}").format(err))
Example #19
0
File: pull.py Project: aszeszo/test
                                t.close(add_to_catalog=False)
                        except trans.TransactionError, e:
                                abort(err=e)

                        # Dump data retrieved so far after each successful
                        # republish to conserve space.
                        try:
                                shutil.rmtree(dest_xport_cfg.incoming_root)
                                shutil.rmtree(pkgdir)
                                if cache_dir in tmpdirs:
                                        # If cache_dir is listed in tmpdirs,
                                        # then it's safe to dump cache contents.
                                        # Otherwise, it's a user cache directory
                                        # and shouldn't be dumped.
                                        shutil.rmtree(cache_dir)
                                        misc.makedirs(cache_dir)
                        except EnvironmentError, e:
                                raise apx._convert_error(e)
                        misc.makedirs(dest_xport_cfg.incoming_root)

                        processed += 1
                        tracker.republish_end_pkg(f)

                tracker.republish_done()
                tracker.reset()

                if processed > 0:
                        # If any packages were published, trigger an update of
                        # the catalog.
                        total_processed += processed
                        dest_xport.publish_refresh_packages(targ_pub)