Beispiel #1
0
    def _check_tarfile(self, app_name, app_tarfile):
        if app_name and app_tarfile:
            if not os.path.isfile(app_tarfile):
                raise wsme.exc.ClientSideError(
                    _("Application-upload rejected: application tar file {} does "
                      "not exist.".format(app_tarfile)))
            if (not app_tarfile.endswith('.tgz')
                    and not app_tarfile.endswith('.tar.gz')):
                raise wsme.exc.ClientSideError(
                    _("Application-upload rejected: {} has unrecognizable tar file "
                      "extension. Supported extensions are: .tgz and .tar.gz.".
                      format(app_tarfile)))

            with TempDirectory() as app_path:
                if not cutils.extract_tarfile(app_path, app_tarfile):
                    raise wsme.exc.ClientSideError(
                        _("Application-upload rejected: failed to extract tar file "
                          "{}.".format(os.path.basename(app_tarfile))))

                # If checksum file is included in the tarball, verify its contents.
                if not cutils.verify_checksum(app_path):
                    raise wsme.exc.ClientSideError(
                        _("Application-upload rejected: checksum validation failed."
                          ))

                mname, mfile = self._find_manifest_file(app_path)

                charts_dir = os.path.join(app_path, 'charts')
                if os.path.isdir(charts_dir):
                    tar_filelist = cutils.get_files_matching(app_path, '.tgz')
                    if len(os.listdir(charts_dir)) == 0:
                        raise wsme.exc.ClientSideError(
                            _("Application-upload rejected: tar file contains no "
                              "Helm charts."))
                    if not tar_filelist:
                        raise wsme.exc.ClientSideError(
                            _("Application-upload rejected: tar file contains no "
                              "Helm charts of expected file extension (.tgz).")
                        )
                    for p, f in tar_filelist:
                        if not cutils.extract_tarfile(p, os.path.join(p, f)):
                            raise wsme.exc.ClientSideError(
                                _("Application-upload rejected: failed to extract tar "
                                  "file {}.".format(os.path.basename(f))))
                LOG.info("Tar file of application %s verified." % app_name)
                return mname, mfile

        else:
            raise ValueError(
                _("Application-upload rejected: both application name and tar "
                  "file must be specified."))
Beispiel #2
0
    def _check_tarfile(self, app_tarfile, app_name, app_version, operation):
        def _handle_upload_failure(reason):
            raise wsme.exc.ClientSideError(
                _("Application-{} rejected: ".format(operation) + reason))

        if app_tarfile:
            if cutils.is_url(app_tarfile):
                # For tarfile that is downloaded remotely, defer the checksum, manifest
                # and tarfile content validations to sysinv-conductor as download can
                # take some time depending on network traffic, target server and file
                # size.
                if not app_name:
                    app_name = self._make_db_placeholder(
                        constants.APP_NAME_PLACEHOLDER, app_tarfile)
                if not app_version:
                    app_version = self._make_db_placeholder(
                        constants.APP_VERSION_PLACEHOLDER, app_tarfile)
                mname = constants.APP_MANIFEST_NAME_PLACEHOLDER
                mfile = constants.APP_TARFILE_NAME_PLACEHOLDER
                return app_name, app_version, mname, mfile

            if not os.path.isfile(app_tarfile):
                _handle_upload_failure(
                    "application tar file {} does not exist.".format(
                        app_tarfile))
            if (not app_tarfile.endswith('.tgz')
                    and not app_tarfile.endswith('.tar.gz')):
                _handle_upload_failure(
                    "{} has unrecognizable tar file extension. Supported "
                    "extensions are: .tgz and .tar.gz.".format(app_tarfile))

            with cutils.TempDirectory() as app_path:
                if not cutils.extract_tarfile(app_path, app_tarfile):
                    _handle_upload_failure(
                        "failed to extract tar file {}.".format(
                            os.path.basename(app_tarfile)))

                # If checksum file is included in the tarball, verify its contents.
                if not cutils.verify_checksum(app_path):
                    _handle_upload_failure("checksum validation failed.")

                app_helper = KubeAppHelper(pecan.request.dbapi)
                try:
                    name, version, patches = app_helper._verify_metadata_file(
                        app_path, app_name, app_version)
                    mname, mfile = app_helper._find_manifest_file(app_path)
                    app_helper._extract_helm_charts(app_path)
                    LOG.info("Tar file of application %s verified." % name)
                    return name, version, mname, mfile
                except exception.SysinvException as e:
                    _handle_upload_failure(str(e))
        else:
            raise ValueError(
                _("Application-{} rejected: tar file must be specified.".
                  format(operation)))
Beispiel #3
0
 def _extract_helm_charts(self, app_path, demote_user=False):
     charts_dir = os.path.join(app_path, 'charts')
     if os.path.isdir(charts_dir):
         tar_filelist = cutils.get_files_matching(app_path, '.tgz')
         if len(os.listdir(charts_dir)) == 0:
             raise exception.SysinvException(
                 _("tar file contains no Helm charts."))
         if not tar_filelist:
             raise exception.SysinvException(
                 _("tar file contains no Helm charts of "
                   "expected file extension (.tgz)."))
         for p, f in tar_filelist:
             if not cutils.extract_tarfile(p, os.path.join(p, f),
                                           demote_user):
                 raise exception.SysinvException(
                     _("failed to extract tar file {}.".format(
                         os.path.basename(f))))