use of org.commonjava.indy.promote.validate.model.ValidationRuleMapping in project indy by Commonjava.
the class PromoteValidationsManager method storeRule.
public synchronized ValidationRuleMapping storeRule(final String name, final String spec, final ChangeSummary changelog) throws PromotionValidationException {
final ValidationRuleMapping mapping = ruleParser.parseRule(spec, name);
ruleMappings.put(mapping.getName(), mapping);
final DataFile dataDir = ffManager.getDataFile(config.getBasedir(), RULES_DIR);
if (!dataDir.exists()) {
dataDir.mkdirs();
}
final DataFile scriptFile = dataDir.getChild(name);
try {
scriptFile.writeString(spec, changelog);
} catch (final IOException e) {
throw new PromotionValidationException("Failed to write rule: %s to: %s. Reason: %s", e, name, scriptFile, e.getMessage());
}
return mapping;
}
use of org.commonjava.indy.promote.validate.model.ValidationRuleMapping in project indy by Commonjava.
the class PromoteValidationsManager method removeRuleNamed.
public synchronized ValidationRuleMapping removeRuleNamed(final String name, final ChangeSummary changelog) throws PromotionValidationException {
ValidationRuleMapping mapping = ruleMappings.remove(name);
if (mapping == null) {
return null;
}
final DataFile dataDir = ffManager.getDataFile(config.getBasedir(), RULES_DIR);
if (!dataDir.exists()) {
// this would be a very strange error...implying addition of a rule without writing it to disk.
return null;
}
final DataFile scriptFile = dataDir.getChild(name);
if (scriptFile.exists()) {
try {
scriptFile.delete(changelog);
return mapping;
} catch (final IOException e) {
throw new PromotionValidationException("Failed to delete rule: %s to: %s. Reason: %s", e, name, scriptFile, e.getMessage());
}
}
return null;
}
use of org.commonjava.indy.promote.validate.model.ValidationRuleMapping in project indy by Commonjava.
the class PromotionValidator method validate.
public void validate(PromoteRequest request, ValidationResult result, String baseUrl) throws PromotionValidationException {
ValidationRuleSet set = validationsManager.getRuleSetMatching(request.getTargetKey());
Logger logger = LoggerFactory.getLogger(getClass());
if (set != null) {
logger.debug("Running validation rule-set for promotion: {}", set.getName());
result.setRuleSet(set.getName());
List<String> ruleNames = set.getRuleNames();
if (ruleNames != null && !ruleNames.isEmpty()) {
final ArtifactStore store = getRequestStore(request, baseUrl);
try {
final ValidationRequest req = new ValidationRequest(request, set, validationTools, store);
for (String ruleRef : ruleNames) {
String ruleName = // flatten in case some path fragment leaks in...
new File(ruleRef).getName();
ValidationRuleMapping rule = validationsManager.getRuleMappingNamed(ruleName);
if (rule != null) {
try {
logger.debug("Running promotion validation rule: {}", rule.getName());
String error = rule.getRule().validate(req);
if (StringUtils.isNotEmpty(error)) {
logger.debug("{} failed", rule.getName());
result.addValidatorError(rule.getName(), error);
} else {
logger.debug("{} succeeded", rule.getName());
}
} catch (Exception e) {
if (e instanceof PromotionValidationException) {
throw (PromotionValidationException) e;
}
throw new PromotionValidationException("Failed to run validation rule: {} for request: {}. Reason: {}", e, rule.getName(), request, e);
}
}
}
} finally {
if (needTempRepo(request)) {
try {
final String changeSum = String.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));
logger.info("Promotion temporary repo {} has been deleted for {}", store.getKey(), request.getSource());
} catch (IndyDataException e) {
logger.warn("StoreDataManager can not remove artifact stores correctly.", e);
}
}
}
}
} else {
logger.info("No validation rule-sets are defined for: {}", request.getTargetKey());
}
}
use of org.commonjava.indy.promote.validate.model.ValidationRuleMapping in project indy by Commonjava.
the class ValidationRuleParser method parseRule.
public ValidationRuleMapping parseRule(final String spec, final String scriptName) throws PromotionValidationException {
if (spec == null) {
return null;
}
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Parsing rule from: {} with content:\n{}\n", scriptName, spec);
ValidationRule rule = null;
try {
rule = scriptEngine.parseScriptInstance(spec, ValidationRule.class);
logger.debug("Parsed: {}", rule.getClass().getName());
} catch (final IndyGroovyException e) {
throw new PromotionValidationException("[PROMOTE] Cannot load validation rule from: {} as an instance of: {}. Reason: {}", e, scriptName, ValidationRule.class.getSimpleName(), e.getMessage());
}
if (rule != null) {
return new ValidationRuleMapping(scriptName, spec, rule);
}
return null;
}
use of org.commonjava.indy.promote.validate.model.ValidationRuleMapping in project indy by Commonjava.
the class PromoteValidationsManager method parseRules.
public synchronized void parseRules() throws PromotionValidationException {
if (!config.isEnabled()) {
this.enabled = false;
this.ruleMappings = Collections.emptyMap();
logger.info("Promotion is disabled.");
return;
}
final Map<String, ValidationRuleMapping> ruleMappings = new HashMap<>();
DataFile dataDir = ffManager.getDataFile(config.getBasedir(), RULES_DIR);
logger.info("Scanning {} for promotion validation rules...", dataDir);
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.info("Checking for promote validation rule script in: {}", pathname);
return pathname.getName().endsWith(".groovy");
});
if (scripts != null && scripts.length > 0) {
for (final DataFile script : scripts) {
logger.info("Reading promotion validation rule from: {}", script);
final ValidationRuleMapping rule = ruleParser.parseRule(script);
if (rule != null) {
ruleMappings.put(rule.getName(), rule);
}
}
} else {
logger.warn("No rule script file was defined for promotion: no rule script found in {} directory", RULES_DIR);
}
} else {
logger.warn("No rule script file was defined for promotion: {} directory not exists", RULES_DIR);
}
this.ruleMappings = ruleMappings;
Map<String, ValidationRuleSet> ruleSets = new HashMap<>();
dataDir = ffManager.getDataFile(config.getBasedir(), RULES_SETS_DIR);
logger.info("Scanning {} for promotion validation rule-set mappings...", dataDir);
if (dataDir.exists()) {
final DataFile[] scripts = dataDir.listFiles((pathname) -> {
logger.info("Checking for promotion rule-set in: {}", pathname);
return pathname.getName().endsWith(".json");
});
if (scripts != null && scripts.length > 0) {
for (final DataFile script : scripts) {
logger.info("Reading promotion validation rule-set from: {}", script);
final ValidationRuleSet set = ruleParser.parseRuleSet(script);
if (set != null) {
ruleSets.put(script.getName(), set);
}
}
} else {
logger.warn("No rule-set json file was defined for promotion: no json file found in {} directory", RULES_SETS_DIR);
}
} else {
logger.warn("No rule-set json file was defined for promotion: {} directory not exists", RULES_SETS_DIR);
}
this.ruleSets = ruleSets;
this.enabled = true;
}
Aggregations