Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ lazy val commonSettings = Seq(
organizationHomepage := Some(url("http://www.indix.com")),
scalaVersion := "2.11.11",
scalacOptions ++= Seq("-encoding", "UTF-8", "-deprecation", "-unchecked"),
javacOptions ++= Seq("-Xlint:deprecation", "-source", "1.7"),
javacOptions ++= Seq("-Xlint:deprecation", "-source", "1.8"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to enforce 1.8? Scala 2.11 can work with 1.6. Forcing this to 1.8 might break compatibility with Scala 2.11 assumption. Unless we're using Lamdas' and Streams API introduced in JDK8.

Copy link
Copy Markdown
Contributor Author

@rucha3 rucha3 Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using lambdas in the gocd-util module. So, I put that in common settings. Should I make it module specific only?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah Okay, see the above message on moving to Scala. If we decide to do that this comment becomes redundant.

resolvers ++= Seq(
"Clojars" at "http://clojars.org/repo",
"Concurrent Maven Repo" at "http://conjars.org/repo",
Expand Down Expand Up @@ -68,7 +68,7 @@ lazy val utils = (project in file(".")).
site.addMappingsToSiteDir(mappings in(ScalaUnidoc, packageDoc), "latest/api"),
git.remoteRepo := "git@github.com:indix/utils.git"
).
aggregate(coreUtils, storeUtils, sparkUtils)
aggregate(coreUtils, storeUtils, sparkUtils, gocdUtils)

lazy val coreUtils = (project in file("util-core")).
settings(commonSettings: _*).
Expand Down Expand Up @@ -118,3 +118,21 @@ lazy val sparkUtils = (project in file("util-spark")).
"org.bdgenomics.utils" %% "utils-misc" % "0.2.13"
)
)

lazy val gocdUtils = (project in file("util-gocd")).
settings(commonSettings: _*).
settings(publishSettings: _*).
settings(
name := "util-gocd",
crossScalaVersions := Seq("2.10.6", "2.11.11"),
libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % "3.1",
"commons-io" % "commons-io" % "1.3.2",
"com.amazonaws" % "aws-java-sdk-s3" % "1.11.127",
"cd.go.plugin" % "go-plugin-api" % "17.2.0" % Provided,
"com.google.code.gson" % "gson" % "2.2.3",
"junit" % "junit" % "4.12" % Test,
"com.novocode" % "junit-interface" % "0.11" % Test,
"org.mockito" % "mockito-all" % "1.10.19" % Test
)
)
1 change: 1 addition & 0 deletions publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -ex
sbt "project coreUtils" +publishSigned
sbt "project sparkUtils" +publishSigned
sbt "project storeUtils" +publishSigned
sbt "project gocdUtils" +publishSigned
sbt sonatypeReleaseAll

echo "Released"
37 changes: 37 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/models/Artifact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.indix.gocd.models;

public class Artifact {
String pipelineName;
String stageName;
String jobName;
Revision revision;

public Artifact(String pipelineName, String stageName, String jobName) {
this.pipelineName = pipelineName;
this.stageName = stageName;
this.jobName = jobName;
}

public Artifact(String pipelineName, String stageName, String jobName, Revision revision) {
this.pipelineName = pipelineName;
this.stageName = stageName;
this.jobName = jobName;
this.revision = revision;
}

public Artifact withRevision(Revision revision) {
this.revision = revision;
return this;
}

public String prefix(){
return String.format("%s/%s/%s/", pipelineName, stageName, jobName);
}

public String prefixWithRevision(){
if(revision != null)
return String.format("%s/%s/%s/%s/", pipelineName, stageName, jobName, revision.getRevision());
else
return prefix();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.indix.gocd.models;

public class ResponseMetadataConstants {
public static final String TRACEBACK_URL = "traceback_url";
public static final String USER = "user";
public static final String REVISION_COMMENT = "revision_comment";
public static final String COMPLETED = "completed";
public static final String GO_PIPELINE_LABEL = "go_pipeline_label";
}
49 changes: 49 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/models/Revision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.indix.gocd.models;

public class Revision implements Comparable {
private String revision;
private String[] parts;
private Integer major;
private Integer minor;
private Integer patch;

public Revision(String revision) {
this.revision = revision;
this.parts = revision.split("\\.");
this.major = Integer.valueOf(parts[0]);
this.minor = Integer.valueOf(parts[1]);
if (parts.length == 3) {
this.patch = Integer.valueOf(parts[2]);
} else {
this.patch = 0;
}
}

public static Revision base() {
return new Revision("0.0.0");
}

@Override
public int compareTo(Object otherInstance) {
if(! (otherInstance instanceof Revision))
throw new RuntimeException("Cannot compare a non-Revision type with Revision type");
Revision that = (Revision)otherInstance;
int majorDiff = this.major.compareTo(that.major);
int minorDiff = this.minor.compareTo(that.minor);
int patchDiff = this.patch.compareTo(that.patch);

if(majorDiff != 0)
return majorDiff;
else if(minorDiff != 0)
return minorDiff;
else if(patchDiff != 0)
return patchDiff;
else
return 0;
}

public String getRevision() {
return revision;
}

}
41 changes: 41 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/models/RevisionStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.indix.gocd.models;

import com.amazonaws.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class RevisionStatus {
public Revision revision;
public Date lastModified;
public String tracebackUrl;
public String user;
public String revisionLabel;
private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

public RevisionStatus(Revision revision, Date lastModified, String tracebackUrl, String user) {
this(revision, lastModified, tracebackUrl, user, "");
}

public RevisionStatus(Revision revision, Date lastModified, String tracebackUrl, String user, String revisionLabel) {
this.revision = revision;
this.lastModified = lastModified;
this.tracebackUrl = tracebackUrl;
this.user = user;
this.revisionLabel = revisionLabel;
}

public Map toMap() {
final HashMap result = new HashMap();
result.put("revision", revision.getRevision());
result.put("timestamp", new SimpleDateFormat(DATE_PATTERN).format(lastModified));
result.put("user", user);
result.put("revisionComment", String.format("Original revision number: %s",
StringUtils.isNullOrEmpty(revisionLabel) ? "unavailable" : revisionLabel));
result.put("trackbackUrl", tracebackUrl);

return result;
}
}
38 changes: 38 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/utils/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.indix.gocd.utils;

public class Constants {
public static final String METADATA_USER = "user";
public static final String METADATA_TRACEBACK_URL = "traceback_url";
public static final String COMPLETED = "completed";

public static final String GO_ARTIFACTS_S3_BUCKET = "GO_ARTIFACTS_S3_BUCKET";
public static final String GO_SERVER_DASHBOARD_URL = "GO_SERVER_DASHBOARD_URL";

public static final String SOURCEDESTINATIONS = "sourceDestinations";
public static final String DESTINATION_PREFIX = "destinationPrefix";
public static final String ARTIFACTS_BUCKET = "artifactsBucket";

public static final String AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY";
public static final String AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID";
public static final String AWS_REGION = "AWS_REGION";
public static final String AWS_USE_IAM_ROLE = "AWS_USE_IAM_ROLE";
public static final String AWS_STORAGE_CLASS = "AWS_STORAGE_CLASS";
public static final String STORAGE_CLASS_STANDARD = "standard";
public static final String STORAGE_CLASS_STANDARD_IA = "standard-ia";
public static final String STORAGE_CLASS_RRS = "rrs";
public static final String STORAGE_CLASS_GLACIER = "glacier";

public static final String GO_PIPELINE_LABEL = "GO_PIPELINE_LABEL";

public static final String MATERIAL_TYPE = "MaterialType";
public static final String REPO = "Repo";
public static final String PACKAGE = "Package";
public static final String MATERIAL = "Material";
public static final String JOB = "Job";
public static final String STAGE = "Stage";
public static final String SOURCE = "Source";
public static final String SOURCE_PREFIX = "SourcePrefix";
public static final String DESTINATION = "Destination";

public static final String REQUIRED_FIELD_MESSAGE = "This field is required";
}
39 changes: 39 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/utils/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.indix.gocd.utils;


import com.thoughtworks.go.plugin.api.task.JobConsoleLogger;

import java.nio.file.Paths;
import java.util.Map;

public class Context {
private final Map environmentVariables;
private final String workingDir;
private final JobConsoleLogger console;

public Context(Map context) {
environmentVariables = (Map) context.get("environmentVariables");
workingDir = (String) context.get("workingDirectory");
console = new JobConsoleLogger() {};
}

public void printMessage(String message) {
console.printLine(message);
}

public void printEnvironment() {
console.printEnvironment(environmentVariables);
}

public Map getEnvironmentVariables() {
return environmentVariables;
}

public String getWorkingDir() {
return workingDir;
}

public String getAbsoluteWorkingDir() {
return Paths.get("").toAbsolutePath().resolve(workingDir).toString();
}
}
124 changes: 124 additions & 0 deletions util-gocd/src/main/java/com/indix/gocd/utils/GoEnvironment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.indix.gocd.utils;

import org.apache.commons.lang3.BooleanUtils;

import java.lang.StringBuffer;

import java.util.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.indix.gocd.utils.Constants.*;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
* Wrapper around Go's Environment variables
*/
public class GoEnvironment {
private Pattern envPat = Pattern.compile("\\$\\{(\\w+)\\}");
private Map<String, String> environment = new HashMap<>();

public GoEnvironment() {
this.environment.putAll(System.getenv());
}

public GoEnvironment(Map<String, String> defaultEnvironment) {
this();
this.environment.putAll(defaultEnvironment);
}

public GoEnvironment putAll(Map<String, String> existing) {
environment.putAll(existing);
return this;
}

public Map<String,String> asMap() { return environment; }

public String get(String name) {
return environment.get(name);
}

public String getOrElse(String name, String defaultValue) {
if(has(name)) return get(name);
else return defaultValue;
}

public boolean has(String name) {
return environment.containsKey(name) && isNotEmpty(get(name));
}

public boolean isAbsent(String name) {
return !has(name);
}

public String traceBackUrl() {
String serverUrl = get(GO_SERVER_DASHBOARD_URL);
String pipelineName = get("GO_PIPELINE_NAME");
String pipelineCounter = get("GO_PIPELINE_COUNTER");
String stageName = get("GO_STAGE_NAME");
String stageCounter = get("GO_STAGE_COUNTER");
String jobName = get("GO_JOB_NAME");
return String.format("%s/go/tab/build/detail/%s/%s/%s/%s/%s", serverUrl, pipelineName, pipelineCounter, stageName, stageCounter, jobName);
}

public String triggeredUser() {
return get("GO_TRIGGER_USER");
}

public String replaceVariables(String str) {
Matcher m = envPat.matcher(str);

StringBuffer sb = new StringBuffer();
while (m.find()) {
String replacement = get(m.group(1));
if(replacement != null) {
m.appendReplacement(sb, replacement);
}
}

m.appendTail(sb);

return sb.toString();
}

/**
* Version Format on S3 is <code>pipeline/stage/job/pipeline_counter.stage_counter</code>
*/
public String artifactsLocationTemplate() {
String pipeline = get("GO_PIPELINE_NAME");
String stageName = get("GO_STAGE_NAME");
String jobName = get("GO_JOB_NAME");

String pipelineCounter = get("GO_PIPELINE_COUNTER");
String stageCounter = get("GO_STAGE_COUNTER");
return artifactsLocationTemplate(pipeline, stageName, jobName, pipelineCounter, stageCounter);
}

public String artifactsLocationTemplate(String pipeline, String stageName, String jobName, String pipelineCounter, String stageCounter) {
return String.format("%s/%s/%s/%s.%s", pipeline, stageName, jobName, pipelineCounter, stageCounter);
}

private static final List<String> validUseIamRoleValues = new ArrayList<String>(Arrays.asList("true", "false", "yes", "no", "on", "off"));
public boolean hasAWSUseIamRole() {
if (!has(AWS_USE_IAM_ROLE)) {
return false;
}

String useIamRoleValue = get(AWS_USE_IAM_ROLE);
Boolean result = BooleanUtils.toBooleanObject(useIamRoleValue);
if (result == null) {
throw new IllegalArgumentException(getEnvInvalidFormatMessage(AWS_USE_IAM_ROLE,
useIamRoleValue, validUseIamRoleValues.toString()));
}
else {
return result.booleanValue();
}
}

private String getEnvInvalidFormatMessage(String environmentVariable, String value, String expected){
return String.format(
"Unexpected value in %s environment variable; was %s, but expected one of the following %s",
environmentVariable, value, expected);
}
}
Loading