Exemplo n.º 1
0
    def run(self):
        try:
            get_cachito(self.workflow)
        except KeyError:
            self.log.info(
                'Aborting plugin execution: missing Cachito configuration')
            return

        if self.workflow.source.config.remote_sources:
            raise ValueError(
                'Multiple remote sources are not supported, use single '
                'remote source in container.yaml')

        remote_source_params = self.workflow.source.config.remote_source
        if not remote_source_params:
            self.log.info(
                'Aborting plugin execution: missing remote_source configuration'
            )
            return

        if self._dependency_replacements and not is_scratch_build(
                self.workflow):
            raise ValueError(
                'Cachito dependency replacements are only allowed for scratch builds'
            )

        user = self.get_koji_user()
        self.log.info('Using user "%s" for cachito request', user)

        source_request = self.cachito_session.request_sources(
            user=user,
            dependency_replacements=self._dependency_replacements,
            **remote_source_params)
        source_request = self.cachito_session.wait_for_request(source_request)

        remote_source_json = self.source_request_to_json(source_request)
        remote_source_url = self.cachito_session.assemble_download_url(
            source_request)
        remote_source_conf_url = remote_source_json.get('configuration_files')
        remote_source_icm_url = remote_source_json.get('content_manifest')
        self.set_worker_params(source_request, remote_source_url,
                               remote_source_conf_url, remote_source_icm_url)

        dest_dir = self.workflow.source.workdir
        dest_path = self.cachito_session.download_sources(source_request,
                                                          dest_dir=dest_dir)

        return {
            # Annotations to be added to the current Build object
            'annotations': {
                'remote_source_url': remote_source_url
            },
            # JSON representation of the remote source request
            'remote_source_json': remote_source_json,
            # Local path to the remote source archive
            'remote_source_path': dest_path,
        }
Exemplo n.º 2
0
 def run(self):
     """
     run the plugin
     """
     try:
         get_cachito(self.workflow)
     except KeyError:
         self.log.info('Aborting plugin execution: missing Cachito configuration')
         return
     self._populate_content_sets()
     self._update_icm_data()
     self._write_json_file()
     self._add_to_dockerfile()
     self.log.info('added "%s" to "%s"', self.icm_file_name, self.content_manifests_dir)
Exemplo n.º 3
0
 def cachito_verify(self):
     if self._cachito_verify is None:
         try:
             cachito_conf = get_cachito(self.workflow)
         except KeyError:
             cachito_conf = {}
         # Get the value of Cachito's 'insecure' key from the active reactor config map,
         #    *flip it*, and let the result tell us whether to verify or not
         self._cachito_verify = not cachito_conf.get('insecure', False)
     return self._cachito_verify
Exemplo n.º 4
0
    def run(self):
        try:
            get_cachito(self.workflow)
        except KeyError:
            self.log.info(
                'Aborting plugin execution: missing Cachito configuration')
            return

        remote_source_config = self.workflow.source.config.remote_source
        if not remote_source_config:
            self.log.info(
                'Aborting plugin execution: missing remote_source configuration'
            )
            return

        user = self.get_koji_user()

        source_request = self.cachito_session.request_sources(
            user=user, **remote_source_config)
        source_request = self.cachito_session.wait_for_request(source_request)

        remote_source_json = self.source_request_to_json(source_request)
        remote_source_url = self.cachito_session.assemble_download_url(
            source_request)
        self.set_worker_params(source_request, remote_source_url)

        dest_dir = self.workflow.source.workdir
        dest_path = self.cachito_session.download_sources(source_request,
                                                          dest_dir=dest_dir)

        return {
            # Annotations to be added to the current Build object
            'annotations': {
                'remote_source_url': remote_source_url
            },
            # JSON representation of the remote source request
            'remote_source_json': remote_source_json,
            # Local path to the remote source archive
            'remote_source_path': dest_path,
        }
Exemplo n.º 5
0
    def get_koji_user(self):
        unknown_user = get_cachito(self.workflow).get('unknown_user',
                                                      'unknown_user')
        try:
            metadata = get_build_json()['metadata']
        except KeyError:
            msg = 'Unable to get koji user: No build metadata'
            self.log.warning(msg)
            return unknown_user

        try:
            koji_task_id = int(metadata.get('labels').get('koji-task-id'))
        except (ValueError, TypeError, AttributeError):
            msg = 'Unable to get koji user: Invalid Koji task ID'
            self.log.warning(msg)
            return unknown_user

        koji_session = get_koji_session(self.workflow)
        return get_koji_task_owner(koji_session,
                                   koji_task_id).get('name', unknown_user)
Exemplo n.º 6
0
    def run(self):
        """
        Run the plugin.
        """
        if not self.remote_sources:
            self.log.info('Missing remote_sources parameters, skipping plugin')
            return

        session = get_retrying_requests_session()

        archives = []
        cachito_config = get_cachito(self.workflow)
        insecure_ssl_conn = cachito_config.get('insecure', False)

        for remote_source in self.remote_sources:
            parsed_url = urlparse(remote_source['url'])
            dest_filename = os.path.basename(parsed_url.path)
            # prepend remote source name to destination filename, so multiple source archives
            # don't have name collision
            if self.multiple_remote_sources:
                dest_filename = "{}_{}".format(remote_source['name'],
                                               dest_filename)

            # Download the source code archive
            archive = download_url(remote_source['url'],
                                   self.workflow.source.workdir,
                                   session=session,
                                   insecure=insecure_ssl_conn,
                                   dest_filename=dest_filename)
            archives.append(archive)

            # Unpack the source code archive into a dedicated dir in container build workdir
            dest_dir = os.path.join(self.workflow.builder.df_dir,
                                    self.REMOTE_SOURCE)
            sub_path = self.REMOTE_SOURCE

            if self.multiple_remote_sources:
                dest_dir = os.path.join(dest_dir, remote_source['name'])
                sub_path = os.path.join(sub_path, remote_source['name'])

            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            else:
                raise RuntimeError(
                    'Conflicting path {} already exists in the dist-git repository'
                    .format(sub_path))

            with tarfile.open(archive) as tf:
                tf.extractall(dest_dir)

            config_files = (self.get_remote_source_config(
                session, remote_source["configs"], insecure_ssl_conn))

            self.generate_cachito_config_files(dest_dir, config_files)

            # Set build args
            if not self.multiple_remote_sources:
                self.workflow.builder.buildargs.update(
                    remote_source['build_args'])

            # Create cachito.env file with environment variables received from cachito request
            self.generate_cachito_env_file(dest_dir,
                                           remote_source['build_args'])

        self.add_general_buildargs()

        return archives
Exemplo n.º 7
0
    def run(self):
        try:
            get_cachito(self.workflow)
        except KeyError:
            self.log.info(
                'Aborting plugin execution: missing Cachito configuration')
            return

        if (not get_allow_multiple_remote_sources(self.workflow)
                and self.multiple_remote_sources_params):
            raise ValueError('Multiple remote sources are not enabled, '
                             'use single remote source in container.yaml')

        if not (self.single_remote_source_params
                or self.multiple_remote_sources_params):
            self.log.info(
                'Aborting plugin execution: missing remote source configuration'
            )
            return

        if self._dependency_replacements and not is_scratch_build(
                self.workflow):
            raise ValueError(
                'Cachito dependency replacements are only allowed for scratch builds'
            )
        if self._dependency_replacements and self.multiple_remote_sources_params:
            raise ValueError('Cachito dependency replacements are not allowed '
                             'for multiple remote sources')

        remote_sources_workers_params = []
        remote_sources_output = []
        user = self.get_koji_user()
        self.log.info('Using user "%s" for cachito request', user)
        if self.multiple_remote_sources_params:
            self.verify_multiple_remote_sources_names_are_unique()

            open_requests = {
                remote_source["name"]: self.cachito_session.request_sources(
                    user=user,
                    dependency_replacements=self._dependency_replacements,
                    **remote_source["remote_source"])
                for remote_source in self.multiple_remote_sources_params
            }

            completed_requests = {
                name: self.cachito_session.wait_for_request(request)
                for name, request in open_requests.items()
            }
            for name, request in completed_requests.items():
                self.process_request(request, name,
                                     remote_sources_workers_params,
                                     remote_sources_output)

        else:
            open_request = self.cachito_session.request_sources(
                user=user,
                dependency_replacements=self._dependency_replacements,
                **self.single_remote_source_params)
            completed_request = self.cachito_session.wait_for_request(
                open_request)
            self.process_request(completed_request, None,
                                 remote_sources_workers_params,
                                 remote_sources_output)

        self.set_worker_params(remote_sources_workers_params)

        return remote_sources_output
Exemplo n.º 8
0
    def run(self):
        """
        Run the plugin.
        """
        if not self.url:
            self.log.info('No remote source url to download, skipping plugin')
            return

        session = get_retrying_requests_session()

        # Download the source code archive
        cachito_config = get_cachito(self.workflow)
        insecure_ssl_conn = cachito_config.get('insecure', False)
        archive = download_url(self.url,
                               self.workflow.source.workdir,
                               session=session,
                               insecure=insecure_ssl_conn)

        # Unpack the source code archive into a dedicated dir in container build workdir
        dest_dir = os.path.join(self.workflow.builder.df_dir,
                                self.REMOTE_SOURCE)
        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)
        else:
            raise RuntimeError(
                'Conflicting path {} already exists in the dist-git repository'
                .format(self.REMOTE_SOURCE))

        with tarfile.open(archive) as tf:
            tf.extractall(dest_dir)

        config_files = (self.get_remote_source_config(
            session, self.remote_source_conf_url, insecure_ssl_conn)
                        if self.remote_source_conf_url else [])

        # Inject cachito provided configuration files
        for config in config_files:
            config_path = os.path.join(dest_dir, config['path'])
            if config['type'] == CFG_TYPE_B64:
                data = base64.b64decode(config['content'])
                with open(config_path, 'wb') as f:
                    f.write(data)
            else:
                err_msg = "Unknown cachito configuration file data type '{}'".format(
                    config['type'])
                raise ValueError(err_msg)

            os.chmod(config_path, 0o444)

        # Set build args
        self.workflow.builder.buildargs.update(self.buildargs)

        # Create cachito.env file with environment variables received from cachito request
        self.generate_cachito_env_file()

        # To copy the sources into the build image, Dockerfile should contain
        # COPY $REMOTE_SOURCE $REMOTE_SOURCE_DIR
        args_for_dockerfile_to_add = {
            'REMOTE_SOURCE': self.REMOTE_SOURCE,
            'REMOTE_SOURCE_DIR': REMOTE_SOURCE_DIR,
        }
        self.workflow.builder.buildargs.update(args_for_dockerfile_to_add)

        return archive