Build: use jar metadata instead of expecting a properties file for xpack

build info

There are many other things that should be cleaned up around this (eg
XpackInfoResponse.BuildInfo should not exist, it is the exact same as
what XPackBuild has), but this change gets the build info output working
again.

closes elastic/elasticsearch#2116

Original commit: elastic/x-pack-elasticsearch@0730daf031
This commit is contained in:
Ryan Ernst 2016-04-27 13:32:59 -07:00
parent f4f156b351
commit 4d1f4a244a
2 changed files with 47 additions and 51 deletions

View File

@ -5,78 +5,74 @@
*/ */
package org.elasticsearch.xpack; package org.elasticsearch.xpack;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; 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.IOException;
import java.io.InputStream; import java.net.URISyntaxException;
import java.util.Properties; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
/** /**
* * Information about the built version of x-pack that is running.
*/ */
public class XPackBuild { public class XPackBuild {
public static final XPackBuild CURRENT; public static final XPackBuild CURRENT;
static { static {
String hash = "NA"; final String shortHash;
String hashShort = "NA"; final String date;
String timestamp = "NA";
try (InputStream is = XPackBuild.class.getResourceAsStream("/xpack-build.properties")) { Path path = getElasticsearchCodebase();
Properties props = new Properties(); if (path.toString().endsWith(".jar")) {
props.load(is); try (JarInputStream jar = new JarInputStream(Files.newInputStream(path))) {
hash = props.getProperty("hash", hash); Manifest manifest = jar.getManifest();
if (!hash.equals("NA")) { shortHash = manifest.getMainAttributes().getValue("Change");
hashShort = hash.substring(0, 7); date = manifest.getMainAttributes().getValue("Build-Date");
} catch (IOException e) {
throw new RuntimeException(e);
} }
String gitTimestampRaw = props.getProperty("timestamp"); } else {
if (gitTimestampRaw != null) { // not running from a jar (unit tests, IDE)
timestamp = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC).print(Long.parseLong(gitTimestampRaw)); shortHash = "Unknown";
} date = "Unknown";
} catch (Exception e) {
// just ignore...
} }
CURRENT = new XPackBuild(hash, hashShort, timestamp); CURRENT = new XPackBuild(shortHash, date);
} }
private String hash; /**
private String hashShort; * Returns path to xpack codebase path
private String timestamp; */
@SuppressForbidden(reason = "looks up path of xpack.jar directly")
XPackBuild(String hash, String hashShort, String timestamp) { static Path getElasticsearchCodebase() {
this.hash = hash; URL url = XPackBuild.class.getProtectionDomain().getCodeSource().getLocation();
this.hashShort = hashShort; try {
this.timestamp = timestamp; return PathUtils.get(url.toURI());
} catch (URISyntaxException bogus) {
throw new RuntimeException(bogus);
}
} }
public String hash() { private String shortHash;
return hash; private String date;
XPackBuild(String shortHash, String date) {
this.shortHash = shortHash;
this.date = date;
} }
public String hashShort() { public String shortHash() {
return hashShort; return shortHash;
} }
public String timestamp() { public String date() {
return timestamp; return date;
}
public static XPackBuild readBuild(StreamInput in) throws IOException {
String hash = in.readString();
String hashShort = in.readString();
String timestamp = in.readString();
return new XPackBuild(hash, hashShort, timestamp);
}
public static void writeBuild(XPackBuild build, StreamOutput out) throws IOException {
out.writeString(build.hash());
out.writeString(build.hashShort());
out.writeString(build.timestamp());
} }
} }

View File

@ -142,7 +142,7 @@ public class XPackInfoResponse extends ActionResponse {
private final String timestamp; private final String timestamp;
public BuildInfo(XPackBuild build) { public BuildInfo(XPackBuild build) {
this(build.hash(), build.timestamp()); this(build.shortHash(), build.date());
} }
public BuildInfo(StreamInput input) throws IOException { public BuildInfo(StreamInput input) throws IOException {
@ -166,7 +166,7 @@ public class XPackInfoResponse extends ActionResponse {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject() return builder.startObject()
.field("hash", hash) .field("hash", hash)
.field("timestamp", timestamp) .field("date", timestamp)
.endObject(); .endObject();
} }