summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java87
1 files changed, 54 insertions, 33 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
index 7e1b228..a166231 100644
--- a/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
+++ b/src/main/java/org/openslx/taskmanager/tasks/RemoteReboot.java
@@ -1,13 +1,11 @@
package org.openslx.taskmanager.tasks;
import java.io.IOException;
+import java.net.InetSocketAddress;
import java.net.Socket;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -18,20 +16,20 @@ import java.util.concurrent.TimeUnit;
import org.openslx.taskmanager.api.AbstractTask;
-import com.jcabi.ssh.Shell;
import com.google.gson.annotations.Expose;
import com.jcabi.ssh.SSH;
+import com.jcabi.ssh.Shell;
public class RemoteReboot extends AbstractTask
{
@Expose
- private HashMap<String, String>[] clients;
+ private Client[] clients;
@Expose
- private boolean shutdown = false;
+ private boolean shutdown;
@Expose
- private int minutes = 0;
+ private int minutes;
@Expose
private String locationId;
@@ -43,7 +41,7 @@ public class RemoteReboot extends AbstractTask
private String sshkey;
@Expose
- private int port = 22;
+ private int port;
private Output status = new Output();
@@ -64,24 +62,27 @@ public class RemoteReboot extends AbstractTask
@Override
protected boolean execute()
{
+ if ( clients.length == 0 )
+ return true;
// try to connect to every client and start the reboot/shutdown process
ExecutorService tp = Executors.newFixedThreadPool( clients.length > 4 ? 4 : clients.length );
- for (HashMap<String, String> client : clients) {
- final String ip = client.get("ip");
- status.clientStatus.put(ip, "connecting");
+ for (final Client client : clients) {
+ if ( client == null || client.clientip == null )
+ continue;
+ status.clientStatus.put(client.clientip, ClientStatus.CONNECTING);
tp.submit(new Runnable() {
public void run() {
try {
- Shell shell = new SSH(ip, port, "root", sshkey);
+ Shell shell = new SSH(client.clientip, port, "root", sshkey);
if (shutdown) {
new Shell.Empty(shell).exec("/sbin/shutdown +" + minutes);
- status.clientStatus.put(ip, minutes == 0 ? "shutdown" : "shutdownat");
+ status.clientStatus.put(client.clientip, minutes == 0 ? ClientStatus.SHUTDOWN : ClientStatus.SHUTDOWN_AT);
} else {
new Shell.Empty(shell).exec("/sbin/reboot");
- status.clientStatus.put(ip, "rebooting");
+ status.clientStatus.put(client.clientip, ClientStatus.REBOOTING);
}
} catch (IOException e) {
- status.clientStatus.put(ip, "error");
+ status.clientStatus.put(client.clientip, ClientStatus.ERROR);
}
}
});
@@ -91,13 +92,14 @@ public class RemoteReboot extends AbstractTask
try {
tp.awaitTermination( clients.length * 5, TimeUnit.SECONDS );
} catch ( InterruptedException e ) {
- // ...
+ Thread.currentThread().interrupt();
+ return false;
}
// wait for rebooting clients to finish rebooting
- ArrayList<String> rebootingClients = new ArrayList<String>();
- for (Map.Entry<String, String> entry : status.clientStatus.entrySet()) {
- if (entry.getValue() == "rebooting") {
+ List<String> rebootingClients = new ArrayList<>();
+ for (Entry<String, ClientStatus> entry : status.clientStatus.entrySet()) {
+ if (entry.getValue() == ClientStatus.REBOOTING) {
rebootingClients.add(entry.getKey());
}
}
@@ -111,9 +113,11 @@ public class RemoteReboot extends AbstractTask
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return;
}
}
- status.clientStatus.put(ip, "online");
+ status.clientStatus.put(ip, ClientStatus.ONLINE);
}
});
}
@@ -122,15 +126,16 @@ public class RemoteReboot extends AbstractTask
try {
statusTP.awaitTermination( 180, TimeUnit.SECONDS );
} catch ( InterruptedException e ) {
- // ...
+ Thread.currentThread().interrupt();
+ return false;
}
}
// change status of clients that got stuck because of timeouts
- for (Map.Entry<String, String> entry : status.clientStatus.entrySet()) {
- String value = entry.getValue();
- if (value == "connecting" || value == "rebooting") {
- entry.setValue("error");
+ for (Map.Entry<String, ClientStatus> entry : status.clientStatus.entrySet()) {
+ ClientStatus value = entry.getValue();
+ if (value == ClientStatus.CONNECTING || value == ClientStatus.REBOOTING) {
+ entry.setValue(ClientStatus.ERROR);
}
}
@@ -140,23 +145,39 @@ public class RemoteReboot extends AbstractTask
private boolean isOnline(String address)
{
- try (Socket s = new Socket(address, port)) {
- return true;
- } catch (IOException ex) {
- }
- return false;
+ try ( Socket s = new Socket() ) {
+ s.connect( new InetSocketAddress( address, port ), 3000 );
+ return true;
+ } catch ( Exception ex ) {
+ }
+ return false;
}
/**
* Output - contains additional status data of this task
*/
- class Output
+ @SuppressWarnings( "unused" )
+ static class Output
{
- private ConcurrentHashMap<String, String> clientStatus = new ConcurrentHashMap<String, String>();
- private HashMap<String, String>[] clients;
+ private Map<String, ClientStatus> clientStatus = new ConcurrentHashMap<>();
+ private Client[] clients;
private String time;
private String locationId;
private String locationName;
}
+
+ @SuppressWarnings( "unused" )
+ static class Client
+ {
+ @Expose
+ private String machineuuid;
+ @Expose
+ private String clientip;
+ }
+
+ static enum ClientStatus {
+ CONNECTING, REBOOTING, SHUTDOWN, SHUTDOWN_AT, ONLINE, ERROR;
+ }
+
}