Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
TW-46415, TW-45071
Browse files Browse the repository at this point in the history
  • Loading branch information
vbedrosova committed Jul 29, 2016
1 parent 2bcc677 commit c01ef80
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static jetbrains.buildServer.runner.codedeploy.CodeDeployConstants.*;
import static jetbrains.buildServer.util.amazon.AWSCommonParams.*;
Expand All @@ -47,21 +46,31 @@ protected BuildFinishedStatus runImpl() throws RunBuildException {
final Map<String, String> runnerParameters = validateParams();
final Map<String, String> configParameters = context.getConfigParameters();

final AtomicBoolean problemOccurred = new AtomicBoolean();
final Mutable m = new Mutable(configParameters);
m.problemOccurred = false;
m.s3ObjectVersion = nullIfEmpty(configParameters.get(S3_OBJECT_VERSION_CONFIG_PARAM));
m.s3ObjectETag = nullIfEmpty(configParameters.get(S3_OBJECT_ETAG_CONFIG_PARAM));

final AWSClient awsClient = createAWSClient(runnerParameters, runningBuild).withListener(
new LoggingDeploymentListener(runnerParameters, runningBuild.getBuildLogger(), runningBuild.getCheckoutDirectory().getAbsolutePath()) {
@Override
protected void problem(int identity, @NotNull String type, @NotNull String descr) {
super.problem(identity, type, descr);
problemOccurred.set(true);
m.problemOccurred = true;
}

@Override
void uploadRevisionFinished(@NotNull File revision, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @Nullable String s3ObjectVersion, @Nullable String s3ObjectETag, @NotNull String url) {
super.uploadRevisionFinished(revision, s3BucketName, s3ObjectKey, s3ObjectVersion, s3ObjectETag, url);
m.s3ObjectVersion = s3ObjectVersion;
m.s3ObjectETag = s3ObjectETag;
}
});

final String s3BucketName = runnerParameters.get(S3_BUCKET_NAME_PARAM);
String s3ObjectKey = runnerParameters.get(S3_OBJECT_KEY_PARAM);

if (CodeDeployUtil.isUploadStepEnabled(runnerParameters) && !problemOccurred.get() && !isInterrupted()) {
if (CodeDeployUtil.isUploadStepEnabled(runnerParameters) && !m.problemOccurred && !isInterrupted()) {
final File readyRevision = new ApplicationRevision(
isEmptyOrSpaces(s3ObjectKey) ? runningBuild.getBuildTypeExternalId() : s3ObjectKey,
runnerParameters.get(REVISION_PATHS_PARAM),
Expand All @@ -77,30 +86,29 @@ protected void problem(int identity, @NotNull String type, @NotNull String descr

final String applicationName = runnerParameters.get(APP_NAME_PARAM);
final String bundleType = "" + AWSUtil.getBundleType(s3ObjectKey);
final String s3ObjectVersion = nullIfEmpty(configParameters.get(S3_OBJECT_VERSION_CONFIG_PARAM));

if (CodeDeployUtil.isRegisterStepEnabled(runnerParameters) && !problemOccurred.get() && !isInterrupted()) {
awsClient.registerRevision(s3BucketName, s3ObjectKey, bundleType, s3ObjectVersion, applicationName);
if (CodeDeployUtil.isRegisterStepEnabled(runnerParameters) && !m.problemOccurred && !isInterrupted()) {
awsClient.registerRevision(s3BucketName, s3ObjectKey, bundleType, m.s3ObjectVersion, m.s3ObjectETag, applicationName);
}

if (CodeDeployUtil.isDeployStepEnabled(runnerParameters) && !problemOccurred.get() && !isInterrupted()) {
if (CodeDeployUtil.isDeployStepEnabled(runnerParameters) && !m.problemOccurred && !isInterrupted()) {
final String deploymentGroupName = runnerParameters.get(DEPLOYMENT_GROUP_NAME_PARAM);
final String deploymentConfigName = nullIfEmpty(runnerParameters.get(DEPLOYMENT_CONFIG_NAME_PARAM));

if (CodeDeployUtil.isDeploymentWaitEnabled(runnerParameters)) {
awsClient.deployRevisionAndWait(
s3BucketName, s3ObjectKey, bundleType, s3ObjectVersion,
s3BucketName, s3ObjectKey, bundleType, m.s3ObjectVersion, m.s3ObjectETag,
applicationName, deploymentGroupName, deploymentConfigName,
Integer.parseInt(runnerParameters.get(WAIT_TIMEOUT_SEC_PARAM)),
getIntegerOrDefault(configParameters.get(WAIT_POLL_INTERVAL_SEC_CONFIG_PARAM), WAIT_POLL_INTERVAL_SEC_DEFAULT));
} else {
awsClient.deployRevision(
s3BucketName, s3ObjectKey, bundleType, s3ObjectVersion,
s3BucketName, s3ObjectKey, bundleType, m.s3ObjectVersion, m.s3ObjectETag,
applicationName, deploymentGroupName, deploymentConfigName);
}
}

return problemOccurred.get() ? BuildFinishedStatus.FINISHED_WITH_PROBLEMS : BuildFinishedStatus.FINISHED_SUCCESS;
return m.problemOccurred ? BuildFinishedStatus.FINISHED_WITH_PROBLEMS : BuildFinishedStatus.FINISHED_SUCCESS;
}

@NotNull
Expand Down Expand Up @@ -147,4 +155,15 @@ public CodeDeployRunnerException(@NotNull String message, @Nullable Throwable ca
this.setLogStacktrace(false);
}
}

private class Mutable {
public Mutable(@NotNull Map<String, String> configParameters) {
problemOccurred = false;
s3ObjectVersion = nullIfEmpty(configParameters.get(S3_OBJECT_VERSION_CONFIG_PARAM));
s3ObjectETag = nullIfEmpty(configParameters.get(S3_OBJECT_ETAG_CONFIG_PARAM));
}
boolean problemOccurred;
String s3ObjectVersion;
String s3ObjectETag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,34 @@ void uploadRevisionStarted(@NotNull File revision, @NotNull String s3BucketName,
}

@Override
void uploadRevisionFinished(@NotNull File revision, @NotNull String s3BucketName, @NotNull String key, @NotNull String url) {
log("Uploaded application revision " + url);
void uploadRevisionFinished(@NotNull File revision, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @Nullable String s3ObjectVersion, @Nullable String s3ObjectETag, @NotNull String url) {
final boolean hasVersion = StringUtil.isNotEmpty(s3ObjectVersion);
final boolean hasETag = StringUtil.isNotEmpty(s3ObjectETag);

final String directUrl =
url +
(hasVersion || hasETag ? "?" : "") +
(hasVersion ? "versionId=" + s3ObjectVersion : "") +
(hasVersion && hasETag ? "&" : "") +
(hasETag ? "etag=" + s3ObjectETag : "");

log("Uploaded application revision " + directUrl);
if (!CodeDeployUtil.isRegisterStepEnabled(myRunnerParameters)) {
statusText("Uploaded " + url);
statusText("Uploaded " + directUrl);
}
if (hasVersion) parameter(CodeDeployConstants.S3_OBJECT_VERSION_CONFIG_PARAM, s3ObjectVersion);
if (hasETag) parameter(CodeDeployConstants.S3_OBJECT_ETAG_CONFIG_PARAM, s3ObjectETag);
close(UPLOAD_REVISION);
}

@Override
void registerRevisionStarted(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
void registerRevisionStarted(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion, @Nullable String s3ObjectETag) {
open(REGISTER_REVISION);
log(String.format("Registering application %s revision from S3 bucket %s with key %s, bundle type %s and %s version", applicationName, s3BucketName, s3ObjectKey, s3BundleType, StringUtil.isEmptyOrSpaces(s3ObjectVersion) ? "latest" : s3ObjectVersion));
log(String.format("Registering application %s revision from S3 bucket %s with key %s, bundle type %s, %s version and %s ETag", applicationName, s3BucketName, s3ObjectKey, s3BundleType, StringUtil.isEmptyOrSpaces(s3ObjectVersion) ? "latest" : s3ObjectVersion, StringUtil.isEmptyOrSpaces(s3ObjectETag) ? "no" : s3ObjectETag));
}

@Override
void registerRevisionFinished(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
void registerRevisionFinished(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String bundleType, @Nullable String s3ObjectVersion, @Nullable String s3ObjectETag) {
if (!CodeDeployUtil.isDeployStepEnabled(myRunnerParameters)) {
statusText("Registered revision");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ public void common_events() throws Exception {
final File revision = writeFile(revisionName);
final String bucketName = "bucketName";
final String key = "path/key.zip";
final String eTag = "12345";

listener.uploadRevisionStarted(revision, bucketName, key);

final String url = "https://s3-eu-west-1.amazonaws.com/bucketName/path/key.zip";

listener.uploadRevisionFinished(revision, bucketName, key, url);
listener.uploadRevisionFinished(revision, bucketName, key, null, eTag, url);

final String applicationName = "App Name";
final String bundleType = ".zip";

listener.registerRevisionStarted(applicationName, bucketName, key, bundleType, null);
listener.registerRevisionFinished(applicationName, bucketName, key, bundleType, null);
listener.registerRevisionStarted(applicationName, bucketName, key, bundleType, null, eTag);
listener.registerRevisionFinished(applicationName, bucketName, key, bundleType, null, eTag);

final String groupName = "Deployment Fleet";

Expand All @@ -76,11 +77,12 @@ public void common_events() throws Exception {
assertLog(
"OPEN " + LoggingDeploymentListener.UPLOAD_REVISION,
"LOG Uploading application revision ##BASE_DIR##/" + revisionName + " to S3 bucket " + bucketName + " using key " + key,
"LOG Uploaded application revision " + url,
"STATUS_TEXT Uploaded " + url,
"LOG Uploaded application revision " + url + "?etag=" + eTag,
"STATUS_TEXT Uploaded " + url + "?etag=" + eTag,
"PARAM " + CodeDeployConstants.S3_OBJECT_ETAG_CONFIG_PARAM + " -> " + eTag,
"CLOSE " + LoggingDeploymentListener.UPLOAD_REVISION,
"OPEN " + LoggingDeploymentListener.REGISTER_REVISION,
"LOG Registering application " + applicationName + " revision from S3 bucket " + bucketName + " with key " + key + ", bundle type " + bundleType + " and latest version",
"LOG Registering application " + applicationName + " revision from S3 bucket " + bucketName + " with key " + key + ", bundle type " + bundleType + ", latest version and " + eTag + " ETag",
"STATUS_TEXT Registered revision",
"CLOSE " + LoggingDeploymentListener.REGISTER_REVISION,
"OPEN " + LoggingDeploymentListener.DEPLOY_APPLICATION,
Expand Down
Loading

0 comments on commit c01ef80

Please sign in to comment.