use of com.iwave.ext.vmware.VCenterAPI in project coprhd-controller by CoprHD.
the class ComputeSystemControllerImpl method unmountAndDetach.
/**
* Unmounts and detaches every datastore and disk associated with the volumes in the export group.
* For each volume in the export group, the backed datastore is unmounted and the associated disk is detached from
* the host.
*
* @param exportGroupId
* export group that contains volumes
* @param hostId
* host to attach and mount to
* @param vcenterId
* vcenter that the host belongs to
* @param vcenterDatacenter
* vcenter datacenter that the host belongs to
* @param stepId
* the id of the workflow step
*/
public void unmountAndDetach(URI exportGroupId, URI hostId, URI vCenterId, URI vcenterDatacenter, String stepId) {
WorkflowStepCompleter.stepExecuting(stepId);
try {
Host esxHost = _dbClient.queryObject(Host.class, hostId);
Vcenter vCenter = _dbClient.queryObject(Vcenter.class, vCenterId);
VcenterDataCenter vCenterDataCenter = _dbClient.queryObject(VcenterDataCenter.class, vcenterDatacenter);
ExportGroup exportGroup = _dbClient.queryObject(ExportGroup.class, exportGroupId);
VCenterAPI api = VcenterDiscoveryAdapter.createVCenterAPI(vCenter);
HostSystem hostSystem = api.findHostSystem(vCenterDataCenter.getLabel(), esxHost.getLabel());
if (hostSystem == null) {
_log.info("Not able to find host " + esxHost.getLabel() + " in vCenter. Unable to validate");
WorkflowStepCompleter.stepSucceded(stepId);
return;
}
HostStorageAPI storageAPI = new HostStorageAPI(hostSystem);
Map<String, Datastore> wwnDatastores = getWwnDatastoreMap(hostSystem);
if (exportGroup != null && exportGroup.getVolumes() != null) {
for (String volume : exportGroup.getVolumes().keySet()) {
BlockObject blockObject = BlockObject.fetch(_dbClient, URI.create(volume));
if (blockObject != null) {
Datastore datastore = getDatastoreByWwn(wwnDatastores, blockObject.getWWN());
if (datastore != null) {
if (VMwareUtils.isDatastoreMountedOnHost(datastore, hostSystem)) {
boolean storageIOControlEnabled = datastore.getIormConfiguration().isEnabled();
if (storageIOControlEnabled) {
setStorageIOControl(api, datastore, false);
}
_log.info("Unmount datastore " + datastore.getName() + " from host " + esxHost.getLabel());
storageAPI.unmountVmfsDatastore(datastore);
if (storageIOControlEnabled) {
setStorageIOControl(api, datastore, true);
}
} else {
_log.info("Datastore " + datastore.getName() + " is not mounted on host " + esxHost.getLabel() + ". Skipping unmounting of datastore.");
}
}
}
// VBDU TODO: COP-28459, If datastore doesn't match, why do we need to run DetachSCSILun() ?
// Test mechanism to invoke a failure. No-op on production systems.
InvokeTestFailure.internalOnlyInvokeTestFailure(InvokeTestFailure.ARTIFICIAL_FAILURE_030);
for (HostScsiDisk entry : storageAPI.listScsiDisks()) {
if (VolumeWWNUtils.wwnMatches(VMwareUtils.getDiskWwn(entry), blockObject.getWWN())) {
if (!VMwareUtils.isDiskOff(entry)) {
_log.info("Detach SCSI Lun " + entry.getCanonicalName() + " from host " + esxHost.getLabel());
storageAPI.detachScsiLun(entry);
} else {
_log.info("SCSI Lun " + entry.getCanonicalName() + " is not in a valid state to detach");
}
}
}
storageAPI.refreshStorage();
// Test mechanism to invoke a failure. No-op on production systems.
InvokeTestFailure.internalOnlyInvokeTestFailure(InvokeTestFailure.ARTIFICIAL_FAILURE_031);
}
}
WorkflowStepCompleter.stepSucceded(stepId);
} catch (Exception ex) {
_log.error(ex.getMessage(), ex);
WorkflowStepCompleter.stepFailed(stepId, DeviceControllerException.errors.jobFailed(ex));
}
}
use of com.iwave.ext.vmware.VCenterAPI in project coprhd-controller by CoprHD.
the class EsxHostDiscoveryAdapter method createVCenterAPI.
/**
* Create helper API instance of VCenter to traverse tree structure of mob.
*
* @param host
* - {@link Host} instance
* @return {@link VCenterAPI}
*/
public static VCenterAPI createVCenterAPI(Host host) {
int port = host.getPortNumber() != null ? host.getPortNumber() : 443;
URL url;
try {
url = new URL("https", host.getHostName(), port, "/sdk");
} catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage());
}
String username = host.getUsername();
String password = host.getPassword();
return new VCenterAPI(url, username, password);
}
use of com.iwave.ext.vmware.VCenterAPI in project coprhd-controller by CoprHD.
the class EsxHostDiscoveryAdapter method discoverHost.
/**
* Discover Esx host
*
* @param host
* {@link Host} instance to be discovered
* @param changes
* {@link HostStateChange} instance containing host changes
* during discovery.
*/
@Override
protected void discoverHost(Host host, HostStateChange changes) {
VCenterAPI api = createVCenterAPI(host);
try {
List<HostSystem> hostSystems = api.listAllHostSystems();
if (null != hostSystems && !hostSystems.isEmpty()) {
// getting the 0th element only coz we are querying an ESX for
// hostsystems and this will always return one or none.
HostSystem hostSystem = hostSystems.get(0);
Host targetHost = null;
HostHardwareInfo hw = hostSystem.getHardware();
String uuid = null;
if (hw != null && hw.systemInfo != null && StringUtils.isNotBlank(hw.systemInfo.uuid)) {
// try finding host by UUID
uuid = hw.systemInfo.uuid;
if (KNOWN_DUPLICATE_UUID.equalsIgnoreCase(uuid)) {
info("Host " + hostSystem.getName() + " contains a known non-unique UUID");
}
// search host by uuid in VIPR if host already discovered
targetHost = findHostByUuid(uuid);
checkDuplicateHost(host, targetHost);
}
if (targetHost == null) {
// if target host is null, this is a new discovery.
targetHost = host;
}
targetHost.setCompatibilityStatus(CompatibilityStatus.COMPATIBLE.name());
targetHost.setDiscoverable(true);
if (targetHost.getId() == null) {
targetHost.setRegistrationStatus(RegistrationStatus.REGISTERED.toString());
}
targetHost.setOsVersion(hostSystem.getConfig().getProduct().getVersion());
if (hw != null && hw.biosInfo != null && StringUtils.isNotBlank(hw.biosInfo.biosVersion)) {
targetHost.setBios(hw.biosInfo.biosVersion);
}
if (null != uuid) {
targetHost.setUuid(uuid);
}
save(targetHost);
DiscoveryStatusUtils.markAsProcessing(getModelClient(), targetHost);
try {
discoverHost(hostSystem, targetHost, changes);
DiscoveryStatusUtils.markAsSucceeded(getModelClient(), targetHost);
} catch (RuntimeException e) {
warn(e, "Problem discovering host %s", targetHost.getLabel());
DiscoveryStatusUtils.markAsFailed(getModelClient(), targetHost, e.getMessage(), e);
}
}
} finally {
api.logout();
}
}
use of com.iwave.ext.vmware.VCenterAPI in project coprhd-controller by CoprHD.
the class EsxHostDiscoveryAdapter method getVersion.
/**
* Get version of host
*
* @param host
* {@link Host} being discovered
* @return
*/
protected EsxVersion getVersion(Host host) {
EsxVersion esxVersion = null;
VCenterAPI api = createVCenterAPI(host);
try {
esxVersion = api.getEsxVersion();
} finally {
api.logout();
}
return esxVersion;
}
use of com.iwave.ext.vmware.VCenterAPI in project coprhd-controller by CoprHD.
the class ComputeUtils method verifyHostInVcenterCluster.
/**
* Validate that the hosts are in their respective cluster. Typically used before
* performing destructive operations, such as decommissioning a host or cluster.
*
* @param hostIds host IDs
* @return false if any host still exists in the vCenter, but is NOT in the cluster assigned to the host in our DB.
*/
public static boolean verifyHostInVcenterCluster(Cluster cluster, List<URI> hostIds) {
// If the cluster isn't returned properly, then something went wrong. We must fail validation.
if (cluster == null || cluster.getInactive()) {
ExecutionUtils.currentContext().logError("The cluster is not active in ViPR DB, therefore we can not proceed with validation.");
return false;
}
// So log it and return.
if (NullColumnValueGetter.isNullURI(cluster.getVcenterDataCenter())) {
ExecutionUtils.currentContext().logInfo("computeutils.decommission.validation.skipped.noVcenterDataCenter", cluster.forDisplay());
return true;
}
VcenterDataCenter dataCenter = execute(new GetVcenterDataCenter(cluster.getVcenterDataCenter()));
// until that's fixed.
if (dataCenter == null || dataCenter.getInactive() || NullColumnValueGetter.isNullURI(dataCenter.getVcenter())) {
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.datacenter", cluster.forDisplay());
return false;
}
Vcenter vcenter = execute(new GetVcenter(dataCenter.getVcenter()));
// until that's fixed.
if (vcenter == null || vcenter.getInactive()) {
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.vcenter", cluster.forDisplay());
return false;
}
VMwareSupport vmware = null;
try {
vmware = new VMwareSupport();
vmware.connect(vcenter.getId());
for (URI hostId : hostIds) {
Host host = BlockStorageUtils.getHost(hostId);
// Do not validate a host no longer in our database
if (host == null || host.getInactive()) {
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host", "N/A", "host not found or inactive");
return false;
}
// in the vCenter cluster, and therefore we can not perform a deep validation.
if (NullColumnValueGetter.isNullURI(host.getVcenterDataCenter())) {
ExecutionUtils.currentContext().logInfo("computeutils.decommission.validation.skipped.vcenternotinhost", host.getHostName());
continue;
}
// any update to the host using the hostService automatically adds this association.
if (!NullColumnValueGetter.isNullURI(host.getVcenterDataCenter()) && host.getType() != null && host.getType().equalsIgnoreCase((Host.HostType.No_OS).name())) {
ExecutionUtils.currentContext().logInfo("computeutils.decommission.validation.skipped.noOShost", host.getHostName());
continue;
}
HostSystem hostSystem = null;
VCenterAPI api = null;
try {
hostSystem = vmware.getHostSystem(dataCenter.getLabel(), host.getHostName(), false);
// we'll need to hunt it down elsewhere.
if (hostSystem == null) {
// Now look for the host system in other datacenters and clusters. If you find it, return false.
// If you do not find it, return true because it couldn't be found.
api = VcenterDiscoveryAdapter.createVCenterAPI(vcenter);
List<HostSystem> hostSystems = api.listAllHostSystems();
if (hostSystems == null || hostSystems.isEmpty()) {
// No host systems were found. We'll assume this is a lie and report a validation failure.
// But the error can be clear that we can not decommission if we're getting empty responses
// from the vSphere API.
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host.nohostsatall", host.getHostName());
return false;
}
for (HostSystem foundHostSystem : hostSystems) {
if (foundHostSystem != null && (foundHostSystem.getName().equalsIgnoreCase(host.getLabel()) || (foundHostSystem.getHardware() != null && foundHostSystem.getHardware().systemInfo != null && foundHostSystem.getHardware().systemInfo.uuid != null && foundHostSystem.getHardware().systemInfo.uuid.equalsIgnoreCase(host.getUuid())))) {
// We found a match someplace else in the vcenter. Post an error and return false.
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host.moved", host.getHostName());
return false;
}
}
// If we get to here, we can't find the host in this vCenter at all and we are going to fail. We don't want to
// delete this host from a vCenter outside of our control.
ExecutionUtils.currentContext().logInfo("computeutils.decommission.failure.host.notinvcenter", host.getHostName());
return false;
} else {
// Make sure the UUID of the host matches what we have in our database.
if (hostSystem.getHardware() != null && hostSystem.getHardware().systemInfo != null && hostSystem.getHardware().systemInfo.uuid != null && !hostSystem.getHardware().systemInfo.uuid.equalsIgnoreCase(host.getUuid())) {
// The host UUID doesn't match what we have in our database. The host may have been renamed.
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host.uuidmismatch", host.getHostName());
return false;
}
// We found the host, so now we check that the host belongs to the correct cluster
if (hostSystem.getParent() != null && hostSystem.getParent() instanceof ClusterComputeResource) {
ClusterComputeResource clusterResource = (ClusterComputeResource) hostSystem.getParent();
if (clusterResource != null && clusterResource.getMOR() != null && clusterResource.getMOR().getVal() != null && !clusterResource.getMOR().getVal().equalsIgnoreCase(cluster.getExternalId())) {
// Host is in a different cluster, fail the validation
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host.moved", host.getHostName());
return false;
}
} else {
// We found the host but it doesn't belong to a cluster, fail the validation
ExecutionUtils.currentContext().logError("computeutils.decommission.failure.host.notincluster", host.getHostName());
return false;
}
}
} finally {
if (api != null) {
api.logout();
}
}
}
} finally {
if (vmware != null) {
vmware.disconnect();
}
}
return true;
}
Aggregations