Search in sources :

Example 1 with Terminal

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

the class InstallPluginCommandTests method assertInstallPluginFromUrl.

void assertInstallPluginFromUrl(final String pluginId, final String name, final String url, final String stagingHash, final boolean isSnapshot, final String shaExtension, final Function<byte[], String> shaCalculator, final PGPSecretKey secretKey, final BiFunction<byte[], PGPSecretKey, String> signature) throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Path pluginZip = createPlugin(name, pluginDir);
    InstallPluginCommand command = new InstallPluginCommand() {

        @Override
        Path downloadZip(Terminal terminal, String urlString, Path tmpDir, boolean isBatch) throws IOException {
            assertEquals(url, urlString);
            Path downloadedPath = tmpDir.resolve("downloaded.zip");
            Files.copy(pluginZip, downloadedPath);
            return downloadedPath;
        }

        @Override
        URL openUrl(String urlString) throws IOException {
            if ((url + shaExtension).equals(urlString)) {
                // calc sha an return file URL to it
                Path shaFile = temp.apply("shas").resolve("downloaded.zip" + shaExtension);
                byte[] zipbytes = Files.readAllBytes(pluginZip);
                String checksum = shaCalculator.apply(zipbytes);
                Files.write(shaFile, checksum.getBytes(StandardCharsets.UTF_8));
                return shaFile.toUri().toURL();
            } else if ((url + ".sig").equals(urlString)) {
                final Path ascFile = temp.apply("sig").resolve("downloaded.zip" + ".sig");
                final byte[] zipBytes = Files.readAllBytes(pluginZip);
                final String asc = signature.apply(zipBytes, secretKey);
                Files.write(ascFile, asc.getBytes(StandardCharsets.UTF_8));
                return ascFile.toUri().toURL();
            }
            return null;
        }

        @Override
        void verifySignature(Path zip, String urlString) throws IOException, PGPException {
            if (InstallPluginCommand.OFFICIAL_PLUGINS.contains(name)) {
                super.verifySignature(zip, urlString);
            } else {
                throw new UnsupportedOperationException("verify signature should not be called for unofficial plugins");
            }
        }

        @Override
        InputStream pluginZipInputStream(Path zip) throws IOException {
            return new ByteArrayInputStream(Files.readAllBytes(zip));
        }

        @Override
        String getPublicKeyId() {
            return Long.toHexString(secretKey.getKeyID()).toUpperCase(Locale.ROOT);
        }

        @Override
        InputStream getPublicKey() {
            try {
                final ByteArrayOutputStream output = new ByteArrayOutputStream();
                final ArmoredOutputStream armored = new ArmoredOutputStream(output);
                secretKey.getPublicKey().encode(armored);
                armored.close();
                return new ByteArrayInputStream(output.toByteArray());
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }

        @Override
        boolean urlExists(Terminal terminal, String urlString) throws IOException {
            return urlString.equals(url);
        }

        @Override
        String getStagingHash() {
            return stagingHash;
        }

        @Override
        boolean isSnapshot() {
            return isSnapshot;
        }

        @Override
        void jarHellCheck(PluginInfo candidateInfo, Path candidate, Path pluginsDir, Path modulesDir) throws Exception {
        // no jarhell check
        }
    };
    installPlugin(pluginId, env.v1(), command);
    assertPlugin(name, pluginDir, env.v2());
}
Also used : Path(java.nio.file.Path) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MockTerminal(org.opensearch.cli.MockTerminal) Terminal(org.opensearch.cli.Terminal) ByteArrayInputStream(java.io.ByteArrayInputStream) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment)

Example 2 with Terminal

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

the class ImportYmlConfigTask 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 settings from elasticsearch.yml ...");
        final Path openSearchYmlPath = taskInput.getOpenSearchConfig().resolve(OPENSEARCH_CONFIG_FILENAME);
        final Path esYamlPath = taskInput.getEsConfig().resolve(ES_CONFIG_FILENAME);
        final Settings esSettings = Settings.builder().loadFromPath(esYamlPath).build();
        final Settings settings = Settings.builder().loadFromPath(openSearchYmlPath).build();
        if (esSettings.size() > 0) {
            if (settings.size() > 0 && terminal.promptYesNo("Existing settings in opensearch.yml will be overwritten, proceed?", false) == false) {
                terminal.println("Import settings cancelled by user");
            }
            final Path backupYmlPath = taskInput.getOpenSearchConfig().resolve(OPENSEARCH_CONFIG_FILENAME + ".bkp");
            if (!Files.exists(backupYmlPath) || terminal.promptYesNo("A backup file for opensearch.yml already exists, overwrite?", false)) {
                Files.copy(openSearchYmlPath, backupYmlPath, StandardCopyOption.REPLACE_EXISTING);
            }
            Files.write(openSearchYmlPath, Collections.singleton(HEADER), StandardOpenOption.TRUNCATE_EXISTING);
            final Settings mergeSettings = mergeSettings(settings, esSettings);
            writeSettings(openSearchYmlPath, mergeSettings);
        }
        terminal.println("Success!" + System.lineSeparator());
    } catch (IOException ex) {
        throw new RuntimeException("Error importing settings from elasticsearch.yml, " + ex);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) Terminal(org.opensearch.cli.Terminal) Settings(org.opensearch.common.settings.Settings)

Example 3 with Terminal

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

the class InstallPluginsTask method accept.

@Override
public void accept(final Tuple<TaskInput, Terminal> input) {
    final TaskInput taskInput = input.v1();
    final Terminal terminal = input.v2();
    if (taskInput.getPlugins() == null || taskInput.getPlugins().isEmpty()) {
        return;
    }
    terminal.println("Installing core plugins ...");
    List<String> manualPlugins = new ArrayList<>();
    for (String plugin : taskInput.getPlugins()) {
        if (OFFICIAL_PLUGINS.contains(plugin)) {
            executeInstallPluginCommand(plugin, taskInput, terminal);
        } else {
            manualPlugins.add(plugin);
        }
    }
    if (!manualPlugins.isEmpty()) {
        terminal.println("Please install the following custom plugins manually: " + manualPlugins);
    }
    terminal.println("Success!" + System.lineSeparator());
}
Also used : ArrayList(java.util.ArrayList) Terminal(org.opensearch.cli.Terminal)

Example 4 with Terminal

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

the class UpgradeCli method execute.

/**
 * Executes the upgrade task. This retrieves an instance of {@link UpgradeTask} which is composed
 * of smaller individual tasks that perform specific operations as part of the overall process.
 *
 * @param terminal current terminal the command is running
 * @param options  options supplied to the command
 * @param env      current environment in which this cli tool is running.
 * @throws UserException if any exception is thrown from the tasks
 */
@Override
protected void execute(final Terminal terminal, final OptionSet options, final Environment env) throws UserException {
    try {
        final Tuple<TaskInput, Terminal> input = new Tuple<>(new TaskInput(env), terminal);
        UpgradeTask.getTask().accept(input);
        terminal.println("Done!");
        terminal.println("Next Steps: ");
        terminal.println(" Stop the running elasticsearch on this node.");
        terminal.println(" Start OpenSearch on this node.");
    } catch (RuntimeException ex) {
        throw new UserException(ExitCodes.DATA_ERROR, ex.getMessage());
    }
}
Also used : UserException(org.opensearch.cli.UserException) Terminal(org.opensearch.cli.Terminal) Tuple(org.opensearch.common.collect.Tuple)

Example 5 with Terminal

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

the class DetectEsInstallationTaskTests method testTaskExecution.

@SuppressForbidden(reason = "Read config directory from test resources.")
public void testTaskExecution() throws Exception {
    Path esConfig = new File(getClass().getResource("/config").getPath()).toPath();
    // path for es_home
    terminal.addTextInput(esConfig.getParent().toString());
    // path for es_config
    terminal.addTextInput(esConfig.toString());
    TaskInput taskInput = new TaskInput(env);
    Tuple<TaskInput, Terminal> input = new Tuple<>(taskInput, terminal);
    task.accept(input);
    assertThat(taskInput.getEsConfig(), is(esConfig));
    assertThat(taskInput.getBaseUrl(), is("http://localhost:42123"));
    assertThat(taskInput.getPlugins(), hasSize(0));
    assertThat(taskInput.getNode(), is("node-x"));
    assertThat(taskInput.getCluster(), is("my-cluster"));
}
Also used : Path(java.nio.file.Path) File(java.io.File) Terminal(org.opensearch.cli.Terminal) MockTerminal(org.opensearch.cli.MockTerminal) Tuple(org.opensearch.common.collect.Tuple) SuppressForbidden(org.opensearch.common.SuppressForbidden)

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