-
Notifications
You must be signed in to change notification settings - Fork 86
/
ApplicationImageMojo.java
220 lines (191 loc) · 9.37 KB
/
ApplicationImageMojo.java
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* JBoss, Home of Professional Open Source.
* Copyright 2022, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.plugin.provision;
import static java.lang.String.format;
import static java.lang.String.join;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Arrays;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
/**
* Build (and push) an application image containing the provisioned server and the deployment.
*
* The {@code}image{@code} goal extends the {@code}package{@code} goal, building and pushing the image occurs after the server
* is provisioned and the deployment deployed in it.
*
* The {@code}image{@code} goal relies on a Docker binary to execute all image commands (build, login, push).
*
* @since 4.0
*/
@Mojo(name = "image", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.PACKAGE)
public class ApplicationImageMojo extends PackageServerMojo {
public static final int DOCKER_CMD_CHECK_TIMEOUT = 3000;
/**
* The configuration of the application image.
*
* The {@code image} goal accepts the following configuration:
*
* <pre>
* <image>
* <!-- (optional) set it to false to skip build the application image (true by default) -->
* <build>true</build>
*
* <!-- (optional) set it to true to (login and) push the application image to the container registry (false by default).
*
* If user and password are not specified, the image goal will not attempt to login to the container
* registry prior to pushing the image.
* The login to the container registry must then be performed before Maven is run.
* -->
* <push>true</push>
*
* <!-- (optional) The binary used to perform image commands (build, login, push) (default is "docker") -->
* <docker-binary>docker</docker-binary>
*
* <!-- (optional) the JDK version used by the application. Default is "11", Allowed values are "11" and "17 -->
* <jdk-version>11</jdk-version>
*
* <!-- (optional) The group part of the name of the application image -->
* <group>${user.name}</group>
*
* <!-- (optional) The name part of the application image. If not set, the value of the artifactId (in lower case) is used -->
* <name>${project.artifactId}</name>
*
* <!-- (optional) The tag part of the application image (default is "latest") -->
* <tag>latest</tag>
*
* <!-- (optional) The container registry. If set, the registry is added to the application name.
* If the image is pushed and the registry is not set, it defaults to "docker.io" to login to the registry
* -->
* <registry>quay.io</registry>
*
* <!-- (optional) The user name to login to the container registry (if push is enabled). -->
* <user>${user.name}</user>
*
* <!-- (optional) The password login to the container registry (if push is enabled) -->
* <password>${my.secret.password}</password>
* </image>
* </pre>
*/
@Parameter(alias = "image")
private ApplicationImageInfo image;
@Override
protected String getGoal() {
return "image";
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
super.execute();
if (image == null) {
image = new ApplicationImageInfo();
}
if (!image.build) {
return;
}
if (!isDockerBinaryAvailable(image.dockerBinary)) {
throw new MojoExecutionException(String.format("Unable to build application image with %s. Please check your %s installation",
image.dockerBinary, image.dockerBinary));
}
String image = this.image.getApplicationImageName(project.getArtifactId());
String runtimeImage = this.image.getWildFlyRuntimeImage();
try {
boolean buildSuccess = buildApplicationImage(image, runtimeImage);
if (!buildSuccess) {
throw new MojoExecutionException(String.format("Unable to build application image %s", image));
}
getLog().info(String.format("Successfully built application image %s", image));
if (this.image.push) {
logToRegistry();
boolean pushSuccess = pushApplicationImage(image);
if (!pushSuccess) {
throw new MojoExecutionException(String.format("Unable to push application image %s", image));
}
getLog().info(String.format("Successfully pushed application image %s", image));
}
} catch (IOException e) {
MojoExecutionException ex = new MojoExecutionException(e.getLocalizedMessage());
ex.initCause(e);
throw ex;
}
}
private void logToRegistry() throws MojoExecutionException {
String registry = image.registry;
if (registry == null) {
getLog().info("Registry was not set. Using docker.io");
}
if (image.user != null && image.password != null) {
String[] dockerArgs = new String[] {
"login", registry,
"-u", image.user,
"-p", image.password
};
boolean loginSuccessful = ExecUtil.exec(getLog(), image.dockerBinary, dockerArgs);
if (!loginSuccessful) {
throw new MojoExecutionException(String.format("Could not log to the container registry with the command %s %s %s",
image.dockerBinary,
String.join(" ", Arrays.copyOf(dockerArgs, dockerArgs.length -1)),
"*******"));
}
}
}
private boolean buildApplicationImage(String image, String runtimeImage) throws IOException {
getLog().info(format("Building application image %s using %s.", image, this.image.dockerBinary));
getLog().info(format("Base image is %s", runtimeImage));
generateDockerfile(runtimeImage, Paths.get(project.getBuild().getDirectory()), provisioningDir);
String[] dockerArgs = new String[] {"build", "-t", image, "."};
getLog().info(format("Executing the following command to build application image: '%s %s'", this.image.dockerBinary, join(" ", dockerArgs)));
return ExecUtil.exec(getLog(), Paths.get(project.getBuild().getDirectory()).toFile(), this.image.dockerBinary, dockerArgs);
}
private boolean pushApplicationImage(String image) {
getLog().info(format("Pushing application image %s using %s.", image, this.image.dockerBinary));
String[] dockerArgs = new String[] {"push", image};
getLog().info(format("Executing the following command to push application image: '%s %s'", this.image.dockerBinary, join(" ", dockerArgs)));
return ExecUtil.exec(getLog(), Paths.get("target").toFile(), this.image.dockerBinary, dockerArgs);
}
private void generateDockerfile(String runtimeImage, Path targetDir, String wildflyDirectory) throws IOException {
Files.writeString(targetDir.resolve("Dockerfile"),
"FROM " + runtimeImage + "\n" +
"COPY --chown=jboss:root " + wildflyDirectory + " $JBOSS_HOME\n" +
"RUN chmod -R ug+rwX $JBOSS_HOME",
StandardCharsets.UTF_8);
}
private boolean isDockerBinaryAvailable(String dockerBinary) {
try {
if (!ExecUtil.execSilentWithTimeout(Duration.ofMillis(DOCKER_CMD_CHECK_TIMEOUT), dockerBinary, "-v")) {
getLog().warn(format("'%s -v' returned an error code. Make sure your %s binary is correct", dockerBinary, dockerBinary));
return false;
}
} catch (Exception e) {
getLog().warn(format("No %s binary found or general error: %s", dockerBinary, e));
return false;
}
return true;
}
}