Search in sources :

Example 46 with Transfer

use of org.commonjava.maven.galley.model.Transfer 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 47 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class PromotionManagerTest method promoteAllByPath_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo.

@Test
public void promoteAllByPath_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo() throws Exception {
    final HostedRepository source = new HostedRepository(MAVEN_PKG_KEY, "source");
    storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final String first = "/first/path";
    final String second = "/second/path";
    contentManager.store(source, first, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    contentManager.store(source, second, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()), FAKE_BASE_URL);
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    final Set<String> pending = result.getPendingPaths();
    assertThat(pending == null || pending.isEmpty(), equalTo(true));
    final Set<String> completed = result.getCompletedPaths();
    assertThat(completed, notNullValue());
    assertThat(completed.size(), equalTo(2));
    assertThat(result.getError(), nullValue());
    Transfer ref = downloadManager.getStorageReference(target, first);
    assertThat(ref.exists(), equalTo(true));
    ref = downloadManager.getStorageReference(target, second);
    assertThat(ref.exists(), equalTo(true));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 48 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class KojiContentManagerDecorator method getTransfer.

@Override
public Transfer getTransfer(final ArtifactStore store, final String path, final TransferOperation operation) throws IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.info("KOJI: Delegating initial getTransfer() attempt for: {}/{}", store.getKey(), path);
    Transfer result = delegate.getTransfer(store, path, operation);
    if (result == null && TransferOperation.DOWNLOAD == operation && StoreType.group == store.getKey().getType()) {
        logger.info("KOJI: Checking for Koji build matching: {}", path);
        Group group = (Group) store;
        RemoteRepository kojiProxy = findKojiBuildAnd(store, path, new EventMetadata(), null, this::createRemoteRepository);
        if (kojiProxy != null) {
            adjustTargetGroup(kojiProxy, group);
            EventMetadata eventMetadata = new EventMetadata().set(ContentManager.ENTRY_POINT_STORE, store.getKey());
            result = delegate.retrieve(kojiProxy, path, eventMetadata);
        }
        if (result != null && result.exists()) {
            nfc.clearMissing(new ConcreteResource(LocationUtils.toLocation(store), path));
        }
    }
    // Finally, pass the Transfer back.
    return result;
}
Also used : Group(org.commonjava.indy.model.core.Group) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) Logger(org.slf4j.Logger) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 49 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class RelatePomStorageListener method onPomStorage.

public void onPomStorage(@Observes final FileStorageEvent event) {
    final Logger logger = LoggerFactory.getLogger(getClass());
    if (!config.isEnabled()) {
        logger.debug("Relate Add-on is not enabled.");
        return;
    }
    logger.debug("FILE STORAGE: {}", event);
    final TransferOperation op = event.getType();
    if (op != TransferOperation.UPLOAD && op != TransferOperation.DOWNLOAD) {
        logger.debug("Not a download/upload transfer operation. No .rel generation.");
        return;
    }
    ThreadContext threadContext = ThreadContext.getContext(false);
    if (threadContext != null) {
        Object obj = threadContext.get(REL_DIRECT_GENERATING);
        if (obj != null) {
            logger.debug("Direct .rel generation in progress. Ignore POM storage event. ");
            return;
        }
    }
    final Transfer transfer = event.getTransfer();
    executor.execute(() -> {
        relateGenerationManager.generateRelationshipFile(transfer, op);
    });
}
Also used : ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) Transfer(org.commonjava.maven.galley.model.Transfer) Logger(org.slf4j.Logger) TransferOperation(org.commonjava.maven.galley.model.TransferOperation)

Example 50 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class ArtifactStoreSubStore method createFolder.

@Override
public void createFolder(final ITransaction transaction, final String folderUri) throws WebdavException {
    final StoreURIMatcher matcher = new StoreURIMatcher(folderUri);
    if (!matcher.hasStorePath()) {
        throw new WebdavException("No store-level path specified: '" + folderUri + "'. This URI references either a list of stores, a root store directory, or something else equally read-only.");
    }
    final StorageAdvice advice = getStorageAdviceFor(matcher);
    final String path = matcher.getStorePath();
    final Transfer item = fileManager.getStorageReference(advice.getHostedStore(), path);
    try {
        item.mkdirs();
    } catch (final IOException e) {
        logger.error("Failed to create folder: {} in store: {}. Reason: {}", e, path, advice.getStore().getKey(), e.getMessage());
        throw new WebdavException("Failed to create folder: " + folderUri);
    }
}
Also used : StoreURIMatcher(org.commonjava.indy.dotmaven.util.StoreURIMatcher) StorageAdvice(org.commonjava.indy.dotmaven.data.StorageAdvice) WebdavException(net.sf.webdav.exceptions.WebdavException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException)

Aggregations

Transfer (org.commonjava.maven.galley.model.Transfer)154 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)63 Test (org.junit.Test)62 IOException (java.io.IOException)39 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)38 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)37 InputStream (java.io.InputStream)27 StoreKey (org.commonjava.indy.model.core.StoreKey)27 Logger (org.slf4j.Logger)26 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)22 ByteArrayInputStream (java.io.ByteArrayInputStream)21 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)21 HostedRepository (org.commonjava.indy.model.core.HostedRepository)21 Group (org.commonjava.indy.model.core.Group)20 ArrayList (java.util.ArrayList)19 IndyDataException (org.commonjava.indy.data.IndyDataException)19 OutputStream (java.io.OutputStream)15 HashSet (java.util.HashSet)15 Location (org.commonjava.maven.galley.model.Location)14 TransferException (org.commonjava.maven.galley.TransferException)12