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]);
}
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);
}
}
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;
}
Aggregations