Search in sources :

Example 26 with DataFile

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

the class LegacyDataMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    if (!(storeDataManager instanceof DataFileStoreDataManager)) {
        logger.info("Store manager: {} is not based on DataFile's. Skipping migration.", storeDataManager.getClass().getName());
        return false;
    }
    final DataFile basedir = dataFileManager.getDataFile(INDY_STORE);
    final ChangeSummary summary = new ChangeSummary(ChangeSummary.SYSTEM_USER, "Migrating legacy store definitions.");
    if (!basedir.exists()) {
        return false;
    }
    StoreType[] storeTypes = StoreType.values();
    final String[] dirs = basedir.list();
    if (dirs == null || dirs.length < 1) {
        return false;
    }
    Map<String, String> migrationCandidates = new HashMap<>();
    //noinspection ConstantConditions
    Stream.of(storeTypes).forEach(type -> {
        File[] files = basedir.getDetachedFile().toPath().resolve(type.singularEndpointName()).toFile().listFiles((dir, fname) -> fname.endsWith(".json"));
        if (files != null) {
            Stream.of(files).forEach((f) -> {
                String src = Paths.get(type.singularEndpointName(), f.getName()).toString();
                String target = Paths.get(MAVEN_PKG_KEY, type.singularEndpointName(), f.getName()).toString();
                migrationCandidates.put(src, target);
            });
        }
    });
    boolean changed = false;
    for (Map.Entry<String, String> entry : migrationCandidates.entrySet()) {
        DataFile src = dataFileManager.getDataFile(INDY_STORE, entry.getKey());
        DataFile target = dataFileManager.getDataFile(INDY_STORE, entry.getValue());
        if (target.exists()) {
            continue;
        }
        DataFile targetDir = target.getParent();
        if (!targetDir.exists() && !targetDir.mkdirs()) {
            throw new IndyLifecycleException("Cannot make directory: %s.", targetDir.getPath());
        } else if (!targetDir.isDirectory()) {
            throw new IndyLifecycleException("Not a directory: %s.", targetDir.getPath());
        }
        try {
            logger.info("Migrating definition {}", src.getPath());
            final String json = src.readString();
            final String migrated = objectMapper.patchLegacyStoreJson(json);
            target.writeString(migrated, summary);
            changed = true;
        } catch (final IOException e) {
            throw new IndyLifecycleException("Failed to migrate artifact-store definition from: %s to: %s. Reason: %s", e, src, target, e.getMessage(), e);
        }
    }
    if (changed) {
        try {
            storeDataManager.reload();
        } catch (IndyDataException e) {
            throw new IndyLifecycleException("Failed to reload migrated store definitions: %s", e, e.getMessage());
        }
    }
    return changed;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) DataFile(org.commonjava.indy.subsys.datafile.DataFile) StoreType(org.commonjava.indy.model.core.StoreType) IndyDataException(org.commonjava.indy.data.IndyDataException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) DataFile(org.commonjava.indy.subsys.datafile.DataFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with DataFile

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

the class DataFileStoreDataManagerTest method getDataFileReturnsRemoteRepoJsonFile.

@Test
public void getDataFileReturnsRemoteRepoJsonFile() throws Exception {
    final String name = "foo";
    final boolean success = mgr.storeArtifactStore(new RemoteRepository(MAVEN_PKG_KEY, name, "http://www.foo.com/"), new ChangeSummary("test-user", "init"), false, true, new EventMetadata());
    assertThat(success, equalTo(true));
    final DataFile dataFile = mgr.getDataFile(new StoreKey(StoreType.remote, name));
    assertThat(dataFile.getDetachedFile().getAbsolutePath(), equalTo(new File(fileCfg.getDataBasedir(), "indy/remote/" + name + ".json").getAbsolutePath()));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) StoreKey(org.commonjava.indy.model.core.StoreKey) File(java.io.File) DataFile(org.commonjava.indy.subsys.datafile.DataFile) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 28 with DataFile

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

the class LegacyDataMigrationActionTest method migrateGroupJSONWithMissingTypeAttribute.

@Test
public void migrateGroupJSONWithMissingTypeAttribute() throws Exception {
    final DataFile dir = dfm.getDataFile(INDY_STORE, group.singularEndpointName());
    dir.mkdirs();
    DataFile in = dfm.getDataFile(INDY_STORE, group.singularEndpointName(), "test.json");
    in.writeString("{\"name\": \"test\", \"packageType\": \"maven\", \"key\": \"maven:group:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test group creation"));
    System.out.println("Wrote: " + in.getPath());
    final boolean result = action.migrate();
    assertThat(result, equalTo(true));
    DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, group.singularEndpointName(), "test.json");
    final String json = out.readString();
    assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
    assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
    assertThat(json.contains("\"key\" : \"maven:group:test\""), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Example 29 with DataFile

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

the class LegacyDataMigrationActionTest method dataFileMigrationForGroupJSONWithExistingTypeAttribute.

@Test
public void dataFileMigrationForGroupJSONWithExistingTypeAttribute() throws Exception {
    final DataFile dir = dfm.getDataFile(INDY_STORE, group.singularEndpointName());
    dir.mkdirs();
    final DataFile file = dir.getChild("test.json");
    file.writeString("{\"name\": \"test\", \"packageType\": \"maven\", \"type\" : \"group\", \"key\": \"maven:group:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test group creation"));
    String json = file.readString();
    assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
    final boolean result = action.migrate();
    assertThat(result, equalTo(true));
    DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, group.singularEndpointName(), "test.json");
    json = out.readString();
    assertThat(json.contains("\"type\" : \"group\""), equalTo(true));
    assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
    assertThat(json.contains("\"key\" : \"maven:group:test\""), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Example 30 with DataFile

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

the class LegacyDataMigrationActionTest method dataFileMigrationForHostedRepoJSONWithExistingTypeAttribute.

@Test
public void dataFileMigrationForHostedRepoJSONWithExistingTypeAttribute() throws Exception {
    final DataFile dir = dfm.getDataFile(INDY_STORE, hosted.singularEndpointName());
    dir.mkdirs();
    final DataFile file = dir.getChild("test.json");
    file.writeString("{\"name\": \"test\", \"type\" : \"hosted\", \"packageType\": \"maven\", \"key\": \"maven:hosted:test\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "test repo creation"));
    String json = file.readString();
    assertThat(json.contains("\"type\" : \"hosted\""), equalTo(true));
    final boolean result = action.migrate();
    assertThat(result, equalTo(true));
    DataFile out = dfm.getDataFile(INDY_STORE, MAVEN_PKG_KEY, hosted.singularEndpointName(), "test.json");
    json = out.readString();
    assertThat(json.contains("\"type\" : \"hosted\""), equalTo(true));
    assertThat(json.contains("\"packageType\" : \"maven\""), equalTo(true));
    assertThat(json.contains("\"key\" : \"maven:hosted:test\""), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) 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