Search in sources :

Example 6 with Terminal

use of org.opensearch.cli.Terminal in project OpenSearch by opensearch-project.

the class ImportJvmOptionsTask method accept.

@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    try {
        terminal.println("Importing JVM options ...");
        final Path jvmOptionsDir = taskInput.getOpenSearchConfig().resolve(JVM_OPTIONS_D);
        if (!Files.exists(jvmOptionsDir)) {
            Files.createDirectory(jvmOptionsDir);
        }
        final Path esJvmOptionsDir = taskInput.getEsConfig().resolve(JVM_OPTIONS_D);
        if (Files.exists(esJvmOptionsDir) && Files.isDirectory(esJvmOptionsDir)) {
            final List<Path> esJvmOptionsFiles = Files.list(esJvmOptionsDir).collect(Collectors.toList());
            for (Path esJvmOptFile : esJvmOptionsFiles) {
                final Path jvmOptFile = jvmOptionsDir.resolve(esJvmOptFile.getFileName().toString());
                Files.copy(esJvmOptFile, jvmOptFile, StandardCopyOption.REPLACE_EXISTING);
            }
        }
        terminal.println("Success!" + System.lineSeparator());
    } catch (Exception e) {
        throw new RuntimeException("Error importing JVM options. " + e);
    }
}
Also used : Path(java.nio.file.Path) Terminal(org.opensearch.cli.Terminal)

Example 7 with Terminal

use of org.opensearch.cli.Terminal in project OpenSearch by opensearch-project.

the class ImportLog4jPropertiesTask method accept.

@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    try {
        terminal.println("Importing log4j.properties ...");
        final Path log4jPropPath = taskInput.getOpenSearchConfig().resolve(LOG4J_PROPERTIES);
        if (Files.exists(log4jPropPath)) {
            Files.copy(log4jPropPath, taskInput.getOpenSearchConfig().resolve(LOG4J_PROPERTIES + ".bkp"), StandardCopyOption.REPLACE_EXISTING);
        }
        final Path esLog4jPropPath = taskInput.getEsConfig().resolve(LOG4J_PROPERTIES);
        try (InputStream esLog4jIs = Files.newInputStream(esLog4jPropPath);
            OutputStream log4jOs = Files.newOutputStream(log4jPropPath, StandardOpenOption.TRUNCATE_EXISTING)) {
            final Properties esLog4JProps = new Properties();
            esLog4JProps.load(esLog4jIs);
            final Properties log4jProps = renameValues(esLog4JProps);
            log4jProps.store(log4jOs, "This is an auto-generated file imported from an existing elasticsearch installation.");
        }
        terminal.println("Success!" + System.lineSeparator());
    } catch (IOException e) {
        throw new RuntimeException("Error copying log4j properties. " + e);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) Terminal(org.opensearch.cli.Terminal)

Example 8 with Terminal

use of org.opensearch.cli.Terminal in project OpenSearch by opensearch-project.

the class ValidateInputTask method accept.

@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    terminal.println("Verifying the details ...");
    // check if the elasticsearch version is supported
    if (taskInput.getVersion().isPresent()) {
        final Version version = taskInput.getVersion().get();
        if (version.equals(LegacyESVersion.V_7_10_2) == false) {
            throw new RuntimeException(String.format(Locale.getDefault(), "The installed version %s of elasticsearch is not supported.", version));
        }
    } else {
        terminal.println("Unable to detect installed elasticsearch version.");
        confirmToProceed(terminal);
    }
    // check if the OpenSearch config is set to an external location
    if (taskInput.getOpenSearchConfig().getParent().equals(taskInput.getOpenSearchBin().getParent())) {
        terminal.println("OpenSearch config directory is set inside the installation directory. " + "It is recommended to use an external config directory and set the environment variable " + "OPENSEARCH_PATH_CONF to it.");
        confirmToProceed(terminal);
    }
    // print summary and confirm with user if everything looks correct.
    final Map<String, String> fieldsMap = getSummaryFieldsMap(taskInput);
    final String format = " %-25s | %s";
    terminal.println("+----------------------- SUMMARY -----------------------+");
    for (Map.Entry<String, String> entry : fieldsMap.entrySet()) {
        terminal.println(String.format(Locale.getDefault(), format, entry.getKey(), entry.getValue()));
    }
    terminal.println("+-------------------------------------------------------+");
    terminal.println("Please verify if everything above looks good.");
    confirmToProceed(terminal);
}
Also used : LegacyESVersion(org.opensearch.LegacyESVersion) Version(org.opensearch.Version) Terminal(org.opensearch.cli.Terminal) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with Terminal

use of org.opensearch.cli.Terminal in project OpenSearch by opensearch-project.

the class DetectEsInstallationTask method accept.

@SuppressForbidden(reason = "We need to read external es config files")
@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    try {
        terminal.println("Looking for an elasticsearch installation ...");
        String esHomeEnv = System.getenv(ES_HOME);
        if (esHomeEnv == null) {
            esHomeEnv = terminal.readText("Missing ES_HOME env variable, enter the path to elasticsearch home: ");
            if (esHomeEnv == null || esHomeEnv.isEmpty()) {
                throw new RuntimeException("Invalid input for path to elasticsearch home directory.");
            }
        }
        taskInput.setEsHome(new File(esHomeEnv).toPath());
        String esConfEnv = System.getenv(ES_CONFIG_ENV);
        if (esConfEnv == null) {
            esConfEnv = terminal.readText("Missing ES_PATH_CONF env variable, enter the path to elasticsearch config directory: ");
            if (esConfEnv == null || esHomeEnv.isEmpty()) {
                throw new RuntimeException("Invalid input for path to elasticsearch config directory.");
            }
        }
        taskInput.setEsConfig(new File(esConfEnv).toPath());
        final Settings esSettings = Settings.builder().loadFromPath(taskInput.getEsConfig().resolve(ES_CONFIG_YML)).build();
        final String url = retrieveUrl(esSettings);
        taskInput.setBaseUrl(url);
        final boolean running = isRunning(url);
        taskInput.setRunning(running);
        if (running) {
            terminal.println("Found a running instance of elasticsearch at " + url);
            taskInput.setRunning(true);
            try {
                updateTaskInput(taskInput, fetchInfoFromUrl(taskInput.getBaseUrl()));
            } catch (RuntimeException e) {
                updateTaskInput(taskInput, fetchInfoFromEsSettings(esSettings));
            }
            try {
                taskInput.setPlugins(fetchPluginsFromUrl(taskInput.getBaseUrl()));
            } catch (RuntimeException e) {
                taskInput.setPlugins(detectPluginsFromEsHome(taskInput.getEsHome()));
            }
        } else {
            terminal.println("Did not find a running instance of elasticsearch at " + url);
            updateTaskInput(taskInput, fetchInfoFromEsSettings(esSettings));
            taskInput.setPlugins(detectPluginsFromEsHome(taskInput.getEsHome()));
        }
    } catch (IOException e) {
        throw new RuntimeException("Error detecting existing elasticsearch installation. " + e);
    }
}
Also used : IOException(java.io.IOException) Terminal(org.opensearch.cli.Terminal) File(java.io.File) Settings(org.opensearch.common.settings.Settings) SuppressForbidden(org.opensearch.common.SuppressForbidden)

Example 10 with Terminal

use of org.opensearch.cli.Terminal in project OpenSearch by opensearch-project.

the class ImportKeystoreTask method accept.

@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    SecureString keyStorePassword = new SecureString(new char[0]);
    try {
        terminal.println("Importing keystore settings ...");
        final KeyStoreWrapper esKeystore = KeyStoreWrapper.load(taskInput.getEsConfig(), ES_KEYSTORE_FILENAME);
        if (esKeystore == null) {
            terminal.println("No elasticsearch keystore settings to import.");
            return;
        }
        KeyStoreWrapper openSearchKeystore = KeyStoreWrapper.load(taskInput.getOpenSearchConfig().resolve(OPENSEARCH_KEYSTORE_FILENAME));
        if (openSearchKeystore == null) {
            openSearchKeystore = KeyStoreWrapper.create();
        }
        if (esKeystore.hasPassword()) {
            final char[] passwordArray = terminal.readSecret("Enter password for the elasticsearch keystore : ");
            keyStorePassword = new SecureString(passwordArray);
        }
        esKeystore.decrypt(keyStorePassword.getChars());
        for (String setting : esKeystore.getSettingNames()) {
            if (setting.equals("keystore.seed")) {
                continue;
            }
            if (!openSearchKeystore.getSettingNames().contains(setting)) {
                InputStream settingIS = esKeystore.getFile(setting);
                byte[] bytes = new byte[settingIS.available()];
                settingIS.read(bytes);
                KeystoreWrapperUtil.saveSetting(openSearchKeystore, setting, bytes);
            }
        }
        openSearchKeystore.save(taskInput.getOpenSearchConfig(), keyStorePassword.getChars());
        terminal.println("Success!" + System.lineSeparator());
    } catch (Exception e) {
        throw new RuntimeException("Error importing keystore settings from elasticsearch, " + e);
    } finally {
        keyStorePassword.close();
    }
}
Also used : InputStream(java.io.InputStream) KeyStoreWrapper(org.opensearch.common.settings.KeyStoreWrapper) SecureString(org.opensearch.common.settings.SecureString) Terminal(org.opensearch.cli.Terminal) SecureString(org.opensearch.common.settings.SecureString)

Aggregations

Terminal (org.opensearch.cli.Terminal)11 Path (java.nio.file.Path)6 IOException (java.io.IOException)5 Tuple (org.opensearch.common.collect.Tuple)3 Settings (org.opensearch.common.settings.Settings)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 Version (org.opensearch.Version)2 MockTerminal (org.opensearch.cli.MockTerminal)2 UserException (org.opensearch.cli.UserException)2 SuppressForbidden (org.opensearch.common.SuppressForbidden)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 EnumSet (java.util.EnumSet)1 LinkedHashMap (java.util.LinkedHashMap)1