summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2018-04-19 17:34:35 +0200
committerSimon Rettberg2018-04-19 17:34:35 +0200
commit81ca3f69c024ced1eec09e0dcbab39d9ec24326b (patch)
tree16decf865c566eb41e3aeb5686fb011d6748c42c
parent[BackupRestore] Increase timeout to 3 minutes (diff)
downloadtmlite-bwlp-81ca3f69c024ced1eec09e0dcbab39d9ec24326b.tar.gz
tmlite-bwlp-81ca3f69c024ced1eec09e0dcbab39d9ec24326b.tar.xz
tmlite-bwlp-81ca3f69c024ced1eec09e0dcbab39d9ec24326b.zip
[RemoteReboot] Refactor, better cleanup
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
index 828165f..d7f17cf 100644
--- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
+++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
@@ -105,44 +105,49 @@ public class RemoteReboot extends AbstractTask
tp.submit( new Runnable() {
public void run()
{
- int ret = -1;
+ int ret = -123;
+ Session session = null;
+ ChannelExec channel = null;
try {
status.clientStatus.put( client.machineuuid, ClientStatus.CONNECTING );
- Session session = sshClient.getSession( "root", client.clientip, port );
+ session = sshClient.getSession( "root", client.clientip, port );
session.connect( 5000 );
- ChannelExec channel = (ChannelExec)session.openChannel( "exec" );
+ channel = (ChannelExec)session.openChannel( "exec" );
String args = " " + minutes + " " + String.format( "'%s'", client.machineuuid.replace( "'", "'\\''" ) );
if ( shutdown ) {
- channel.setCommand( SHUTDOWN_CMD + args );
- channel.connect( 2000 );
- waitForCommand( channel, 2000 );
- ret = channel.getExitStatus();
+ ret = execCommand( channel, SHUTDOWN_CMD + args );
status.clientStatus.put( client.machineuuid, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT );
} else {
- channel.setCommand( REBOOT_CMD + args );
- channel.connect( 2000 );
- waitForCommand( channel, 2000 );
- ret = channel.getExitStatus();
+ ret = execCommand( channel, REBOOT_CMD + args );
if ( ret == 0 ) {
status.clientStatus.put( client.machineuuid, minutes == 0 ? ClientStatus.REBOOTING : ClientStatus.REBOOT_AT );
rebootingClients.add( client );
}
}
- channel.disconnect();
- session.disconnect();
} catch ( JSchException e ) {
- if ( e.toString().contains( "Auth fail" ) ) {
+ if ( "Auth fail".equals( e.getMessage() ) || "Auth cancel".equals( e.getMessage() ) ) {
status.clientStatus.put( client.machineuuid, ClientStatus.AUTH_FAIL );
ret = 0;
} else {
status.addError( client.clientip + ": " + e.toString() );
- ret = -1;
+ ret = -123;
+ }
+ } finally {
+ if ( session != null ) {
+ try {
+ channel.disconnect();
+ } catch ( Exception e ) {
+ }
+ try {
+ session.disconnect();
+ } catch ( Exception e ) {
+ }
}
}
if ( ret != 0 ) {
- if ( ret != -1 ) {
+ if ( ret != -123 ) {
status.addError( client.clientip + ": Exit Code " + ret );
}
status.clientStatus.put( client.machineuuid, ClientStatus.ERROR );
@@ -210,17 +215,22 @@ public class RemoteReboot extends AbstractTask
return true;
}
- private void waitForCommand( ChannelExec channel, int timeout )
+ private int execCommand( ChannelExec channel, String cmd ) throws JSchException
{
- for ( int i = 0; i < timeout / 100; i++ ) {
+ channel.setCommand( cmd );
+ channel.connect( 2000 );
+ for ( int i = 0; i < 2000 / 100; i++ ) { // Wait 2 seconds
if ( channel.isClosed() ) {
break;
}
try {
Thread.sleep( 100 );
} catch ( InterruptedException e ) {
+ Thread.currentThread().interrupt();
+ break;
}
}
+ return channel.getExitStatus();
}
private boolean isOnline( String address, int... ports )