use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareManagerImpl method importVsphereStoragePoliciesInternal.
public List<? extends VsphereStoragePolicy> importVsphereStoragePoliciesInternal(Long zoneId, Long vmwareDcId) {
// Get DC associated with this zone
VmwareDatacenterVO vmwareDatacenter = vmwareDcDao.findById(vmwareDcId);
String vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
String vCenterHost = vmwareDatacenter.getVcenterHost();
String userName = vmwareDatacenter.getUser();
String password = vmwareDatacenter.getPassword();
List<PbmProfile> storageProfiles = null;
try {
s_logger.debug(String.format("Importing vSphere Storage Policies for the vmware DC %d in zone %d", vmwareDcId, zoneId));
VmwareContext context = VmwareContextFactory.getContext(vCenterHost, userName, password);
PbmProfileManagerMO profileManagerMO = new PbmProfileManagerMO(context);
storageProfiles = profileManagerMO.getStorageProfiles();
s_logger.debug(String.format("Import vSphere Storage Policies for the vmware DC %d in zone %d is successful", vmwareDcId, zoneId));
} catch (Exception e) {
String msg = String.format("Unable to list storage profiles from DC %s due to : %s", vmwareDcName, VmwareHelper.getExceptionMessage(e));
s_logger.error(msg);
throw new CloudRuntimeException(msg);
}
for (PbmProfile storageProfile : storageProfiles) {
VsphereStoragePolicyVO storagePolicyVO = vsphereStoragePolicyDao.findByPolicyId(zoneId, storageProfile.getProfileId().getUniqueId());
if (storagePolicyVO == null) {
storagePolicyVO = new VsphereStoragePolicyVO(zoneId, storageProfile.getProfileId().getUniqueId(), storageProfile.getName(), storageProfile.getDescription());
vsphereStoragePolicyDao.persist(storagePolicyVO);
} else {
storagePolicyVO.setDescription(storageProfile.getDescription());
storagePolicyVO.setName(storageProfile.getName());
vsphereStoragePolicyDao.update(storagePolicyVO.getId(), storagePolicyVO);
}
}
List<VsphereStoragePolicyVO> allStoragePolicies = vsphereStoragePolicyDao.listAll();
List<PbmProfile> finalStorageProfiles = storageProfiles;
List<VsphereStoragePolicyVO> needToMarkRemoved = allStoragePolicies.stream().filter(existingPolicy -> !finalStorageProfiles.stream().anyMatch(storageProfile -> storageProfile.getProfileId().getUniqueId().equals(existingPolicy.getPolicyId()))).collect(Collectors.toList());
for (VsphereStoragePolicyVO storagePolicy : needToMarkRemoved) {
vsphereStoragePolicyDao.remove(storagePolicy.getId());
}
List<VsphereStoragePolicyVO> storagePolicies = vsphereStoragePolicyDao.listAll();
return storagePolicies;
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(CheckVirtualMachineCommand cmd) {
final String vmName = cmd.getVmName();
PowerState powerState = PowerState.PowerUnknown;
Integer vncPort = null;
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo != null) {
powerState = getVmPowerState(vmMo);
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
} else {
s_logger.warn("Can not find vm " + vmName + " to execute CheckVirtualMachineCommand");
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
}
} catch (Throwable e) {
createLogMessageException(e, cmd);
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(PingTestCommand cmd) {
String controlIp = cmd.getRouterIp();
if (controlIp != null) {
String args = " -c 1 -n -q " + cmd.getPrivateIp();
try {
Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", getSystemVmKeyFile(), null, "/bin/ping" + args);
if (result.first())
return new Answer(cmd);
} catch (Exception e) {
s_logger.error("Unable to execute ping command on DomR (" + controlIp + "), domR may not be ready yet. failure due to " + VmwareHelper.getExceptionMessage(e), e);
}
return new Answer(cmd, false, "PingTestCommand failed");
} else {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
HostMO hostMo = (HostMO) hyperHost;
ClusterMO clusterMo = new ClusterMO(context, hostMo.getHyperHostCluster());
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
List<Pair<ManagedObjectReference, String>> hosts = clusterMo.getClusterHosts();
for (Pair<ManagedObjectReference, String> entry : hosts) {
HostMO hostInCluster = new HostMO(context, entry.first());
String hostIp = hostInCluster.getHostManagementIp(mgr.getManagementPortGroupName());
if (hostIp != null && hostIp.equals(cmd.getComputingHostIp())) {
if (hostInCluster.isHyperHostConnected())
return new Answer(cmd);
else
return new Answer(cmd, false, "PingTestCommand failed");
}
}
} catch (Exception e) {
s_logger.error("Unable to execute ping command on host (" + cmd.getComputingHostIp() + "). failure due to " + VmwareHelper.getExceptionMessage(e), e);
}
return new Answer(cmd, false, "PingTestCommand failed");
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
private Answer execute(SetupPersistentNetworkCommand cmd) {
VmwareHypervisorHost host = getHyperHost(getServiceContext());
String hostname = null;
VmwareContext context = getServiceContext();
HostMO hostMO = new HostMO(context, host.getMor());
try {
prepareNetworkFromNicInfo(hostMO, cmd.getNic(), false, null);
hostname = host.getHyperHostName();
} catch (Exception e) {
return new SetupPersistentNetworkAnswer(cmd, false, "failed to setup port-group due to: " + e.getLocalizedMessage());
}
return new SetupPersistentNetworkAnswer(cmd, true, hostname);
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
VmwareContext context = getServiceContext();
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
int ramMb = getReservedMemoryMb(vmSpec);
long hotaddIncrementSizeInMb;
long hotaddMemoryLimitInMb;
long requestedMaxMemoryInMb = vmSpec.getMaxRam() / (1024 * 1024);
// Check if VM is really running on hypervisor host
if (getVmPowerState(vmMo) != PowerState.PowerOn) {
throw new CloudRuntimeException("Found that the VM " + vmMo.getVmName() + " is not running. Unable to scale-up this VM");
}
// Check max hot add limit
hotaddIncrementSizeInMb = vmMo.getHotAddMemoryIncrementSizeInMb();
hotaddMemoryLimitInMb = vmMo.getHotAddMemoryLimitInMb();
if (requestedMaxMemoryInMb > hotaddMemoryLimitInMb) {
throw new CloudRuntimeException("Memory of VM " + vmMo.getVmName() + " cannot be scaled to " + requestedMaxMemoryInMb + "MB." + " Requested memory limit is beyond the hotadd memory limit for this VM at the moment is " + hotaddMemoryLimitInMb + "MB.");
}
// Check increment is multiple of increment size
long reminder = hotaddIncrementSizeInMb > 0 ? requestedMaxMemoryInMb % hotaddIncrementSizeInMb : 0;
if (reminder != 0) {
requestedMaxMemoryInMb = requestedMaxMemoryInMb + hotaddIncrementSizeInMb - reminder;
}
// Check if license supports the feature
VmwareHelper.isFeatureLicensed(hyperHost, FeatureKeyConstants.HOTPLUG);
VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), getReservedCpuMHZ(vmSpec), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Unable to execute ScaleVmCommand");
}
} catch (Exception e) {
s_logger.error(String.format("ScaleVmCommand failed due to: [%s].", VmwareHelper.getExceptionMessage(e)), e);
return new ScaleVmAnswer(cmd, false, String.format("Unable to execute ScaleVmCommand due to: [%s].", e.toString()));
}
return new ScaleVmAnswer(cmd, true, null);
}
Aggregations