Search in sources :

Example 6 with ClientChannel

use of org.apache.sshd.client.channel.ClientChannel in project karaf by apache.

the class DeployMojo method deployWithSsh.

protected void deployWithSsh(List<String> locations) throws MojoExecutionException {
    SshClient client = null;
    try {
        final Console console = System.console();
        client = SshClient.setUpDefaultClient();
        setupAgent(user, keyFile, client);
        client.setUserInteraction(new UserInteraction() {

            @Override
            public void welcome(ClientSession s, String banner, String lang) {
                console.printf(banner);
            }

            @Override
            public String[] interactive(ClientSession s, String name, String instruction, String lang, String[] prompt, boolean[] echo) {
                String[] answers = new String[prompt.length];
                try {
                    for (int i = 0; i < prompt.length; i++) {
                        if (console != null) {
                            if (echo[i]) {
                                answers[i] = console.readLine(prompt[i] + " ");
                            } else {
                                answers[i] = new String(console.readPassword(prompt[i] + " "));
                            }
                        }
                    }
                } catch (IOError e) {
                }
                return answers;
            }

            @Override
            public boolean isInteractionAllowed(ClientSession session) {
                return true;
            }

            @Override
            public void serverVersionInfo(ClientSession session, List<String> lines) {
            }

            @Override
            public String getUpdatedPassword(ClientSession session, String prompt, String lang) {
                return null;
            }
        });
        client.start();
        if (console != null) {
            console.printf("Logging in as %s\n", user);
        }
        ClientSession session = connect(client);
        if (password != null) {
            session.addPasswordIdentity(password);
        }
        session.auth().verify();
        StringWriter writer = new StringWriter();
        PrintWriter print = new PrintWriter(writer, true);
        for (String location : locations) {
            print.println("bundle:install -s " + location);
        }
        final ClientChannel channel = session.createChannel("exec", print.toString().concat(NEW_LINE));
        channel.setIn(new ByteArrayInputStream(new byte[0]));
        final ByteArrayOutputStream sout = new ByteArrayOutputStream();
        final ByteArrayOutputStream serr = new ByteArrayOutputStream();
        channel.setOut(AnsiConsole.wrapOutputStream(sout));
        channel.setErr(AnsiConsole.wrapOutputStream(serr));
        channel.open();
        channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);
        sout.writeTo(System.out);
        serr.writeTo(System.err);
        // Expects issue KARAF-2623 is fixed
        final boolean isError = (channel.getExitStatus() != null && channel.getExitStatus().intValue() != 0);
        if (isError) {
            final String errorMarker = Ansi.ansi().fg(Color.RED).toString();
            final int fromIndex = sout.toString().indexOf(errorMarker) + errorMarker.length();
            final int toIndex = sout.toString().lastIndexOf(Ansi.ansi().fg(Color.DEFAULT).toString());
            throw new MojoExecutionException(NEW_LINE + sout.toString().substring(fromIndex, toIndex));
        }
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Throwable t) {
        throw new MojoExecutionException(t, t.getMessage(), t.toString());
    } finally {
        try {
            client.stop();
        } catch (Throwable t) {
            throw new MojoExecutionException(t, t.getMessage(), t.toString());
        }
    }
}
Also used : SshClient(org.apache.sshd.client.SshClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientChannel(org.apache.sshd.client.channel.ClientChannel) StringWriter(java.io.StringWriter) IOError(java.io.IOError) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientSession(org.apache.sshd.client.session.ClientSession) Console(java.io.Console) AnsiConsole(org.fusesource.jansi.AnsiConsole) UserInteraction(org.apache.sshd.client.auth.keyboard.UserInteraction) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6 SshClient (org.apache.sshd.client.SshClient)6 ClientChannel (org.apache.sshd.client.channel.ClientChannel)6 ClientSession (org.apache.sshd.client.session.ClientSession)6 UserInteraction (org.apache.sshd.client.auth.keyboard.UserInteraction)5 Console (java.io.Console)4 IOError (java.io.IOError)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 ChannelShell (org.apache.sshd.client.channel.ChannelShell)3 Attributes (org.jline.terminal.Attributes)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 PtyCapableChannelSession (org.apache.sshd.client.channel.PtyCapableChannelSession)2 PtyMode (org.apache.sshd.common.channel.PtyMode)2 FileKeyPairProvider (org.apache.sshd.common.keyprovider.FileKeyPairProvider)2 NoCloseInputStream (org.apache.sshd.common.util.io.NoCloseInputStream)2