Skip to content

Commit

Permalink
Connect to go server to determine the agent ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-jaswanth committed Jul 7, 2016
1 parent c3c28d0 commit b283b42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 13 additions & 4 deletions agent/src/com/thoughtworks/go/agent/AgentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
import com.thoughtworks.go.server.service.AgentBuildingInfo;
import com.thoughtworks.go.server.service.AgentRuntimeInfo;
import com.thoughtworks.go.server.service.ElasticAgentRuntimeInfo;
import com.thoughtworks.go.util.*;
import com.thoughtworks.go.util.HttpService;
import com.thoughtworks.go.util.SubprocessLogger;
import com.thoughtworks.go.util.SystemEnvironment;
import com.thoughtworks.go.util.SystemUtil;
import com.thoughtworks.go.util.TimeProvider;
import com.thoughtworks.go.util.URLService;
import com.thoughtworks.go.util.ZipUtil;
import com.thoughtworks.go.websocket.Action;
import com.thoughtworks.go.websocket.Message;
import com.thoughtworks.go.websocket.MessageCallback;
Expand All @@ -57,6 +63,8 @@

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -102,7 +110,7 @@ public AgentController(BuildRepositoryRemote server, GoArtifactsManipulator mani
this.taskExtension = taskExtension;
this.websocketService = websocketService;
this.httpService = httpService;
ipAddress = SystemUtil.getFirstLocalNonLoopbackIpAddress();
ipAddress = SystemUtil.getClientIp(systemEnvironment.getServiceUrl());
hostName = SystemUtil.getLocalhostNameOrRandomNameIfNotFound();
this.server = server;
this.manipulator = manipulator;
Expand All @@ -114,6 +122,7 @@ public AgentController(BuildRepositoryRemote server, GoArtifactsManipulator mani
this.agentAutoRegistrationProperties = new AgentAutoRegistrationPropertiesImpl(new File("config", "autoregister.properties"));
}


void init() throws IOException {
websocketService.setController(this);
createPipelinesFolderIfNotExist();
Expand Down Expand Up @@ -360,11 +369,11 @@ private void runBuild(BuildSettings buildSettings) {

private void cancelBuild() throws InterruptedException {
BuildSession build = this.buildSession.get();
if(build == null) {
if (build == null) {
return;
}
agentRuntimeInfo.cancel();
if(!build.cancel(30, TimeUnit.SECONDS)) {
if (!build.cancel(30, TimeUnit.SECONDS)) {
LOG.error("Waited 30 seconds for canceling job finish, but the job is still running. Maybe canceling job does not work as expected, here is buildSession details: " + buildSession.get());
}
}
Expand Down
12 changes: 12 additions & 0 deletions base/src/com/thoughtworks/go/util/SystemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -189,4 +190,15 @@ public static int getIntProperty(String propertyName, int defaultValue) {
return defaultValue;
}
}

public static String getClientIp(String serviceUrl) {
try {
URL url = new URL(serviceUrl);
try (Socket socket = new Socket(url.getHost(), url.getPort())) {
return socket.getLocalAddress().getHostAddress();
}
} catch (Exception e){
return SystemUtil.getFirstLocalNonLoopbackIpAddress();
}
}
}

0 comments on commit b283b42

Please sign in to comment.