From 8278c9887e427772c9fd909a5c4fcfd6e1a0c878 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 5 Nov 2015 14:10:59 -0500 Subject: [PATCH] Restore build properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit restores the build properties provided in org.elasticsearch.Build. This class previously obtained the build hash and timestamp from a resource es-build.properties that was included in the jar and produced by the Maven resources plugin. After the switch to Gradle, the production of this file was lost and these build properties defaulted to “NA” in all instances. The most important place that the build hash is used is in the plugin manager to determine the URL of staging artifacts for plugins. The build hash is also used in several responses including the /_nodes response and the response to HTTP GET requests on the root path. These properties can now be obtained from the jar manifest as they are currently placed there by the gradle-info plugin. However, only the short hash is provided. We now read the manifest for these properties and no longer provide the full hash in responses to HTTP GET requests on the root path. --- .../main/java/org/elasticsearch/Build.java | 70 ++++++++----------- .../main/java/org/elasticsearch/Version.java | 2 +- .../cluster/node/info/NodesInfoResponse.java | 2 +- .../bootstrap/BootstrapCLIParser.java | 2 +- .../http/client/HttpDownloadHelper.java | 2 +- .../java/org/elasticsearch/node/Node.java | 2 +- .../elasticsearch/plugins/PluginManager.java | 2 +- .../rest/action/cat/RestNodesAction.java | 2 +- .../rest/action/main/RestMainAction.java | 4 +- core/src/main/resources/es-build.properties | 3 - .../bootstrap/BootstrapCliParserTests.java | 12 ++-- .../plugins/PluginManagerUnitTests.java | 4 +- 12 files changed, 45 insertions(+), 62 deletions(-) delete mode 100644 core/src/main/resources/es-build.properties diff --git a/core/src/main/java/org/elasticsearch/Build.java b/core/src/main/java/org/elasticsearch/Build.java index 508b4dc4375..1fb0b60b341 100644 --- a/core/src/main/java/org/elasticsearch/Build.java +++ b/core/src/main/java/org/elasticsearch/Build.java @@ -19,81 +19,67 @@ package org.elasticsearch; +import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.joda.time.DateTimeZone; -import org.joda.time.format.ISODateTimeFormat; import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; +import java.util.jar.JarFile; +import java.util.jar.Manifest; /** */ +@SuppressForbidden(reason = "needs JarFile to read the manifest") public class Build { public static final Build CURRENT; static { - String hash = "NA"; - String hashShort = "NA"; - String timestamp = "NA"; + String shortHash = "Unknown"; + String date = "Unknown"; - try (InputStream is = Build.class.getResourceAsStream("/es-build.properties")){ - Properties props = new Properties(); - props.load(is); - hash = props.getProperty("hash", hash); - if (!hash.equals("NA")) { - hashShort = hash.substring(0, 7); - } - String gitTimestampRaw = props.getProperty("timestamp"); - if (gitTimestampRaw != null) { - timestamp = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).print(Long.parseLong(gitTimestampRaw)); - } - } catch (Exception e) { + String path = Build.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + try { + JarFile jar = new JarFile(path); + Manifest manifest = jar.getManifest(); + shortHash = manifest.getMainAttributes().getValue("Change"); + date = manifest.getMainAttributes().getValue("Build-Date"); + } catch (IOException e) { // just ignore... } - CURRENT = new Build(hash, hashShort, timestamp); + CURRENT = new Build(shortHash, date); } - private String hash; - private String hashShort; - private String timestamp; + private String shortHash; + private String date; - Build(String hash, String hashShort, String timestamp) { - this.hash = hash; - this.hashShort = hashShort; - this.timestamp = timestamp; + Build(String shortHash, String date) { + this.shortHash = shortHash; + this.date = date; } - public String hash() { - return hash; + public String shortHash() { + return shortHash; } - public String hashShort() { - return hashShort; - } - - public String timestamp() { - return timestamp; + public String date() { + return date; } public static Build readBuild(StreamInput in) throws IOException { String hash = in.readString(); - String hashShort = in.readString(); - String timestamp = in.readString(); - return new Build(hash, hashShort, timestamp); + String date = in.readString(); + return new Build(hash, date); } public static void writeBuild(Build build, StreamOutput out) throws IOException { - out.writeString(build.hash()); - out.writeString(build.hashShort()); - out.writeString(build.timestamp()); + out.writeString(build.shortHash()); + out.writeString(build.date()); } @Override public String toString() { - return "[" + hash + "][" + timestamp + "]"; + return "[" + shortHash + "][" + date + "]"; } } diff --git a/core/src/main/java/org/elasticsearch/Version.java b/core/src/main/java/org/elasticsearch/Version.java index 2da8087734d..1b4bd35faa3 100644 --- a/core/src/main/java/org/elasticsearch/Version.java +++ b/core/src/main/java/org/elasticsearch/Version.java @@ -661,7 +661,7 @@ public class Version { @SuppressForbidden(reason = "System.out.*") public static void main(String[] args) { - System.out.println("Version: " + Version.CURRENT + ", Build: " + Build.CURRENT.hashShort() + "/" + Build.CURRENT.timestamp() + ", JVM: " + JvmInfo.jvmInfo().version()); + System.out.println("Version: " + Version.CURRENT + ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date() + ", JVM: " + JvmInfo.jvmInfo().version()); } @Override diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoResponse.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoResponse.java index 108bb314d4a..329be6c7614 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoResponse.java @@ -77,7 +77,7 @@ public class NodesInfoResponse extends BaseNodesResponse implements To builder.field("ip", nodeInfo.getNode().getHostAddress(), XContentBuilder.FieldCaseConversion.NONE); builder.field("version", nodeInfo.getVersion()); - builder.field("build", nodeInfo.getBuild().hashShort()); + builder.field("build_hash", nodeInfo.getBuild().shortHash()); if (nodeInfo.getServiceAttributes() != null) { for (Map.Entry nodeAttribute : nodeInfo.getServiceAttributes().entrySet()) { diff --git a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCLIParser.java b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCLIParser.java index 03ec082a285..1e5175b4e5d 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCLIParser.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCLIParser.java @@ -81,7 +81,7 @@ final class BootstrapCLIParser extends CliTool { @Override public ExitStatus execute(Settings settings, Environment env) throws Exception { - terminal.println("Version: %s, Build: %s/%s, JVM: %s", org.elasticsearch.Version.CURRENT, Build.CURRENT.hashShort(), Build.CURRENT.timestamp(), JvmInfo.jvmInfo().version()); + terminal.println("Version: %s, Build: %s/%s, JVM: %s", org.elasticsearch.Version.CURRENT, Build.CURRENT.shortHash(), Build.CURRENT.date(), JvmInfo.jvmInfo().version()); return ExitStatus.OK_AND_EXIT; } } diff --git a/core/src/main/java/org/elasticsearch/common/http/client/HttpDownloadHelper.java b/core/src/main/java/org/elasticsearch/common/http/client/HttpDownloadHelper.java index 7fe26ed81d9..01079ecf45a 100644 --- a/core/src/main/java/org/elasticsearch/common/http/client/HttpDownloadHelper.java +++ b/core/src/main/java/org/elasticsearch/common/http/client/HttpDownloadHelper.java @@ -358,7 +358,7 @@ public class HttpDownloadHelper { connection.setConnectTimeout(5000); } connection.setRequestProperty("ES-Version", Version.CURRENT.toString()); - connection.setRequestProperty("ES-Build-Hash", Build.CURRENT.hashShort()); + connection.setRequestProperty("ES-Build-Hash", Build.CURRENT.shortHash()); connection.setRequestProperty("User-Agent", "elasticsearch-plugin-manager"); // connect to the remote site (may take some time) diff --git a/core/src/main/java/org/elasticsearch/node/Node.java b/core/src/main/java/org/elasticsearch/node/Node.java index eb7748cfd9a..d245692acc9 100644 --- a/core/src/main/java/org/elasticsearch/node/Node.java +++ b/core/src/main/java/org/elasticsearch/node/Node.java @@ -135,7 +135,7 @@ public class Node implements Releasable { Settings tmpSettings = TribeService.processSettings(tmpEnv.settings()); ESLogger logger = Loggers.getLogger(Node.class, tmpSettings.get("name")); - logger.info("version[{}], pid[{}], build[{}/{}]", version, JvmInfo.jvmInfo().pid(), Build.CURRENT.hashShort(), Build.CURRENT.timestamp()); + logger.info("version[{}], pid[{}], build[{}/{}]", version, JvmInfo.jvmInfo().pid(), Build.CURRENT.shortHash(), Build.CURRENT.date()); logger.info("initializing ..."); diff --git a/core/src/main/java/org/elasticsearch/plugins/PluginManager.java b/core/src/main/java/org/elasticsearch/plugins/PluginManager.java index 16254c7ca64..410bf1a8411 100644 --- a/core/src/main/java/org/elasticsearch/plugins/PluginManager.java +++ b/core/src/main/java/org/elasticsearch/plugins/PluginManager.java @@ -574,7 +574,7 @@ public class PluginManager { // Elasticsearch new download service uses groupId org.elasticsearch.plugin from 2.0.0 if (user == null) { if (!Strings.isNullOrEmpty(System.getProperty(PROPERTY_SUPPORT_STAGING_URLS))) { - addUrl(urls, String.format(Locale.ROOT, "https://download.elastic.co/elasticsearch/staging/%s-%s/org/elasticsearch/plugin/%s/%s/%s-%s.zip", version, Build.CURRENT.hashShort(), name, version, name, version)); + addUrl(urls, String.format(Locale.ROOT, "https://download.elastic.co/elasticsearch/staging/%s-%s/org/elasticsearch/plugin/%s/%s/%s-%s.zip", version, Build.CURRENT.shortHash(), name, version, name, version)); } addUrl(urls, String.format(Locale.ROOT, "https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/%s/%s/%s-%s.zip", name, version, name, version)); } else { diff --git a/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java b/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java index 337dd41b403..50f1baa5ad0 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java @@ -245,7 +245,7 @@ public class RestNodesAction extends AbstractCatAction { } table.addCell(node.getVersion().number()); - table.addCell(info == null ? null : info.getBuild().hashShort()); + table.addCell(info == null ? null : info.getBuild().shortHash()); table.addCell(jvmInfo == null ? null : jvmInfo.version()); table.addCell(fsInfo == null ? null : fsInfo.getTotal().getAvailable()); table.addCell(jvmStats == null ? null : jvmStats.getMem().getHeapUsed()); diff --git a/core/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java b/core/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java index 5b567b0e32c..df99979eb6b 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java @@ -77,8 +77,8 @@ public class RestMainAction extends BaseRestHandler { builder.field("cluster_name", clusterName.value()); builder.startObject("version") .field("number", version.number()) - .field("build_hash", Build.CURRENT.hash()) - .field("build_timestamp", Build.CURRENT.timestamp()) + .field("build_hash", Build.CURRENT.shortHash()) + .field("build_date", Build.CURRENT.date()) .field("build_snapshot", version.snapshot) .field("lucene_version", version.luceneVersion.toString()) .endObject(); diff --git a/core/src/main/resources/es-build.properties b/core/src/main/resources/es-build.properties deleted file mode 100644 index 563ecddcaf6..00000000000 --- a/core/src/main/resources/es-build.properties +++ /dev/null @@ -1,3 +0,0 @@ -version=${project.version} -hash=${buildNumber} -timestamp=${timestamp} diff --git a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCliParserTests.java b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCliParserTests.java index 8b6803420a6..567a664e2f8 100644 --- a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCliParserTests.java +++ b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCliParserTests.java @@ -54,8 +54,8 @@ public class BootstrapCliParserTests extends CliToolTestCase { assertStatus(status, OK_AND_EXIT); assertThatTerminalOutput(containsString(Version.CURRENT.toString())); - assertThatTerminalOutput(containsString(Build.CURRENT.hashShort())); - assertThatTerminalOutput(containsString(Build.CURRENT.timestamp())); + assertThatTerminalOutput(containsString(Build.CURRENT.shortHash())); + assertThatTerminalOutput(containsString(Build.CURRENT.date())); assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version())); } @@ -65,8 +65,8 @@ public class BootstrapCliParserTests extends CliToolTestCase { assertStatus(status, OK_AND_EXIT); assertThatTerminalOutput(containsString(Version.CURRENT.toString())); - assertThatTerminalOutput(containsString(Build.CURRENT.hashShort())); - assertThatTerminalOutput(containsString(Build.CURRENT.timestamp())); + assertThatTerminalOutput(containsString(Build.CURRENT.shortHash())); + assertThatTerminalOutput(containsString(Build.CURRENT.date())); assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version())); CaptureOutputTerminal terminal = new CaptureOutputTerminal(); @@ -75,8 +75,8 @@ public class BootstrapCliParserTests extends CliToolTestCase { assertStatus(status, OK_AND_EXIT); assertThatTerminalOutput(containsString(Version.CURRENT.toString())); - assertThatTerminalOutput(containsString(Build.CURRENT.hashShort())); - assertThatTerminalOutput(containsString(Build.CURRENT.timestamp())); + assertThatTerminalOutput(containsString(Build.CURRENT.shortHash())); + assertThatTerminalOutput(containsString(Build.CURRENT.date())); assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version())); } diff --git a/core/src/test/java/org/elasticsearch/plugins/PluginManagerUnitTests.java b/core/src/test/java/org/elasticsearch/plugins/PluginManagerUnitTests.java index 8814a2110bb..86cdd32344f 100644 --- a/core/src/test/java/org/elasticsearch/plugins/PluginManagerUnitTests.java +++ b/core/src/test/java/org/elasticsearch/plugins/PluginManagerUnitTests.java @@ -78,7 +78,7 @@ public class PluginManagerUnitTests extends ESTestCase { if (supportStagingUrls) { String expectedStagingURL = String.format(Locale.ROOT, "https://download.elastic.co/elasticsearch/staging/%s-%s/org/elasticsearch/plugin/%s/%s/%s-%s.zip", - Version.CURRENT.number(), Build.CURRENT.hashShort(), pluginName, Version.CURRENT.number(), pluginName, Version.CURRENT.number()); + Version.CURRENT.number(), Build.CURRENT.shortHash(), pluginName, Version.CURRENT.number(), pluginName, Version.CURRENT.number()); assertThat(iterator.next().toExternalForm(), is(expectedStagingURL)); } @@ -103,7 +103,7 @@ public class PluginManagerUnitTests extends ESTestCase { if (supportStagingUrls) { String expectedStagingUrl = String.format(Locale.ROOT, "https://download.elastic.co/elasticsearch/staging/%s-%s/org/elasticsearch/plugin/%s/%s/%s-%s.zip", - Version.CURRENT.number(), Build.CURRENT.hashShort(), randomPluginName, Version.CURRENT.number(), randomPluginName, Version.CURRENT.number()); + Version.CURRENT.number(), Build.CURRENT.shortHash(), randomPluginName, Version.CURRENT.number(), randomPluginName, Version.CURRENT.number()); assertThat(iterator.next().toExternalForm(), is(expectedStagingUrl)); }