use of org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverResponse in project cloudstack by apache.
the class OutOfBandManagementServiceImpl method executeOutOfBandManagementPowerOperation.
@Override
@ActionEvent(eventType = EventTypes.EVENT_HOST_OUTOFBAND_MANAGEMENT_ACTION, eventDescription = "issuing host out-of-band management action", async = true)
public OutOfBandManagementResponse executeOutOfBandManagementPowerOperation(final Host host, final OutOfBandManagement.PowerOperation powerOperation, final Long timeout) {
checkOutOfBandManagementEnabledByZoneClusterHost(host);
final OutOfBandManagement outOfBandManagementConfig = getConfigForHost(host);
final ImmutableMap<OutOfBandManagement.Option, String> options = getOptions(outOfBandManagementConfig);
final OutOfBandManagementDriver driver = getDriver(outOfBandManagementConfig);
Long actionTimeOut = timeout;
if (actionTimeOut == null) {
actionTimeOut = ActionTimeout.valueIn(host.getClusterId());
}
final OutOfBandManagementDriverPowerCommand cmd = new OutOfBandManagementDriverPowerCommand(options, actionTimeOut, powerOperation);
final OutOfBandManagementDriverResponse driverResponse = driver.execute(cmd);
if (driverResponse == null) {
throw new CloudRuntimeException(String.format("Out-of-band Management action (%s) on host (%s) failed due to no response from the driver", powerOperation, host.getUuid()));
}
if (powerOperation.equals(OutOfBandManagement.PowerOperation.STATUS)) {
transitionPowerState(driverResponse.toEvent(), outOfBandManagementConfig);
}
if (!driverResponse.isSuccess()) {
String errorMessage = String.format("Out-of-band Management action (%s) on host (%s) failed with error: %s", powerOperation, host.getUuid(), driverResponse.getError());
if (driverResponse.hasAuthFailure()) {
errorMessage = String.format("Out-of-band Management action (%s) on host (%s) failed due to authentication error: %s. Please check configured credentials.", powerOperation, host.getUuid(), driverResponse.getError());
sendAuthError(host, errorMessage);
}
if (!powerOperation.equals(OutOfBandManagement.PowerOperation.STATUS)) {
LOG.debug(errorMessage);
}
throw new CloudRuntimeException(errorMessage);
}
final OutOfBandManagementResponse response = new OutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId()));
response.setSuccess(driverResponse.isSuccess());
response.setResultDescription(driverResponse.getResult());
response.setId(host.getUuid());
response.setOutOfBandManagementAction(powerOperation.toString());
return response;
}
use of org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverResponse in project cloudstack by apache.
the class OutOfBandManagementServiceImpl method changeOutOfBandManagementPassword.
@Override
@ActionEvent(eventType = EventTypes.EVENT_HOST_OUTOFBAND_MANAGEMENT_CHANGE_PASSWORD, eventDescription = "updating out-of-band management password")
public OutOfBandManagementResponse changeOutOfBandManagementPassword(final Host host, final String newPassword) {
checkOutOfBandManagementEnabledByZoneClusterHost(host);
if (Strings.isNullOrEmpty(newPassword)) {
throw new CloudRuntimeException(String.format("Cannot change out-of-band management password as provided new-password is null or empty for the host %s.", host.getUuid()));
}
final OutOfBandManagement outOfBandManagementConfig = outOfBandManagementDao.findByHost(host.getId());
final ImmutableMap<OutOfBandManagement.Option, String> options = getOptions(outOfBandManagementConfig);
if (!(options.containsKey(OutOfBandManagement.Option.PASSWORD) && !Strings.isNullOrEmpty(options.get(OutOfBandManagement.Option.PASSWORD)))) {
throw new CloudRuntimeException(String.format("Cannot change out-of-band management password as we've no previously configured password for the host %s.", host.getUuid()));
}
final OutOfBandManagementDriver driver = getDriver(outOfBandManagementConfig);
final OutOfBandManagementDriverChangePasswordCommand changePasswordCmd = new OutOfBandManagementDriverChangePasswordCommand(options, ActionTimeout.valueIn(host.getClusterId()), newPassword);
final boolean changePasswordResult = Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
final OutOfBandManagement updatedOutOfBandManagementConfig = outOfBandManagementDao.findByHost(host.getId());
updatedOutOfBandManagementConfig.setPassword(newPassword);
boolean result = outOfBandManagementDao.update(updatedOutOfBandManagementConfig.getId(), (OutOfBandManagementVO) updatedOutOfBandManagementConfig);
if (!result) {
throw new CloudRuntimeException(String.format("Failed to change out-of-band management password for host (%s) in the database.", host.getUuid()));
}
final OutOfBandManagementDriverResponse driverResponse;
try {
driverResponse = driver.execute(changePasswordCmd);
} catch (Exception e) {
LOG.error("Out-of-band management change password failed due to driver error: " + e.getMessage());
throw new CloudRuntimeException(String.format("Failed to change out-of-band management password for host (%s) due to driver error: %s", host.getUuid(), e.getMessage()));
}
if (!driverResponse.isSuccess()) {
throw new CloudRuntimeException(String.format("Failed to change out-of-band management password for host (%s) with error: %s", host.getUuid(), driverResponse.getError()));
}
return result && driverResponse.isSuccess();
}
});
final OutOfBandManagementResponse response = new OutOfBandManagementResponse();
response.setSuccess(changePasswordResult);
response.setId(host.getUuid());
return response;
}
Aggregations