-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from mesos/feature/jarMode
Jar mode
- Loading branch information
Showing
20 changed files
with
525 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
logstash-commons/src/main/java/org/apache/mesos/logstash/common/network/NetworkUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package org.apache.mesos.logstash.common.network; | ||
|
||
import org.apache.commons.exec.CommandLine; | ||
import org.apache.commons.exec.DefaultExecutor; | ||
import org.apache.commons.exec.PumpStreamHandler; | ||
import org.apache.commons.exec.environment.EnvironmentUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.InterfaceAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.Charset; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.Map; | ||
|
||
/** | ||
* Utilities to help with networking | ||
*/ | ||
@SuppressWarnings("PMD.AvoidUsingHardCodedIP") | ||
@Service | ||
public class NetworkUtils { | ||
private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class); | ||
public static final String DOCKER_MACHINE_IP = "docker-machine ip"; | ||
public static final String LOCALHOST = "127.0.0.1"; | ||
public static final String DOCKER_MACHINE_NAME = "DOCKER_MACHINE_NAME"; | ||
|
||
public InetAddress hostAddress() { | ||
try { | ||
return InetAddress.getLocalHost(); | ||
} catch (UnknownHostException e) { | ||
LOG.error("", e); | ||
throw new RuntimeException("Unable to bind to local host."); | ||
} | ||
} | ||
|
||
public InetSocketAddress hostSocket(int port) { | ||
return new InetSocketAddress(hostAddress(), port); | ||
} | ||
|
||
public String addressToString(InetSocketAddress address, Boolean useIpAddress) { | ||
if (useIpAddress) { | ||
return "http://" + address.getAddress().getHostAddress() + ":" + address.getPort(); | ||
} else { | ||
return "http://" + address.getAddress().getHostName() + ":" + address.getPort(); | ||
} | ||
} | ||
|
||
public String getDockerMachineName(Map<String, String> environment) { | ||
String envVar = DOCKER_MACHINE_NAME; | ||
String dockerMachineName = environment.getOrDefault(envVar, ""); | ||
if (dockerMachineName == null || dockerMachineName.isEmpty()) { | ||
LOG.debug("The environmental variable DOCKER_MACHINE_NAME was not found. Using docker0 address."); | ||
} | ||
return dockerMachineName; | ||
} | ||
|
||
public String getDockerHostIpAddress(Map<String, String> environment) { | ||
String ipAddress = LOCALHOST; // Default of localhost | ||
String dockerMachineName = getDockerMachineName(environment); | ||
|
||
if (!dockerMachineName.isEmpty()) { | ||
LOG.debug("Docker machine name = " + dockerMachineName); | ||
CommandLine commandline = CommandLine.parse(DOCKER_MACHINE_IP); | ||
commandline.addArgument(dockerMachineName); | ||
LOG.debug("Running exec: " + commandline.toString()); | ||
try { | ||
ipAddress = StringUtils.strip(runCommand(commandline)); | ||
} catch (IOException e) { | ||
LOG.error("Unable to run exec command to find ip address.", e); | ||
} | ||
} else { | ||
ipAddress = getDocker0AdapterIPAddress(); | ||
} | ||
LOG.debug("Returned IP address: " + ipAddress); | ||
return ipAddress; | ||
} | ||
|
||
public Map<String, String> getEnvironment() { | ||
Map<String, String> env = Collections.emptyMap(); | ||
try { | ||
env = EnvironmentUtils.getProcEnvironment(); | ||
} catch (IOException e) { | ||
LOG.error("Unable to get environmental variables", e); | ||
} | ||
return env; | ||
} | ||
|
||
public String runCommand(CommandLine commandline) throws IOException { | ||
DefaultExecutor exec = new DefaultExecutor(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); | ||
exec.setStreamHandler(streamHandler); | ||
exec.execute(commandline); | ||
return outputStream.toString(Charset.defaultCharset().name()); | ||
} | ||
|
||
public String getDocker0AdapterIPAddress() { | ||
InetAddress docker0 = getLocalAddress("docker0"); | ||
if (docker0 == null) { | ||
LOG.error("Could not get address for docker0"); | ||
return LOCALHOST; | ||
} else { | ||
return docker0.getHostAddress(); | ||
} | ||
} | ||
|
||
private InetAddress getLocalAddress(String adaptorName){ | ||
try { | ||
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces(); | ||
while (b.hasMoreElements()) { | ||
NetworkInterface networkInterface = b.nextElement(); | ||
if (networkInterface.getName().equals(adaptorName)) { | ||
for (InterfaceAddress f : networkInterface.getInterfaceAddresses()) { | ||
if (f.getAddress().isSiteLocalAddress()) { | ||
return f.getAddress(); | ||
} | ||
} | ||
} | ||
} | ||
} catch (SocketException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ns/src/test/java/org/apache/mesos/logstash/common/zookeeper/network/NetworkUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.apache.mesos.logstash.common.zookeeper.network; | ||
|
||
import org.apache.commons.validator.routines.InetAddressValidator; | ||
import org.apache.mesos.logstash.common.network.NetworkUtils; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
*/ | ||
public class NetworkUtilsTest { | ||
|
||
public static boolean validate(final String ip) { | ||
return InetAddressValidator.getInstance().isValid(ip); | ||
} | ||
|
||
private NetworkUtils networkUtils = new NetworkUtils(); | ||
|
||
@Test | ||
// Note: On OSX, when not connected to a network, it will return a IPv6 address, which will not validate properly. | ||
// Please connect to a network to obtain a IPv4 address. | ||
public void shouldProvideIPAddress() { | ||
int port = 1234; | ||
String string = networkUtils.addressToString(networkUtils.hostSocket(port), true); | ||
assertTrue(validate(string.replace("http://", "").replace(":" + port, ""))); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldProvideHostname() { | ||
int port = 1234; | ||
String string = networkUtils.addressToString(networkUtils.hostSocket(port), false); | ||
assertFalse(validate(string.replace("http://", "").replace(":" + port, ""))); | ||
} | ||
|
||
@Test | ||
public void shouldGetDockerIPAddress() throws IOException { | ||
// Should always be either a valid IP or 127.0.0.1 | ||
assertTrue(validate( networkUtils.getDockerHostIpAddress(networkUtils.getEnvironment()))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnLocahostOrDocker0AddressWhenNoEnvVar() { | ||
if ( networkUtils.getDockerHostIpAddress(Collections.emptyMap()).equals(NetworkUtils.LOCALHOST)) { | ||
assertEquals(NetworkUtils.LOCALHOST, networkUtils.getDockerHostIpAddress(Collections.emptyMap())); | ||
} else { | ||
assertEquals(networkUtils.getDocker0AdapterIPAddress(), networkUtils.getDockerHostIpAddress(Collections.emptyMap())); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnDockerMachineNameWhenIncluded() { | ||
HashMap<String, String> map = new HashMap<>(); | ||
String dev = "dev"; | ||
map.put(NetworkUtils.DOCKER_MACHINE_NAME, dev); | ||
assertEquals(dev, networkUtils.getDockerMachineName(map)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.