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 = requestedMaxMemoryInMb % hotaddIncrementSizeInMb;
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(), vmSpec.getMinSpeed(), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Unable to execute ScaleVmCommand");
}
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new ScaleVmAnswer(cmd, false, "Unable to execute ScaleVmCommand due to " + e.toString());
}
return new ScaleVmAnswer(cmd, true, null);
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method gcAndKillHungWorkerVMs.
private void gcAndKillHungWorkerVMs() {
try {
// take the chance to do left-over dummy VM cleanup from previous run
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
if (hyperHost.isHyperHostConnected()) {
mgr.gcLeftOverVMs(context);
s_logger.info("Scan hung worker VM to recycle");
int workerKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER);
int workerTagKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER_TAG);
String workerPropName = String.format("value[%d]", workerKey);
String workerTagPropName = String.format("value[%d]", workerTagKey);
// GC worker that has been running for too long
ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "config.template", workerPropName, workerTagPropName });
if (ocs != null) {
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
boolean template = false;
boolean isWorker = false;
String workerTag = null;
for (DynamicProperty prop : props) {
if (prop.getName().equals("config.template")) {
template = (Boolean) prop.getVal();
} else if (prop.getName().equals(workerPropName)) {
CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
if (val != null && val.getValue() != null && val.getValue().equalsIgnoreCase("true"))
isWorker = true;
} else if (prop.getName().equals(workerTagPropName)) {
CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
workerTag = val.getValue();
}
}
VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
if (!template && isWorker) {
boolean recycle = false;
recycle = mgr.needRecycle(workerTag);
if (recycle) {
s_logger.info("Recycle pending worker VM: " + vmMo.getName());
vmMo.powerOff();
vmMo.detachAllDisks();
vmMo.destroy();
}
}
}
}
}
} else {
s_logger.error("Host is no longer connected.");
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.
@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
String vmName = srcVolume.getVmName();
VmwareContext context = hostService.getServiceContext(cmd);
try {
DataStoreTO primaryStorage = srcVolume.getDataStore();
NfsTO destStore = (NfsTO) destVolume.getDataStore();
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
Pair<String, String> result;
result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0));
VolumeObjectTO newVolume = new VolumeObjectTO();
newVolume.setPath(result.first() + File.separator + result.second());
return new CopyCmdAnswer(newVolume);
} catch (Throwable e) {
if (e instanceof RemoteException) {
hostService.invalidateServiceContext(context);
}
String msg = "Unable to execute CopyVolumeCommand due to exception";
s_logger.error(msg, e);
return new CopyCmdAnswer("copy volume from primary to secondary failed due to exception: " + VmwareHelper.getExceptionMessage(e));
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method removeManagedTargetsFromCluster.
private void removeManagedTargetsFromCluster(List<String> iqns) throws Exception {
List<HostInternetScsiHbaStaticTarget> lstManagedTargets = new ArrayList<HostInternetScsiHbaStaticTarget>();
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO cluster = new ClusterMO(context, morCluster);
List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
HostMO host = new HostMO(context, lstHosts.get(0).first());
HostStorageSystemMO hostStorageSystem = host.getHostStorageSystemMO();
for (HostHostBusAdapter hba : hostStorageSystem.getStorageDeviceInfo().getHostBusAdapter()) {
if (hba instanceof HostInternetScsiHba) {
List<HostInternetScsiHbaStaticTarget> lstTargets = ((HostInternetScsiHba) hba).getConfiguredStaticTarget();
if (lstTargets != null) {
for (HostInternetScsiHbaStaticTarget target : lstTargets) {
if (iqns.contains(target.getIScsiName())) {
lstManagedTargets.add(target);
}
}
}
}
}
addRemoveInternetScsiTargetsToAllHosts(context, false, lstManagedTargets, lstHosts);
rescanAllHosts(context, lstHosts);
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareStorageProcessor method attachIso.
private Answer attachIso(DiskTO disk, boolean isAttach, String vmName) {
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
String storeUrl = null;
if (nfsImageStore != null) {
storeUrl = nfsImageStore.getUrl();
}
if (storeUrl == null) {
if (!iso.getName().equalsIgnoreCase("vmware-tools.iso")) {
String msg = "ISO store root url is not found in AttachIsoCommand";
s_logger.error(msg);
throw new Exception(msg);
} else {
if (isAttach) {
vmMo.mountToolsInstaller();
} else {
try {
if (!vmMo.unmountToolsInstaller()) {
return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
}
} catch (Throwable e) {
vmMo.detachIso(null);
}
}
return new AttachAnswer(disk);
}
}
ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
if (!isoPath.startsWith(storeUrl)) {
assert (false);
String msg = "ISO path does not start with the secondary storage root";
s_logger.error(msg);
throw new Exception(msg);
}
int isoNameStartPos = isoPath.lastIndexOf('/');
String isoFileName = isoPath.substring(isoNameStartPos + 1);
String isoStorePathFromRoot = isoPath.substring(storeUrl.length(), isoNameStartPos);
// TODO, check if iso is already attached, or if there is a previous
// attachment
DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
String storeName = secondaryDsMo.getName();
String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
if (isAttach) {
vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false);
} else {
vmMo.detachIso(isoDatastorePath);
}
return new AttachAnswer(disk);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
if (isAttach) {
String msg = "AttachIsoCommand(attach) failed due to " + VmwareHelper.getExceptionMessage(e);
msg = msg + " Also check if your guest os is a supported version";
s_logger.error(msg, e);
return new AttachAnswer(msg);
} else {
String msg = "AttachIsoCommand(detach) failed due to " + VmwareHelper.getExceptionMessage(e);
msg = msg + " Also check if your guest os is a supported version";
s_logger.warn(msg, e);
return new AttachAnswer(msg);
}
}
}
Aggregations