use of com.vmware.vim25.mo.Task in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method powerOffNoCheck.
private boolean powerOffNoCheck() throws Exception {
ManagedObjectReference morTask = _context.getService().powerOffVM_Task(_mor);
String result = _context.getServiceUtil().waitForTask(morTask);
if (result.equals("sucess")) {
_context.waitForTaskProgressDone(morTask);
// It seems that even if a power-off task is returned done, VM state may still not be marked,
// wait up to 5 seconds to make sure to avoid race conditioning for immediate following on operations
// that relies on a powered-off VM
long startTick = System.currentTimeMillis();
while (getPowerState() != VirtualMachinePowerState.poweredOff && System.currentTimeMillis() - startTick < 5000) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
return true;
} else {
if (getPowerState() == VirtualMachinePowerState.poweredOff) {
// to help deal with possible race-condition
s_logger.info("Current power-off task failed. However, VM has been switched to the state we are expecting for");
return true;
}
s_logger.error("VMware powerOffVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
return false;
}
use of com.vmware.vim25.mo.Task in project cloudstack by apache.
the class VmwareStorageProcessor method rescanAllHosts.
private void rescanAllHosts(VmwareContext context, List<Pair<ManagedObjectReference, String>> lstHosts) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(lstHosts.size());
final List<Exception> exceptions = new ArrayList<Exception>();
for (Pair<ManagedObjectReference, String> hostPair : lstHosts) {
HostMO host = new HostMO(context, hostPair.first());
HostStorageSystemMO hostStorageSystem = host.getHostStorageSystemMO();
boolean iScsiHbaConfigured = false;
for (HostHostBusAdapter hba : hostStorageSystem.getStorageDeviceInfo().getHostBusAdapter()) {
if (hba instanceof HostInternetScsiHba) {
// just finding an instance of HostInternetScsiHba means that we have found at least one configured iSCSI HBA
// at least one iSCSI HBA must be configured before a CloudStack user can use this host for iSCSI storage
iScsiHbaConfigured = true;
final String iScsiHbaDevice = hba.getDevice();
final HostStorageSystemMO hss = hostStorageSystem;
executorService.submit(new Thread() {
@Override
public void run() {
try {
hss.rescanHba(iScsiHbaDevice);
hss.rescanVmfs();
} catch (Exception ex) {
synchronized (exceptions) {
exceptions.add(ex);
}
}
}
});
}
}
if (!iScsiHbaConfigured) {
throw new Exception("An iSCSI HBA must be configured before a host can use iSCSI storage.");
}
}
executorService.shutdown();
if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES)) {
throw new Exception("The system timed out before completing the task 'rescanAllHosts'.");
}
if (exceptions.size() > 0) {
throw new Exception(exceptions.get(0).getMessage());
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterApiClient method reconnectHost.
private void reconnectHost(HostSystem hostSystem) throws VcenterSystemException {
try {
String hostname = hostSystem.getName();
HostSystemConnectionState hostSystemConnectionState = hostSystem.getRuntime().getConnectionState();
if (hostSystemConnectionState == HostSystemConnectionState.disconnected || hostSystemConnectionState == HostSystemConnectionState.notResponding) {
Integer operationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_operation_timeout"));
_log.info("Host " + hostname + " is in a " + hostSystemConnectionState + "state - Attempt to reconnect");
// might need to provide conn info, no arg should use
Task reconnectHostTask = hostSystem.reconnectHost_Task(null);
// 'defaults' guessing means what it was registered originally
// with
// wait
VcenterTaskMonitor.TaskStatus taskStatus = (new VcenterTaskMonitor(operationTimeout)).monitor(reconnectHostTask);
// configured
// timeout
_log.info("After running reconnect task for host " + hostname + " the task result is " + taskStatus + " and host is in connection state " + hostSystem.getRuntime().getConnectionState());
}
} catch (Exception e) {
_log.error("reconnectHost exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterApiClient method exitMaintenanceModeHost.
private void exitMaintenanceModeHost(HostSystem hostSystem) throws VcenterSystemException {
try {
String hostname = hostSystem.getName();
if (hostSystem.getRuntime().isInMaintenanceMode()) {
Integer operationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_operation_timeout"));
_log.info("Host " + hostname + " is in maintenance mode - Attempt to exit");
Task exitMaintenanceModeTask = hostSystem.exitMaintenanceMode(operationTimeout);
VcenterTaskMonitor.TaskStatus taskStatus = (new VcenterTaskMonitor(operationTimeout)).monitor(exitMaintenanceModeTask);
_log.info("After running exit maintenance mode task for host " + hostname + " the task result is " + taskStatus + " and host is in maintenance mode " + hostSystem.getRuntime().isInMaintenanceMode());
if (hostSystem.getRuntime().isInMaintenanceMode()) {
_log.error("Host " + hostname + " failed to exit maintenance mode");
throw new VcenterSystemException("Host " + hostname + " failed to exit maintenance mode");
}
}
} catch (VcenterSystemException e) {
throw e;
} catch (Exception e) {
_log.error("exitMaintenanceMode exception " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.
the class VcenterApiClient method removeHost.
public void removeHost(String datacenterName, String clusterNameOrMoRef, String hostname) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
try {
_log.info("Request to remove host " + hostname + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
ClusterComputeResource clusterComputeResource = (ClusterComputeResource) createManagedEntityMap(datacenterName, clusterNameOrMoRef, null, false).get("ClusterComputeResource");
HostSystem hostSystem = findByHostname(clusterComputeResource, hostname);
if (hostSystem == null) {
_log.info("Host not found thus no delete necessary");
} else {
// Check that host can be removed (either in maintenance mode or in a !poweredOn or !connected state
try {
checkHostConnectedPoweredOn(hostSystem);
_log.info("Host " + hostname + " connected and powered on so now check maintenance mode");
if (!hostSystem.getRuntime().isInMaintenanceMode()) {
_log.error("Host " + hostname + " must be in maintenance mode before deletion");
throw new VcenterSystemException("Host " + hostname + " must be in maintenance mode before deletion");
}
} catch (VcenterObjectConnectionException e) {
_log.info("Host is not connected and/or powered on so go ahead and remove without maintenance mode check");
}
// Remove
Integer hostOperationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_host_operation_timeout"));
VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(hostOperationTimeout);
Task deleteHostTask = hostSystem.destroy_Task();
// call blocks
VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(deleteHostTask);
if (taskStatus == VcenterTaskMonitor.TaskStatus.SUCCESS) {
_log.info("Delete host " + hostname + " task succeeded");
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
String errorMessage = "Delete host " + hostname + " task failed - " + taskMonitor.errorDescription;
_log.error(errorMessage);
throw new VcenterSystemException(errorMessage);
} else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
_log.error("Delete host " + hostname + " task timed out at " + taskMonitor.progressPercent);
throw new VcenterSystemException("Delete host " + hostname + " task timed out at " + taskMonitor.progressPercent);
} else {
// Should not execute - Just here in case someone ever added a new state so we catch it
_log.error("Unknown task status encountered tracking delete host " + taskStatus);
throw new VcenterSystemException("Unknown task status encountered tracking delete host " + taskStatus);
}
}
} catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
throw e;
} catch (Exception e) {
_log.error("Exception removing host: " + e);
throw new VcenterSystemException(e.getLocalizedMessage());
}
}
Aggregations