|
22 | 22 | import java.io.File; |
23 | 23 | import java.io.PrintWriter; |
24 | 24 | import java.io.StringWriter; |
| 25 | +import java.net.DatagramSocket; |
| 26 | +import java.net.InetAddress; |
| 27 | +import java.net.InetSocketAddress; |
25 | 28 | import java.net.NetworkInterface; |
26 | 29 | import java.net.SocketException; |
27 | 30 | import java.util.ArrayList; |
28 | 31 | import java.util.Arrays; |
29 | 32 | import java.util.Collections; |
30 | 33 | import java.util.Date; |
31 | 34 | import java.util.LinkedList; |
| 35 | +import java.util.LinkedHashSet; |
32 | 36 | import java.util.List; |
33 | 37 | import java.util.Map; |
| 38 | +import java.util.Set; |
34 | 39 |
|
35 | 40 | import javax.naming.ConfigurationException; |
36 | 41 |
|
@@ -75,6 +80,12 @@ public boolean configure(final String name, Map<String, Object> params) throws C |
75 | 80 |
|
76 | 81 | defineResourceNetworkInterfaces(params); |
77 | 82 |
|
| 83 | + if (privateNic == null) { |
| 84 | + checkForPrivateInterfaceDefinedByIp(params); |
| 85 | + } |
| 86 | + if (privateNic == null) { |
| 87 | + tryToAutoDiscoverResourcePrivateNetworkInterfaceByRouteLookup(params); |
| 88 | + } |
78 | 89 | if (privateNic == null) { |
79 | 90 | tryToAutoDiscoverResourcePrivateNetworkInterface(); |
80 | 91 | } |
@@ -106,8 +117,74 @@ protected void defineResourceNetworkInterfaces(Map<String, Object> params) { |
106 | 117 | this.storageNic2 = NetUtils.getNetworkInterface(storageNic2); |
107 | 118 | } |
108 | 119 |
|
| 120 | + private void checkForPrivateInterfaceDefinedByIp(Map<String, Object> params) { |
| 121 | + final String ifAddr = (String) params.get("private.network.address"); |
| 122 | + if (ifAddr != null) { |
| 123 | + logger.debug(String.format("Trying to use private address to resolve interface: [%s]", ifAddr)); |
| 124 | + try { |
| 125 | + InetAddress rawAddr = InetAddress.getByAddress(InetAddress.getByName(ifAddr).getAddress()); |
| 126 | + final NetworkInterface nic = NetworkInterface.getByInetAddress(rawAddr); |
| 127 | + if (nic != null) { |
| 128 | + logger.info(String.format("Using NIC [%s] as private NIC. Source: InterfaceAddress [%s]", nic, ifAddr)); |
| 129 | + privateNic = nic; |
| 130 | + } else { |
| 131 | + logger.info(String.format("Unable to found private NIC with defined ip [%s]", ifAddr)); |
| 132 | + } |
| 133 | + } catch (Throwable e) { |
| 134 | + // Logging only, if this method was unnable to find a valid interface, iteration will be tested |
| 135 | + logger.info(String.format("Unable to use private address to get the management interface: [%s]", e.getMessage())); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private String[] collectMgmtHostIp(Map<String, Object> params) { |
| 141 | + final String hosts = (String) params.get("host"); |
| 142 | + if (hosts != null && hosts.trim().length() > 0) { |
| 143 | + logger.info(String.format("Parsing host setting: [%s]", hosts)); |
| 144 | + final Set<String> hostCollection = new LinkedHashSet<String>(); |
| 145 | + if (hosts.contains(",")) { |
| 146 | + for(final String ip : hosts.split(",")) { |
| 147 | + final String h = ip.contains("@") ? ip.split("@")[0] : ip; |
| 148 | + hostCollection.add(h.trim()); |
| 149 | + } |
| 150 | + } else { |
| 151 | + hostCollection.add(hosts.trim()); |
| 152 | + } |
| 153 | + return hostCollection.isEmpty() ? null : hostCollection.toArray(new String[0]); |
| 154 | + } |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + protected void tryToAutoDiscoverResourcePrivateNetworkInterfaceByRouteLookup(Map<String, Object> params) throws ConfigurationException { |
| 159 | + logger.info("Trying to autodiscover this resource's private network interface by route lookup"); |
| 160 | + final String[] mgmtIps = collectMgmtHostIp(params); |
| 161 | + if (mgmtIps == null) { |
| 162 | + logger.info("Unable to resolve any management server ip address. Aborting private network search by route lookup."); |
| 163 | + return; |
| 164 | + } |
| 165 | + for (String mgmtIp : mgmtIps) { |
| 166 | + logger.info(String.format("Using management server IP [%s] to lookup", mgmtIp)); |
| 167 | + try { |
| 168 | + try (DatagramSocket socket = new DatagramSocket()) { |
| 169 | + socket.connect(new InetSocketAddress(mgmtIp, 8250)); |
| 170 | + // Asking for source address to mgmgt destination to O.S routing tables. |
| 171 | + InetAddress localAddress = socket.getLocalAddress(); |
| 172 | + NetworkInterface nic = NetworkInterface.getByInetAddress(localAddress); |
| 173 | + if (nic != null) { |
| 174 | + logger.info(String.format("Using NIC [%s] as private NIC.", nic)); |
| 175 | + privateNic = nic; |
| 176 | + break; |
| 177 | + } |
| 178 | + } |
| 179 | + } catch (Throwable e) { |
| 180 | + // Logging only, if this method was unnable to find a valid interface, iteration will be tested |
| 181 | + logger.debug(String.format("Unable to use routing table to determine private management interface: [%s]", e.getMessage())); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
109 | 186 | protected void tryToAutoDiscoverResourcePrivateNetworkInterface() throws ConfigurationException { |
110 | | - logger.info("Trying to autodiscover this resource's private network interface."); |
| 187 | + logger.info("Trying to autodiscover this resource's private network interface by simple iteration."); |
111 | 188 |
|
112 | 189 | List<NetworkInterface> nics; |
113 | 190 | try { |
|
0 commit comments