Search in sources :

Example 36 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class ImpliedReposQueryDelegate method getGroupsContaining.

@Override
public Set<Group> getGroupsContaining(StoreKey key) throws IndyDataException {
    ArtifactStore store = dataManager.getArtifactStore(key);
    if (store == null) {
        return Collections.emptySet();
    }
    boolean storeIsImplied = IMPLIED_REPO_ORIGIN.equals(store.getMetadata(METADATA_ORIGIN));
    Set<Group> delegateGroups = delegate().getGroupsContaining(key);
    if (!storeIsImplied || delegateGroups == null || delegateGroups.isEmpty()) {
        return delegateGroups;
    }
    Set<Group> result = delegateGroups.stream().filter((group) -> config.isEnabledForGroup(group.getName())).collect(Collectors.toSet());
    return result;
}
Also used : ArtifactStoreQuery(org.commonjava.indy.data.ArtifactStoreQuery) StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IMPLIED_REPO_ORIGIN(org.commonjava.indy.implrepo.data.ImpliedReposStoreDataManagerDecorator.IMPLIED_REPO_ORIGIN) LoggerFactory(org.slf4j.LoggerFactory) METADATA_ORIGIN(org.commonjava.indy.model.core.ArtifactStore.METADATA_ORIGIN) DelegatingArtifactStoreQuery(org.commonjava.indy.data.DelegatingArtifactStoreQuery) Set(java.util.Set) Collectors(java.util.stream.Collectors) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ImpliedRepoConfig(org.commonjava.indy.implrepo.conf.ImpliedRepoConfig) Collections(java.util.Collections) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreKey(org.commonjava.indy.model.core.StoreKey) Group(org.commonjava.indy.model.core.Group) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore)

Example 37 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class FoloPomDownloadListener method onFileUpload.

public void onFileUpload(@Observes final FileStorageEvent event) {
    // check for a TransferOperation of DOWNLOAD
    final TransferOperation op = event.getType();
    if (op != TransferOperation.DOWNLOAD) {
        logger.trace("Not a download transfer operation. No pom existence check performed.");
        return;
    }
    // check if it is a path that doesn't end in with ".pom"
    final Transfer transfer = event.getTransfer();
    if (transfer == null) {
        logger.trace("No transfer. No pom existence check performed.");
        return;
    }
    String txfrPath = transfer.getPath();
    if (txfrPath.endsWith(".pom")) {
        logger.trace("This is a pom download.");
        return;
    }
    // use ArtifactPathInfo to parse into a GAV, just to verify that it's looking at an artifact download
    ArtifactPathInfo artPathInfo = ArtifactPathInfo.parse(txfrPath);
    if (artPathInfo == null) {
        logger.trace("Not an artifact download ({}). No pom existence check performed.", txfrPath);
        return;
    }
    // verify that the associated .pom file exists
    String pomFilename = String.format("%s-%s.pom", artPathInfo.getArtifactId(), artPathInfo.getVersion());
    ConcreteResource pomResource = transfer.getResource().getParent().getChild(pomFilename);
    if (cacheProvider.exists(pomResource)) {
        logger.trace("Pom {} already exists.", cacheProvider.getFilePath(pomResource));
        return;
    }
    // trigger pom download by requesting it from the same repository as the original artifact
    StoreKey storeKey = StoreKey.fromString(transfer.getLocation().getName());
    ArtifactStore store;
    try {
        store = storeManager.getArtifactStore(storeKey);
    } catch (final IndyDataException ex) {
        logger.error("Error retrieving artifactStore with key " + storeKey, ex);
        return;
    }
    try {
        logger.debug("Downloading POM as automatic response to associated artifact download: {}/{}", storeKey, pomResource.getPath());
        contentManager.retrieve(store, pomResource.getPath(), event.getEventMetadata());
    } catch (final IndyWorkflowException ex) {
        logger.error("Error while retrieving pom artifact " + pomResource.getPath() + " from store " + store, ex);
        return;
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) ArtifactPathInfo(org.commonjava.maven.atlas.ident.util.ArtifactPathInfo) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 38 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class PromotionManager method promotePaths.

/**
     * Promote artifacts from the source to the target given in the {@link PathsPromoteRequest}. If a set of paths are given, only try to promotePaths those.
     * Otherwise, build a recursive list of available artifacts in the source store, and try to promotePaths them all.
     *
     * @param request The request containing source and target store keys, and an optional list of paths to promotePaths
     *
     * @return The result, including the source and target store keys used, the paths completed (promoted successfully), the pending paths (those that
     * weren't processed due to some error...or null), and a nullable error explaining what (if anything) went wrong with the promotion.
     *
     * @throws PromotionException
     * @throws IndyWorkflowException
     */
public PathsPromoteResult promotePaths(final PathsPromoteRequest request, final String baseUrl) throws PromotionException, IndyWorkflowException {
    final Set<String> paths = request.getPaths();
    final StoreKey source = request.getSource();
    List<Transfer> contents;
    if (paths == null || paths.isEmpty()) {
        contents = downloadManager.listRecursively(source, DownloadManager.ROOT_PATH);
    } else {
        contents = getTransfersForPaths(source, paths);
    }
    final Set<String> pending = new HashSet<>();
    for (final Transfer transfer : contents) {
        pending.add(transfer.getPath());
    }
    ValidationResult validation = new ValidationResult();
    validator.validate(request, validation, baseUrl);
    if (request.isDryRun()) {
        return new PathsPromoteResult(request, pending, Collections.emptySet(), Collections.emptySet(), validation);
    } else if (validation.isValid()) {
        return runPathPromotions(request, pending, Collections.emptySet(), Collections.emptySet(), contents, validation);
    } else {
        return new PathsPromoteResult(request, pending, Collections.emptySet(), Collections.emptySet(), validation);
    }
}
Also used : PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) StoreKey(org.commonjava.indy.model.core.StoreKey) HashSet(java.util.HashSet)

Example 39 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class PromotionManager method runPathPromotions.

private PathsPromoteResult runPathPromotions(final PathsPromoteRequest request, final Set<String> pending, final Set<String> prevComplete, final Set<String> prevSkipped, final List<Transfer> contents, ValidationResult validation) {
    if (pending == null || pending.isEmpty()) {
        return new PathsPromoteResult(request, pending, prevComplete, prevSkipped, validation);
    }
    StoreKey targetKey = request.getTarget();
    ReentrantLock lock;
    synchronized (byPathTargetLocks) {
        lock = byPathTargetLocks.get(targetKey);
        if (lock == null) {
            lock = new ReentrantLock();
            byPathTargetLocks.put(targetKey, lock);
        }
    }
    final Set<String> complete = prevComplete == null ? new HashSet<>() : new HashSet<>(prevComplete);
    final Set<String> skipped = prevSkipped == null ? new HashSet<>() : new HashSet<>(prevSkipped);
    List<String> errors = new ArrayList<>();
    boolean locked = false;
    try {
        ArtifactStore sourceStore = storeManager.getArtifactStore(request.getSource());
        ArtifactStore targetStore = storeManager.getArtifactStore(request.getTarget());
        if (errors.isEmpty()) {
            locked = lock.tryLock(config.getLockTimeoutSeconds(), TimeUnit.SECONDS);
            if (!locked) {
                String error = String.format("Failed to acquire promotion lock on target: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
                errors.add(error);
                logger.warn(error);
            }
        }
        if (errors.isEmpty()) {
            logger.info("Running promotions from: {} (key: {})\n  to: {} (key: {})", sourceStore, request.getSource(), targetStore, request.getTarget());
            final boolean purgeSource = request.isPurgeSource();
            contents.forEach((transfer) -> {
                try {
                    final String path = transfer.getPath();
                    Transfer target = contentManager.getTransfer(targetStore, path, TransferOperation.UPLOAD);
                    // TODO: Should the request object have an overwrite attribute? Is that something the user is qualified to decide?
                    if (target != null && target.exists()) {
                        logger.warn("NOT promoting: {} from: {} to: {}. Target file already exists.", path, request.getSource(), request.getTarget());
                        // TODO: There's no guarantee that the pre-existing content is the same!
                        pending.remove(path);
                        skipped.add(path);
                    } else {
                        try (InputStream stream = transfer.openInputStream(true)) {
                            contentManager.store(targetStore, path, stream, TransferOperation.UPLOAD, new EventMetadata());
                            pending.remove(path);
                            complete.add(path);
                            stream.close();
                            if (purgeSource) {
                                contentManager.delete(sourceStore, path, new EventMetadata());
                            }
                        } catch (final IOException e) {
                            String msg = String.format("Failed to open input stream for: %s. Reason: %s", transfer, e.getMessage());
                            errors.add(msg);
                            logger.error(msg, e);
                        }
                    }
                } catch (final IndyWorkflowException e) {
                    String msg = String.format("Failed to promote path: %s to: %s. Reason: %s", transfer, targetStore, e.getMessage());
                    errors.add(msg);
                    logger.error(msg, e);
                }
            });
        }
    } catch (InterruptedException e) {
        String error = String.format("Interrupted waiting for promotion lock on target: %s", targetKey);
        errors.add(error);
        logger.warn(error);
    } catch (final IndyDataException e) {
        String msg = String.format("Failed to retrieve artifact store: %s. Reason: %s", request.getSource(), e.getMessage());
        errors.add(msg);
        logger.error(msg, e);
    } finally {
        if (locked) {
            lock.unlock();
        }
    }
    String error = null;
    if (!errors.isEmpty()) {
        error = StringUtils.join(errors, "\n");
    }
    return new PathsPromoteResult(request, pending, complete, skipped, error, validation);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StoreKey(org.commonjava.indy.model.core.StoreKey) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer)

Example 40 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class GroupPromoteRequestTest method roundTripJSON_Basic.

@Test
public void roundTripJSON_Basic() throws Exception {
    final IndyObjectMapper mapper = new IndyObjectMapper(true);
    final GroupPromoteRequest req = new GroupPromoteRequest(new StoreKey(StoreType.hosted, "source"), "target");
    final String json = mapper.writeValueAsString(req);
    System.out.println(json);
    final GroupPromoteRequest result = mapper.readValue(json, GroupPromoteRequest.class);
    assertThat(result.getSource(), equalTo(req.getSource()));
    assertThat(result.getTargetGroup(), equalTo(req.getTargetGroup()));
}
Also used : IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Aggregations

StoreKey (org.commonjava.indy.model.core.StoreKey)186 Test (org.junit.Test)92 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)40 StoreType (org.commonjava.indy.model.core.StoreType)39 InputStream (java.io.InputStream)33 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)32 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)31 IndyDataException (org.commonjava.indy.data.IndyDataException)30 Group (org.commonjava.indy.model.core.Group)29 Transfer (org.commonjava.maven.galley.model.Transfer)27 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)24 Response (javax.ws.rs.core.Response)23 IOException (java.io.IOException)22 Logger (org.slf4j.Logger)21 ApiOperation (io.swagger.annotations.ApiOperation)20 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)20 ArrayList (java.util.ArrayList)19 Path (javax.ws.rs.Path)19 ApiResponse (io.swagger.annotations.ApiResponse)18 IndyObjectMapper (org.commonjava.indy.model.core.io.IndyObjectMapper)18