View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package br.com.ingenieux.mojo.beanstalk.version;
18  
19  import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest;
20  import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionResult;
21  import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest;
22  import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult;
23  import com.amazonaws.services.elasticbeanstalk.model.S3Location;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  
30  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
31  
32  /**
33   * Creates an Application Version, optionally creating the application itself.
34   *
35   * See the <a href= "http://docs.amazonwebservices.com/elasticbeanstalk/latest/api/API_CreateApplicationVersion.html"
36   * >CreateApplicationVersion API</a> call.
37   *
38   * @since 0.1.0
39   */
40  @Mojo(name = "create-application-version")
41  public class CreateApplicationVersionMojo extends AbstractBeanstalkMojo {
42  
43    /**
44     * Beanstalk Application Name
45     */
46    @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
47    String applicationName;
48  
49    /**
50     * Version Description
51     *
52     * Unfortunately, this is incorrectly named. Anyway...
53     */
54    @Parameter(property = "beanstalk.versionDescription", defaultValue = "Update from beanstalk-maven-plugin")
55    String versionDescription;
56  
57    /**
58     * Auto-Create Application? Defaults to true
59     */
60    @Parameter(property = "beanstalk.autoCreateApplication", defaultValue = "true")
61    boolean autoCreateApplication;
62  
63    /**
64     * S3 Bucket
65     */
66    @Parameter(property = "beanstalk.s3Bucket", defaultValue = "${project.artifactId}", required = true)
67    String s3Bucket;
68  
69    /**
70     * S3 Key
71     */
72    @Parameter(
73      property = "beanstalk.s3Key",
74      defaultValue = "${project.artifactId}/${project.build.finalName}-${beanstalk.versionLabel}.${project.packaging}",
75      required = true
76    )
77    String s3Key;
78  
79    /**
80     * Version Label to use. Defaults to Project Version
81     */
82    @Parameter(property = "beanstalk.versionLabel", defaultValue = "${project.version}", required = true)
83    String versionLabel;
84  
85    /**
86     * Skip when this versionLabel already exists?
87     */
88    @Parameter(property = "beanstalk.skipExisting", defaultValue = "true")
89    boolean skipExisting;
90  
91    protected Object executeInternal() throws MojoExecutionException {
92      if (skipExisting) {
93        if (versionLabelExists()) {
94          getLog().info("VersionLabel " + versionLabel + " already exists. Skipping creation of new application-version");
95  
96          return null;
97        }
98      }
99  
100     CreateApplicationVersionRequest request = new CreateApplicationVersionRequest();
101 
102     request.setApplicationName(applicationName);
103     request.setDescription(versionDescription);
104     request.setAutoCreateApplication(autoCreateApplication);
105 
106     if (StringUtils.isNotBlank(s3Bucket) && StringUtils.isNotBlank(s3Key)) {
107       request.setSourceBundle(new S3Location(s3Bucket, s3Key));
108     }
109 
110     request.setDescription(versionDescription);
111 
112     request.setVersionLabel(versionLabel);
113 
114     CreateApplicationVersionResult result = getService().createApplicationVersion(request);
115 
116     return result.getApplicationVersion();
117   }
118 
119   private boolean versionLabelExists() {
120     /*
121      * Builds a request for this very specific version label
122      */
123     DescribeApplicationVersionsRequest davRequest =
124         new DescribeApplicationVersionsRequest().withApplicationName(applicationName).withVersionLabels(versionLabel);
125 
126     /*
127      * Sends the request
128      */
129     DescribeApplicationVersionsResult result = getService().describeApplicationVersions(davRequest);
130 
131     /*
132      * Non-empty means the application version label *DOES* exist.
133      */
134     return !result.getApplicationVersions().isEmpty();
135   }
136 }