Search in sources :

Example 6 with IncorrectUsage

use of org.neo4j.commandline.admin.IncorrectUsage in project neo4j by neo4j.

the class OnlineBackupCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    final HostnamePort address;
    final Path folder;
    final String name;
    final boolean fallbackToFull;
    final boolean doConsistencyCheck;
    final Optional<Path> additionalConfig;
    final Path reportDir;
    final long timeout;
    final boolean checkGraph;
    final boolean checkIndexes;
    final boolean checkLabelScanStore;
    final boolean checkPropertyOwners;
    try {
        address = toHostnamePort(new HostnamePort("localhost", 6362)).apply(arguments.parse("from", args));
        folder = arguments.parseMandatoryPath("backup-dir", args);
        name = arguments.parse("name", args);
        fallbackToFull = arguments.parseBoolean("fallback-to-full", args);
        doConsistencyCheck = arguments.parseBoolean("check-consistency", args);
        timeout = parseTimeout(args);
        additionalConfig = arguments.parseOptionalPath("additional-config", args);
        reportDir = arguments.parseOptionalPath("cc-report-dir", args).orElseThrow(() -> new IllegalArgumentException("cc-report-dir must be a path"));
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    // Make sure destination exists
    if (!outsideWorld.fileSystem().isDirectory(folder.toFile())) {
        throw new CommandFailed(String.format("Directory '%s' does not exist.", folder));
    }
    if (!outsideWorld.fileSystem().isDirectory(reportDir.toFile())) {
        throw new CommandFailed(String.format("Directory '%s' does not exist.", reportDir));
    }
    File destination = folder.resolve(name).toFile();
    Config config = loadConfig(additionalConfig);
    boolean done = false;
    try {
        // We can remove the loading from config file in 4.0
        if (arguments.has("cc-graph", args)) {
            checkGraph = arguments.parseBoolean("cc-graph", args);
        } else {
            checkGraph = ConsistencyCheckSettings.consistency_check_graph.from(config);
        }
        if (arguments.has("cc-indexes", args)) {
            checkIndexes = arguments.parseBoolean("cc-indexes", args);
        } else {
            checkIndexes = ConsistencyCheckSettings.consistency_check_indexes.from(config);
        }
        if (arguments.has("cc-label-scan-store", args)) {
            checkLabelScanStore = arguments.parseBoolean("cc-label-scan-store", args);
        } else {
            checkLabelScanStore = ConsistencyCheckSettings.consistency_check_label_scan_store.from(config);
        }
        if (arguments.has("cc-property-owners", args)) {
            checkPropertyOwners = arguments.parseBoolean("cc-property-owners", args);
        } else {
            checkPropertyOwners = ConsistencyCheckSettings.consistency_check_property_owners.from(config);
        }
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    File[] listFiles = outsideWorld.fileSystem().listFiles(destination);
    if (listFiles != null && listFiles.length > 0) {
        outsideWorld.stdOutLine("Destination is not empty, doing incremental backup...");
        try {
            backupService.doIncrementalBackup(address.getHost(), address.getPort(), destination, timeout, config);
            done = true;
        } catch (Exception e) {
            if (fallbackToFull) {
                outsideWorld.stdErrLine("Incremental backup failed: " + e.getMessage());
                String renamed = renameExistingBackup(folder, name);
                outsideWorld.stdErrLine(String.format("Old backup renamed to '%s'.", renamed));
            } else {
                throw new CommandFailed("Backup failed: " + e.getMessage(), e);
            }
        }
    }
    if (!done) {
        outsideWorld.stdOutLine("Doing full backup...");
        try {
            backupService.doFullBackup(address.getHost(), address.getPort(), destination, ConsistencyCheck.NONE, config, timeout, false);
        } catch (Exception e) {
            throw new CommandFailed("Backup failed: " + e.getMessage(), e);
        }
    }
    if (doConsistencyCheck) {
        try {
            outsideWorld.stdOutLine("Doing consistency check...");
            ConsistencyCheckService.Result ccResult = consistencyCheckService.runFullConsistencyCheck(destination, config, ProgressMonitorFactory.textual(outsideWorld.errorStream()), FormattedLogProvider.toOutputStream(outsideWorld.outStream()), outsideWorld.fileSystem(), false, reportDir.toFile(), new CheckConsistencyConfig(checkGraph, checkIndexes, checkLabelScanStore, checkPropertyOwners));
            if (!ccResult.isSuccessful()) {
                throw new CommandFailed(String.format("Inconsistencies found. See '%s' for details.", ccResult.reportFile()), STATUS_CC_INCONSISTENT);
            }
        } catch (Throwable e) {
            if (e instanceof CommandFailed) {
                throw (CommandFailed) e;
            }
            throw new CommandFailed("Failed to do consistency check on backup: " + e.getMessage(), e, STATUS_CC_ERROR);
        }
    }
    outsideWorld.stdOutLine("Backup complete.");
}
Also used : Path(java.nio.file.Path) OptionalCanonicalPath(org.neo4j.commandline.arguments.common.OptionalCanonicalPath) MandatoryCanonicalPath(org.neo4j.commandline.arguments.common.MandatoryCanonicalPath) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) Config(org.neo4j.kernel.configuration.Config) HostnamePort(org.neo4j.helpers.HostnamePort) Converters.toHostnamePort(org.neo4j.kernel.impl.util.Converters.toHostnamePort) IOException(java.io.IOException) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) CommandFailed(org.neo4j.commandline.admin.CommandFailed) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) File(java.io.File)

Example 7 with IncorrectUsage

use of org.neo4j.commandline.admin.IncorrectUsage in project neo4j by neo4j.

the class UnbindFromClusterCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    try {
        Config config = loadNeo4jConfig(homeDir, configDir, arguments.parse("database", args));
        File dataDirectory = config.get(DatabaseManagementSystemSettings.data_directory);
        Path pathToSpecificDatabase = config.get(DatabaseManagementSystemSettings.database_path).toPath();
        Validators.CONTAINS_EXISTING_DATABASE.validate(pathToSpecificDatabase.toFile());
        confirmTargetDirectoryIsWritable(pathToSpecificDatabase);
        ClusterStateDirectory clusterStateDirectory = new ClusterStateDirectory(dataDirectory);
        clusterStateDirectory.initialize(outsideWorld.fileSystem());
        deleteClusterStateIn(clusterStateDirectory.get().toPath());
    } catch (StoreLockException e) {
        throw new CommandFailed("Database is currently locked. Please shutdown Neo4j.", e);
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    } catch (UnbindFailureException | CannotWriteException | IOException | ClusterStateException e) {
        throw new CommandFailed(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) ClusterStateDirectory(org.neo4j.causalclustering.core.state.ClusterStateDirectory) Config(org.neo4j.kernel.configuration.Config) StoreLockException(org.neo4j.kernel.StoreLockException) IOException(java.io.IOException) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) CommandFailed(org.neo4j.commandline.admin.CommandFailed) ClusterStateException(org.neo4j.causalclustering.core.state.ClusterStateException) File(java.io.File)

Example 8 with IncorrectUsage

use of org.neo4j.commandline.admin.IncorrectUsage in project neo4j by neo4j.

the class VersionCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    final Path storeDir = arguments.parseMandatoryPath("store", args);
    Validators.CONTAINS_EXISTING_DATABASE.validate(storeDir.toFile());
    try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem)) {
        final String storeVersion = new StoreVersionCheck(pageCache).getVersion(storeDir.resolve(MetaDataStore.DEFAULT_NAME).toFile()).orElseThrow(() -> new CommandFailed(String.format("Could not find version metadata in store '%s'", storeDir)));
        final String fmt = "%-25s%s";
        out.accept(String.format(fmt, "Store format version:", storeVersion));
        RecordFormats format = RecordFormatSelector.selectForVersion(storeVersion);
        out.accept(String.format(fmt, "Introduced in version:", format.introductionVersion()));
        findSuccessor(format).map(next -> String.format(fmt, "Superseded in version:", next.introductionVersion())).ifPresent(out);
        out.accept(String.format(fmt, "Current version:", Version.getNeo4jVersion()));
    } catch (IOException e) {
        throw new CommandFailed(e.getMessage(), e);
    }
}
Also used : MandatoryCanonicalPath(org.neo4j.commandline.arguments.common.MandatoryCanonicalPath) Path(java.nio.file.Path) PageCache(org.neo4j.io.pagecache.PageCache) Validators(org.neo4j.kernel.impl.util.Validators) StandalonePageCacheFactory(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory) Version(org.neo4j.kernel.internal.Version) IOException(java.io.IOException) RecordFormatSelector(org.neo4j.kernel.impl.store.format.RecordFormatSelector) StoreVersionCheck(org.neo4j.kernel.impl.storemigration.StoreVersionCheck) AdminCommand(org.neo4j.commandline.admin.AdminCommand) RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) Consumer(java.util.function.Consumer) MandatoryCanonicalPath(org.neo4j.commandline.arguments.common.MandatoryCanonicalPath) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) Arguments(org.neo4j.commandline.arguments.Arguments) OutsideWorld(org.neo4j.commandline.admin.OutsideWorld) RecordFormatSelector.findSuccessor(org.neo4j.kernel.impl.store.format.RecordFormatSelector.findSuccessor) MetaDataStore(org.neo4j.kernel.impl.store.MetaDataStore) CommandFailed(org.neo4j.commandline.admin.CommandFailed) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) Path(java.nio.file.Path) StoreVersionCheck(org.neo4j.kernel.impl.storemigration.StoreVersionCheck) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) CommandFailed(org.neo4j.commandline.admin.CommandFailed) IOException(java.io.IOException) PageCache(org.neo4j.io.pagecache.PageCache)

Example 9 with IncorrectUsage

use of org.neo4j.commandline.admin.IncorrectUsage in project neo4j by neo4j.

the class DatabaseImporterTest method failIfSourceIsNotAStore.

@Test
public void failIfSourceIsNotAStore() throws Exception {
    File from = testDir.directory("empty");
    String[] arguments = { "--mode=database", "--database=bar", "--from=" + from.getAbsolutePath() };
    try {
        new DatabaseImporter(Args.parse(arguments), Config.defaults(), new NullOutsideWorld());
        fail("Should have thrown an exception.");
    } catch (IncorrectUsage e) {
        assertThat(e.getMessage(), containsString("does not contain a database"));
    }
}
Also used : NullOutsideWorld(org.neo4j.commandline.admin.NullOutsideWorld) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 10 with IncorrectUsage

use of org.neo4j.commandline.admin.IncorrectUsage in project neo4j by neo4j.

the class ImportCommandTest method requiresDatabaseArgument.

@Test
public void requiresDatabaseArgument() throws Exception {
    try (NullOutsideWorld outsideWorld = new NullOutsideWorld()) {
        ImportCommand importCommand = new ImportCommand(testDir.directory("home").toPath(), testDir.directory("conf").toPath(), outsideWorld);
        String[] arguments = { "--mode=database", "--from=bar" };
        try {
            importCommand.execute(arguments);
            fail("Should have thrown an exception.");
        } catch (IncorrectUsage e) {
            assertThat(e.getMessage(), containsString("database"));
        }
    }
}
Also used : NullOutsideWorld(org.neo4j.commandline.admin.NullOutsideWorld) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

IncorrectUsage (org.neo4j.commandline.admin.IncorrectUsage)10 IOException (java.io.IOException)6 File (java.io.File)5 Path (java.nio.file.Path)5 CommandFailed (org.neo4j.commandline.admin.CommandFailed)5 Config (org.neo4j.kernel.configuration.Config)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 NullOutsideWorld (org.neo4j.commandline.admin.NullOutsideWorld)4 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)3 MandatoryCanonicalPath (org.neo4j.commandline.arguments.common.MandatoryCanonicalPath)2 OptionalCanonicalPath (org.neo4j.commandline.arguments.common.OptionalCanonicalPath)2 CheckConsistencyConfig (org.neo4j.consistency.checking.full.CheckConsistencyConfig)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 Consumer (java.util.function.Consumer)1 ClusterStateDirectory (org.neo4j.causalclustering.core.state.ClusterStateDirectory)1 ClusterStateException (org.neo4j.causalclustering.core.state.ClusterStateException)1 AdminCommand (org.neo4j.commandline.admin.AdminCommand)1 OutsideWorld (org.neo4j.commandline.admin.OutsideWorld)1 Arguments (org.neo4j.commandline.arguments.Arguments)1