From 22b2e1ff666bc9c85c203b297de5feab4c765b06 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 11 Apr 2017 15:13:20 +0200 Subject: [RemoteReboot] Use idleaction scripts, more error handling/logging --- .../openslx/taskmanager/tasks/RemoteReboot.java | 42 +++++++++++++++------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java index 230480f..f6ce4a7 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java @@ -9,7 +9,6 @@ import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -46,6 +45,10 @@ public class RemoteReboot extends AbstractTask private Output status = new Output(); + private static final String REBOOT_CMD = "/opt/openslx/scripts/idleaction-scheduled_action --detach reboot"; + + private static final String SHUTDOWN_CMD = "/opt/openslx/scripts/idleaction-scheduled_action --detach poweroff"; + @Override protected boolean initTask() { @@ -89,17 +92,32 @@ public class RemoteReboot extends AbstractTask status.clientStatus.put(client.machineuuid, ClientStatus.CONNECTING); tp.submit(new Runnable() { public void run() { + int ret = -1; try { Shell shell = new SSH(client.clientip, port, "root", sshkey); if (shutdown) { - new Shell.Empty(shell).exec("/sbin/shutdown +" + minutes); + ret = new Shell.Empty(shell).exec(SHUTDOWN_CMD + " " + minutes); status.clientStatus.put(client.machineuuid, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT); } else { - new Shell.Empty(shell).exec("/sbin/reboot"); - status.clientStatus.put(client.machineuuid, ClientStatus.REBOOTING); - rebootingClients.add( client ); + ret = new Shell.Empty(shell).exec(REBOOT_CMD + " " + minutes); + if (ret == 0) { + status.clientStatus.put(client.machineuuid, minutes == 0 ? ClientStatus.REBOOTING : ClientStatus.REBOOT_AT); + rebootingClients.add( client ); + } } } catch (IOException e) { + if (e.toString().contains( "Auth fail" )) { + status.clientStatus.put(client.machineuuid, ClientStatus.AUTH_FAIL); + ret = 0; + } else { + status.addError( client.clientip + ": " + e.toString() ); + ret = -1; + } + } + if (ret != 0) { + if (ret != -1) { + status.addError( client.clientip + ": Exit Code " + ret ); + } status.clientStatus.put(client.machineuuid, ClientStatus.ERROR); } } @@ -114,22 +132,24 @@ public class RemoteReboot extends AbstractTask return false; } - if (rebootingClients.size() > 0) { + if (minutes == 0 && rebootingClients.size() > 0) { // Give about 3 minutes for reboot, should be plenty + // Determine online state if either ssh or winrpc/smb is open final int[] ports; if ( this.port == 22 ) { ports = new int[] { 22, 445 }; } else { ports = new int[] { this.port, 22, 445 }; } + // Assume the boot loop takes at least 30 secs, don't even try before that try { Thread.sleep(30000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } - long lastcheck = System.currentTimeMillis(); - long deadline = lastcheck + 120 * 1000; + long lastcheck = 0; + long deadline = System.currentTimeMillis() + 120 * 1000; while ( rebootingClients.size() > 0 && System.currentTimeMillis() < deadline ) { long delay = 10000 - (System.currentTimeMillis() - lastcheck); if ( delay > 0 ) { @@ -155,7 +175,7 @@ public class RemoteReboot extends AbstractTask // change status of clients that got stuck because of timeouts for (Map.Entry entry : status.clientStatus.entrySet()) { ClientStatus value = entry.getValue(); - if (value == ClientStatus.CONNECTING || value == ClientStatus.REBOOTING) { + if (value == ClientStatus.CONNECTING || (minutes == 0 && value == ClientStatus.REBOOTING)) { entry.setValue(ClientStatus.ERROR); } } @@ -163,7 +183,6 @@ public class RemoteReboot extends AbstractTask return true; } - private boolean isOnline(String address, int... ports) { for ( int port : ports ) { @@ -199,7 +218,6 @@ public class RemoteReboot extends AbstractTask } } - @SuppressWarnings( "unused" ) static class Client { @Expose @@ -209,7 +227,7 @@ public class RemoteReboot extends AbstractTask } static enum ClientStatus { - CONNECTING, REBOOTING, SHUTDOWN, SHUTDOWN_AT, ONLINE, ERROR; + CONNECTING, REBOOTING, REBOOT_AT, SHUTDOWN, SHUTDOWN_AT, ONLINE, ERROR, AUTH_FAIL; } } -- cgit v1.2.3-55-g7522