Skip to content

Commit b283b42

Browse files
committed
Connect to go server to determine the agent ip address
1 parent c3c28d0 commit b283b42

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

agent/src/com/thoughtworks/go/agent/AgentController.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@
4444
import com.thoughtworks.go.server.service.AgentBuildingInfo;
4545
import com.thoughtworks.go.server.service.AgentRuntimeInfo;
4646
import com.thoughtworks.go.server.service.ElasticAgentRuntimeInfo;
47-
import com.thoughtworks.go.util.*;
47+
import com.thoughtworks.go.util.HttpService;
48+
import com.thoughtworks.go.util.SubprocessLogger;
49+
import com.thoughtworks.go.util.SystemEnvironment;
50+
import com.thoughtworks.go.util.SystemUtil;
51+
import com.thoughtworks.go.util.TimeProvider;
52+
import com.thoughtworks.go.util.URLService;
53+
import com.thoughtworks.go.util.ZipUtil;
4854
import com.thoughtworks.go.websocket.Action;
4955
import com.thoughtworks.go.websocket.Message;
5056
import com.thoughtworks.go.websocket.MessageCallback;
@@ -57,6 +63,8 @@
5763

5864
import java.io.File;
5965
import java.io.IOException;
66+
import java.net.Socket;
67+
import java.net.URL;
6068
import java.security.GeneralSecurityException;
6169
import java.util.Map;
6270
import java.util.concurrent.ConcurrentHashMap;
@@ -102,7 +110,7 @@ public AgentController(BuildRepositoryRemote server, GoArtifactsManipulator mani
102110
this.taskExtension = taskExtension;
103111
this.websocketService = websocketService;
104112
this.httpService = httpService;
105-
ipAddress = SystemUtil.getFirstLocalNonLoopbackIpAddress();
113+
ipAddress = SystemUtil.getClientIp(systemEnvironment.getServiceUrl());
106114
hostName = SystemUtil.getLocalhostNameOrRandomNameIfNotFound();
107115
this.server = server;
108116
this.manipulator = manipulator;
@@ -114,6 +122,7 @@ public AgentController(BuildRepositoryRemote server, GoArtifactsManipulator mani
114122
this.agentAutoRegistrationProperties = new AgentAutoRegistrationPropertiesImpl(new File("config", "autoregister.properties"));
115123
}
116124

125+
117126
void init() throws IOException {
118127
websocketService.setController(this);
119128
createPipelinesFolderIfNotExist();
@@ -360,11 +369,11 @@ private void runBuild(BuildSettings buildSettings) {
360369

361370
private void cancelBuild() throws InterruptedException {
362371
BuildSession build = this.buildSession.get();
363-
if(build == null) {
372+
if (build == null) {
364373
return;
365374
}
366375
agentRuntimeInfo.cancel();
367-
if(!build.cancel(30, TimeUnit.SECONDS)) {
376+
if (!build.cancel(30, TimeUnit.SECONDS)) {
368377
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());
369378
}
370379
}

base/src/com/thoughtworks/go/util/SystemUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.NetworkInterface;
2525
import java.net.Socket;
2626
import java.net.SocketException;
27+
import java.net.URL;
2728
import java.net.UnknownHostException;
2829
import java.util.Collections;
2930
import java.util.Enumeration;
@@ -189,4 +190,15 @@ public static int getIntProperty(String propertyName, int defaultValue) {
189190
return defaultValue;
190191
}
191192
}
193+
194+
public static String getClientIp(String serviceUrl) {
195+
try {
196+
URL url = new URL(serviceUrl);
197+
try (Socket socket = new Socket(url.getHost(), url.getPort())) {
198+
return socket.getLocalAddress().getHostAddress();
199+
}
200+
} catch (Exception e){
201+
return SystemUtil.getFirstLocalNonLoopbackIpAddress();
202+
}
203+
}
192204
}

0 commit comments

Comments
 (0)