Search in sources :

Example 16 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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());
    }
}
Also used : Task(com.vmware.vim25.mo.Task) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Example 17 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException 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());
    }
}
Also used : Task(com.vmware.vim25.mo.Task) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) HostSystem(com.vmware.vim25.mo.HostSystem) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Example 18 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException in project coprhd-controller by CoprHD.

the class VcenterApiClient method enterMaintenanceModeHost.

private void enterMaintenanceModeHost(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 not in maintenance mode - Attempt to enter");
            Task enterMaintenanceModeTask = hostSystem.enterMaintenanceMode(operationTimeout, true);
            VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(operationTimeout);
            VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(enterMaintenanceModeTask);
            _log.info("After running enter 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 enter maintenance mode");
                String message = "Host " + hostname + " failed to enter maintenance mode. " + "Safety check for running virtual machines failed. " + "Vcenter was either unable to migrate them or ViPR could not verify VMs are not running.";
                if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
                    throw new VcenterSystemException(message + " Vcenter reported: " + taskMonitor.errorDescription);
                } else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
                    throw new VcenterSystemException(message + " Task timed out.");
                } else {
                    // task success but host did not enter maint mode - probably will not happen
                    throw new VcenterSystemException(message);
                }
            }
        }
    } catch (VcenterSystemException e) {
        throw e;
    } catch (Exception e) {
        _log.error("enterMaintenanceMode exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : Task(com.vmware.vim25.mo.Task) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)

Example 19 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException in project coprhd-controller by CoprHD.

the class VcenterApiClient method trackHostTasks.

private void trackHostTasks(HostSystem hostSystem, Integer timeout) throws VcenterSystemException {
    try {
        // Track and wait for other tasks, ie HA, initiated by the add before progressing
        String hostname = hostSystem.getName();
        int taskStartupWait = 5 * 1000;
        _log.info("Wait " + taskStartupWait + " seconds for any tasks to run against host before monitoring");
        // wait for post register tasks to begin
        Thread.sleep(taskStartupWait);
        Task[] tasks = hostSystem.getRecentTasks() == null ? new Task[0] : hostSystem.getRecentTasks();
        _log.info("Host " + hostname + " has " + tasks.length + " tasks currently running or queued to run");
        if (tasks.length > 0) {
            // stateful since this instance used for ALL tasks -
            VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(timeout);
            // timeout is basically for ALL tasks to finish in
            for (Task task : tasks) {
                TaskInfo taskInfo = task.getTaskInfo();
                _log.info("Begin waiting on task " + taskInfo.getName() + " " + taskInfo.getDescriptionId());
                // call blocks until task reaches terminal state or timeout is met
                taskMonitor.waitForTask(task);
                _log.info("Quit waiting for task " + taskInfo.getName() + " " + taskInfo.getDescriptionId() + " in state " + taskInfo.getState());
            }
            _log.info("Done waiting for tasks for host " + hostname);
            for (Task task : tasks) {
                TaskInfo taskInfo = task.getTaskInfo();
                _log.info("Host " + hostname + " task " + taskInfo.getName() + " " + taskInfo.getDescriptionId() + " in state " + taskInfo.getState());
            }
        }
    } catch (Exception e) {
        _log.error("trackHostTasks exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) Task(com.vmware.vim25.mo.Task) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)

Example 20 with VcenterSystemException

use of com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException in project coprhd-controller by CoprHD.

the class VcenterApiClient method addHost.

public String addHost(String datacenterName, String clusterNameOrMoRef, String hostname, String username, String password) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    try {
        _log.info("Request to add 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 " + hostname + " does not exist and will be added");
            if (username == null || username.trim().equals("") || password == null || password.trim().equals("")) {
                _log.error("Username and/or password missing - Both required to add host to cluster");
                throw new VcenterSystemException("Username and/or password missing - Both required to add host to cluster");
            }
            HostConnectSpec hostConnectSpec = new HostConnectSpec();
            hostConnectSpec.setHostName(hostname);
            hostConnectSpec.setUserName(username);
            hostConnectSpec.setPassword(password);
            // ie
            hostConnectSpec.setSslThumbprint(getHostCertificate(hostname, username, password));
            // 1D:0C:63:FC:58:58:1C:66:F0:5B:C4:0B:F3:84:0E:27:E9:59:83:F7
            _log.info("Attempt to add host " + hostname + " to " + datacenterName + "/" + clusterComputeResource.getName());
            Integer hostOperationTimeout = Integer.parseInt(propertyInfo.getProperty("vcenter_host_operation_timeout"));
            Integer hostOperationTimeoutMillis = hostOperationTimeout * 1000;
            Integer retryCount = 1;
            Long startTimeMillis = System.currentTimeMillis();
            Long cutoffTimeMillis = startTimeMillis + hostOperationTimeoutMillis;
            VcenterTaskMonitor taskMonitor = new VcenterTaskMonitor(hostOperationTimeout);
            Task addHostTask = clusterComputeResource.addHost_Task(hostConnectSpec, true, null, null);
            // call blocks
            VcenterTaskMonitor.TaskStatus taskStatus = taskMonitor.monitor(addHostTask);
            while ((System.currentTimeMillis() < cutoffTimeMillis) && taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
                _log.info("Add host " + hostname + " retry error " + taskMonitor.errorDescription + " count " + retryCount);
                // Retry is time based and if each retry executes very quickly (ie milliseconds) then we should
                Thread.sleep(60000);
                // throttle and only retry every 60 seconds
                addHostTask = clusterComputeResource.addHost_Task(hostConnectSpec, true, null, null);
                // call blocks
                taskStatus = taskMonitor.monitor(addHostTask);
                retryCount++;
            }
            if (taskStatus == VcenterTaskMonitor.TaskStatus.SUCCESS) {
                _log.info("Add host " + hostname + " task succeeded - Attempt to find host in cluster");
                hostSystem = findByHostname(clusterComputeResource, hostname);
            } else if (taskStatus == VcenterTaskMonitor.TaskStatus.ERROR) {
                String errorMessage = "Add host " + hostname + " task failed - " + taskMonitor.errorDescription;
                if (taskMonitor.errorDescription.contains("already exists.")) {
                    errorMessage += " - Ensure adding host to correct cluster or remove host from its existing cluster";
                }
                _log.error(errorMessage);
                throw new VcenterSystemException(errorMessage);
            } else if (taskStatus == VcenterTaskMonitor.TaskStatus.TIMED_OUT) {
                _log.error("Add host " + hostname + " task timed out at " + taskMonitor.progressPercent);
                throw new VcenterSystemException("Add 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 add host " + taskStatus);
                throw new VcenterSystemException("Unknown task status encountered tracking add host " + taskStatus);
            }
            trackHostTasks(hostSystem, hostOperationTimeout);
            // Only take host out of maintenance mode if it's being added. We don't want to exit maintenance mode on other hosts since
            // customer may have intentionally put it on that.
            exitMaintenanceModeHost(hostSystem);
        }
        // Some nice conveniences to reconnect and exit maintenance mode to ready the host for action
        reconnectHost(hostSystem);
        // Collect some details
        StringBuffer hostDetails = new StringBuffer();
        hostDetails.append("Host ").append(datacenterName).append("/").append(clusterComputeResource.getName()).append("/").append(hostname).append(" ");
        String os = hostSystem.getPropertyByPath("config.product.version").toString();
        hostDetails.append("OS ").append(os).append(" ");
        String key = hostSystem.getMOR().getVal();
        hostDetails.append("key ").append(key).append(" ");
        String connectionState = hostSystem.getRuntime().getConnectionState().toString();
        hostDetails.append("Connection State ").append(connectionState).append(" ");
        String powerState = hostSystem.getRuntime().getPowerState().toString();
        hostDetails.append("Power State ").append(powerState).append(" ");
        boolean maintenanceMode = hostSystem.getRuntime().isInMaintenanceMode();
        hostDetails.append("Maintenance Mode ").append(maintenanceMode).append(" ");
        _log.info(hostDetails.toString());
        return hostSystem.getMOR().getVal();
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("Exception adding host: " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : Task(com.vmware.vim25.mo.Task) ClusterComputeResource(com.vmware.vim25.mo.ClusterComputeResource) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) VcenterServerConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) HostSystem(com.vmware.vim25.mo.HostSystem) HostConnectSpec(com.vmware.vim25.HostConnectSpec) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)

Aggregations

VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)21 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)21 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)21 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)21 HostSystem (com.vmware.vim25.mo.HostSystem)8 ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)7 Task (com.vmware.vim25.mo.Task)7 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)4 Datacenter (com.vmware.vim25.mo.Datacenter)3 InventoryNavigator (com.vmware.vim25.mo.InventoryNavigator)3 ArrayList (java.util.ArrayList)3 HostScsiDisk (com.vmware.vim25.HostScsiDisk)2 HostStorageDeviceInfo (com.vmware.vim25.HostStorageDeviceInfo)2 ScsiLun (com.vmware.vim25.ScsiLun)2 VmfsDatastoreInfo (com.vmware.vim25.VmfsDatastoreInfo)2 Datastore (com.vmware.vim25.mo.Datastore)2 HostStorageSystem (com.vmware.vim25.mo.HostStorageSystem)2 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)2 ClusterConfigSpecEx (com.vmware.vim25.ClusterConfigSpecEx)1 HostConfigFault (com.vmware.vim25.HostConfigFault)1