Skip to content

Commit

Permalink
[JENKINS-30512] use java API instead of ifconfig to lookup docker0 ip…
Browse files Browse the repository at this point in the history
…v4 address
  • Loading branch information
ydubreuil committed Sep 21, 2015
1 parent e8dd7e4 commit 7b9b8a6
Showing 1 changed file with 14 additions and 13 deletions.
Expand Up @@ -18,6 +18,10 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -194,17 +198,14 @@ public String runDetached(String image, String workdir, Map<String, String> volu

private String getDocker0Ip(Launcher launcher, String image) throws IOException, InterruptedException {

ByteArrayOutputStream out = new ByteArrayOutputStream();
int status = launcher.launch()
.cmds("ifconfig", "docker0")
.stdout(out)
.join();

if (status == 0) {
final String s = out.toString();
int i = s.indexOf("inet addr:")+10;
int j = s.indexOf(' ', i);
return s.substring(i, j);
NetworkInterface docker0 = NetworkInterface.getByName("docker0");
if (docker0 != null) {
for (InterfaceAddress address : docker0.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (inetAddress != null && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}

// Docker daemon might be configured with a custom bridge, or maybe we are just running from Windows/OSX
Expand All @@ -216,9 +217,9 @@ private String getDocker0Ip(Launcher launcher, String image) throws IOException,
.add(image)
.add("/sbin/ip", "route");

out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();

status = launcher.launch()
int status = launcher.launch()
.envs(getEnvVars())
.cmds(args)
.stdout(out).quiet(!verbose).stderr(listener.getLogger()).join();
Expand Down

0 comments on commit 7b9b8a6

Please sign in to comment.