Search in sources :

Example 16 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class AutoProxAproxMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    final DataFile dataDir = ffManager.getDataFile(config.getBasedir());
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Scanning {} for autoprox rules...", dataDir);
    int changed = 0;
    if (dataDir.exists()) {
        final DataFile[] scripts = dataDir.listFiles((pathname) -> {
            logger.trace("Checking for autoprox rule in: {}", pathname);
            return pathname.getName().endsWith(".groovy");
        });
        logger.info("Migrating autoprox rules.");
        for (final DataFile script : scripts) {
            try {
                String scriptContent = script.readString();
                String migrated = scriptContent.replaceAll("A[Pp]rox", "Indy").replaceAll("aprox", "indy");
                if (!migrated.equals(scriptContent)) {
                    logger.info("Migrating autoprox rule in: {}", script.getPath());
                    script.writeString(migrated, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating to Indy packages / naming"));
                    changed++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return changed != 0;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) IOException(java.io.IOException) Logger(org.slf4j.Logger) ChangeSummary(org.commonjava.indy.audit.ChangeSummary)

Example 17 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class TemplatingEngine method getTemplate.

// TODO Cache these...though this will hurt hot-reloading. Perhaps a debug mode configuration?
private Template getTemplate(final String acceptHeader, final String templateKey) throws IndyGroovyException {
    final String accept = (acceptHeader == null ? "" : acceptHeader.replace('/', '_') + "/");
    try {
        final String filename = accept + templateKey + ".groovy";
        final DataFile templateFile = manager.getDataFile(TEMPLATES, filename);
        logger.info("Looking for template: {} for ACCEPT header: {} in: {}", templateKey, acceptHeader, templateFile);
        Template template;
        if (templateFile.exists() && !templateFile.isDirectory()) {
            template = engine.createTemplate(templateFile.readString());
        } else {
            final String urlpath = TEMPLATES + "/" + accept + templateKey + ".groovy";
            logger.info("Looking for template: {} for ACCEPT header: {} in: {}", templateKey, acceptHeader, urlpath);
            final URL u = Thread.currentThread().getContextClassLoader().getResource(urlpath);
            template = u == null ? null : engine.createTemplate(u);
        }
        if (template == null) {
            throw new IndyGroovyException("Failed to locate template: %s (with ACCEPT header: %s)", templateKey, acceptHeader);
        }
        return template;
    } catch (final CompilationFailedException e) {
        throw new IndyGroovyException("Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
    } catch (final ClassNotFoundException e) {
        throw new IndyGroovyException("Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
    } catch (final IOException e) {
        throw new IndyGroovyException("Failed to read template: %s. Reason: %s", e, templateKey, e.getMessage());
    }
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) URL(java.net.URL) Template(groovy.text.Template)

Example 18 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class PromotionRuleAproxMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    DataFile dataDir = ffManager.getDataFile(config.getBasedir(), PromoteValidationsManager.RULES_DIR);
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Scanning {} for promotion validation rules...", dataDir);
    int changed = 0;
    if (dataDir.exists()) {
        final DataFile[] scripts = dataDir.listFiles((pathname) -> {
            logger.trace("Checking for promote validation rule script in: {}", pathname);
            return pathname.getName().endsWith(".groovy");
        });
        logger.info("Migrating promotion validation rules.");
        for (final DataFile script : scripts) {
            try {
                String scriptContent = script.readString();
                String migrated = scriptContent.replaceAll("A[Pp]rox", "Indy").replaceAll("aprox", "indy");
                if (!migrated.equals(scriptContent)) {
                    logger.info("Migrating promotion validation rule in: {}", script.getPath());
                    script.writeString(migrated, new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating to Indy packages / naming"));
                    changed++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return changed != 0;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) IOException(java.io.IOException) Logger(org.slf4j.Logger) ChangeSummary(org.commonjava.indy.audit.ChangeSummary)

Example 19 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile 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;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) HashMap(java.util.HashMap) ValidationRuleMapping(org.commonjava.indy.promote.validate.model.ValidationRuleMapping) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet)

Example 20 with DataFile

use of org.commonjava.indy.subsys.datafile.DataFile in project indy by Commonjava.

the class PromoteValidationsManagerTest method testRuleSetParseAndMatchOnStoreKey.

@Test
public void testRuleSetParseAndMatchOnStoreKey() throws Exception {
    DataFile dataFile = fileManager.getDataFile("promote/rule-sets/test.json");
    dataFile.writeString("{\"name\":\"test\",\"storeKeyPattern\":\".*\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "writing test data"));
    promoteValidations = new PromoteValidationsManager(fileManager, config, parser);
    ValidationRuleSet ruleSet = promoteValidations.getRuleSetMatching(new StoreKey(StoreType.hosted, "repo"));
    assertThat(ruleSet, notNullValue());
    assertThat(ruleSet.matchesKey("hosted:repo"), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Aggregations

DataFile (org.commonjava.indy.subsys.datafile.DataFile)36 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)21 IOException (java.io.IOException)16 Test (org.junit.Test)14 IndyDataException (org.commonjava.indy.data.IndyDataException)5 StoreKey (org.commonjava.indy.model.core.StoreKey)5 File (java.io.File)3 HashMap (java.util.HashMap)3 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)3 StoreType (org.commonjava.indy.model.core.StoreType)3 ValidationRuleMapping (org.commonjava.indy.promote.validate.model.ValidationRuleMapping)3 DataFileEvent (org.commonjava.indy.subsys.datafile.change.DataFileEvent)3 Logger (org.slf4j.Logger)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)2 ValidationRuleSet (org.commonjava.indy.promote.model.ValidationRuleSet)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 Template (groovy.text.Template)1