use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getVmName();
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
}
if (vmMo == null) {
String msg = "VM " + vmName + " no longer exists to execute UnPlugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
String errMsg = "vmware tools not installed or not running, cannot remove nic from vm " + vmName;
s_logger.debug(errMsg);
return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + errMsg);
}
*/
VirtualDevice nic = findVirtualNicDevice(vmMo, cmd.getNic().getMac());
if (nic == null) {
return new UnPlugNicAnswer(cmd, true, "success");
}
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.REMOVE, "unplugNicCommand");
return new UnPlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e.toString());
}
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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.cancelPendingTasks();
vmMo.powerOff();
vmMo.detachAllDisksAndDestroy();
}
}
}
}
}
} 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.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
protected GetVolumeStatsAnswer execute(GetVolumeStatsCommand cmd) {
try {
VmwareHypervisorHost srcHyperHost = getHyperHost(getServiceContext());
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, cmd.getPoolUuid());
assert (morDs != null);
DatastoreMO primaryStorageDatastoreMo = new DatastoreMO(getServiceContext(), morDs);
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dcMo = new DatacenterMO(getServiceContext(), dcMor);
HashMap<String, VolumeStatsEntry> statEntry = new HashMap<String, VolumeStatsEntry>();
for (String chainInfo : cmd.getVolumeUuids()) {
if (chainInfo != null) {
VirtualMachineDiskInfo infoInChain = _gson.fromJson(chainInfo, VirtualMachineDiskInfo.class);
if (infoInChain != null) {
String[] disks = infoInChain.getDiskChain();
if (disks.length > 0) {
for (String diskPath : disks) {
DatastoreFile file = new DatastoreFile(diskPath);
VirtualMachineMO vmMo = dcMo.findVm(file.getDir());
Pair<VirtualDisk, String> vds = vmMo.getDiskDevice(file.getFileName(), true);
long virtualsize = vds.first().getCapacityInKB() * 1024;
long physicalsize = primaryStorageDatastoreMo.fileDiskSize(file.getPath());
if (statEntry.containsKey(chainInfo)) {
VolumeStatsEntry vse = statEntry.get(chainInfo);
if (vse != null) {
vse.setPhysicalSize(vse.getPhysicalSize() + physicalsize);
}
} else {
VolumeStatsEntry vse = new VolumeStatsEntry(chainInfo, physicalsize, virtualsize);
statEntry.put(chainInfo, vse);
}
}
}
}
}
}
s_logger.debug(String.format("Volume Stats Entry is: [%s].", _gson.toJson(statEntry)));
return new GetVolumeStatsAnswer(cmd, "", statEntry);
} catch (Exception e) {
s_logger.error(String.format("VOLSTAT GetVolumeStatsCommand failed due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
}
return new GetVolumeStatsAnswer(cmd, "", null);
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(GetHostStatsCommand 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) {
s_logger.debug(String.format("Host stats response from hypervisor is: [%s].", _gson.toJson(entry)));
entry.setHostId(cmd.getHostId());
answer = new GetHostStatsAnswer(cmd, entry);
}
} catch (Exception e) {
s_logger.error(createLogMessageException(e, cmd), e);
}
if (s_logger.isTraceEnabled()) {
s_logger.trace("GetHostStats Answer: " + _gson.toJson(answer));
}
return answer;
}
use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost in project cloudstack by apache.
the class VmwareStorageProcessor method prepareSecondaryDatastoreOnHost.
private synchronized ManagedObjectReference prepareSecondaryDatastoreOnHost(String storeUrl) throws Exception {
String storeName = getSecondaryDatastoreUUID(storeUrl);
URI uri = new URI(storeUrl);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(hostService.getServiceContext(null), null);
ManagedObjectReference morDatastore = hyperHost.mountDatastore(false, uri.getHost(), 0, uri.getPath(), storeName.replace("-", ""), false);
if (morDatastore == null) {
throw new Exception("Unable to mount secondary storage on host. storeUrl: " + storeUrl);
}
return morDatastore;
}
Aggregations