use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method initialize.
@Override
public StartupCommand[] initialize() {
try {
String hostApiVersion = "4.1";
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
assert (hyperHost instanceof HostMO);
if (!((HostMO) hyperHost).isHyperHostConnected()) {
s_logger.info("Host " + hyperHost.getHyperHostName() + " is not in connected state");
return null;
}
((HostMO) hyperHost).enableVncOnHostFirewall();
AboutInfo aboutInfo = ((HostMO) hyperHost).getHostAboutInfo();
hostApiVersion = aboutInfo.getApiVersion();
} catch (Exception e) {
String msg = "VmwareResource intialize() failed due to : " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
invalidateServiceContext();
return null;
}
StartupRoutingCommand cmd = new StartupRoutingCommand();
fillHostInfo(cmd);
cmd.setHypervisorType(HypervisorType.VMware);
cmd.setCluster(_cluster);
cmd.setHypervisorVersion(hostApiVersion);
List<StartupStorageCommand> storageCmds = initializeLocalStorage();
StartupCommand[] answerCmds = new StartupCommand[1 + storageCmds.size()];
answerCmds[0] = cmd;
for (int i = 0; i < storageCmds.size(); i++) {
answerCmds[i + 1] = storageCmds.get(i);
}
return answerCmds;
} finally {
recycleServiceContext();
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(GetVmIpAddressCommand cmd) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("Executing resource command GetVmIpAddressCommand: " + _gson.toJson(cmd));
}
String details = "Unable to find IP Address of VM. ";
String vmName = cmd.getVmName();
boolean result = false;
String ip = null;
Answer answer = null;
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
if (vmName == null || vmName.isEmpty()) {
details += "Name of instance provided is NULL or empty.";
return new Answer(cmd, result, details);
}
try {
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo != null) {
GuestInfo guestInfo = vmMo.getGuestInfo();
VirtualMachineToolsStatus toolsStatus = guestInfo.getToolsStatus();
if (toolsStatus == VirtualMachineToolsStatus.TOOLS_NOT_INSTALLED) {
details += "Vmware tools not installed.";
} else {
ip = guestInfo.getIpAddress();
if (ip != null) {
result = true;
}
details = ip;
}
} else {
details += "VM " + vmName + " no longer exists on vSphere host: " + hyperHost.getHyperHostName();
s_logger.info(details);
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
details += "Encountered exception : " + VmwareHelper.getExceptionMessage(e);
s_logger.error(details);
}
answer = new Answer(cmd, result, details);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Returning GetVmIpAddressAnswer: " + _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(CreatePrivateTemplateFromVolumeCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd));
}
try {
VmwareContext context = getServiceContext();
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
return mgr.getStorageManager().execute(this, cmd);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String details = "CreatePrivateTemplateFromVolumeCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(details, e);
return new CreatePrivateTemplateAnswer(cmd, false, details);
}
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method initializeLocalStorage.
private List<StartupStorageCommand> initializeLocalStorage() {
List<StartupStorageCommand> storageCmds = new ArrayList<StartupStorageCommand>();
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
if (hyperHost instanceof HostMO) {
HostMO hostMo = (HostMO) hyperHost;
List<Pair<ManagedObjectReference, String>> dsList = hostMo.getLocalDatastoreOnHost();
for (Pair<ManagedObjectReference, String> dsPair : dsList) {
DatastoreMO dsMo = new DatastoreMO(context, dsPair.first());
String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
if (poolUuid == null || poolUuid.isEmpty()) {
poolUuid = UUID.randomUUID().toString();
dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid);
}
DatastoreSummary dsSummary = dsMo.getSummary();
String address = hostMo.getHostName();
StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().getValue(), "", StoragePoolType.VMFS, dsSummary.getCapacity(), dsSummary.getFreeSpace());
StartupStorageCommand cmd = new StartupStorageCommand();
cmd.setName(poolUuid);
cmd.setPoolInfo(pInfo);
// give storage host the same UUID as the local storage pool itself
cmd.setGuid(poolUuid);
cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
cmd.setDataCenter(_dcId);
cmd.setPod(_pod);
cmd.setCluster(_cluster);
s_logger.info("Add local storage startup command: " + _gson.toJson(cmd));
storageCmds.add(cmd);
}
} else {
s_logger.info("Cluster host does not support local storage, skip it");
}
} catch (Exception e) {
String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
invalidateServiceContext();
throw new CloudRuntimeException(msg);
}
return storageCmds;
}
use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.
the class VmwareResource method execute.
private PlugNicAnswer execute(PlugNicCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd));
}
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
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 = "Router " + vmName + " no longer exists to execute PlugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
s_logger.debug(errMsg);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
}
*/
// Fallback to E1000 if no specific nicAdapter is passed
VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
Map<String, String> details = cmd.getDetails();
if (details != null) {
nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
}
// find a usable device number in VMware environment
VirtualDevice[] nicDevices = vmMo.getNicDevices();
int deviceNumber = -1;
for (VirtualDevice device : nicDevices) {
if (device.getUnitNumber() > deviceNumber)
deviceNumber = device.getUnitNumber();
}
deviceNumber++;
NicTO nicTo = cmd.getNic();
VirtualDevice nic;
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
}
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(nic);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
setNuageVspVrIpInExtraConfig(vmConfigSpec.getExtraConfig(), nicTo, dvSwitchUuid);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure devices when running PlugNicCommand");
}
return new PlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
}
}
Aggregations