forked from jenkinsci/s3-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from martazobro/mz-cloudfrontInvalidation
implemented CloudFront invalidation among all available distributions
- Loading branch information
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/hudson/plugins/s3/callable/cloudfront/AbstractCloudFrontCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package hudson.plugins.s3.callable.cloudfront; | ||
|
||
import hudson.util.Secret; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.cloudfront.AmazonCloudFront; | ||
import com.amazonaws.services.cloudfront.AmazonCloudFrontClient; | ||
|
||
public class AbstractCloudFrontCallable implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final String accessKey; | ||
private final Secret secretKey; | ||
private final boolean useRole; | ||
private AmazonCloudFront cloudFrontClient; | ||
|
||
public AbstractCloudFrontCallable(String accessKey, Secret secretKey, boolean useRole) { | ||
this.accessKey = accessKey; | ||
this.secretKey = secretKey; | ||
this.useRole = useRole; | ||
} | ||
|
||
protected AmazonCloudFront getClient() { | ||
if (cloudFrontClient == null) { | ||
if (useRole) { | ||
cloudFrontClient = new AmazonCloudFrontClient(); | ||
} else { | ||
cloudFrontClient = new AmazonCloudFrontClient(new BasicAWSCredentials(accessKey, secretKey.getPlainText())); | ||
} | ||
} | ||
return cloudFrontClient; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/hudson/plugins/s3/callable/cloudfront/CloudFrontInvalidationCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package hudson.plugins.s3.callable.cloudfront; | ||
|
||
import hudson.FilePath; | ||
import hudson.plugins.s3.Destination; | ||
import hudson.util.Secret; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import com.amazonaws.services.cloudfront.model.CreateInvalidationRequest; | ||
import com.amazonaws.services.cloudfront.model.CreateInvalidationResult; | ||
import com.amazonaws.services.cloudfront.model.DistributionSummary; | ||
import com.amazonaws.services.cloudfront.model.Invalidation; | ||
import com.amazonaws.services.cloudfront.model.InvalidationBatch; | ||
import com.amazonaws.services.cloudfront.model.ListDistributionsRequest; | ||
import com.amazonaws.services.cloudfront.model.Paths; | ||
import com.google.common.base.Function; | ||
import com.google.common.collect.Lists; | ||
|
||
public class CloudFrontInvalidationCallable extends AbstractCloudFrontCallable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Logger log = Logger.getLogger(CloudFrontInvalidationCallable.class.getName()); | ||
|
||
private static final String UNIX_SEPARATOR = "/"; | ||
private final String bucket; | ||
private final int searchPathLength; | ||
private Function<FilePath, String> filePathToS3KeysTransformer; | ||
|
||
public CloudFrontInvalidationCallable(String accessKey, Secret secretKey, | ||
boolean useRole, String bucket, int searchPathLength) { | ||
super(accessKey, secretKey, useRole); | ||
this.bucket = bucket; | ||
this.searchPathLength = searchPathLength; | ||
filePathToS3KeysTransformer = new Function<FilePath, String>() { | ||
|
||
@Override | ||
public String apply(FilePath filePath) { | ||
if (!isValidFile(filePath)) { | ||
return null; | ||
} | ||
String fileName = filePath.getRemote().substring(CloudFrontInvalidationCallable.this.searchPathLength); | ||
Destination dest = new Destination(CloudFrontInvalidationCallable.this.bucket, fileName); | ||
|
||
String key = dest.objectName; | ||
if (!key.startsWith(UNIX_SEPARATOR)) { | ||
key = UNIX_SEPARATOR.concat(key); | ||
} | ||
return key; | ||
} | ||
|
||
private boolean isValidFile(FilePath filePath) { | ||
try { | ||
return !filePath.isDirectory(); | ||
} catch (Exception e) { | ||
log.info(String.format("Failed to check filePath %s, %s", | ||
filePath.getName(), e.toString())); | ||
} | ||
return false; | ||
} | ||
|
||
}; | ||
} | ||
|
||
public void invoke(FilePath...paths) { | ||
String[] keysToInvalidate = getS3Keys(paths).toArray(new String[paths.length]); | ||
List<DistributionSummary> distributionitems = getClient().listDistributions(new ListDistributionsRequest()).getDistributionList().getItems(); | ||
for (DistributionSummary distribution: distributionitems){ | ||
if (distribution.isEnabled()){ | ||
Invalidation invalidationResult = invalidateFiles(distribution, keysToInvalidate); | ||
log.info(String.format( | ||
"Invalidated cache %s of path %s with status = %s", distribution.getAliases().toString(), | ||
invalidationResult.getInvalidationBatch().getPaths().toString(), invalidationResult.getStatus())); | ||
} | ||
|
||
} | ||
} | ||
|
||
private List<String> getS3Keys(FilePath[] paths) { | ||
return Lists.transform(Arrays.asList(paths), filePathToS3KeysTransformer); | ||
} | ||
|
||
private Invalidation invalidateFiles(DistributionSummary distributionSummary, String[] keysToInvalidate) { | ||
String distributionId = distributionSummary.getId(); | ||
String callerReference = String.valueOf(System.currentTimeMillis()); | ||
CreateInvalidationResult invalidationResult = getClient().createInvalidation( | ||
new CreateInvalidationRequest(distributionId, | ||
new InvalidationBatch(new Paths().withItems(keysToInvalidate) | ||
.withQuantity(keysToInvalidate.length), callerReference))); | ||
return invalidationResult.getInvalidation(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/resources/hudson/plugins/s3/Entry/help-invalidateAfterUpload.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>When enabled, Jenkins will perform CloudFront invalidation of uploaded files among available distributions</div> |