diff options
author | Simon Rettberg | 2017-04-11 15:13:51 +0200 |
---|---|---|
committer | Simon Rettberg | 2017-04-11 15:13:51 +0200 |
commit | 10a74e73d4c0ce57ffa5440621cd794e5478bbed (patch) | |
tree | 2f85875bb4aef6c12c557726c9a23bdad8c0f8fa | |
parent | [RemoteReboot] Use idleaction scripts, more error handling/logging (diff) | |
download | tmlite-bwlp-10a74e73d4c0ce57ffa5440621cd794e5478bbed.tar.gz tmlite-bwlp-10a74e73d4c0ce57ffa5440621cd794e5478bbed.tar.xz tmlite-bwlp-10a74e73d4c0ce57ffa5440621cd794e5478bbed.zip |
[RemoteReboot] Apply code formatting
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java | 127 |
1 files changed, 68 insertions, 59 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java index f6ce4a7..041daaa 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java +++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java @@ -24,31 +24,31 @@ public class RemoteReboot extends AbstractTask { @Expose private Client[] clients; - + @Expose private boolean shutdown; - + @Expose private int minutes; - + @Expose private String locationId; - + @Expose private String locationName; - + @Expose private String sshkey; - + @Expose private int port; - + 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() { @@ -68,9 +68,9 @@ public class RemoteReboot extends AbstractTask return false; status.clients = clients; - Date shutdownTime = new Date(System.currentTimeMillis()+minutes*60*1000); - SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); - status.time = sdf.format(shutdownTime); + Date shutdownTime = new Date( System.currentTimeMillis() + minutes * 60 * 1000 ); + SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm" ); + status.time = sdf.format( shutdownTime ); status.locationId = locationId; status.locationName = locationName; return true; @@ -84,55 +84,56 @@ public class RemoteReboot extends AbstractTask final List<Client> rebootingClients = new ArrayList<>(); // try to connect to every client and start the reboot/shutdown process ExecutorService tp = Executors.newFixedThreadPool( clients.length > 4 ? 4 : clients.length ); - for (final Client client : clients) { + for ( final Client client : clients ) { if ( client == null || client.clientip == null || client.machineuuid == null ) { status.addError( "null Client or missing ip/uuid in list, ignoring." ); continue; } - status.clientStatus.put(client.machineuuid, ClientStatus.CONNECTING); - tp.submit(new Runnable() { - public void run() { - int ret = -1; + 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) { - ret = new Shell.Empty(shell).exec(SHUTDOWN_CMD + " " + minutes); - status.clientStatus.put(client.machineuuid, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT); + Shell shell = new SSH( client.clientip, port, "root", sshkey ); + if ( shutdown ) { + ret = new Shell.Empty( shell ).exec( SHUTDOWN_CMD + " " + minutes ); + status.clientStatus.put( client.machineuuid, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT ); } else { - 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 ); + 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); + } 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) { + if ( ret != 0 ) { + if ( ret != -1 ) { status.addError( client.clientip + ": Exit Code " + ret ); } - status.clientStatus.put(client.machineuuid, ClientStatus.ERROR); + status.clientStatus.put( client.machineuuid, ClientStatus.ERROR ); } } - }); + } ); } tp.shutdown(); - + try { tp.awaitTermination( clients.length * 5, TimeUnit.SECONDS ); } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); return false; } - - if (minutes == 0 && 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; @@ -143,47 +144,47 @@ public class RemoteReboot extends AbstractTask } // Assume the boot loop takes at least 30 secs, don't even try before that try { - Thread.sleep(30000); - } catch (InterruptedException e) { + Thread.sleep( 30000 ); + } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); return false; } long lastcheck = 0; long deadline = System.currentTimeMillis() + 120 * 1000; while ( rebootingClients.size() > 0 && System.currentTimeMillis() < deadline ) { - long delay = 10000 - (System.currentTimeMillis() - lastcheck); + long delay = 10000 - ( System.currentTimeMillis() - lastcheck ); if ( delay > 0 ) { try { - Thread.sleep(delay); - } catch (InterruptedException e) { + Thread.sleep( delay ); + } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); return false; } } lastcheck = System.currentTimeMillis(); Iterator<Client> it = rebootingClients.iterator(); - while (it.hasNext()) { + while ( it.hasNext() ) { Client client = it.next(); - if (isOnline(client.clientip, ports)) { + if ( isOnline( client.clientip, ports ) ) { it.remove(); - status.clientStatus.put(client.machineuuid, ClientStatus.ONLINE); + status.clientStatus.put( client.machineuuid, ClientStatus.ONLINE ); } } } } - + // change status of clients that got stuck because of timeouts - for (Map.Entry<String, ClientStatus> entry : status.clientStatus.entrySet()) { + for ( Map.Entry<String, ClientStatus> entry : status.clientStatus.entrySet() ) { ClientStatus value = entry.getValue(); - if (value == ClientStatus.CONNECTING || (minutes == 0 && value == ClientStatus.REBOOTING)) { - entry.setValue(ClientStatus.ERROR); - } + if ( value == ClientStatus.CONNECTING || ( minutes == 0 && value == ClientStatus.REBOOTING ) ) { + entry.setValue( ClientStatus.ERROR ); + } } - + return true; } - - private boolean isOnline(String address, int... ports) + + private boolean isOnline( String address, int... ports ) { for ( int port : ports ) { try ( Socket s = new Socket() ) { @@ -194,8 +195,7 @@ public class RemoteReboot extends AbstractTask } return false; } - - + /** * Output - contains additional status data of this task */ @@ -208,6 +208,7 @@ public class RemoteReboot extends AbstractTask private String locationId; private String locationName; private String error; + private void addError( String e ) { if ( error == null ) { @@ -217,7 +218,7 @@ public class RemoteReboot extends AbstractTask } } } - + static class Client { @Expose @@ -225,9 +226,17 @@ public class RemoteReboot extends AbstractTask @Expose private String clientip; } - - static enum ClientStatus { - CONNECTING, REBOOTING, REBOOT_AT, SHUTDOWN, SHUTDOWN_AT, ONLINE, ERROR, AUTH_FAIL; + + static enum ClientStatus + { + CONNECTING, + REBOOTING, + REBOOT_AT, + SHUTDOWN, + SHUTDOWN_AT, + ONLINE, + ERROR, + AUTH_FAIL; } - + } |