diff options
author | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2018-11-08 13:27:20 +0100 |
---|---|---|
committer | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2018-11-08 13:27:20 +0100 |
commit | 22f119ef6f01b6be2700d36cb8ed3e61129012f9 (patch) | |
tree | ed9971c25e4f0a6b2b94642ac748ce6061709ba6 /controller-api | |
parent | abbc7d2b6fa550e31054c001a343e0afebfef418 (diff) |
Move SourceRevision and ApplicationVersion to controller-api
Diffstat (limited to 'controller-api')
2 files changed, 147 insertions, 0 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/ApplicationVersion.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/ApplicationVersion.java new file mode 100644 index 00000000000..0e14be29fc5 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/ApplicationVersion.java @@ -0,0 +1,99 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.deployment; + +import java.util.Objects; +import java.util.Optional; +import java.util.OptionalLong; + +/** + * An application package version, identified by a source revision and a build number. + * + * @author bratseth + * @author mpolden + */ +public class ApplicationVersion implements Comparable<ApplicationVersion> { + + /** + * Used in cases where application version cannot be determined, such as manual deployments (e.g. in dev + * environment) + */ + public static final ApplicationVersion unknown = new ApplicationVersion(Optional.empty(), OptionalLong.empty()); + + // This never changes and is only used to create a valid semantic version number, as required by application bundles + private static final String majorVersion = "1.0"; + + private final Optional<SourceRevision> source; + private final OptionalLong buildNumber; + + private ApplicationVersion(Optional<SourceRevision> source, OptionalLong buildNumber) { + Objects.requireNonNull(source, "source cannot be null"); + Objects.requireNonNull(buildNumber, "buildNumber cannot be null"); + if (source.isPresent() != buildNumber.isPresent()) { + throw new IllegalArgumentException("both buildNumber and source must be set together"); + } + if (buildNumber.isPresent() && buildNumber.getAsLong() <= 0) { + throw new IllegalArgumentException("buildNumber must be > 0"); + } + this.source = source; + this.buildNumber = buildNumber; + } + + /** Create an application package version from a completed build */ + public static ApplicationVersion from(SourceRevision source, long buildNumber) { + return new ApplicationVersion(Optional.of(source), OptionalLong.of(buildNumber)); + } + + /** Returns an unique identifier for this version or "unknown" if version is not known */ + public String id() { + if (isUnknown()) { + return "unknown"; + } + return String.format("%s.%d-%s", majorVersion, buildNumber.getAsLong(), abbreviateCommit(source.get().commit())); + } + + /** + * Returns information about the source of this revision, or empty if the source is not know/defined + * (which is the case for command-line deployment from developers, but never for deployment jobs) + */ + public Optional<SourceRevision> source() { return source; } + + /** Returns the build number that built this version */ + public OptionalLong buildNumber() { return buildNumber; } + + /** Returns whether this is unknown */ + public boolean isUnknown() { + return this.equals(unknown); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ApplicationVersion that = (ApplicationVersion) o; + return Objects.equals(source, that.source) && + Objects.equals(buildNumber, that.buildNumber); + } + + @Override + public int hashCode() { + return Objects.hash(source, buildNumber); + } + + @Override + public String toString() { + return "Application package version: " + id() + source.map(s -> ", " + s.toString()).orElse(""); + } + + /** Abbreviate given commit hash to 9 characters */ + private static String abbreviateCommit(String hash) { + return hash.length() <= 9 ? hash : hash.substring(0, 9); + } + + @Override + public int compareTo(ApplicationVersion o) { + if ( ! buildNumber().isPresent() || ! o.buildNumber().isPresent()) + return Boolean.compare(buildNumber().isPresent(), o.buildNumber.isPresent()); // Application package hash sorts first + + return Long.compare(buildNumber().getAsLong(), o.buildNumber().getAsLong()); + } +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/SourceRevision.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/SourceRevision.java new file mode 100644 index 00000000000..a9c1155c5ef --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/deployment/SourceRevision.java @@ -0,0 +1,48 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.deployment; + +import java.util.Objects; + +/** + * A revision in a source repository + * + * @author bratseth + */ +public class SourceRevision { + + private final String repository; + private final String branch; + private final String commit; + + public SourceRevision(String repository, String branch, String commit) { + Objects.requireNonNull(repository, "repository cannot be null"); + Objects.requireNonNull(branch, "branch cannot be null"); + Objects.requireNonNull(commit, "commit cannot be null"); + this.repository = repository; + this.branch = branch; + this.commit = commit; + } + + public String repository() { return repository; } + public String branch() { return branch; } + public String commit() { return commit; } + + @Override + public int hashCode() { return Objects.hash(repository, branch, commit); } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if ( ! (o instanceof SourceRevision)) return false; + + SourceRevision other = (SourceRevision)o; + return this.repository.equals(other.repository) && + this.branch.equals(other.branch) && + this.commit.equals(other.commit); + } + + @Override + public String toString() { return "source revision of repository '" + repository + + "', branch '" + branch + "' with commit '" + commit + "'"; } + +} |