-
Notifications
You must be signed in to change notification settings - Fork 642
/
SaveMojo.java
139 lines (118 loc) · 4.99 KB
/
SaveMojo.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
package io.fabric8.maven.docker;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import io.fabric8.maven.docker.config.ArchiveCompression;
import io.fabric8.maven.docker.util.ImageName;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProjectHelper;
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.ServiceHub;
@Mojo(name = "save")
public class SaveMojo extends AbstractDockerMojo {
// Used when not automatically determined
private final static ArchiveCompression STANDARD_ARCHIVE_COMPRESSION = ArchiveCompression.gzip;
@Component
private MavenProjectHelper projectHelper;
@Parameter(property = "docker.save.name")
private String saveName;
@Parameter(property = "docker.save.alias")
private String saveAlias;
@Parameter
private String saveFile;
@Parameter(property = "docker.skip.save", defaultValue = "false")
private boolean skipSave;
@Override
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {
if (skipSave) {
return;
}
String imageName = getImageName();
String fileName = getFileName(imageName);
ensureSaveDir(fileName);
log.info("Saving image %s to %s", imageName, fileName);
if (!serviceHub.getQueryService().hasImage(imageName)) {
throw new MojoExecutionException("No image " + imageName + " exists");
}
serviceHub.getDockerAccess().saveImage(imageName, fileName, ArchiveCompression.fromFileName(fileName));
}
private String getFileName(String iName) throws MojoExecutionException {
String configuredFileName = getConfiguredFileName();
if (configuredFileName != null) {
return configuredFileName;
}
if (saveAlias != null) {
return completeCalculatedFileName(saveAlias +
"-" + project.getVersion() +
"." + STANDARD_ARCHIVE_COMPRESSION.getFileSuffix());
}
ImageName imageName = new ImageName(iName);
return completeCalculatedFileName(imageName.getSimpleName() +
"-" + imageName.getTag()) +
"." + STANDARD_ARCHIVE_COMPRESSION.getFileSuffix();
}
private String getConfiguredFileName() {
Properties[] propsList = new Properties[] { System.getProperties(), project.getProperties() };
for (String key : new String[] { "docker.save.file", "docker.file", "file" }) {
for (Properties props : propsList) {
if (props.containsKey(key)) {
return props.getProperty(key);
}
}
}
return saveFile;
}
private String completeCalculatedFileName(String file) throws MojoExecutionException {
return project.getBuild().getDirectory() + "/" + file.replace("/","-");
}
private void ensureSaveDir(String fileName) throws MojoExecutionException {
File saveDir = new File(fileName).getParentFile();
if (!saveDir.exists()) {
if (!saveDir.mkdirs()) {
throw new MojoExecutionException("Can not create directory " + saveDir + " for storing save file");
}
}
}
private String getImageName() throws MojoExecutionException {
List<ImageConfiguration> images = getResolvedImages();
// specify image by name or alias
if (saveName == null && saveAlias == null) {
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
if (buildImages.size() == 1) {
return buildImages.get(0).getName();
}
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
if (saveName != null && saveAlias != null) {
throw new MojoExecutionException("Cannot specify both name and alias.");
}
for (ImageConfiguration ic : images) {
if (equalName(ic) || equalAlias(ic)) {
return ic.getName();
}
}
throw new MojoExecutionException(saveName != null ?
"Can not find image with name '" + saveName + "'" :
"Can not find image with alias '"+ saveAlias + "'");
}
private List<ImageConfiguration> getImagesWithBuildConfig(List<ImageConfiguration> images) {
List<ImageConfiguration> ret = new ArrayList<>();
for (ImageConfiguration image : images) {
if (image.getBuildConfiguration() != null) {
ret.add(image);
}
}
return ret;
}
private boolean equalAlias(ImageConfiguration ic) {
return saveAlias != null && saveAlias.equals(ic.getAlias());
}
private boolean equalName(ImageConfiguration ic) {
return saveName != null && saveName.equals(ic.getName());
}
}