Search in sources :

Example 1 with ValidationRequest

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

the class PromotionValidationTools method getValidationStoreKeys.

public StoreKey[] getValidationStoreKeys(final ValidationRequest request, final boolean includeSource) throws PromotionValidationException {
    String verifyStores = request.getValidationParameter(PromotionValidationTools.AVAILABLE_IN_STORES);
    if (verifyStores == null) {
        verifyStores = request.getValidationParameter(PromotionValidationTools.AVAILABLE_IN_STORE_KEY);
    }
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Got extra validation keys string: '{}'", verifyStores);
    Set<StoreKey> verifyStoreKeys = new HashSet<>();
    if (includeSource) {
        verifyStoreKeys.add(request.getSourceRepository().getKey());
    }
    if (verifyStores == null) {
        logger.warn("No external store (availableInStoreKey parameter) specified for validating path availability in rule-set: {}. Using target: {} instead.", request.getRuleSet().getName(), request.getTarget());
    } else {
        List<StoreKey> extras = Stream.of(verifyStores.split("\\s*,\\s*")).map(StoreKey::fromString).filter(item -> item != null).collect(Collectors.toList());
        if (extras.isEmpty()) {
            throw new PromotionValidationException("No valid StoreKey instances could be parsed from '%s'", verifyStores);
        } else {
            verifyStoreKeys.addAll(extras);
        }
    }
    if (verifyStoreKeys.isEmpty()) {
        verifyStoreKeys.add(request.getTarget());
    }
    logger.debug("Using validation StoreKeys: {}", verifyStoreKeys);
    return verifyStoreKeys.toArray(new StoreKey[0]);
}
Also used : Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Closure(groovy.lang.Closure) ReadOnlyTransfer.readOnlyWrappers(org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrappers) WeftExecutorService(org.commonjava.cdi.util.weft.WeftExecutorService) Batcher.getParalleledBatchSize(org.commonjava.indy.promote.util.Batcher.getParalleledBatchSize) ProjectRelationship(org.commonjava.atlas.maven.graph.rel.ProjectRelationship) MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) TransferManager(org.commonjava.maven.galley.TransferManager) MavenPomReader(org.commonjava.maven.galley.maven.parse.MavenPomReader) Map(java.util.Map) ContentDigest(org.commonjava.maven.galley.io.checksum.ContentDigest) URI(java.net.URI) StoreKey(org.commonjava.indy.model.core.StoreKey) ArtifactStoreQuery(org.commonjava.indy.data.ArtifactStoreQuery) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) IndyObjectMapper(org.commonjava.indy.model.core.io.IndyObjectMapper) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ContentManager(org.commonjava.indy.content.ContentManager) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) ArtifactRef(org.commonjava.atlas.maven.ident.ref.ArtifactRef) Optional(java.util.Optional) PackagePath(org.commonjava.indy.pkg.npm.content.PackagePath) IndyDataException(org.commonjava.indy.data.IndyDataException) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) ContentDigester(org.commonjava.indy.content.ContentDigester) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) PoolWeftExecutorService(org.commonjava.cdi.util.weft.PoolWeftExecutorService) Batcher.batch(org.commonjava.indy.promote.util.Batcher.batch) ReadOnlyTransfer.readOnlyWrapper(org.commonjava.indy.promote.validate.util.ReadOnlyTransfer.readOnlyWrapper) MavenModelProcessor(org.commonjava.maven.galley.maven.rel.MavenModelProcessor) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Transfer(org.commonjava.maven.galley.model.Transfer) FORCE_CHECKSUM(org.commonjava.maven.galley.io.ChecksummingTransferDecorator.FORCE_CHECKSUM) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ModelProcessorConfig(org.commonjava.maven.galley.maven.rel.ModelProcessorConfig) Location(org.commonjava.maven.galley.model.Location) ArtifactPathUtils(org.commonjava.maven.galley.maven.util.ArtifactPathUtils) LocationUtils(org.commonjava.indy.util.LocationUtils) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) PackageMetadata(org.commonjava.indy.pkg.npm.model.PackageMetadata) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) PromoteConfig(org.commonjava.indy.promote.conf.PromoteConfig) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) MavenMetadataReader(org.commonjava.maven.galley.maven.parse.MavenMetadataReader) TypeMapper(org.commonjava.maven.galley.maven.spi.type.TypeMapper) MDC(org.slf4j.MDC) StoreResource(org.commonjava.indy.content.StoreResource) RequestContextHelper(org.commonjava.indy.util.RequestContextHelper) StoreDataManager(org.commonjava.indy.data.StoreDataManager) TransferException(org.commonjava.maven.galley.TransferException) HttpExchangeMetadata(org.commonjava.maven.galley.transport.htcli.model.HttpExchangeMetadata) InputStream(java.io.InputStream) Logger(org.slf4j.Logger) StoreKey(org.commonjava.indy.model.core.StoreKey) HashSet(java.util.HashSet)

Example 2 with ValidationRequest

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

the class PromotionValidator method validate.

/**
 * NOTE: As of Indy 1.2.6, ValidationRequest passed back to enable further post-processing, especially of promotion
 * paths, after promotion takes place. This enables us to avoid re-executing recursive path discovery, for instance.
 *
 * @param request
 * @param result
 * @param baseUrl
 * @return
 * @throws PromotionValidationException
 * @throws IndyWorkflowException
 */
@Measure
public ValidationRequest validate(PromoteRequest request, ValidationResult result, String baseUrl) throws PromotionValidationException, IndyWorkflowException {
    ValidationRuleSet set = validationsManager.getRuleSetMatching(request.getTargetKey());
    ArtifactStore source;
    try {
        source = storeDataMgr.getArtifactStore(request.getSource());
    } catch (IndyDataException e) {
        throw new PromotionValidationException(String.format("Failed to retrieve source ArtifactStore: %s for validation", request.getSource()), e);
    }
    if (set != null) {
        result.setRuleSet(set.getName());
        RequestContextHelper.setContext(PROMOTION_VALIDATION_RULE_SET, set.getName());
        logger.debug("Running validation rule-set for promotion: {}", set.getName());
        List<String> ruleNames = set.getRuleNames();
        if (ruleNames != null && !ruleNames.isEmpty()) {
            final ArtifactStore store = getRequestStore(request, baseUrl);
            final ValidationRequest req = new ValidationRequest(request, set, validationTools, store);
            try {
                DrainingExecutorCompletionService<Exception> svc = new DrainingExecutorCompletionService<>(validateService);
                detectOverloadVoid(() -> {
                    for (String ruleRef : ruleNames) {
                        svc.submit(() -> {
                            RequestContextHelper.setContext(PROMOTION_VALIDATION_RULE, ruleRef);
                            Exception err = null;
                            try {
                                executeValidationRule(ruleRef, req, result, request);
                            } catch (Exception e) {
                                err = e;
                            } finally {
                                RequestContextHelper.clearContext(PROMOTION_VALIDATION_RULE);
                            }
                            return err;
                        });
                    }
                });
                List<String> errors = new ArrayList<>();
                svc.drain(err -> {
                    if (err != null) {
                        logger.error("Promotion validation failure", err);
                        errors.add(err.getMessage());
                    }
                });
                if (!errors.isEmpty()) {
                    throw new PromotionValidationException(format("Failed to do promotion validation: \n\n%s", join(errors, "\n")));
                }
            } catch (InterruptedException e) {
                throw new PromotionValidationException("Failed to do promotion validation: validation execution has been interrupted ", e);
            } catch (ExecutionException e) {
                throw new PromotionValidationException("Failed to execute promotion validations", e);
            } finally {
                if (needTempRepo(request)) {
                    try {
                        final String changeSum = format("Removes the temp remote repo [%s] after promote operation.", store);
                        storeDataMgr.deleteArtifactStore(store.getKey(), new ChangeSummary(ChangeSummary.SYSTEM_USER, changeSum), new EventMetadata().set(ContentManager.SUPPRESS_EVENTS, true));
                        Transfer root = downloadManager.getStoreRootDirectory(store);
                        if (root.exists()) {
                            root.delete(false);
                        }
                        logger.debug("Promotion temporary repo {} has been deleted for {}", store.getKey(), request.getSource());
                    } catch (IndyDataException | IOException e) {
                        logger.warn("Temporary promotion validation repository was NOT removed correctly.", e);
                    }
                }
            }
            return req;
        } else {
            logger.info("No validation rules are defined for: {}", request.getTargetKey());
            return new ValidationRequest(request, set, validationTools, source);
        }
    } else {
        logger.info("No validation rule-sets are defined for: {}", request.getTargetKey());
        return new ValidationRequest(request, set, validationTools, source);
    }
}
Also used : ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IndyDataException(org.commonjava.indy.data.IndyDataException) DrainingExecutorCompletionService(org.commonjava.cdi.util.weft.DrainingExecutorCompletionService) 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) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet) ExecutionException(java.util.concurrent.ExecutionException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 3 with ValidationRequest

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

the class PromotionManager method doValidationAndPromote.

private ValidationResult doValidationAndPromote(GroupPromoteRequest request, AtomicReference<Exception> error, String user, String baseUrl) {
    ValidationResult validation = new ValidationResult();
    logger.info("Running validations for promotion of: {} to group: {}", request.getSource(), request.getTarget());
    final StoreKey targetKey = getTargetKey(request.getTarget().getName(), request.getTarget().getPackageType());
    byGroupTargetLocks.lockAnd(targetKey, config.getLockTimeoutSeconds(), k -> {
        Group target;
        try {
            target = (Group) storeManager.getArtifactStore(request.getTargetKey());
        } catch (IndyDataException e) {
            error.set(new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTarget(), e.getMessage()));
            return null;
        }
        try {
            ValidationRequest validationRequest = 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());
                        final Group targetForNfcCleaning = target;
                        final String name = String.format("PromoteNFCClean-method(%s)-source(%s)-target(%s)", "doValidationAndPromote", validationRequest.getSource(), targetForNfcCleaning.getKey());
                        final String context = String.format("Class: %s, method: %s, source: %s, target: %s", this.getClass().getName(), "doValidationAndPromote", validationRequest.getSource(), targetForNfcCleaning.getKey());
                        storeManager.asyncGroupAffectedBy(new StoreDataManager.ContextualTask(name, context, () -> {
                            try {
                                promotionHelper.clearStoreNFC(validationRequest.getSourcePaths(), targetForNfcCleaning, null);
                            } catch (PromotionValidationException e) {
                                logger.warn("Error happened for clear nfc during promote validation: {}", e.getMessage());
                            }
                        }));
                        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) {
                        error.set(new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage()));
                    }
                }
            }
        } catch (PromotionValidationException | IndyWorkflowException e) {
            error.set(e);
        }
        return null;
    }, (k, lock) -> {
        // FIXME: should we consider to repeat the promote process several times when lock failed?
        String errorMsg = String.format("Failed to acquire group promotion lock on target when promote: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
        logger.error(errorMsg);
        error.set(new PromotionException(errorMsg));
        return Boolean.FALSE;
    });
    return validation;
}
Also used : Group(org.commonjava.indy.model.core.Group) ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) 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) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) PromotionValidationException(org.commonjava.indy.promote.validate.PromotionValidationException) StoreDataManager(org.commonjava.indy.data.StoreDataManager) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository)

Aggregations

IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)3 IndyDataException (org.commonjava.indy.data.IndyDataException)3 ValidationRequest (org.commonjava.indy.promote.validate.model.ValidationRequest)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)2 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)2 Closure (groovy.lang.Closure)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1