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());
}
}
}
Aggregations