Skip to content

Commit 0d899eb

Browse files
committed
comment fixes and tunnel stop changes
1 parent dc619d1 commit 0d899eb

File tree

6 files changed

+115
-22
lines changed

6 files changed

+115
-22
lines changed

src/main/java/com/lambdatest/jenkins/credential/MagicPlugCredentialsImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public String getCredentialsPage() {
8383

8484
public FormValidation doVerifyCredentials(@QueryParameter("userName") final String userName,
8585
@QueryParameter("accessToken") final String accessToken) throws IOException, ServletException {
86-
logger.info(userName + ":" + accessToken);
8786
if (StringUtils.isBlank(userName) || StringUtils.isBlank(accessToken)) {
8887
return FormValidation.error("Please enter valid username and authKey");
8988
}

src/main/java/com/lambdatest/jenkins/freestyle/MagicPlugBuildWrapper.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lambdatest.jenkins.freestyle;
22

3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
35
import java.io.IOException;
46
import java.io.Serializable;
57
import java.lang.reflect.Field;
@@ -171,11 +173,10 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener l
171173

172174
// Create Grid URL
173175
if (!CollectionUtils.isEmpty(seleniumCapabilityRequest)) {
174-
this.gridURL = CapabilityService.buildHubURL(this.username, this.accessToken.getPlainText(),"production");
175-
logger.info(this.gridURL);
176+
this.gridURL = CapabilityService.buildHubURL(this.username, this.accessToken.getEncryptedValue(),"production");
176177
} else if (!CollectionUtils.isEmpty(appAutomationCapabilityRequest)) {
177178
logger.info("appAutomationCR : " + appAutomationCapabilityRequest);
178-
this.gridURL = AppAutomationCapabilityService.appAutomationBuildHubURL(this.username, this.accessToken.getPlainText(),"production");
179+
this.gridURL = AppAutomationCapabilityService.appAutomationBuildHubURL(this.username, this.accessToken.getEncryptedValue(),"production");
179180
}
180181
logger.info("grid URL : " + this.gridURL);
181182
return new MagicPlugEnvironment(build);
@@ -302,34 +303,65 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx
302303
/*
303304
* Runs after the build
304305
*/
306+
String buildnumber = String.valueOf(build.getNumber());
307+
305308
try {
306309
logger.info("tearDown");
307-
int x=1;
308-
while(x!=-1) {
309-
x=stopTunnel();
310+
int result = stopTunnel(buildnumber, build.getWorkspace());
311+
312+
if (result == -1) {
313+
logger.info("Tunnel was not active.");
314+
} else {
315+
logger.info("Tunnel stopped successfully.");
310316
}
311317
} catch (Exception e) {
312-
logger.warning("Forcefully Tear Down due to :"+e.getMessage());
318+
logger.warning("Forcefully Tear Down due to: " + e.getMessage());
313319
if (tunnelProcess != null && tunnelProcess.isAlive()) {
314320
tunnelProcess.destroy();
315321
}
316322
}
317323
return super.tearDown(build, listener);
318-
}
319-
320-
private int stopTunnel() throws IOException, InterruptedException {
324+
}
325+
326+
private int stopTunnel(String buildnumber, FilePath workspacePath) throws IOException, InterruptedException {
321327
if (tunnelProcess != null && tunnelProcess.isAlive()) {
322-
logger.info("tunnel is active, going to stop tunnel binary");
328+
logger.info("Tunnel is active, going to stop the tunnel binary");
329+
323330
if (OSValidator.isWindows()) {
324331
tunnelProcess.destroy();
325332
logger.info("Windows Tunnel Stopped");
326333
return 10;
327334
}
328-
long tunnelProcessId=getPidOfProcess(tunnelProcess);
335+
336+
long tunnelProcessId = getPidOfProcess(tunnelProcess);
337+
338+
logger.info("Tunnel is active, with tunnelProcessId: " + tunnelProcessId);
339+
340+
if (tunnelProcessId == -1) {
341+
String pidFilePath = workspacePath + "/lambda-tunnel/" + buildnumber + ".pid";
342+
logger.info(pidFilePath);
343+
344+
try (BufferedReader br = new BufferedReader(new FileReader(pidFilePath))) {
345+
String pidString = br.readLine();
346+
347+
if (pidString != null) {
348+
long pid = Integer.parseInt(pidString.trim());
349+
stopTunnelProcessUsingPID(pid);
350+
return 10;
351+
} else {
352+
logger.info("PID not found in the file.");
353+
return -1;
354+
}
355+
} catch (IOException e) {
356+
logger.info("Error reading PID file: " + e.getMessage());
357+
return -1;
358+
}
359+
}
360+
329361
stopTunnelProcessUsingPID(tunnelProcessId);
330362
Thread.sleep(2000);
331363
return 10;
332-
}else {
364+
} else {
333365
logger.info("Tunnel Stopped");
334366
return -1;
335367
}

src/main/java/com/lambdatest/jenkins/freestyle/report/AppAutomationReportBuildAction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ public AppAutomationReportBuildAction(final Run<?, ?> build, String name,String
3535
this.buildName = buildName;
3636
}
3737
public void generateLambdaTestAppAutomationReport() {
38-
logger.info("authString : " + authString);
3938
byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
4039
String authStringEnc = new String(authEncBytes);
41-
logger.info("in generate generateLambdaTestAppAutomationReport function");
4240
try {
4341
URL buildUrl = new URL(Constant.AppAutomationReport.BUILD_INFO_URL);
4442
URLConnection buildUrlConnection = buildUrl.openConnection();

src/main/java/com/lambdatest/jenkins/freestyle/report/LambdaTestAppAutomationReportPublisher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonn
3939
final String accessKey = parentEnvs.get(Constant.LT_ACCESS_KEY);
4040
final String buildName = parentEnvs.get(Constant.LT_BUILD_NAME);
4141

42-
logger.info("Generating LambdaTest App Automation Report" + "\n" + "username : " + username + "\n" + "accessKey : " + accessKey + "\n" + "buildName : " + buildName);
42+
logger.info("Generating LambdaTest App Automation Report" + "\n" + "buildName : " + buildName);
4343
AppAutomationReportBuildAction ltReportAction = new AppAutomationReportBuildAction(build, username, accessKey, buildName);
4444
ltReportAction.generateLambdaTestAppAutomationReport();
4545
build.addAction(ltReportAction);

src/main/java/com/lambdatest/jenkins/freestyle/report/ReportBuildAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public ReportBuildAction(final Run<?, ?> build, String name,String password, Str
3535
this.buildName = buildName;
3636
}
3737
public void generateLambdaTestReport() {
38-
logger.info("authString : " + authString);
3938
byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
4039
String authStringEnc = new String(authEncBytes);
4140
logger.info("in generate generateLambdaTestReport function");

src/main/java/com/lambdatest/jenkins/freestyle/service/LambdaTunnelService.java

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ public static Process setUp(String user, String key, LocalTunnel localTunnel, St
5959
logger.info("Tunnel Binary downloaded from " + Constant.LINUX_BINARY_URL);
6060
}
6161
// Get Tunnel Log path name
62+
String tunnelPidPath = getPidPath(workspacePath, buildnumber);
6263
String tunnelLogPath = getTunnelLogPath(workspacePath, buildnumber);
6364
logger.info("Tunnel Log Path:" + tunnelLogPath);
64-
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel);
65+
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel, tunnelPidPath);
6566
} else {
6667
logger.warning("tunnelFolderPath empty");
6768
}
@@ -95,9 +96,10 @@ public static Process setUp(String user, String key, LocalTunnel localTunnel, St
9596
logger.info("Tunnel Binary downloaded from " + Constant.MAC_BINARY_URL);
9697
}
9798
// Get Tunnel Log path name
99+
String tunnelPidPath = getPidPath(workspacePath, buildnumber);
98100
String tunnelLogPath = getTunnelLogPath(workspacePath, buildnumber);
99101
logger.info("Tunnel Log Path:" + tunnelLogPath);
100-
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel);
102+
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel, tunnelPidPath);
101103
} else {
102104
logger.warning("tunnelFolderPath empty");
103105
}
@@ -131,9 +133,10 @@ public static Process setUp(String user, String key, LocalTunnel localTunnel, St
131133
logger.info("Tunnel Binary downloaded from " + binaryURL);
132134
}
133135
// Get Tunnel Log path name
136+
String tunnelPidPath = getTunnelPidPathForWindows(workspacePath, buildnumber);
134137
String tunnelLogPath = getTunnelLogPathForWindows(workspacePath, buildnumber);
135138
logger.info("Tunnel Log Path:" + tunnelLogPath);
136-
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel,Constant.OS.WIN);
139+
return runCommandLine(tunnelBinaryLocation, tunnelLogPath, user, key, tunnelName,localTunnel,tunnelPidPath, Constant.OS.WIN);
137140
} else {
138141
logger.warning("tunnelFolderPath empty");
139142
}
@@ -210,6 +213,36 @@ private static String getTunnelLogPath(FilePath workspacePath, String buildnumbe
210213
}
211214
return tunnelLogPath;
212215
}
216+
private static String getPidPath(FilePath workspacePath, String buildnumber) {
217+
String tunnelPidPath = ".pid";
218+
try {
219+
if (workspacePath != null) {
220+
// Create Tunnel Log Path
221+
tunnelPidPath = new StringBuilder(buildnumber).append(".pid").toString();
222+
223+
// Create a Folder in workspace
224+
FilePath tunnelFolderPath = new FilePath(workspacePath, Constant.DEFAULT_TUNNEL_FOLDER_NAME);
225+
File folder = new File(tunnelFolderPath.getRemote());
226+
if (!folder.exists()) {
227+
if (folder.mkdir()) {
228+
logger.info("Directory is created! at " + tunnelFolderPath.getRemote());
229+
FilePath tunnelPath = new FilePath(tunnelFolderPath, tunnelPidPath);
230+
return tunnelPath.getRemote();
231+
} else {
232+
logger.info("Failed to create directory! at " + tunnelFolderPath.getRemote());
233+
FilePath tunnelPath = new FilePath(workspacePath, tunnelPidPath);
234+
return tunnelPath.getRemote();
235+
}
236+
} else {
237+
FilePath tunnelPath = new FilePath(tunnelFolderPath, tunnelPidPath);
238+
return tunnelPath.getRemote();
239+
}
240+
}
241+
} catch (Exception e) {
242+
logger.info(e.getMessage());
243+
}
244+
return tunnelPidPath;
245+
}
213246

214247
private static String getTunnelLogPathForWindows(FilePath workspacePath, String buildnumber) {
215248
String tunnelLogPath = "tunnel.log";
@@ -242,6 +275,37 @@ private static String getTunnelLogPathForWindows(FilePath workspacePath, String
242275
return tunnelLogPath;
243276
}
244277

278+
private static String getTunnelPidPathForWindows(FilePath workspacePath, String buildnumber) {
279+
String tunnelPidPath = ".pid";
280+
try {
281+
if (workspacePath != null) {
282+
// Create Tunnel Log Path
283+
tunnelPidPath = new StringBuilder(buildnumber).append(".log").toString();
284+
285+
// Create a Folder in workspace
286+
FilePath tunnelFolderPath = new FilePath(workspacePath, Constant.DEFAULT_TUNNEL_FOLDER_NAME);
287+
File folder = new File(tunnelFolderPath.getRemote());
288+
if (!folder.exists()) {
289+
if (folder.mkdir()) {
290+
logger.info("Directory is created! at " + tunnelFolderPath.getRemote());
291+
FilePath tunnelPath = new FilePath(tunnelFolderPath, tunnelPidPath);
292+
return tunnelPath.getRemote();
293+
} else {
294+
logger.info("Failed to create directory! at " + tunnelFolderPath.getRemote());
295+
FilePath tunnelPath = new FilePath(workspacePath, tunnelPidPath);
296+
return tunnelPath.getRemote();
297+
}
298+
} else {
299+
FilePath tunnelPath = new FilePath(tunnelFolderPath, tunnelPidPath);
300+
return tunnelPath.getRemote();
301+
}
302+
}
303+
} catch (Exception e) {
304+
logger.info(e.getMessage());
305+
}
306+
return tunnelPidPath;
307+
}
308+
245309
private static String getLatestHash(String url) throws TunnelHashNotFoundException {
246310
try {
247311
return CapabilityService.sendGetRequest(url);
@@ -327,7 +391,7 @@ public static void unZipIt(String tunnelBinaryZipFileName, String tunnelBinaryFi
327391
}
328392

329393
public static Process runCommandLine(String filePath, String tunnelLogPath, String user, String key,
330-
String tunnelName, LocalTunnel localTunnel,String ...args) throws IOException {
394+
String tunnelName, LocalTunnel localTunnel, String tunnelPidPath, String ...args) throws IOException {
331395
try {
332396
int availablePort= PortAvailabilityUtils.randomFreePort();
333397
//Updating permissions
@@ -351,6 +415,7 @@ public static Process runCommandLine(String filePath, String tunnelLogPath, Stri
351415
list.addAll(Arrays.asList(extCommands));
352416
}
353417
list.add("-v");
418+
list.add("--pidfile");list.add(tunnelPidPath);
354419

355420
// create the process
356421
ProcessBuilder processBuilder = new ProcessBuilder(list);

0 commit comments

Comments
 (0)