Skip to content

Commit

Permalink
[JENKINS-51523] Move constants to S3BlobStore
Browse files Browse the repository at this point in the history
and add a new one for separately control stash deletion
  • Loading branch information
carlossg committed May 24, 2018
1 parent 735de30 commit 11f2455
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Expand Up @@ -59,6 +59,14 @@ public enum HttpMethod {
@NonNull
public abstract String getContainer();

/** A constant to define whether we should delete blobs or leave them to be managed on the blob service side. */
@NonNull
public abstract boolean isDeleteBlobs();

/** A constant to define whether we should delete stashes or leave them to be managed on the blob service side. */
@NonNull
public abstract boolean isDeleteStashes();

/** Creates the jclouds handle for working with blobs. */
@NonNull
public abstract BlobStoreContext getContext() throws IOException;
Expand Down
Expand Up @@ -75,8 +75,6 @@ public final class JCloudsArtifactManager extends ArtifactManager implements Sta

private static final Logger LOGGER = Logger.getLogger(JCloudsArtifactManager.class.getName());

private static boolean DELETE_BLOBS = Boolean.getBoolean(JCloudsArtifactManager.class.getName() + ".deleteBlobs");

private final BlobStoreProvider provider;

private transient String key; // e.g. myorg/myrepo/master/123
Expand Down Expand Up @@ -131,7 +129,12 @@ public void archive(FilePath workspace, Launcher launcher, BuildListener listene

@Override
public boolean delete() throws IOException, InterruptedException {
return DELETE_BLOBS ? delete(provider, getContext().getBlobStore(), getBlobPath("")) : false;
String blobPath = getBlobPath("");
if (!provider.isDeleteBlobs()) {
LOGGER.log(Level.FINEST, "Ignoring blob deletion: {0}", blobPath);
return false;
}
return delete(provider, getContext().getBlobStore(), blobPath);
}

/**
Expand Down Expand Up @@ -248,6 +251,12 @@ public Void invoke(File f, VirtualChannel channel) throws IOException, Interrupt
@Override
public void clearAllStashes(TaskListener listener) throws IOException, InterruptedException {
String stashPrefix = getBlobPath("stashes/");

if (!provider.isDeleteStashes()) {
LOGGER.log(Level.FINEST, "Ignoring stash deletion: {0}", stashPrefix);
return;
}

BlobStore blobStore = getContext().getBlobStore();
Iterator<StorageMetadata> it = new JCloudsVirtualFile.PageSetIterable(blobStore, provider.getContainer(), ListContainerOptions.Builder.prefix(stashPrefix).recursive());
int count = 0;
Expand Down
Expand Up @@ -24,8 +24,6 @@

package io.jenkins.plugins.artifact_manager_s3;

import io.jenkins.plugins.artifact_manager_jclouds.BlobStoreProvider;
import io.jenkins.plugins.artifact_manager_jclouds.BlobStoreProviderDescriptor;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand Down Expand Up @@ -56,6 +54,8 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import io.jenkins.plugins.artifact_manager_jclouds.BlobStoreProvider;
import io.jenkins.plugins.artifact_manager_jclouds.BlobStoreProviderDescriptor;
import shaded.com.google.common.base.Supplier;

/**
Expand All @@ -74,6 +74,10 @@ public class S3BlobStore extends BlobStoreProvider {
private static String BLOB_CONTAINER = System.getenv("S3_BUCKET");
@SuppressWarnings("FieldMayBeFinal")
private static String PREFIX = System.getenv("S3_DIR");
@SuppressWarnings("FieldMayBeFinal")
private static boolean DELETE_BLOBS = Boolean.getBoolean(S3BlobStore.class.getName() + ".deleteBlobs");
@SuppressWarnings("FieldMayBeFinal")
private static boolean DELETE_STASHES = Boolean.getBoolean(S3BlobStore.class.getName() + ".deleteStashes");

@DataBoundConstructor
public S3BlobStore() {}
Expand All @@ -88,6 +92,16 @@ public String getContainer() {
return BLOB_CONTAINER;
}

@Override
public boolean isDeleteBlobs() {
return DELETE_BLOBS;
}

@Override
public boolean isDeleteStashes() {
return DELETE_STASHES;
}

@Override
public BlobStoreContext getContext() throws IOException {
LOGGER.log(Level.FINEST, "Building context");
Expand Down

0 comments on commit 11f2455

Please sign in to comment.