forked from fabric8io/docker-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ImageConfiguration.java
214 lines (173 loc) · 5.89 KB
/
ImageConfiguration.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
package io.fabric8.maven.docker.config;
import java.io.Serializable;
import java.util.*;
import io.fabric8.maven.docker.util.*;
import org.apache.maven.plugins.annotations.Parameter;
/**
* @author roland
* @since 02.09.14
*/
public class ImageConfiguration implements StartOrderResolver.Resolvable, Serializable {
@Parameter(required = true)
private String name;
@Parameter
private String alias;
@Parameter
private RunImageConfiguration run;
@Parameter
private BuildImageConfiguration build;
@Parameter
private WatchImageConfiguration watch;
@Parameter
private Map<String,String> external;
@Parameter
private String registry;
// Used for injection
public ImageConfiguration() {}
@Override
public String getName() {
return name;
}
/**
* Change the name which can be useful in long running runs e.g. for updating
* images when doing updates. Use with caution and only for those circumstances.
*
* @param name image name to set.
*/
public void setName(String name) {
this.name = name;
}
@Override
public String getAlias() {
return alias;
}
public RunImageConfiguration getRunConfiguration() {
return (run == null) ? RunImageConfiguration.DEFAULT : run;
}
public BuildImageConfiguration getBuildConfiguration() {
return build;
}
public WatchImageConfiguration getWatchConfiguration() {
return watch;
}
public Map<String, String> getExternalConfig() {
return external;
}
@Override
public List<String> getDependencies() {
RunImageConfiguration runConfig = getRunConfiguration();
List<String> ret = new ArrayList<>();
if (runConfig != null) {
addVolumes(runConfig, ret);
addLinks(runConfig, ret);
addContainerNetwork(runConfig, ret);
addDependsOn(runConfig, ret);
}
return ret;
}
private void addVolumes(RunImageConfiguration runConfig, List<String> ret) {
RunVolumeConfiguration volConfig = runConfig.getVolumeConfiguration();
if (volConfig != null) {
List<String> volumeImages = volConfig.getFrom();
if (volumeImages != null) {
ret.addAll(volumeImages);
}
}
}
private void addLinks(RunImageConfiguration runConfig, List<String> ret) {
// Custom networks can have circular links, no need to be considered for the starting order.
if (!runConfig.getNetworkingConfig().isCustomNetwork()) {
for (String[] link : EnvUtil.splitOnLastColon(runConfig.getLinks())) {
ret.add(link[0]);
}
}
}
private void addContainerNetwork(RunImageConfiguration runConfig, List<String> ret) {
NetworkConfig config = runConfig.getNetworkingConfig();
String alias = config.getContainerAlias();
if (alias != null) {
ret.add(alias);
}
}
private void addDependsOn(RunImageConfiguration runConfig, List<String> ret) {
// Only used in custom networks.
if (runConfig.getNetworkingConfig().isCustomNetwork()) {
for (String link : runConfig.getDependsOn()) {
ret.add(link);
}
}
}
public boolean isDataImage() {
// If there is no explicit run configuration, its a data image
// TODO: Probably add an explicit property so that a user can indicated whether it
// is a data image or not on its own.
return getRunConfiguration() == null;
}
public String getDescription() {
return String.format("[%s] %s", new ImageName(name).getFullName(), (alias != null ? "\"" + alias + "\"" : "")).trim();
}
public String getRegistry() {
return registry;
}
@Override
public String toString() {
return String.format("ImageConfiguration {name='%s', alias='%s'}", name, alias);
}
public String initAndValidate(ConfigHelper.NameFormatter nameFormatter, Logger log) {
name = nameFormatter.format(name);
String minimalApiVersion = null;
if (build != null) {
minimalApiVersion = build.initAndValidate(log);
}
if (run != null) {
minimalApiVersion = EnvUtil.extractLargerVersion(minimalApiVersion, run.initAndValidate());
}
return minimalApiVersion;
}
// =========================================================================
// Builder for image configurations
public static class Builder {
private final ImageConfiguration config;
public Builder() {
this(null);
}
public Builder(ImageConfiguration that) {
if (that == null) {
this.config = new ImageConfiguration();
} else {
this.config = DeepCopy.copy(that);
}
}
public Builder name(String name) {
config.name = name;
return this;
}
public Builder alias(String alias) {
config.alias = alias;
return this;
}
public Builder runConfig(RunImageConfiguration runConfig) {
config.run = runConfig;
return this;
}
public Builder buildConfig(BuildImageConfiguration buildConfig) {
config.build = buildConfig;
return this;
}
public Builder externalConfig(Map<String, String> externalConfig) {
config.external = externalConfig;
return this;
}
public Builder registry(String registry) {
config.registry = registry;
return this;
}
public ImageConfiguration build() {
return config;
}
public Builder watchConfig(WatchImageConfiguration watchConfig) {
config.watch = watchConfig;
return this;
}
}
}