use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource UnPlugNicCommand " + _gson.toJson(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");
}
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(nic);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure devices when running 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.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
@Override
public Answer execute(DestroyCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource DestroyCommand to evict template from storage pool: " + _gson.toJson(cmd));
}
try {
VmwareContext context = getServiceContext(null);
VmwareHypervisorHost hyperHost = getHyperHost(context, null);
VolumeTO vol = cmd.getVolume();
VirtualMachineMO vmMo = findVmOnDatacenter(context, hyperHost, vol);
if (vmMo != null && vmMo.isTemplate()) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy template volume " + vol.getPath());
}
vmMo.destroy();
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Template volume " + vol.getPath() + " is not found, no need to delete.");
}
}
return new Answer(cmd, true, "Success");
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext(null);
}
String msg = "DestroyCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new Answer(cmd, false, msg);
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method getCurrentStatus.
@Override
public PingCommand getCurrentStatus(long id) {
try {
gcAndKillHungWorkerVMs();
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
if (!hyperHost.isHyperHostConnected()) {
return null;
}
} catch (Exception e) {
s_logger.error("Unexpected exception", e);
return null;
}
return new PingRoutingCommand(getType(), id, syncHostVmStates());
} finally {
recycleServiceContext();
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(PingTestCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource PingTestCommand: " + _gson.toJson(cmd));
}
String controlIp = cmd.getRouterIp();
if (controlIp != null) {
String args = " -c 1 -n -q " + cmd.getPrivateIp();
try {
VmwareManager mgr = getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DefaultDomRSshPort, "root", mgr.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.
protected Answer execute(CheckVirtualMachineCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource CheckVirtualMachineCommand: " + _gson.toJson(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) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
s_logger.error("Unexpected exception: " + VmwareHelper.getExceptionMessage(e), e);
return new CheckVirtualMachineAnswer(cmd, powerState, vncPort);
}
}
Aggregations