| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.beanstalk.maven.plugin; |
| 31 | |
|
| 32 | |
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk; |
| 33 | |
import com.amazonaws.services.elasticbeanstalk.model.ApplicationVersionDescription; |
| 34 | |
import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest; |
| 35 | |
import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionResult; |
| 36 | |
import com.amazonaws.services.elasticbeanstalk.model.DeleteApplicationVersionRequest; |
| 37 | |
import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest; |
| 38 | |
import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult; |
| 39 | |
import com.jcabi.aspects.Loggable; |
| 40 | |
import com.jcabi.log.Logger; |
| 41 | |
import javax.validation.constraints.NotNull; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | @EqualsAndHashCode(of = { "client", "application", "bundle" }) |
| 52 | |
@Loggable(Loggable.DEBUG) |
| 53 | |
final class OverridingVersion implements Version { |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private final transient AWSElasticBeanstalk client; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient String application; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private final transient Bundle bundle; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
protected OverridingVersion(@NotNull final AWSElasticBeanstalk clnt, |
| 77 | 1 | @NotNull final String app, @NotNull final Bundle bndl) { |
| 78 | 1 | this.client = clnt; |
| 79 | 1 | this.application = app; |
| 80 | 1 | this.bundle = bndl; |
| 81 | 1 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
@Override |
| 87 | |
public String toString() { |
| 88 | 0 | return this.bundle.name(); |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public String label() { |
| 96 | 2 | if (this.exists()) { |
| 97 | 0 | Logger.info( |
| 98 | |
this, |
| 99 | |
"Version '%s' already exists for '%s'", |
| 100 | |
this.bundle.name(), |
| 101 | |
this.application |
| 102 | |
); |
| 103 | |
} else { |
| 104 | 1 | final CreateApplicationVersionResult res = |
| 105 | |
this.client.createApplicationVersion( |
| 106 | |
new CreateApplicationVersionRequest() |
| 107 | |
.withApplicationName(this.application) |
| 108 | |
.withVersionLabel(this.bundle.name()) |
| 109 | |
.withSourceBundle(this.bundle.location()) |
| 110 | |
.withDescription(this.bundle.etag()) |
| 111 | |
); |
| 112 | 1 | final ApplicationVersionDescription desc = |
| 113 | |
res.getApplicationVersion(); |
| 114 | 1 | Logger.info( |
| 115 | |
this, |
| 116 | |
"Version '%s' created for '%s' (%s): '%s'", |
| 117 | |
desc.getVersionLabel(), |
| 118 | |
desc.getApplicationName(), |
| 119 | |
this.bundle.location(), |
| 120 | |
desc.getDescription() |
| 121 | |
); |
| 122 | 1 | if (!desc.getVersionLabel().equals(this.bundle.name())) { |
| 123 | 0 | throw new DeploymentException( |
| 124 | |
String.format( |
| 125 | |
"version label is '%s' while '%s' expected", |
| 126 | |
desc.getVersionLabel(), |
| 127 | |
this.bundle.name() |
| 128 | |
) |
| 129 | |
); |
| 130 | |
} |
| 131 | |
} |
| 132 | 1 | return this.bundle.name(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private boolean exists() { |
| 140 | 1 | final DescribeApplicationVersionsResult res = |
| 141 | |
this.client.describeApplicationVersions( |
| 142 | |
new DescribeApplicationVersionsRequest() |
| 143 | |
.withApplicationName(this.application) |
| 144 | |
.withVersionLabels(this.bundle.name()) |
| 145 | |
); |
| 146 | 1 | boolean exists = false; |
| 147 | 1 | if (res.getApplicationVersions().isEmpty()) { |
| 148 | 1 | Logger.info( |
| 149 | |
this, |
| 150 | |
"Version '%s' is absent in '%s'", |
| 151 | |
this.bundle.name(), |
| 152 | |
this.application |
| 153 | |
); |
| 154 | |
} else { |
| 155 | 0 | final ApplicationVersionDescription ver = |
| 156 | |
res.getApplicationVersions().get(0); |
| 157 | 0 | if (ver.getSourceBundle().equals(this.bundle.location()) |
| 158 | |
&& ver.getDescription().equals(this.bundle.etag())) { |
| 159 | 0 | Logger.info( |
| 160 | |
this, |
| 161 | |
"Version '%s' already exists for '%s', etag='%s'", |
| 162 | |
ver.getVersionLabel(), |
| 163 | |
ver.getApplicationName(), |
| 164 | |
ver.getDescription() |
| 165 | |
); |
| 166 | 0 | exists = true; |
| 167 | |
} else { |
| 168 | 0 | this.client.deleteApplicationVersion( |
| 169 | |
new DeleteApplicationVersionRequest() |
| 170 | |
.withApplicationName(this.application) |
| 171 | |
.withVersionLabel(this.bundle.name()) |
| 172 | |
); |
| 173 | 0 | Logger.info( |
| 174 | |
this, |
| 175 | |
|
| 176 | |
"Version '%s' deleted in '%s' because of its outdated S3 location", |
| 177 | |
this.bundle.name(), |
| 178 | |
this.application |
| 179 | |
); |
| 180 | |
} |
| 181 | |
} |
| 182 | 1 | return exists; |
| 183 | |
} |
| 184 | |
|
| 185 | |
} |