Search in sources :

Example 6 with IndyLifecycleException

use of org.commonjava.indy.action.IndyLifecycleException in project indy by Commonjava.

the class LegacyQuartzDBMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    final Logger logger = LoggerFactory.getLogger(LegacyQuartzDBMigrationAction.class);
    Path path = Paths.get(fileConfig.getDataBasedir().toURI());
    final Stream<Path> listFiles;
    try {
        listFiles = Files.list(path);
    } catch (IOException e) {
        logger.warn("IOException happened when list path {}", path);
        return false;
    }
    if (listFiles != null) {
        listFiles.filter(filePath -> Files.isRegularFile(filePath) && filePath.getFileName().toString().startsWith(SCHEDULE_DB_NAME)).forEach(file -> {
            try {
                Files.delete(file);
            } catch (IOException e) {
                logger.warn("IOException happened when delete file {}", file);
            }
        });
    }
    return true;
}
Also used : Path(java.nio.file.Path) Inject(javax.inject.Inject) Logger(org.slf4j.Logger) Stream(java.util.stream.Stream) Files(java.nio.file.Files) Paths(java.nio.file.Paths) MigrationAction(org.commonjava.indy.action.MigrationAction) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) DataFileConfiguration(org.commonjava.indy.subsys.datafile.conf.DataFileConfiguration) Named(javax.inject.Named) Path(java.nio.file.Path) IOException(java.io.IOException) Logger(org.slf4j.Logger)

Example 7 with IndyLifecycleException

use of org.commonjava.indy.action.IndyLifecycleException 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 8 with IndyLifecycleException

use of org.commonjava.indy.action.IndyLifecycleException in project indy by Commonjava.

the class PackageTypedStorageMigrationAction method migrate.

@Override
public boolean migrate() throws IndyLifecycleException {
    Set<ArtifactStore> stores;
    try {
        stores = storeDataManager.getAllArtifactStores();
    } catch (IndyDataException e) {
        throw new IndyLifecycleException("Cannot retrieve list of repositories and groups in order to review storage locations. Reason: %s", e, e.getMessage());
    }
    File storageRoot = config.getStorageRootDirectory();
    File nfsStorageRoot = config.getNFSStorageRootDirectory();
    int migrations = 0;
    Map<File, File> unmigratedNfs = new HashMap<>();
    for (ArtifactStore store : stores) {
        File old = deprecatedStoragePath(storageRoot, store);
        File migrated = packageTypedStoragePath(storageRoot, store);
        if (old.exists()) {
            logger.info("Attempting to migrate existing storage from old directory structure: {} " + "to package-typed structure: {}", old, migrated);
            try {
                if (migrated.exists()) {
                    FileUtils.copyDirectory(old, migrated);
                    FileUtils.forceDelete(old);
                } else {
                    FileUtils.moveDirectory(old, migrated);
                }
                migrations++;
            } catch (IOException e) {
                throw new IndyLifecycleException("Failed to migrate: %s to: %s. Reason: %s", e, old, migrated);
            }
        }
        if (nfsStorageRoot != null) {
            File oldNfs = deprecatedStoragePath(nfsStorageRoot, store);
            File migratedNfs = packageTypedStoragePath(nfsStorageRoot, store);
            if (oldNfs.exists() && !migratedNfs.exists()) {
                unmigratedNfs.put(oldNfs, migratedNfs);
            }
        }
    }
    if (!unmigratedNfs.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        sb.append("ERROR: Un-migrated directories detected on NFS storage!!!!");
        sb.append("\n\nThese directories still use the old <type>/<name> directory format. Indy now supports");
        sb.append("\nmultiple package types, and the storage format has changed accordingly. The new format is:");
        sb.append("\n\n    <package-type>/<type>/<name>");
        sb.append("\n\nPlease migrate these NFS directories manually. For Maven repositories:");
        sb.append("\n\n    maven/<type>/<name>");
        sb.append("\n\nFor HTTProx repositories (httprox_*):");
        sb.append("\n\n    generic-http/<type>/<name>");
        sb.append("\n\nThe following directories were detected:\n");
        unmigratedNfs.forEach((o, n) -> sb.append("\n    ").append(o).append("  =>  ").append(n));
        sb.append("\n\n");
        logger.error(sb.toString());
        throw new IndyLifecycleException("Un-migrated NFS directories detected. Indy cannot start until this has been resolved.");
    }
    return migrations > 0;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) HashMap(java.util.HashMap) IOException(java.io.IOException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException) File(java.io.File)

Aggregations

IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)8 IOException (java.io.IOException)5 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)5 IndyDataException (org.commonjava.indy.data.IndyDataException)5 ArrayList (java.util.ArrayList)3 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Inject (javax.inject.Inject)2 DataFile (org.commonjava.indy.subsys.datafile.DataFile)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 InetSocketAddress (java.net.InetSocketAddress)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Map (java.util.Map)1 Stream (java.util.stream.Stream)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1