Skip to content

Commit

Permalink
JENKINS-28648
Browse files Browse the repository at this point in the history
Implemented S3 artifact locations
  • Loading branch information
cast committed May 29, 2015
1 parent bc35a9e commit 978953d
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -45,7 +45,7 @@
</developers>

<properties>
<aws.version>1.9.35</aws.version>
<aws.version>1.9.39</aws.version>
<jsonpath.version>2.0.0</jsonpath.version>
</properties>

Expand Down
@@ -1,7 +1,11 @@
package com.xti.jenkins.plugin.awslambda.exception;

public class AWSLambdaPluginException extends Exception {
public class AWSLambdaPluginException extends RuntimeException {
public AWSLambdaPluginException(String message) {
super(message);
}

public AWSLambdaPluginException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,12 @@
package com.xti.jenkins.plugin.awslambda.exception;

public class LambdaDeployException extends AWSLambdaPluginException {

public LambdaDeployException(String message) {
super(message);
}

public LambdaDeployException(String message, Throwable cause) {
super(message, cause);
}
}
Expand Up @@ -3,6 +3,7 @@
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.*;
import com.xti.jenkins.plugin.awslambda.exception.LambdaDeployException;
import com.xti.jenkins.plugin.awslambda.upload.UpdateModeValue;
import com.xti.jenkins.plugin.awslambda.upload.DeployConfig;
import com.xti.jenkins.plugin.awslambda.util.LogUtils;
Expand All @@ -29,18 +30,18 @@ public LambdaDeployService(AWSLambdaClient client, JenkinsLogger logger) {
* - No function found:
* - Always call createFunction regardless of UpdateMode value
* @param config configuration to be updated or added
* @param zipFile zipfile containing code to be updated or added
* @param functionCode FunctionCode containing either zipfile or s3 location.
* @param updateModeValue Full, Code or Config, only used if function does not already exists.
* @return true if successful, false in case of failure.
*/
public Boolean deployLambda(DeployConfig config, File zipFile, UpdateModeValue updateModeValue){
public Boolean deployLambda(DeployConfig config, FunctionCode functionCode, UpdateModeValue updateModeValue){
if(functionExists(config.getFunctionName())){

//update code
if(UpdateModeValue.Full.equals(updateModeValue) || UpdateModeValue.Code.equals(updateModeValue)){
if(zipFile != null) {
if(functionCode != null) {
try {
updateCodeOnly(config.getFunctionName(), zipFile);
updateCodeOnly(config.getFunctionName(), functionCode);
} catch (IOException e) {
logger.log(LogUtils.getStackTrace(e));
return false;
Expand All @@ -66,9 +67,9 @@ public Boolean deployLambda(DeployConfig config, File zipFile, UpdateModeValue u
return true;

}else {
if(zipFile != null) {
if(functionCode != null) {
try {
createLambdaFunction(config, zipFile);
createLambdaFunction(config, functionCode);
return true;
} catch (IOException e) {
logger.log(LogUtils.getStackTrace(e));
Expand All @@ -87,12 +88,10 @@ public Boolean deployLambda(DeployConfig config, File zipFile, UpdateModeValue u
/**
* This method calls the AWS Lambda createFunction method based on the given configuration and file.
* @param config configuration to setup the createFunction call
* @param zipFile zipfile that will be uploaded
* @param functionCode FunctionCode containing either zipfile or s3 location.
* @throws IOException
*/
private void createLambdaFunction(DeployConfig config, File zipFile) throws IOException {

FunctionCode functionCode = new FunctionCode().withZipFile(getFunctionZip(zipFile));
private void createLambdaFunction(DeployConfig config, FunctionCode functionCode) throws IOException {

CreateFunctionRequest createFunctionRequest = new CreateFunctionRequest()
.withDescription(config.getDescription())
Expand All @@ -112,15 +111,17 @@ private void createLambdaFunction(DeployConfig config, File zipFile) throws IOEx
/**
* This method calls the AWS Lambda updateFunctionCode method based for the given file.
* @param functionName name of the function to update code for
* @param zipFile zipfile that will be uploaded
* @param functionCode FunctionCode containing either zipfile or s3 location.
* @throws IOException
*/
private void updateCodeOnly(String functionName, File zipFile) throws IOException {
ByteBuffer functionCode = getFunctionZip(zipFile);
private void updateCodeOnly(String functionName, FunctionCode functionCode) throws IOException {

UpdateFunctionCodeRequest updateFunctionCodeRequest = new UpdateFunctionCodeRequest()
.withFunctionName(functionName)
.withZipFile(functionCode);
.withZipFile(functionCode.getZipFile())
.withS3Bucket(functionCode.getS3Bucket())
.withS3Key(functionCode.getS3Key())
.withS3ObjectVersion(functionCode.getS3ObjectVersion());

logger.log("Lambda update code request:%n%s%n", updateFunctionCodeRequest.toString());

Expand Down Expand Up @@ -165,6 +166,45 @@ private Boolean functionExists(String functionName){
}
}

public FunctionCode getFunctionCode(String artifactLocation, WorkSpaceZipper workSpaceZipper){
if(artifactLocation.startsWith("s3://")){
String bucket = null;
String key = null;
String versionId = null;

String s3String = artifactLocation.substring(5);
int versionIndex = s3String.indexOf("?versionId=");
if(versionIndex != -1){
versionId = s3String.substring(versionIndex + 11);
s3String = s3String.substring(0, versionIndex);
}
int separatorIndex = s3String.indexOf("/");
if(separatorIndex != -1){
bucket = s3String.substring(0, separatorIndex);
if(s3String.length() > separatorIndex + 1) {
key = s3String.substring(separatorIndex + 1);
}
}

return new FunctionCode()
.withS3Bucket(bucket)
.withS3Key(key)
.withS3ObjectVersion(versionId);

} else {
try {
File zipFile = workSpaceZipper.getZip(artifactLocation);
return new FunctionCode()
.withZipFile(getFunctionZip(zipFile));
} catch (IOException ioe){
throw new LambdaDeployException("Error processing zip file.", ioe);
} catch (InterruptedException ie){
throw new LambdaDeployException("Error processing zip file.", ie);
}

}
}

/**
* Get ByteBuffer from zip file.
* @param zipFile file to be wrapped.
Expand Down
@@ -1,5 +1,6 @@
package com.xti.jenkins.plugin.awslambda.service;

import com.xti.jenkins.plugin.awslambda.exception.LambdaDeployException;
import hudson.FilePath;
import hudson.util.DirScanner;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -35,8 +36,12 @@ private File getArtifactZip(FilePath artifactLocation) throws IOException, Inter
File resultFile = File.createTempFile("awslambda-", ".zip");

if (!artifactLocation.isDirectory()) {
logger.log("Copying zip file");
artifactLocation.copyTo(new FileOutputStream(resultFile));
if(artifactLocation.exists()) {
logger.log("Copying zip file");
artifactLocation.copyTo(new FileOutputStream(resultFile));
} else {
throw new LambdaDeployException("Could not find zipfile or folder.");
}
} else {
logger.log("Zipping folder ..., copying zip file");
artifactLocation.zip(new FileOutputStream(resultFile), new DirScanner.Glob("**", null, false));
Expand Down
Expand Up @@ -26,11 +26,11 @@
* #L%
*/

import com.amazonaws.services.lambda.model.FunctionCode;
import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaDeployService;
import com.xti.jenkins.plugin.awslambda.service.WorkSpaceZipper;

import java.io.File;
import java.io.IOException;

public class LambdaUploader {
Expand All @@ -47,7 +47,7 @@ public LambdaUploader(LambdaDeployService lambda, WorkSpaceZipper zipper, Jenkin
public Boolean upload(DeployConfig config) throws IOException, InterruptedException {
logger.log("%nStarting lambda deployment procedure");

File zipFile = zipper.getZip(config.getArtifactLocation());
return lambda.deployLambda(config, zipFile, UpdateModeValue.fromString(config.getUpdateMode()));
FunctionCode functionCode = lambda.getFunctionCode(config.getArtifactLocation(), zipper);
return lambda.deployLambda(config, functionCode, UpdateModeValue.fromString(config.getUpdateMode()));
}
}
@@ -1,9 +1,6 @@
package com.xti.jenkins.plugin.awslambda.util;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSCredentialsProviderChain;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;
Expand All @@ -12,8 +9,7 @@ public class LambdaClientConfig {
private AWSLambdaClient client;

public LambdaClientConfig(String accessKeyId, String secretKey, String region) {
AWSCredentialsProvider credentials = new AWSCredentialsProviderChain(new StaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)));
client = new AWSLambdaClient(credentials);
client = new AWSLambdaClient(new BasicAWSCredentials(accessKeyId, secretKey));
client.setRegion(Region.getRegion(Regions.fromName(region)));
}

Expand Down

0 comments on commit 978953d

Please sign in to comment.