From 7ae57628de90acfeca43a2a431cf94aa51ba6ae0 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Mon, 16 Sep 2024 14:58:38 +0200 Subject: [PATCH 1/3] Improve job hostname detection on Windows --- .../plugins/datadog/DatadogUtilities.java | 62 ++++++++++++++----- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java index ff36c5e8..5d03224f 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java @@ -743,19 +743,29 @@ public static boolean isMainNode(String nodeName) { } public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Computer computer) { - if (envVars != null) { - String ddHostname = envVars.get(DatadogGlobalConfiguration.DD_CI_HOSTNAME); - if (DatadogUtilities.isValidHostname(ddHostname)) { - return ddHostname; - } - String hostname = envVars.get("HOSTNAME"); - if (DatadogUtilities.isValidHostname(hostname)) { - return hostname; + if (computer != null) { + try { + EnvVars computerEnvironment = computer.getEnvironment(); + String computerEnvVarsHostname = getNodeHostname(computerEnvironment); + if (isValidHostname(computerEnvVarsHostname)) { + return computerEnvVarsHostname; + } + + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logException(logger, Level.FINE, "Interrupted while trying to get computer env vars", e); + } catch (Exception e) { + logException(logger, Level.FINE, "Error getting computer env vars", e); } } + String envVarsHostname = getNodeHostname(envVars); + if (isValidHostname(envVarsHostname)) { + return envVarsHostname; + } + try { - if(computer != null) { + if (computer != null) { String computerNodeName = DatadogUtilities.getNodeName(computer); if (DatadogUtilities.isMainNode(computerNodeName)) { String masterHostname = DatadogUtilities.getHostname(null); @@ -771,27 +781,45 @@ public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Comput Node node = computer.getNode(); if (node != null) { + String[] command; FilePath rootPath = node.getRootPath(); if (isUnix(rootPath)) { - ShellCommandCallable hostnameCommand = new ShellCommandCallable( - Collections.emptyMap(), HOSTNAME_CMD_TIMEOUT_MILLIS, "hostname", "-f"); - String shellHostname = rootPath.act(hostnameCommand).trim(); - if (DatadogUtilities.isValidHostname(shellHostname)) { - return shellHostname; - } + command = new String[]{"hostname", "-f"}; + } else { + command = new String[]{"hostname"}; + } + + ShellCommandCallable hostnameCommand = new ShellCommandCallable(Collections.emptyMap(), HOSTNAME_CMD_TIMEOUT_MILLIS, command); + String shellHostname = rootPath.act(hostnameCommand).trim(); + if (DatadogUtilities.isValidHostname(shellHostname)) { + return shellHostname; } } } - } catch (InterruptedException e){ + } catch (InterruptedException e) { Thread.currentThread().interrupt(); logException(logger, Level.FINE, "Interrupted while trying to extract hostname from StepContext.", e); - } catch (Exception e){ + } catch (Exception e) { logException(logger, Level.FINE, "Unable to get hostname for node " + computer.getName(), e); } return null; } + private static String getNodeHostname(@Nullable EnvVars envVars) { + if (envVars != null) { + String ddHostname = envVars.get(DatadogGlobalConfiguration.DD_CI_HOSTNAME); + if (DatadogUtilities.isValidHostname(ddHostname)) { + return ddHostname; + } + String hostname = envVars.get("HOSTNAME"); + if (DatadogUtilities.isValidHostname(hostname)) { + return hostname; + } + } + return null; + } + private static boolean isUnix(FilePath filePath) throws IOException, InterruptedException { return filePath != null && filePath.act(new IsUnix()); } From 01e404ec33ba82da7e7a508829f0cb7219dd0232 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Tue, 17 Sep 2024 13:04:37 +0200 Subject: [PATCH 2/3] Fix spotbugs warning --- .../plugins/datadog/DatadogUtilities.java | 79 ++++++------------- 1 file changed, 25 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java index 5d03224f..4148ef1b 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java @@ -26,49 +26,9 @@ of this software and associated documentation files (the "Software"), to deal package org.datadog.jenkins.plugins.datadog; import edu.umd.cs.findbugs.annotations.NonNull; -import hudson.EnvVars; -import hudson.ExtensionList; -import hudson.FilePath; -import hudson.PluginManager; -import hudson.PluginWrapper; -import hudson.model.Actionable; -import hudson.model.Computer; -import hudson.model.Item; -import hudson.model.Node; -import hudson.model.Result; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.model.User; +import hudson.*; +import hudson.model.*; import hudson.model.labels.LabelAtom; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.Inet4Address; -import java.net.UnknownHostException; -import java.nio.charset.Charset; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import java.util.function.Function; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import jenkins.model.Jenkins; import jenkins.security.MasterToSlaveCallable; import org.apache.commons.lang.StringEscapeUtils; @@ -82,15 +42,7 @@ of this software and associated documentation files (the "Software"), to deal import org.datadog.jenkins.plugins.datadog.util.SuppressFBWarnings; import org.datadog.jenkins.plugins.datadog.util.TagsUtil; import org.jenkinsci.plugins.pipeline.StageStatus; -import org.jenkinsci.plugins.workflow.actions.ArgumentsAction; -import org.jenkinsci.plugins.workflow.actions.ErrorAction; -import org.jenkinsci.plugins.workflow.actions.LabelAction; -import org.jenkinsci.plugins.workflow.actions.NotExecutedNodeAction; -import org.jenkinsci.plugins.workflow.actions.QueueItemAction; -import org.jenkinsci.plugins.workflow.actions.StageAction; -import org.jenkinsci.plugins.workflow.actions.ThreadNameAction; -import org.jenkinsci.plugins.workflow.actions.TimingAction; -import org.jenkinsci.plugins.workflow.actions.WarningAction; +import org.jenkinsci.plugins.workflow.actions.*; import org.jenkinsci.plugins.workflow.flow.FlowExecution; import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner; import org.jenkinsci.plugins.workflow.graph.BlockEndNode; @@ -98,6 +50,22 @@ of this software and associated documentation files (the "Software"), to deal import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.io.*; +import java.net.Inet4Address; +import java.net.UnknownHostException; +import java.nio.charset.Charset; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.function.Function; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + public class DatadogUtilities { private static final Logger logger = Logger.getLogger(DatadogUtilities.class.getName()); @@ -790,9 +758,12 @@ public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Comput } ShellCommandCallable hostnameCommand = new ShellCommandCallable(Collections.emptyMap(), HOSTNAME_CMD_TIMEOUT_MILLIS, command); - String shellHostname = rootPath.act(hostnameCommand).trim(); - if (DatadogUtilities.isValidHostname(shellHostname)) { - return shellHostname; + String shellHostname = rootPath.act(hostnameCommand); + if (shellHostname != null) { + String trimmedHostname = shellHostname.trim(); + if (DatadogUtilities.isValidHostname(trimmedHostname)) { + return trimmedHostname; + } } } } From 4a038a2403fa142335ccc8672a6ab053bdbf3d80 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Tue, 17 Sep 2024 17:26:20 +0200 Subject: [PATCH 3/3] Fix spotbugs warning --- .../plugins/datadog/DatadogUtilities.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java index 4148ef1b..42688c0b 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogUtilities.java @@ -749,20 +749,19 @@ public static String getNodeHostname(@Nullable EnvVars envVars, @Nullable Comput Node node = computer.getNode(); if (node != null) { - String[] command; FilePath rootPath = node.getRootPath(); - if (isUnix(rootPath)) { - command = new String[]{"hostname", "-f"}; - } else { - command = new String[]{"hostname"}; - } + if (rootPath != null) { + String[] command; + if (isUnix(rootPath)) { + command = new String[]{"hostname", "-f"}; + } else { + command = new String[]{"hostname"}; + } - ShellCommandCallable hostnameCommand = new ShellCommandCallable(Collections.emptyMap(), HOSTNAME_CMD_TIMEOUT_MILLIS, command); - String shellHostname = rootPath.act(hostnameCommand); - if (shellHostname != null) { - String trimmedHostname = shellHostname.trim(); - if (DatadogUtilities.isValidHostname(trimmedHostname)) { - return trimmedHostname; + ShellCommandCallable hostnameCommand = new ShellCommandCallable(Collections.emptyMap(), HOSTNAME_CMD_TIMEOUT_MILLIS, command); + String shellHostname = rootPath.act(hostnameCommand).trim(); + if (DatadogUtilities.isValidHostname(shellHostname)) { + return shellHostname; } } }