1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package br.com.ingenieux.mojo.beanstalk.version;
18
19 import com.amazonaws.services.elasticbeanstalk.model.ApplicationVersionDescription;
20 import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest;
21 import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult;
22 import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsRequest;
23 import com.amazonaws.services.elasticbeanstalk.model.DescribeEnvironmentsResult;
24 import com.amazonaws.services.elasticbeanstalk.model.EnvironmentDescription;
25 import com.amazonaws.services.elasticbeanstalk.model.UpdateEnvironmentRequest;
26
27 import org.apache.commons.lang.builder.CompareToBuilder;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Comparator;
36 import java.util.List;
37 import java.util.ListIterator;
38
39 import br.com.ingenieux.mojo.beanstalk.AbstractNeedsEnvironmentMojo;
40
41
42
43
44
45
46
47 @Mojo(name = "rollback-version")
48 public class RollbackVersionMojo extends AbstractNeedsEnvironmentMojo {
49
50
51
52
53 @Parameter(property = "beanstalk.dryRun", defaultValue = "true")
54 boolean dryRun;
55
56
57
58
59 @Parameter(property = "beanstalk.latestVersionInstead")
60 boolean latestVersionInstead;
61
62 @Override
63 protected Object executeInternal() throws MojoExecutionException, MojoFailureException {
64
65 DescribeApplicationVersionsRequest describeApplicationVersionsRequest = new DescribeApplicationVersionsRequest().withApplicationName(applicationName);
66
67 DescribeApplicationVersionsResult appVersions = getService().describeApplicationVersions(describeApplicationVersionsRequest);
68
69 DescribeEnvironmentsRequest describeEnvironmentsRequest =
70 new DescribeEnvironmentsRequest()
71 .withApplicationName(applicationName)
72 .withEnvironmentIds(curEnv.getEnvironmentId())
73 .withEnvironmentNames(curEnv.getEnvironmentName())
74 .withIncludeDeleted(false);
75
76 DescribeEnvironmentsResult environments = getService().describeEnvironments(describeEnvironmentsRequest);
77
78 List<ApplicationVersionDescription> appVersionList = new ArrayList<ApplicationVersionDescription>(appVersions.getApplicationVersions());
79
80 List<EnvironmentDescription> environmentList = environments.getEnvironments();
81
82 if (environmentList.isEmpty()) {
83 throw new MojoFailureException("No environments were found");
84 }
85
86 EnvironmentDescription d = environmentList.get(0);
87
88 Collections.sort(
89 appVersionList,
90 new Comparator<ApplicationVersionDescription>() {
91 @Override
92 public int compare(ApplicationVersionDescription o1, ApplicationVersionDescription o2) {
93 return new CompareToBuilder().append(o1.getDateUpdated(), o2.getDateUpdated()).toComparison();
94 }
95 });
96
97 Collections.reverse(appVersionList);
98
99 if (latestVersionInstead) {
100 ApplicationVersionDescription latestVersionDescription = appVersionList.get(0);
101
102 return changeToVersion(d, latestVersionDescription);
103 }
104
105 ListIterator<ApplicationVersionDescription> versionIterator = appVersionList.listIterator();
106
107 String curVersionLabel = d.getVersionLabel();
108
109 while (versionIterator.hasNext()) {
110 ApplicationVersionDescription versionDescription = versionIterator.next();
111
112 String versionLabel = versionDescription.getVersionLabel();
113
114 if (curVersionLabel.equals(versionLabel) && versionIterator.hasNext()) {
115 return changeToVersion(d, versionIterator.next());
116 }
117 }
118
119 throw new MojoFailureException("No previous version was found (current version: " + curVersionLabel);
120 }
121
122 Object changeToVersion(EnvironmentDescription d, ApplicationVersionDescription latestVersionDescription) {
123 String curVersionLabel = d.getVersionLabel();
124 String versionLabel = latestVersionDescription.getVersionLabel();
125
126 UpdateEnvironmentRequest request = new UpdateEnvironmentRequest().withEnvironmentId(d.getEnvironmentId()).withVersionLabel(versionLabel);
127
128 getLog()
129 .info(
130 "Changing versionLabel for Environment[name="
131 + curEnv.getEnvironmentName()
132 + "; environmentId="
133 + curEnv.getEnvironmentId()
134 + "] from version "
135 + curVersionLabel
136 + " to version "
137 + latestVersionDescription.getVersionLabel());
138
139 if (dryRun) {
140 return null;
141 }
142
143 return getService().updateEnvironment(request);
144 }
145 }