Search in sources :

Example 11 with VcenterSystemException

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

the class VcenterApiClient method createDatastore.

public String createDatastore(String datacenterName, String clusterNameOrMoRef, String hostname, String volumeUuid, String datastoreName) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    try {
        _log.info("Request to create datastore on volume " + volumeUuid + " host " + hostname + " to datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
        HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get("HostSystem");
        if (volumeUuid == null || volumeUuid.trim().equals("")) {
            _log.error("Volume UUID not specified");
            throw new VcenterSystemException("Volume UUID not specified");
        }
        Datastore[] datastores = hostSystem.getDatastores();
        if (datastores != null && datastores.length > 0) {
            _log.info("Check host " + hostname + " for existing datastore on volume " + volumeUuid);
            String specifiedVolumeDevicePath = null;
            HostStorageSystem hostStorageSystem = hostSystem.getHostStorageSystem();
            HostStorageDeviceInfo hostStorageDeviceInfo = hostStorageSystem.getStorageDeviceInfo();
            ScsiLun[] hostScsiLuns = hostStorageDeviceInfo.getScsiLun();
            for (ScsiLun scsiLun : hostScsiLuns) {
                if (scsiLun instanceof HostScsiDisk) {
                    HostScsiDisk hostScsiDisk = (HostScsiDisk) scsiLun;
                    if (hostScsiDisk.getUuid().toLowerCase().contains(volumeUuid.toLowerCase())) {
                        _log.info("Found disk " + hostScsiDisk.getUuid() + " on " + hostname + " for volume UUID " + volumeUuid);
                        specifiedVolumeDevicePath = hostScsiDisk.getDevicePath();
                        break;
                    }
                }
            }
            // datastore already exists.
            if (specifiedVolumeDevicePath != null) {
                for (Datastore datastore : datastores) {
                    if (datastore.getInfo() instanceof VmfsDatastoreInfo) {
                        VmfsDatastoreInfo vmfsDatastoreInfo = (VmfsDatastoreInfo) datastore.getInfo();
                        _log.info("Found datastore " + vmfsDatastoreInfo.getName() + " " + vmfsDatastoreInfo.getVmfs().getUuid());
                        String diskName = vmfsDatastoreInfo.getVmfs().getExtent()[0].getDiskName();
                        _log.info("Found datastore " + vmfsDatastoreInfo.getName() + " on disk " + diskName);
                        String devicePath = "/vmfs/devices/disks/" + diskName;
                        if (devicePath.equalsIgnoreCase(specifiedVolumeDevicePath)) {
                            _log.info("Datastore " + vmfsDatastoreInfo.getName() + " " + devicePath + " " + datastore.getMOR().getVal() + " already present");
                            return datastore.getMOR().getVal();
                        }
                    }
                }
            }
        }
        _log.info("Search for candidate disk via host " + hostname);
        HostDatastoreSystem hostDatastoreSystem = hostSystem.getHostDatastoreSystem();
        HostScsiDisk[] hostScsiDisks = hostDatastoreSystem.queryAvailableDisksForVmfs(null);
        HostScsiDisk candidateHostScsiDisk = null;
        for (HostScsiDisk hostScsiDisk : hostScsiDisks) {
            _log.info("Found disk " + hostScsiDisk.getDevicePath() + " " + hostScsiDisk.getUuid());
            if (hostScsiDisk.getUuid().toLowerCase().contains(volumeUuid.toLowerCase())) {
                candidateHostScsiDisk = hostScsiDisk;
                break;
            }
        }
        if (candidateHostScsiDisk == null) {
            _log.error("Disk " + volumeUuid + " not found - Ensure underlying storage properly configured and disk accessible to host");
            throw new VcenterSystemException("Disk " + volumeUuid + " not found - Ensure underlying storage properly configured and disk accessible to host");
        }
        String devicePath = candidateHostScsiDisk.getDevicePath();
        _log.info("Create datastore via host " + hostname + " on disk " + devicePath);
        VmfsDatastoreOption[] vmfsDatastoreOption = hostDatastoreSystem.queryVmfsDatastoreCreateOptions(devicePath);
        VmfsDatastoreCreateSpec vmfsDatastoreCreateSpec = (VmfsDatastoreCreateSpec) vmfsDatastoreOption[0].getSpec();
        vmfsDatastoreCreateSpec.getVmfs().setVolumeName(datastoreName);
        // TODO externalize
        vmfsDatastoreCreateSpec.getVmfs().setBlockSizeMb(1);
        Datastore datastore = null;
        try {
            datastore = hostDatastoreSystem.createVmfsDatastore(vmfsDatastoreCreateSpec);
        } catch (HostConfigFault hcf) {
            _log.info("HostConfigFault creating datastore on disk " + devicePath + " thus retry");
            if (hcf.getFaultMessage() != null && hcf.getFaultMessage().length > 0 && hcf.getFaultMessage()[0] != null) {
                String errorMessage = hcf.getFaultMessage()[0].toString();
                _log.error("HostConfigFault details are " + errorMessage);
            }
            datastore = hostDatastoreSystem.createVmfsDatastore(vmfsDatastoreCreateSpec);
        }
        if (datastore == null) {
            // Should not happen
            _log.error("Datastore null after create");
            throw new VcenterSystemException("Error creating datastore");
        }
        return datastore.getMOR().getVal();
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("Exception creating datastore: " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : VmfsDatastoreInfo(com.vmware.vim25.VmfsDatastoreInfo) VmfsDatastoreOption(com.vmware.vim25.VmfsDatastoreOption) VmfsDatastoreCreateSpec(com.vmware.vim25.VmfsDatastoreCreateSpec) ScsiLun(com.vmware.vim25.ScsiLun) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) HostStorageSystem(com.vmware.vim25.mo.HostStorageSystem) HostStorageDeviceInfo(com.vmware.vim25.HostStorageDeviceInfo) 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) Datastore(com.vmware.vim25.mo.Datastore) HostSystem(com.vmware.vim25.mo.HostSystem) HostScsiDisk(com.vmware.vim25.HostScsiDisk) VcenterSystemException(com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException) HostDatastoreSystem(com.vmware.vim25.mo.HostDatastoreSystem) HostConfigFault(com.vmware.vim25.HostConfigFault)

Example 12 with VcenterSystemException

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

the class VcenterApiClient method exitMaintenanceMode.

public void exitMaintenanceMode(String datacenterName, String clusterNameOrMoRef, String hostname) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    try {
        _log.info("Request to exit maintenance mode for host " + hostname + " in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
        exitMaintenanceModeHost((HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get("HostSystem"));
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("exitMaintenanceMode exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) VcenterObjectConnectionException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException) 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 13 with VcenterSystemException

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

the class VcenterApiClient method getVirtualMachines.

private List<VirtualMachine> getVirtualMachines(HostSystem hostSystem, boolean runningOnly) throws VcenterSystemException {
    try {
        String hostname = hostSystem.getName();
        _log.info("Inspect host " + hostname + " for virtual machines running only " + runningOnly);
        List<VirtualMachine> virtualMachines = new ArrayList<VirtualMachine>();
        VirtualMachine[] hostVirtualMachines = hostSystem.getVms();
        for (VirtualMachine virtualMachine : hostVirtualMachines) {
            _log.info("Found virtual machine " + virtualMachine.getName() + " on host " + hostname);
            _log.info(toDetails(virtualMachine));
            if (runningOnly) {
                // Anything !poweredOff (poweredOn and suspended) will be considered running
                if (!virtualMachine.getRuntime().getPowerState().equals(VirtualMachinePowerState.poweredOff)) {
                    virtualMachines.add(virtualMachine);
                }
            } else {
                virtualMachines.add(virtualMachine);
            }
        }
        _log.info("Host " + hostname + " has " + virtualMachines.size() + " virtual machines");
        return virtualMachines;
    } catch (Exception e) {
        _log.error("getVirtualMachines hostSystem exception " + e);
        throw new VcenterSystemException("Error checking host for virtual machines");
    }
}
Also used : ArrayList(java.util.ArrayList) 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) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 14 with VcenterSystemException

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

the class VcenterApiClient method refreshRescanHostStorage.

public void refreshRescanHostStorage(String datacenterName, String clusterNameOrMoRef, String hostname) throws VcenterSystemException, VcenterObjectNotFoundException, VcenterObjectConnectionException {
    try {
        _log.info("Request to check connected and powered on for host " + hostname + " in datacenter " + datacenterName + " cluster " + clusterNameOrMoRef);
        HostSystem hostSystem = (HostSystem) createManagedEntityMap(datacenterName, clusterNameOrMoRef, hostname, true).get("HostSystem");
        _log.info("Refresh and rescan storage before disk discovery on host " + hostname);
        // expensive but needed to pickup any storage changes
        hostSystem.getHostStorageSystem().rescanAllHba();
        _log.info("Rescan HBAs complete");
        hostSystem.getHostStorageSystem().refreshStorageSystem();
        _log.info("Refresh storage system complete");
    } catch (VcenterSystemException | VcenterObjectNotFoundException | VcenterObjectConnectionException e) {
        throw e;
    } catch (Exception e) {
        _log.error("refreshRescanStorage exception " + e);
        throw new VcenterSystemException(e.getLocalizedMessage());
    }
}
Also used : VcenterObjectNotFoundException(com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException) 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 15 with VcenterSystemException

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

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