|
6 | 6 | import java.net.URI; |
7 | 7 | import java.net.URISyntaxException; |
8 | 8 | import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
9 | 10 | import java.util.List; |
| 11 | +import java.util.Map; |
10 | 12 | import java.util.UUID; |
11 | 13 | import java.util.regex.Matcher; |
12 | 14 | import java.util.regex.Pattern; |
@@ -35,7 +37,9 @@ public class BuildImgCmd extends AbstrDockerCmd<BuildImgCmd, ClientResponse> { |
35 | 37 |
|
36 | 38 | private static final Logger LOGGER = LoggerFactory.getLogger(BuildImgCmd.class); |
37 | 39 |
|
38 | | - private static final Pattern ADD_PATTERN = Pattern.compile("^ADD\\s+(.*)\\s+(.*)$"); |
| 40 | + private static final Pattern ADD_OR_COPY_PATTERN = Pattern.compile("^(ADD|COPY)\\s+(.*)\\s+(.*)$"); |
| 41 | + |
| 42 | + private static final Pattern ENV_PATTERN = Pattern.compile("^ENV\\s+(.*)\\s+(.*)$"); |
39 | 43 |
|
40 | 44 | private File dockerFolder = null; |
41 | 45 | private InputStream tarInputStream = null; |
@@ -133,14 +137,40 @@ protected File buildDockerFolderTar() { |
133 | 137 | List<File> filesToAdd = new ArrayList<File>(); |
134 | 138 | filesToAdd.add(dockerFile); |
135 | 139 |
|
| 140 | + Map<String, String>environmentMap = new HashMap<String, String>(); |
| 141 | + |
| 142 | + int lineNumber = 0; |
| 143 | + |
136 | 144 | for (String cmd : dockerFileContent) { |
137 | | - final Matcher matcher = ADD_PATTERN.matcher(cmd.trim()); |
| 145 | + |
| 146 | + lineNumber++; |
| 147 | + |
| 148 | + if (cmd.trim().isEmpty() || cmd.startsWith("#")) |
| 149 | + continue; // skip emtpy and commend lines |
| 150 | + |
| 151 | + final Matcher envMatcher = ENV_PATTERN.matcher(cmd.trim()); |
| 152 | + |
| 153 | + if (envMatcher.find()) { |
| 154 | + if (envMatcher.groupCount() != 2) |
| 155 | + throw new DockerException(String.format("Wrong ENV format on line [%d]", lineNumber)); |
| 156 | + |
| 157 | + String variable = envMatcher.group(1).trim(); |
| 158 | + |
| 159 | + String value = envMatcher.group(2).trim(); |
| 160 | + |
| 161 | + environmentMap.put(variable, value); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + final Matcher matcher = ADD_OR_COPY_PATTERN.matcher(cmd.trim()); |
138 | 166 | if (matcher.find()) { |
139 | | - if (matcher.groupCount() != 2) { |
140 | | - throw new DockerException(String.format("Wrong format on line [%s]", cmd)); |
| 167 | + if (matcher.groupCount() != 3) { |
| 168 | + throw new DockerException(String.format("Wrong ADD or COPY format on line [%d]", lineNumber)); |
141 | 169 | } |
142 | 170 |
|
143 | | - String resource = matcher.group(1).trim(); |
| 171 | + String extractedResource = matcher.group(2); |
| 172 | + |
| 173 | + String resource = filterForEnvironmentVars(extractedResource, environmentMap); |
144 | 174 |
|
145 | 175 | if(isFileResource(resource)) { |
146 | 176 | File src = new File(resource); |
@@ -170,6 +200,33 @@ protected File buildDockerFolderTar() { |
170 | 200 | } |
171 | 201 | } |
172 | 202 |
|
| 203 | + private String filterForEnvironmentVars(String extractedResource, |
| 204 | + Map<String, String> environmentMap) { |
| 205 | + |
| 206 | + if (environmentMap.size() > 0) { |
| 207 | + |
| 208 | + String currentResourceContent = extractedResource; |
| 209 | + |
| 210 | + for (Map.Entry<String, String> entry : environmentMap.entrySet()) { |
| 211 | + |
| 212 | + String variable = entry.getKey(); |
| 213 | + |
| 214 | + String replacementValue = entry.getValue(); |
| 215 | + |
| 216 | + // handle: $VARIABLE case |
| 217 | + currentResourceContent = currentResourceContent.replaceAll("\\$" + variable, replacementValue); |
| 218 | + |
| 219 | + // handle ${VARIABLE} case |
| 220 | + currentResourceContent = currentResourceContent.replaceAll("\\$\\{" + variable + "\\}", replacementValue); |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + return currentResourceContent; |
| 225 | + } |
| 226 | + else |
| 227 | + return extractedResource; |
| 228 | + } |
| 229 | + |
173 | 230 | private static boolean isFileResource(String resource) { |
174 | 231 | URI uri; |
175 | 232 | try { |
|
0 commit comments