Search in sources :

Example 11 with ValidationResult

use of org.commonjava.indy.promote.model.ValidationResult in project indy by Commonjava.

the class PromotionManager method rollbackPathsPromote.

/**
     * Attempt to rollbackPathsPromote a previously failing {@link PathsPromoteResult}. This is meant to handle cases where an unrecoverable error
     * occurs on the server side, and promotion can NOT proceed afterward. All paths in the completed paths set are deleted from the target, if
     * possible. The output {@link PathsPromoteResult} contains the previous content, with any successfully removed target paths moved back from the
     * completed-paths list to the pending-paths list. If an error occurs during rollbackPathsPromote, the error field will be set...otherwise, it will be null.
     *
     * @param result The result to rollbackPathsPromote
     *
     * @return The same result, with any successful deletions moved from the completed to pending paths list, and the error cleared (or set to a
     * new error)
     *
     * @throws PromotionException
     * @throws IndyWorkflowException
     */
public PathsPromoteResult rollbackPathsPromote(final PathsPromoteResult result) throws PromotionException, IndyWorkflowException {
    StoreKey targetKey = result.getRequest().getTarget();
    ReentrantLock lock;
    synchronized (byPathTargetLocks) {
        lock = byPathTargetLocks.get(targetKey);
        if (lock == null) {
            lock = new ReentrantLock();
            byPathTargetLocks.put(targetKey, lock);
        }
    }
    final List<Transfer> contents = getTransfersForPaths(targetKey, result.getCompletedPaths());
    final Set<String> completed = result.getCompletedPaths();
    final Set<String> skipped = result.getSkippedPaths();
    if (completed == null || completed.isEmpty()) {
        result.setError(null);
        return result;
    }
    Set<String> pending = result.getPendingPaths();
    pending = pending == null ? new HashSet<String>() : new HashSet<String>(pending);
    String error = null;
    final boolean copyToSource = result.getRequest().isPurgeSource();
    ArtifactStore source = null;
    try {
        source = storeManager.getArtifactStore(result.getRequest().getSource());
    } catch (final IndyDataException e) {
        error = String.format("Failed to retrieve artifact store: %s. Reason: %s", result.getRequest().getSource(), e.getMessage());
        logger.error(error, e);
    }
    boolean locked = false;
    try {
        if (error == null) {
            locked = lock.tryLock(config.getLockTimeoutSeconds(), TimeUnit.SECONDS);
            if (!locked) {
                error = String.format("Failed to acquire promotion lock on target: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
                logger.warn(error);
            }
        }
        if (error == null) {
            for (final Transfer transfer : contents) {
                if (transfer != null && transfer.exists()) {
                    InputStream stream = null;
                    try {
                        if (copyToSource) {
                            stream = transfer.openInputStream(true);
                            final String path = transfer.getPath();
                            contentManager.store(source, path, stream, TransferOperation.UPLOAD, new EventMetadata());
                            stream.close();
                        }
                        transfer.delete(true);
                        completed.remove(transfer.getPath());
                        pending.add(transfer.getPath());
                    } catch (final IOException e) {
                        error = String.format("Failed to rollback path promotion of: %s from: %s. Reason: %s", transfer, result.getRequest().getSource(), e.getMessage());
                        logger.error(error, e);
                    } finally {
                        closeQuietly(stream);
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        error = String.format("Interrupted waiting for promotion lock on target: %s", targetKey);
        logger.warn(error);
    } finally {
        if (locked) {
            lock.unlock();
        }
    }
    return new PathsPromoteResult(result.getRequest(), pending, completed, skipped, error, new ValidationResult());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) InputStream(java.io.InputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) IOException(java.io.IOException) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) 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) Transfer(org.commonjava.maven.galley.model.Transfer) HashSet(java.util.HashSet)

Example 12 with ValidationResult

use of org.commonjava.indy.promote.model.ValidationResult in project indy by Commonjava.

the class PromotionManager method promoteToGroup.

public GroupPromoteResult promoteToGroup(GroupPromoteRequest request, String user, String baseUrl) throws PromotionException {
    if (!storeManager.hasArtifactStore(request.getSource())) {
        String error = String.format("Cannot promote from missing source: %s", request.getSource());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    synchronized (getTargetKey(request.getTargetGroup())) {
        Group target;
        try {
            target = (Group) storeManager.getArtifactStore(request.getTargetKey());
        } catch (IndyDataException e) {
            throw new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTargetGroup(), e.getMessage());
        }
        if (target == null) {
            String error = String.format("No such target group: %s.", request.getTargetGroup());
            logger.warn(error);
            return new GroupPromoteResult(request, error);
        }
        ValidationResult validation = new ValidationResult();
        logger.info("Running validations for promotion of: {} to group: {}", request.getSource(), request.getTargetGroup());
        validator.validate(request, validation, baseUrl);
        if (validation.isValid()) {
            if (!request.isDryRun() && !target.getConstituents().contains(request.getSource())) {
                // give the preUpdate event a different object to compare vs. the original group.
                target = target.copyOf();
                target.addConstituent(request.getSource());
                try {
                    final ChangeSummary changeSummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
                    storeManager.storeArtifactStore(target, changeSummary, false, true, new EventMetadata());
                    if (hosted == request.getSource().getType() && config.isAutoLockHostedRepos()) {
                        HostedRepository source = (HostedRepository) storeManager.getArtifactStore(request.getSource());
                        source.setReadonly(true);
                        final ChangeSummary readOnlySummary = new ChangeSummary(user, "Promoting " + request.getSource() + " into membership of group: " + target.getKey());
                        storeManager.storeArtifactStore(source, readOnlySummary, false, true, new EventMetadata());
                    }
                } catch (IndyDataException e) {
                    throw new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage());
                }
            }
        }
        return new GroupPromoteResult(request, validation);
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HostedRepository(org.commonjava.indy.model.core.HostedRepository)

Example 13 with ValidationResult

use of org.commonjava.indy.promote.model.ValidationResult in project indy by Commonjava.

the class GroupPromoteFailsValidationTest method run.

@Test
public void run() throws Exception {
    final GroupPromoteResult result = client.module(IndyPromoteClientModule.class).promoteToGroup(new GroupPromoteRequest(source.getKey(), target.getName()));
    assertFalse(result.succeeded());
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTargetGroup(), equalTo(target.getName()));
    assertThat(result.getError(), nullValue());
    ValidationResult validations = result.getValidations();
    assertThat(validations, notNullValue());
    Map<String, String> errors = validations.getValidatorErrors();
    assertThat(errors, notNullValue());
    String error = errors.get("fail-all.groovy");
    assertThat(error, notNullValue());
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(false));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(false));
    Group g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(false));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Example 14 with ValidationResult

use of org.commonjava.indy.promote.model.ValidationResult in project indy by Commonjava.

the class GroupPromoteMatchesSucceedingValidationTest method run.

@Test
public void run() throws Exception {
    final GroupPromoteResult result = client.module(IndyPromoteClientModule.class).promoteToGroup(new GroupPromoteRequest(source.getKey(), target.getName()));
    assertThat(result.succeeded(), equalTo(true));
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTargetGroup(), equalTo(target.getName()));
    assertThat(result.getError(), nullValue());
    ValidationResult validations = result.getValidations();
    assertThat(validations, notNullValue());
    assertThat(validations.isValid(), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(true));
    Group g = client.stores().load(StoreType.group, target.getName(), Group.class);
    assertThat(g.getConstituents().contains(source.getKey()), equalTo(true));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Example 15 with ValidationResult

use of org.commonjava.indy.promote.model.ValidationResult in project indy by Commonjava.

the class ProjectArtifactsRuleTest method run.

@Test
@Category(EventDependent.class)
public void run() throws Exception {
    String invalidGav = "org.foo:invalid:1";
    String validGav = "org.foo:valid:1.1";
    String invalidPom = "org/foo/invalid/1/invalid-1.pom";
    String invalidJar = "org/foo/invalid/1/invalid-1.jar";
    String validPom = "org/foo/valid/1.1/valid-1.1.pom";
    String validJar = "org/foo/valid/1.1/valid-1.1.jar";
    String validSources = "org/foo/valid/1.1/valid-1.1-sources.jar";
    String validJavadocs = "org/foo/valid/1.1/valid-1.1-javadoc.jar";
    deploy(invalidPom, CONTENT);
    deploy(invalidJar, CONTENT);
    deploy(validPom, CONTENT);
    deploy(validJar, CONTENT);
    deploy(validSources, CONTENT);
    deploy(validJavadocs, CONTENT);
    waitForEventPropagation();
    GroupPromoteRequest request = new GroupPromoteRequest(source.getKey(), target.getName());
    GroupPromoteResult result = module.promoteToGroup(request);
    assertThat(result, notNullValue());
    ValidationResult validations = result.getValidations();
    assertThat(validations, notNullValue());
    Map<String, String> validatorErrors = validations.getValidatorErrors();
    assertThat(validatorErrors, notNullValue());
    System.out.println(validatorErrors);
    String errors = validatorErrors.get(RULE);
    assertThat(errors, notNullValue());
    System.out.println(validatorErrors);
    assertThat(errors.contains(validGav), equalTo(false));
    assertThat(errors.contains(invalidGav), equalTo(true));
}
Also used : GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

ValidationResult (org.commonjava.indy.promote.model.ValidationResult)20 Test (org.junit.Test)17 GroupPromoteResult (org.commonjava.indy.promote.model.GroupPromoteResult)16 GroupPromoteRequest (org.commonjava.indy.promote.model.GroupPromoteRequest)15 Category (org.junit.experimental.categories.Category)14 InputStream (java.io.InputStream)7 PathsPromoteResult (org.commonjava.indy.promote.model.PathsPromoteResult)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Group (org.commonjava.indy.model.core.Group)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 IndyDataException (org.commonjava.indy.data.IndyDataException)2 StoreKey (org.commonjava.indy.model.core.StoreKey)2 IndyPromoteClientModule (org.commonjava.indy.promote.client.IndyPromoteClientModule)2 PathsPromoteRequest (org.commonjava.indy.promote.model.PathsPromoteRequest)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)1 IndyClientException (org.commonjava.indy.client.core.IndyClientException)1