private String getIpAddress(GuestInfo guestInfo) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= paramHandler.getNumberOfNetworkAdapter(); i++) { GuestNicInfo info = getNicInfo(guestInfo, paramHandler.getNetworkAdapter(i)); if (info != null) { if (paramHandler.getNetworkAdapter(i) != null && !paramHandler .getNetworkAdapter(i).trim().isEmpty()) { sb.append(paramHandler.getNetworkAdapter(i) + ": "); } sb.append(info.getIpAddress()); if (i < paramHandler.getNumberOfNetworkAdapter()) { sb.append(", "); } } } return sb.toString(); }
public VM(VMwareClient vmw, String instanceName) throws Exception { this.vmw = vmw; this.instanceName = instanceName; vmInstance = vmw.getServiceUtil().getDecendentMoRef(null, "VirtualMachine", instanceName); configSpec = (VirtualMachineConfigInfo) vmw.getServiceUtil() .getDynamicProperty(vmInstance, "config"); folder = (ManagedObjectReference) vmw.getServiceUtil() .getDynamicProperty(vmInstance, "parent"); guestInfo = (GuestInfo) vmw.getServiceUtil() .getDynamicProperty(vmInstance, "guest"); if (vmInstance == null || configSpec == null || folder == null || guestInfo == null) { LOG.warn("failed to retrieve VM"); throw new Exception( "Failed to retrieve information of VM " + instanceName); } }
/** * Returns the generated access info for the given VM. */ public String generateAccessInfo(GuestInfo guestInfo) throws APPlatformException { String myHOST = guestInfo.getHostName(); String hostName; if (myHOST != null) { hostName = guestInfo.getHostName().split("\\.", 2)[0]; } else { hostName = "Unkown hostname (probably missing vmware tools).\nInstance name " + paramHandler.getInstanceName() + "."; myHOST = "Unkown(InstanceName " + paramHandler.getInstanceName() + ")"; } String accessInfoPattern = paramHandler.getAccessInfo(); if (accessInfoPatternUndefined(accessInfoPattern)) { return hostName; } String accessInfo = accessInfoPattern.replace(PATTERN_IP, getIpAddress(guestInfo)); accessInfo = accessInfo.replace(PATTERN_HOST, myHOST); accessInfo = accessInfo.replace(PATTERN_CPU, Integer.toString(paramHandler.getConfigCPUs())); accessInfo = accessInfo.replace(PATTERN_MEM, paramHandler.formatMBasGB(paramHandler.getConfigMemoryMB())); accessInfo = accessInfo.replace(PATTERN_DISKS, paramHandler.getDataDisksAsString()); accessInfo = accessInfo.replace(PATTERN_RESPUSER, getResponsibleUser()); accessInfo = accessInfo.replace("<br>", "<br>\r\n"); return accessInfo; }
GuestNicInfo getNicInfo(GuestInfo guestInfo, String adapter) { for (GuestNicInfo info : guestInfo.getNet()) { if (info != null && adapter.equals(info.getNetwork())) { return info; } } return null; }
@Test public void applyTest() { VirtualMachineToSshClient virtualMachineToSshClient = new VirtualMachineToSshClient(new StubSshClientModule.Factory()); VirtualMachine vm = PowerMock.createMock(VirtualMachine.class); GuestInfo guest = PowerMock.createMock(GuestInfo.class); expect(vm.getGuest()).andReturn(guest).anyTimes(); expect(guest.getIpAddress()).andReturn("10.10.0.2").anyTimes(); expect(guest.getToolsStatus()).andReturn(VirtualMachineToolsStatus.toolsOk).anyTimes(); replay(vm, guest); SshClient sshClient = virtualMachineToSshClient.apply(vm); Assert.assertNotNull(sshClient); }
public boolean isVMwareToolsRunning() throws Exception { GuestInfo guestInfo = getVmGuestInfo(); if (guestInfo != null) { if ("guestToolsRunning".equalsIgnoreCase(guestInfo.getToolsRunningStatus())) return true; } return false; }
protected String getVRouterVMIpFabricAddress(String hostName, HostSystem host, String vmNamePrefix) throws Exception { // Find if vRouter Ip Fabric mapping exists.. String vRouterIpAddress = esxiToVRouterIpMap.get(hostName); if (host.getRuntime().isInMaintenanceMode()) { VRouterNotifier.setVrouterActive(vRouterIpAddress, false); } if (vRouterIpAddress != null) { return vRouterIpAddress; } else { s_logger.debug(" vRouter IP mapping for Host: " + hostName + "does not exist"); } VirtualMachine[] vms = host.getVms(); for (VirtualMachine vm : vms) { String vmName = vm.getName(); if (!vmName.toLowerCase().contains(vmNamePrefix.toLowerCase())) { continue; } // Assumption here is that VMware Tools are installed // and IP address is available GuestInfo guestInfo = vm.getGuest(); if (guestInfo == null) { s_logger.debug(" Host: " + hostName + " vm:" + vmName + " GuestInfo - VMware Tools " + " NOT installed"); continue; } GuestNicInfo[] nicInfos = guestInfo.getNet(); if (nicInfos == null) { s_logger.debug(" Host: " + hostName + " vm:" + vmName + " GuestNicInfo - VMware Tools " + " NOT installed"); continue; } for (GuestNicInfo nicInfo : nicInfos) { // Extract the IP address associated with simple port // group. Assumption here is that Contrail VRouter VM will // have only one standard port group String networkName = nicInfo.getNetwork(); if (networkName == null || !networkName.equals(contrailIpFabricPgName)) { continue; } Network network = (Network) inventoryNavigator.searchManagedEntity("Network", networkName); if (network == null) { s_logger.debug("Host: " + hostName + " vm: " + vmName + " network: " + networkName + " NOT found"); continue; } NetIpConfigInfo ipConfigInfo = nicInfo.getIpConfig(); if (ipConfigInfo == null) { continue; } NetIpConfigInfoIpAddress[] ipAddrConfigInfos = ipConfigInfo.getIpAddress(); if (ipAddrConfigInfos == null || ipAddrConfigInfos.length == 0) { continue; } for (NetIpConfigInfoIpAddress ipAddrConfigInfo : ipAddrConfigInfos) { String ipAddress = ipAddrConfigInfo.getIpAddress(); // Choose IPv4 only InetAddress ipAddr = InetAddress.getByName(ipAddress); if (ipAddr instanceof Inet4Address) { // found vRouter VM ip-fabric address. Store it. esxiToVRouterIpMap.put(hostName, ipAddress); return ipAddress; } } } } return null; }
public static String getVirtualMachineIpAddress(VirtualMachine vm, String dvPgName) throws Exception { // Assumption here is that VMware Tools are installed // and IP address is available GuestInfo guestInfo = vm.getGuest(); String vmName = vm.getName(); if (guestInfo == null) { s_logger.debug("dvPg: " + dvPgName + " vm:" + vmName + " GuestInfo - VMware Tools " + " NOT installed"); return null; } GuestNicInfo[] nicInfos = guestInfo.getNet(); if (nicInfos == null) { s_logger.debug("dvPg: " + dvPgName + " vm:" + vmName + " GuestNicInfo - VMware Tools " + " NOT installed"); return null; } for (GuestNicInfo nicInfo : nicInfos) { // Extract the IP address associated with simple port // group. Assumption here is that Contrail VRouter VM will // have only one standard port group String networkName = nicInfo.getNetwork(); if (networkName == null || !networkName.equals(dvPgName)) { continue; } NetIpConfigInfo ipConfigInfo = nicInfo.getIpConfig(); if (ipConfigInfo == null) { continue; } NetIpConfigInfoIpAddress[] ipAddrConfigInfos = ipConfigInfo.getIpAddress(); if (ipAddrConfigInfos == null || ipAddrConfigInfos.length == 0) { continue; } for (NetIpConfigInfoIpAddress ipAddrConfigInfo : ipAddrConfigInfos) { String ipAddress = ipAddrConfigInfo.getIpAddress(); InetAddress ipAddr = InetAddress.getByName(ipAddress); if (ipAddr instanceof Inet4Address) { // the VMI can have multiple IPv4 and IPv6 addresses, // but we pick only the first IPv4 address return ipAddress; } } } return null; }
public GuestInfo getVmGuestInfo() throws Exception { return (GuestInfo)getContext().getVimClient().getDynamicProperty(_mor, "guest"); }
public GuestInfo getGuestInfo() throws Exception { return (GuestInfo)_context.getVimClient().getDynamicProperty(_mor, "guest"); }
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; }