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(); }
private String createLogForGetState(boolean validHostname, VMPropertyHandler configuration, boolean isConnected, boolean validIp) { StringBuilder sb = new StringBuilder(); sb.append("Guest system is not ready yet "); sb.append("["); sb.append("hostname (" + validHostname + ") =" + guestInfo.getHostName() + ", "); sb.append("ipReady=" + validIp + ", "); for (int i = 1; i <= configuration.getNumberOfNetworkAdapter(); i++) { GuestNicInfo info = getNicInfo(configuration, i); if (info != null) { sb.append(info.getNetwork() + "="); sb.append(info.getIpAddress()); sb.append(","); } } sb.append("guestState=" + guestInfo.getGuestState() + ", "); sb.append("toolsState=" + guestInfo.getToolsStatus() + ", "); sb.append("toolsRunning=" + guestInfo.getToolsRunningStatus() + ", "); sb.append("isConnected=" + isConnected); sb.append("]"); String logStatement = sb.toString(); return logStatement; }
boolean isValidIp(VMPropertyHandler configuration) { for (int i = 1; i <= configuration.getNumberOfNetworkAdapter(); i++) { GuestNicInfo info = getNicInfo(configuration, i); if (info == null) { return false; } if (configuration.isAdapterConfiguredManually(i)) { if (!containsIpAddress(info, configuration.getIpAddress(i))) { return false; } } else { if (!ipAddressExists(info)) { return false; } } } return true; }
public void updatedGuestNics(GuestNicInfo[] nics, VncDB vncDB) throws Exception { if (nics == null) { return; } for (GuestNicInfo nic: nics) { if (nic == null) { continue; } String mac = nic.getMacAddress(); if (vmiInfoMap.containsKey(mac)) { VirtualMachineInterfaceInfo oldVmi = vmiInfoMap.get(mac); oldVmi.updatedGuestNic(nic, vncDB); } } }
/** * Searches for all ip addresses of a virtual machine * * @param virtualMachine the virtual machine to query * @return the ip addresses of the virtual machine, the first one is the primary * @throws RemoteException */ public TreeSet<String> getVirtualMachineIpAddresses(VirtualMachine virtualMachine) throws RemoteException { TreeSet<String> ipAddresses = new TreeSet<String>(); // add the Ip address reported by VMware tools, this should be primary if (virtualMachine.getGuest().getIpAddress() != null) ipAddresses.add(virtualMachine.getGuest().getIpAddress()); // if possible, iterate over all virtual networks networks and add interface Ip addresses if (virtualMachine.getGuest().getNet() != null) { for (GuestNicInfo guestNicInfo : virtualMachine.getGuest().getNet()) { if (guestNicInfo.getIpAddress() != null) { for (String ipAddress : guestNicInfo.getIpAddress()) { ipAddresses.add(ipAddress); } } } } return ipAddresses; }
GuestNicInfo getNicInfo(GuestInfo guestInfo, String adapter) { for (GuestNicInfo info : guestInfo.getNet()) { if (info != null && adapter.equals(info.getNetwork())) { return info; } } return null; }
boolean areNetworkCardsConnected() { boolean isConnected = false; if (guestInfo.getNet() != null && !guestInfo.getNet().isEmpty()) { isConnected = true; } for (GuestNicInfo nicInfo : guestInfo.getNet()) { isConnected = isConnected && nicInfo.isConnected(); } return isConnected; }
GuestNicInfo getNicInfo(VMPropertyHandler configuration, int i) { if (configuration.getNetworkAdapter(i) == null) { return null; } for (GuestNicInfo info : guestInfo.getNet()) { if (configuration.isAdapterConfiguredByDhcp(i)) { return info; } if (configuration.getNetworkAdapter(i).equals(info.getNetwork())) { return info; } } return null; }
boolean guestInfoContainsNic(String adapter) { for (GuestNicInfo info : guestInfo.getNet()) { if (info.getNetwork().equals(adapter)) { return true; } } return false; }
boolean ipAddressExists(GuestNicInfo info) { if (info.getIpAddress().isEmpty()) { return false; } for (String ip : info.getIpAddress()) { if (ip == null || ip.trim().length() == 0) { return false; } } return true; }
@Before public void setup() { ObjectContent cont = new ObjectContent(); ManagedObjectReference ref = new ManagedObjectReference(); ref.setType(VimNames.TYPE_VM); ref.setValue("vm-123"); cont.setObj(ref); Map<String, Object> props = new HashMap<>(); ArrayOfGuestNicInfo arrayOfGuestNicInfo = new ArrayOfGuestNicInfo(); List<GuestNicInfo> listGuestNicInfo = arrayOfGuestNicInfo.getGuestNicInfo(); GuestNicInfo nic1 = new GuestNicInfo(); List<String> ipsNic1 = nic1.getIpAddress(); String mac1Address = "00:50:56:8b:54:bd"; String mac2Address = "98:87:fd:9e:ed:6d"; nic1.setMacAddress(mac1Address); ipsNic1.add("192.168.1.10"); ipsNic1.add("192.168.1.11"); GuestNicInfo nic2 = new GuestNicInfo(); List<String> ipsNic2 = nic2.getIpAddress(); nic2.setMacAddress(mac2Address); ipsNic2.add("10.10.10.20"); listGuestNicInfo.add(nic1); listGuestNicInfo.add(nic2); props.put(vm_guest_net, arrayOfGuestNicInfo); this.overlay = new VmOverlay(ref, props); }
@Override public boolean apply(@Nullable VirtualMachine input) { if (input == null) return false; GuestNicInfo[] nics = input.getGuest().getNet(); boolean nicConnected = false; if (null != nics) { for (GuestNicInfo nic : nics) { nicConnected = nicConnected || nic.connected; } } return nicConnected; }
public void updatedGuestNic(GuestNicInfo nic, VncDB vncDB) throws Exception { if (nic == null) { return; } NetIpConfigInfo ipConfig = nic.getIpConfig(); if (ipConfig == null ) { return; } NetIpConfigInfoIpAddress[] ipAddrs = nic.getIpConfig().getIpAddress(); if (ipAddrs == null || ipAddrs.length <= 0) { return; } String newIpAddress = ipAddrs[0].getIpAddress(); if (newIpAddress != null && newIpAddress.equals(ipAddress)) { // IP address has not changed return; } s_logger.info("Updated guestNic Address " + " to " + newIpAddress + " for " + this); if (ipAddress != null) { vncDB.deleteInstanceIp(this); } deletePort(); setIpAddress(newIpAddress); if (ipAddress != null || vnInfo.getExternalIpam() == false) { vncDB.createInstanceIp(this); } addPort(); }
public void updateServiceParameter(VMPropertyHandler paramHandler) throws Exception { LOG.debug("instanceName: " + instanceName); int key = getDataDiskKey(); if (key != -1) { paramHandler.setDataDiskKey(1, key); } if (!paramHandler .isServiceSettingTrue(VMPropertyHandler.TS_IMPORT_EXISTING_VM) && !paramHandler.getInstanceName() .equals(guestInfo.getHostName())) { throw new Exception( "Instancename and hostname do not match. Hostname: " + guestInfo.getHostName() + " Instancename: " + paramHandler.getInstanceName()); } String targetFolder = (String) vmw.getServiceUtil() .getDynamicProperty(folder, "name"); Integer ramMB = (Integer) vmw.getServiceUtil() .getDynamicProperty(vmInstance, "summary.config.memorySizeMB"); paramHandler.setSetting(VMPropertyHandler.TS_AMOUNT_OF_RAM, ramMB.toString()); paramHandler.setSetting(VMPropertyHandler.TS_NUMBER_OF_CPU, Integer.toString(getNumCPU())); paramHandler.setSetting(VMPropertyHandler.TS_TARGET_FOLDER, targetFolder); paramHandler.setSetting(VMPropertyHandler.TS_DISK_SIZE, getDiskSizeInGB(1)); paramHandler.setSetting( VMPropertyHandler.TS_DATA_DISK_SIZE.replace("#", "1"), getDiskSizeInGB(2)); paramHandler.setSetting(VMPropertyHandler.TS_NUMBER_OF_NICS, Integer.toString(getNumberOfNICs())); int i = 1; List<GuestNicInfo> nicList = guestInfo.getNet(); for (GuestNicInfo info : nicList) { if (info.getIpAddress() != null && info.getIpAddress().size() > 0) { paramHandler.setSetting("NIC" + i + "_IP_ADDRESS", info.getIpAddress().get(0)); if (info.getNetwork() != null) { paramHandler.setSetting("NIC" + i + "_NETWORK_ADAPTER", info.getNetwork()); } i++; } } }
boolean containsIpAddress(GuestNicInfo info, String address) { return info.getIpAddress().contains(address); }
/** * TODO: メソッドコメントを記述 * * @param vmwareProcessClient * @param instanceNo */ public void getGuestInfo(VmwareProcessClient vmwareProcessClient, Long instanceNo) { Instance instance = instanceDao.read(instanceNo); VmwareInstance vmwareInstance = vmwareInstanceDao.read(instanceNo); List<VmwareNetwork> vmwareNetworks = vmwareNetworkDao.readByFarmNo(instance.getFarmNo()); // ネットワーク名の取得 Platform platform = platformDao.read(instance.getPlatformNo()); PlatformVmware platformVmware = platformVmwareDao.read(instance.getPlatformNo()); String publicNetworkName = platformVmware.getPublicNetwork(); String privateNetworkName = platformVmware.getPrivateNetwork(); for (VmwareNetwork vmwareNetwork : vmwareNetworks) { if (BooleanUtils.isTrue(vmwareNetwork.getPublicNetwork())) { publicNetworkName = vmwareNetwork.getNetworkName(); } else { privateNetworkName = vmwareNetwork.getNetworkName(); } } String publicIpAddress = null; String privateIpAddress = null; VirtualMachine machine = vmwareProcessClient.getVirtualMachine(vmwareInstance.getMachineName()); for (GuestNicInfo nicInfo : machine.getGuest().getNet()) { // NIC情報からIPv4のアドレスを取得 NetIpConfigInfoIpAddress[] tmpAddresses = nicInfo.getIpConfig().getIpAddress(); if (tmpAddresses == null) { continue; } String ipAddress = null; for (NetIpConfigInfoIpAddress tmpAdress : tmpAddresses) { try { InetAddress inetAddress = InetAddress.getByName(tmpAdress.getIpAddress()); if (inetAddress instanceof Inet4Address) { ipAddress = tmpAdress.getIpAddress(); break; } } catch (UnknownHostException ignore) { } } // NIC情報がPublicかPrivateかの判定 if (StringUtils.isNotEmpty(publicNetworkName) && publicNetworkName.equals(nicInfo.getNetwork())) { publicIpAddress = ipAddress; } else if (privateNetworkName.equals(nicInfo.getNetwork())) { privateIpAddress = ipAddress; } } if (StringUtils.isNotEmpty(publicNetworkName) && publicIpAddress == null) { // パブリックIPを取得できない場合 throw new AutoException("EPROCESS-000510", vmwareInstance.getMachineName()); } else if (privateIpAddress == null) { // プライベートIPを取得できない場合 throw new AutoException("EPROCESS-000511", vmwareInstance.getMachineName()); } // イベントログ出力 processLogger.debug(null, instance, "VmwareInstanceStartFinish", new Object[] { platform.getPlatformName(), vmwareInstance.getMachineName() }); // データベースに格納 vmwareInstance = vmwareInstanceDao.read(instanceNo); vmwareInstance.setIpAddress(publicIpAddress); vmwareInstance.setPrivateIpAddress(privateIpAddress); vmwareInstanceDao.update(vmwareInstance); if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100432", vmwareInstance.getMachineName())); } }
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; }