Search in sources :

Example 56 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class KojiContentManagerDecorator method proxyKojiBuild.

private <T> T proxyKojiBuild(final StoreKey inStore, final ArtifactRef artifactRef, final String path, EventMetadata eventMetadata, T defValue, KojiBuildAction<T> consumer) throws IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    try {
        return kojiClient.withKojiSession((session) -> {
            List<KojiBuildInfo> builds = kojiClient.listBuildsContaining(artifactRef, session);
            Collections.sort(builds, (build1, build2) -> build2.getCreationTime().compareTo(build1.getCreationTime()));
            logger.debug("Got {} builds from koji. Looking for best match.", builds.size());
            for (KojiBuildInfo build : builds) {
                if (build.getBuildState() != KojiBuildState.COMPLETE) {
                    logger.debug("Build: {} is not completed. The state is {}. Skipping.", build.getNvr(), build.getBuildState());
                    continue;
                }
                boolean buildAllowed = false;
                if (isBinaryBuild(build)) {
                    // This is not a real build, it's a binary import.
                    if (config.isProxyBinaryBuilds()) {
                        logger.info("Trying binary build: {} with id: {}", build.getNvr(), build.getId());
                        buildAllowed = true;
                    } else {
                        logger.debug("Skipping binary build: {} with id: {}", build.getNvr(), build.getId());
                    }
                } else {
                    logger.info("Trying build: {} with id: {}", build.getNvr(), build.getId());
                    if (!config.isTagPatternsEnabled()) {
                        buildAllowed = true;
                    } else {
                        List<KojiTagInfo> tags = kojiClient.listTags(build.getId(), session);
                        logger.debug("Build is in {} tags...", tags.size());
                        for (KojiTagInfo tag : tags) {
                            // If the tags match patterns configured in whitelist, construct a new remote repo.
                            if (config.isTagAllowed(tag.getName())) {
                                logger.info("Koji tag is on whitelist: {}", tag.getName());
                                buildAllowed = true;
                                break;
                            } else {
                                logger.debug("Tag: {} is not in the whitelist.", tag.getName());
                            }
                        }
                    }
                }
                if (buildAllowed) {
                    // or the artifact matches the one in the authoritative store, go ahead.
                    if (buildAuthority.isAuthorized(path, eventMetadata, artifactRef, build, session)) {
                        return consumer.execute(inStore, artifactRef, build, session);
                    }
                } else {
                    logger.debug("No whitelisted tags found for: {}", build.getNvr());
                }
            }
            logger.debug("No builds were found that matched the restrictions.");
            return defValue;
        });
    } catch (KojiClientException e) {
        throw new IndyWorkflowException("Cannot retrieve builds for: %s. Error: %s", e, artifactRef, e.getMessage());
    }
}
Also used : KojiClientException(com.redhat.red.build.koji.KojiClientException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Logger(org.slf4j.Logger) KojiTagInfo(com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)

Example 57 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class KojiContentManagerDecorator method adjustTargetGroup.

private Group adjustTargetGroup(final RemoteRepository buildRepo, final Group group) throws IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    // try to lookup the group -> targetGroup mapping in config, using the
    // entry-point group as the lookup key. If that returns null, the targetGroup is
    // the entry-point group.
    Group targetGroup = group;
    boolean isBinaryBuild = KOJI_ORIGIN_BINARY.equals(buildRepo.getMetadata(ArtifactStore.METADATA_ORIGIN));
    String targetName = isBinaryBuild ? config.getTargetBinaryGroup(group.getName()) : config.getTargetGroup(group.getName());
    if (targetName != null) {
        try {
            targetGroup = storeDataManager.query().packageType(group.getPackageType()).getGroup(targetName);
        } catch (IndyDataException e) {
            throw new IndyWorkflowException("Cannot lookup koji-addition target group: %s (source group: %s). Reason: %s", e, targetName, group.getName(), e.getMessage());
        }
    }
    logger.info("Adding Koji build proxy: {} to group: {}", buildRepo.getKey(), targetGroup.getKey());
    // Append the new remote repo as a member of the targetGroup.
    targetGroup.addConstituent(buildRepo);
    try {
        final ChangeSummary changeSummary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "Adding remote repository for Koji build: " + buildRepo.getMetadata(NVR));
        storeDataManager.storeArtifactStore(targetGroup, changeSummary, false, true, new EventMetadata());
    } catch (IndyDataException e) {
        throw new IndyWorkflowException("Cannot store target-group: %s changes for: %s. Error: %s", e, targetGroup.getName(), buildRepo.getMetadata(NVR), e.getMessage());
    }
    logger.info("Retrieving GAV: {} from: {}", buildRepo.getMetadata(CREATION_TRIGGER_GAV), buildRepo);
    return targetGroup;
// TODO: how to index it for the group...?
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Logger(org.slf4j.Logger) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 58 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class MavenMetadataGenerator method generateDirectoryContent.

@Override
public List<StoreResource> generateDirectoryContent(final ArtifactStore store, final String path, final List<StoreResource> existing, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final StoreResource mdResource = new StoreResource(LocationUtils.toLocation(store), Paths.get(path, MavenMetadataMerger.METADATA_NAME).toString());
    if (existing.contains(mdResource)) {
        return null;
    }
    int pathElementsCount = StringUtils.strip(path, "/").split("/").length;
    // if there is a possibility we are listing an artifactId
    if (pathElementsCount >= 2) {
        // regardless, we will need this first level of listings. What we do with it will depend on the logic below...
        final List<StoreResource> firstLevelFiles = fileManager.listRaw(store, path);
        ArtifactPathInfo samplePomInfo = null;
        for (final StoreResource topResource : firstLevelFiles) {
            final String topPath = topResource.getPath();
            if (topPath.endsWith(".pom")) {
                samplePomInfo = ArtifactPathInfo.parse(topPath);
                break;
            }
        }
        // if this dir does not contain a pom check if a subdir contain a pom
        if (samplePomInfo == null) {
            List<String> firstLevelDirs = firstLevelFiles.stream().map((res) -> res.getPath()).filter((subpath) -> subpath.endsWith("/")).collect(Collectors.toList());
            final Map<String, List<StoreResource>> secondLevelMap = fileManager.listRaw(store, firstLevelDirs);
            nextTopResource: for (final String topPath : firstLevelDirs) {
                final List<StoreResource> secondLevelListing = secondLevelMap.get(topPath);
                for (final StoreResource fileResource : secondLevelListing) {
                    if (fileResource.getPath().endsWith(".pom")) {
                        if (samplePomInfo == null) {
                            samplePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                            break nextTopResource;
                        }
                        continue nextTopResource;
                    }
                }
            }
        }
        // We won't worry about this for now.
        if (samplePomInfo != null) {
            final List<StoreResource> result = new ArrayList<StoreResource>();
            result.add(mdResource);
            result.add(new StoreResource(LocationUtils.toLocation(store), Paths.get(path, MavenMetadataMerger.METADATA_MD5_NAME).toString()));
            result.add(new StoreResource(LocationUtils.toLocation(store), Paths.get(path, MavenMetadataMerger.METADATA_SHA_NAME).toString()));
            return result;
        }
    }
    return null;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) MergedContentAction(org.commonjava.indy.content.MergedContentAction) PathUtils.normalize(org.commonjava.maven.galley.util.PathUtils.normalize) HashMap(java.util.HashMap) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) VersionUtils(org.commonjava.maven.atlas.ident.util.VersionUtils) Transfer(org.commonjava.maven.galley.model.Transfer) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) Document(org.w3c.dom.Document) SimpleTypeAndClassifier(org.commonjava.maven.atlas.ident.ref.SimpleTypeAndClassifier) Map(java.util.Map) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) PathUtils.parentPath(org.commonjava.maven.galley.util.PathUtils.parentPath) TypeAndClassifier(org.commonjava.maven.atlas.ident.ref.TypeAndClassifier) OutputStream(java.io.OutputStream) GroupMergeHelper(org.commonjava.indy.core.content.group.GroupMergeHelper) MavenMetadataMerger(org.commonjava.indy.pkg.maven.content.group.MavenMetadataMerger) LocationUtils(org.commonjava.indy.util.LocationUtils) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IOUtils.closeQuietly(org.apache.commons.io.IOUtils.closeQuietly) StoreType(org.commonjava.indy.model.core.StoreType) Set(java.util.Set) IOException(java.io.IOException) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) Collectors(java.util.stream.Collectors) File(java.io.File) SnapshotPart(org.commonjava.maven.atlas.ident.version.part.SnapshotPart) List(java.util.List) Element(org.w3c.dom.Element) Paths(java.nio.file.Paths) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) TypeMapper(org.commonjava.maven.galley.maven.spi.type.TypeMapper) StoreResource(org.commonjava.indy.content.StoreResource) XMLInfrastructure(org.commonjava.maven.galley.maven.parse.XMLInfrastructure) TypeMapping(org.commonjava.maven.galley.model.TypeMapping) AbstractMergedContentGenerator(org.commonjava.indy.core.content.AbstractMergedContentGenerator) ArtifactPathInfo(org.commonjava.maven.atlas.ident.util.ArtifactPathInfo) Collections(java.util.Collections) DirectContentAccess(org.commonjava.indy.content.DirectContentAccess) StoreDataManager(org.commonjava.indy.data.StoreDataManager) SnapshotUtils(org.commonjava.maven.atlas.ident.util.SnapshotUtils) StoreResource(org.commonjava.indy.content.StoreResource) ArtifactPathInfo(org.commonjava.maven.atlas.ident.util.ArtifactPathInfo) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class MavenMetadataGenerator method generateGroupFileContent.

@Override
public Transfer generateGroupFileContent(final Group group, final List<ArtifactStore> members, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    if (!canProcess(path)) {
        return null;
    }
    final Transfer target = fileManager.getTransfer(group, path);
    logger.debug("Working on metadata file: {} (already exists? {})", target, target != null && target.exists());
    if (!target.exists()) {
        String toMergePath = path;
        if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
            toMergePath = normalize(normalize(parentPath(toMergePath)), MavenMetadataMerger.METADATA_NAME);
        }
        final List<Transfer> sources = fileManager.retrieveAllRaw(members, toMergePath, new EventMetadata());
        final byte[] merged = merger.merge(sources, group, toMergePath);
        if (merged != null) {
            OutputStream fos = null;
            try {
                fos = target.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
                fos.write(merged);
            } catch (final IOException e) {
                throw new IndyWorkflowException("Failed to write merged metadata to: {}.\nError: {}", e, target, e.getMessage());
            } finally {
                closeQuietly(fos);
            }
            helper.writeMergeInfo(merged, sources, group, toMergePath);
        }
    }
    if (target.exists()) {
        return target;
    }
    return null;
}
Also used : OutputStream(java.io.OutputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 60 with IndyWorkflowException

use of org.commonjava.indy.IndyWorkflowException in project indy by Commonjava.

the class MavenMetadataGenerator method generateFileContent.

@Override
public Transfer generateFileContent(final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    // metadata merging is something else...don't handle it here.
    if (StoreType.group == store.getKey().getType()) {
        return null;
    }
    if (!canProcess(path)) {
        return null;
    }
    boolean generated = false;
    // TODO: Generation of plugin metadata files (groupId-level) is harder, and requires cracking open the jar file
    // This is because that's the only place the plugin prefix can be reliably retrieved from.
    // regardless, we will need this first level of listings. What we do with it will depend on the logic below...
    final String parentPath = Paths.get(path).getParent().toString();
    List<StoreResource> firstLevel;
    try {
        firstLevel = fileManager.listRaw(store, parentPath);
    } catch (final IndyWorkflowException e) {
        logger.error(String.format("SKIP: Failed to generate maven-metadata.xml from listing of directory contents for: %s under path: %s", store, parentPath), e);
        return null;
    }
    //
    //        if ( firstLevel == null || firstLevel.isEmpty() )
    //        {
    //            logger.error( String.format( "SKIP: Listing is empty for: %s under path: %s", store, parentPath ) );
    //            return null;
    //        }
    String toGenPath = path;
    if (!path.endsWith(MavenMetadataMerger.METADATA_NAME)) {
        toGenPath = normalize(normalize(parentPath(toGenPath)), MavenMetadataMerger.METADATA_NAME);
    }
    ArtifactPathInfo snapshotPomInfo = null;
    if (parentPath.endsWith(SnapshotUtils.LOCAL_SNAPSHOT_VERSION_PART)) {
        // If we're in a version directory, first-level listing should include a .pom file
        for (final StoreResource resource : firstLevel) {
            if (resource.getPath().endsWith(".pom")) {
                snapshotPomInfo = ArtifactPathInfo.parse(resource.getPath());
                break;
            }
        }
    }
    if (snapshotPomInfo != null) {
        logger.info("Generating maven-metadata.xml for snapshots");
        generated = writeSnapshotMetadata(snapshotPomInfo, firstLevel, store, toGenPath, eventMetadata);
    } else {
        logger.info("Generating maven-metadata.xml for releases");
        generated = writeVersionMetadata(firstLevel, store, toGenPath, eventMetadata);
    }
    return generated ? fileManager.getTransfer(store, path) : null;
}
Also used : StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.maven.atlas.ident.util.ArtifactPathInfo)

Aggregations

IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)109 Response (javax.ws.rs.core.Response)40 Transfer (org.commonjava.maven.galley.model.Transfer)39 IOException (java.io.IOException)36 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)36 StoreKey (org.commonjava.indy.model.core.StoreKey)36 ApiOperation (io.swagger.annotations.ApiOperation)35 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)34 ApiResponse (io.swagger.annotations.ApiResponse)33 Path (javax.ws.rs.Path)32 StoreType (org.commonjava.indy.model.core.StoreType)26 IndyDataException (org.commonjava.indy.data.IndyDataException)25 GET (javax.ws.rs.GET)24 Logger (org.slf4j.Logger)22 ApiResponses (io.swagger.annotations.ApiResponses)21 ArrayList (java.util.ArrayList)19 Produces (javax.ws.rs.Produces)18 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)18 InputStream (java.io.InputStream)15 List (java.util.List)13