Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update order of hostname detection logic #360

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ of this software and associated documentation files (the "Software"), to deal
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.datadog.jenkins.plugins.datadog.clients.HttpClient;
import org.datadog.jenkins.plugins.datadog.model.CIGlobalTagsAction;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
import org.datadog.jenkins.plugins.datadog.model.GitRepositoryAction;
Expand Down Expand Up @@ -465,36 +466,23 @@ public static Map<String, Set<String>> computeTagListFromVarList(EnvVars envVars

public static String getAwsInstanceID() throws IOException {
String metadataUrl = "http://169.254.169.254/latest/meta-data/instance-id";
HttpURLConnection conn = null;
String instance_id = null;
HttpClient client = null;
// Make request
conn = getHttpURLConnection(new URL(metadataUrl), 300);
conn.setRequestMethod("GET");

// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();

// Validate
instance_id = result.toString();
try {
if (conn.getResponseCode() == 404) {
logger.fine("Could not retrieve AWS instance ID");
}
conn.disconnect();
} catch (IOException e) {
logger.info("Failed to inspect HTTP response when getting AWS Instance ID");
}

if (instance_id.equals("")) {
client = new HttpClient(60_000);
return client.get(metadataUrl, Collections.emptyMap(), result -> {
String instance_id = result.toString();
logger.fine("Instance ID detected: " + instance_id);
return instance_id;
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
DatadogUtilities.severe(logger, e, "Could not retrieve the AWS instance ID");
return null;
} catch (Exception e) {
DatadogUtilities.severe(logger, e, "Could not retrieve the AWS instance ID");
return null;
}
return instance_id;
}

/**
Expand Down Expand Up @@ -523,24 +511,17 @@ public static String getHostname(EnvVars envVars) {
} catch (NullPointerException e) {
// noop
}

if (isValidHostname(hostname)) {
logger.fine("Using hostname set in 'Manage Plugins'. Hostname: " + hostname);
return hostname;
}

// Check hostname using jenkins env variables
if (envVars != null) {
hostname = envVars.get("HOSTNAME");
if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME agent environment variable. Hostname: " + hostname);
return hostname;
}
}

final DatadogGlobalConfiguration datadogGlobalConfig = getDatadogGlobalDescriptor();
if (datadogGlobalConfig != null){
if (datadogGlobalConfig.isUseAwsInstanceHostname()) {
try {
logger.fine("Attempting to resolve AWS instance ID for hostname");
hostname = getAwsInstanceID();
} catch (IOException e) {
logger.fine("Error retrieving AWS hostname: " + e);
Expand All @@ -552,6 +533,15 @@ public static String getHostname(EnvVars envVars) {
}
}

// Check hostname using jenkins env variables
if (envVars != null) {
hostname = envVars.get("HOSTNAME");
if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME agent environment variable. Hostname: " + hostname);
return hostname;
}
}

if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME controller environment variable. Hostname: " + hostname);
return hostname;
Expand Down