use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(GetHostStatsCommand cmd) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Executing resource GetHostStatsCommand: " + _gson.toJson(cmd));
}
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
HostStatsEntry hostStats = new HostStatsEntry(cmd.getHostId(), 0, 0, 0, "host", 0, 0, 0, 0);
Answer answer = new GetHostStatsAnswer(cmd, hostStats);
try {
HostStatsEntry entry = getHyperHostStats(hyperHost);
if (entry != null) {
entry.setHostId(cmd.getHostId());
answer = new GetHostStatsAnswer(cmd, entry);
}
} catch (Exception e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "Unable to execute GetHostStatsCommand due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
}
if (s_logger.isTraceEnabled()) {
s_logger.trace("GetHostStats Answer: " + _gson.toJson(answer));
}
return answer;
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(RebootCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource RebootCommand: " + _gson.toJson(cmd));
}
boolean toolsInstallerMounted = false;
VirtualMachineMO vmMo = null;
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo != null) {
if (vmMo.isToolsInstallerMounted()) {
toolsInstallerMounted = true;
s_logger.trace("Detected mounted vmware tools installer for :[" + cmd.getVmName() + "]");
}
try {
vmMo.rebootGuest();
return new RebootAnswer(cmd, "reboot succeeded", true);
} catch (ToolsUnavailableFaultMsg e) {
s_logger.warn("VMware tools is not installed at guest OS, we will perform hard reset for reboot");
} catch (Exception e) {
s_logger.warn("We are not able to perform gracefull guest reboot due to " + VmwareHelper.getExceptionMessage(e));
}
// continue to try with hard-reset
if (vmMo.reset()) {
return new RebootAnswer(cmd, "reboot succeeded", true);
}
String msg = "Reboot failed in vSphere. vm: " + cmd.getVmName();
s_logger.warn(msg);
return new RebootAnswer(cmd, msg, false);
} else {
String msg = "Unable to find the VM in vSphere to reboot. vm: " + cmd.getVmName();
s_logger.warn(msg);
return new RebootAnswer(cmd, msg, false);
}
} catch (Exception e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "RebootCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
return new RebootAnswer(cmd, msg, false);
} finally {
if (toolsInstallerMounted) {
try {
vmMo.mountToolsInstaller();
s_logger.debug("Successfully re-mounted vmware tools installer for :[" + cmd.getVmName() + "]");
} catch (Exception e) {
s_logger.warn("Unabled to re-mount vmware tools installer for :[" + cmd.getVmName() + "]");
}
}
}
}
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));
}
}
Aggregations