-
Notifications
You must be signed in to change notification settings - Fork 258
/
SshHostRebootCommand.java
89 lines (79 loc) · 3.31 KB
/
SshHostRebootCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.ovirt.engine.core.bll;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.ovirt.engine.core.bll.context.CommandContext;
import org.ovirt.engine.core.bll.utils.EngineSSHClient;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.VdsActionParameters;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
import org.ovirt.engine.core.common.config.Config;
import org.ovirt.engine.core.common.config.ConfigValues;
@NonTransactiveCommandAttribute
public class SshHostRebootCommand <T extends VdsActionParameters> extends VdsCommand<T> {
public SshHostRebootCommand(T parameters, CommandContext commandContext) {
super(parameters, commandContext);
}
/**
* Try to reboot the host using a clean ssh systemctl method
*/
@Override
protected void executeCommand() {
boolean result = executeSshReboot(getVds().getClusterCompatibilityVersion().toString());
if (result) {
setVdsStatus(VDSStatus.Reboot);
// preserve maintenance status after reboot or set non-responsive for letting the monitor control the host
runSleepOnReboot(getStatusAfterReboot());
}
setSucceeded(result);
}
private VDSStatus getStatusAfterReboot() {
return getParameters().getPrevVdsStatus() == VDSStatus.Maintenance
? VDSStatus.Maintenance
: VDSStatus.NonResponsive;
}
/**
* Executes SSH reboot command
*
* @return {@code true} if command has been executed successfully, {@code false} otherwise
*/
private boolean executeSshReboot(String version) {
try (
final EngineSSHClient sshClient = new EngineSSHClient();
final ByteArrayOutputStream cmdOut = new ByteArrayOutputStream();
final ByteArrayOutputStream cmdErr = new ByteArrayOutputStream()
) {
try {
log.info("Opening SSH reboot session on host {}", getVds().getHostName());
sshClient.setVds(getVds());
sshClient.useDefaultKeyPair();
sshClient.connect();
sshClient.authenticate();
log.info("Executing SSH reboot command on host {}", getVds().getHostName());
sshClient.executeCommand(
Config.getValue(ConfigValues.SshHostRebootCommand, version),
null,
cmdOut,
cmdErr
);
return true;
} catch (Exception ex) {
log.error("SSH reboot command failed on host '{}': {}\nStdout: {}\nStderr: {}",
getVds().getHostName(),
ex.getMessage(),
cmdOut,
cmdErr);
log.debug("Exception", ex);
}
} catch (IOException e) {
log.error("Error opening SSH connection to '{}': {}",
getVds().getHostName(),
e.getMessage());
log.debug("Exception", e);
}
return false;
}
@Override
public AuditLogType getAuditLogTypeValue() {
return getSucceeded() ? AuditLogType.SYSTEM_SSH_HOST_RESTART : AuditLogType.SYSTEM_FAILED_SSH_HOST_RESTART;
}
}