servermanager/src/main/java/de/gurkengewuerz/monitoring/object/MassCommand.java

129 lines
4.2 KiB
Java

package de.gurkengewuerz.monitoring.object;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
import de.gurkengewuerz.monitoring.GUI;
import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.List;
/**
* Created by gurkengewuerz.de on 22.10.2017.
*/
public class MassCommand {
private String command;
private PrivateKey private_key;
private List<Server> serverList = new ArrayList<>();
private StatusReply answer;
private boolean stop = false;
private Thread t;
public MassCommand(String command, PrivateKey private_key, List<Server> serverList, StatusReply answer) {
this.command = command;
this.private_key = private_key;
this.serverList = serverList;
this.answer = answer;
}
public MassCommand(String command, List<Server> serverList, StatusReply answer) {
this(command, new PrivateKey(""), serverList, answer);
}
public static void run(String command, MultiWindowTextGUI gui, List<Server> serverarray, PrivateKey ssh_private) {
Label label = new Label("[ ] 00%");
Button button = new Button("Abbruch");
BasicWindow windowStatus = GUI.getCallbackWindow(label, button);
gui.addWindow(windowStatus);
gui.setActiveWindow(windowStatus);
MassCommand mc = new MassCommand(command, serverarray, new StatusReply() {
@Override
public void onFinish() {
windowStatus.close();
MessageDialog.showMessageDialog(gui, "Erfolgreich", "Befehle erfolgreich auf allen Servern ausgeführt", MessageDialogButton.OK);
}
@Override
public void onStart() {
}
@Override
public void onUpdate(int current, int to_go) {
double perc = (double) current / (double) to_go;
double d = Math.round(perc * 20) / 20.0; // round up to multiple of 0.05
int percInt = (int) (d * 100);
int count = percInt / 5;
StringBuilder stringtext = new StringBuilder("[");
for (int i = 0; i < 20; i++) {
if (i + 1 <= count) {
stringtext.append("#");
} else {
stringtext.append(" ");
}
}
stringtext.append("] ").append(percInt).append("%");
label.setText(stringtext.toString());
}
@Override
public void onFail(String message) {
windowStatus.close();
MessageDialog.showMessageDialog(gui, "ERROR!", "Verbindung zum Server konnte nicht hergestellt werden!\n" + message, MessageDialogButton.Close);
}
}).setPrivateKey(ssh_private);
mc.start();
button.addListener(button1 -> mc.stop());
button.takeFocus();
}
public MassCommand setPrivateKey(PrivateKey private_key) {
this.private_key = private_key;
return this;
}
public void start() {
t = new Thread(() -> {
final boolean[] failed = {false};
final int[] counter = {0};
answer.onStart();
serverList.forEach(server -> {
if (stop) return;
failed[0] = false;
SSHManager manager = server.getSSHSession(private_key);
if (manager.hasError()) {
failed[0] = true;
answer.onFail(manager.getErrorMessage());
return;
}
if (failed[0]) return;
String result = manager.sendCommand(command);
Logger.info(result);
manager.close();
answer.onUpdate(counter[0] + 1, serverList.size());
counter[0]++;
});
if (!failed[0]) answer.onFinish();
});
t.start();
}
public void stop() {
stop = true;
t.interrupt();
}
}