use of io.fabric8.maven.docker.model.ImageArchiveManifest in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtil method findEntriesByRepoTagPattern.
/**
* Search the manifest for an entry that has a repository and tag matching the provided pattern.
*
* @param repoTagPattern the repository and tag to search (e.g. busybox:latest).
* @param manifest the manifest to be searched
* @return a pair containing the matched tag and the entry found, or null if no match.
*/
public static Map<String, ImageArchiveManifestEntry> findEntriesByRepoTagPattern(Pattern repoTagPattern, ImageArchiveManifest manifest) throws PatternSyntaxException {
Map<String, ImageArchiveManifestEntry> entries = new LinkedHashMap<>();
if (repoTagPattern == null || manifest == null) {
return entries;
}
Matcher matcher = repoTagPattern.matcher("");
for (ImageArchiveManifestEntry entry : manifest.getEntries()) {
for (String entryRepoTag : entry.getRepoTags()) {
if (matcher.reset(entryRepoTag).find()) {
entries.putIfAbsent(entryRepoTag, entry);
}
}
}
return entries;
}
use of io.fabric8.maven.docker.model.ImageArchiveManifest in project docker-maven-plugin by fabric8io.
the class BuildService method getArchiveImageName.
private String getArchiveImageName(BuildImageConfiguration buildConfig, File tarArchive) throws MojoExecutionException {
if (buildConfig.getLoadNamePattern() == null || buildConfig.getLoadNamePattern().length() == 0) {
return null;
}
ImageArchiveManifest manifest;
try {
manifest = readArchiveManifest(tarArchive);
} catch (IOException | JsonParseException e) {
throw new MojoExecutionException("Unable to read image manifest in archive " + buildConfig.getDockerArchive(), e);
}
String archiveImageName;
try {
archiveImageName = matchArchiveImagesToPattern(buildConfig.getLoadNamePattern(), manifest);
} catch (PatternSyntaxException e) {
throw new MojoExecutionException("Unable to interpret loadNamePattern " + buildConfig.getLoadNamePattern(), e);
}
if (archiveImageName == null) {
throw new MojoExecutionException("No image in the archive has a tag that matches pattern " + buildConfig.getLoadNamePattern());
}
return archiveImageName;
}
use of io.fabric8.maven.docker.model.ImageArchiveManifest in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtil method readManifest.
/**
* Read the (possibly compressed) image archive stream provided and return the archive manifest.
*
* If there is no manifest found, then null is returned. Incomplete manifests are returned
* with as much information parsed as possible.
*
* @param inputStream
* @return the parsed manifest, or null if none found.
* @throws IOException
* @throws JsonParseException
*/
public static ImageArchiveManifest readManifest(InputStream inputStream) throws IOException, JsonParseException {
Map<String, JsonParseException> parseExceptions = new LinkedHashMap<>();
Map<String, JsonElement> parsedEntries = new LinkedHashMap<>();
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(createUncompressedStream(inputStream))) {
TarArchiveEntry tarEntry;
Gson gson = new Gson();
while ((tarEntry = tarStream.getNextTarEntry()) != null) {
if (tarEntry.isFile() && tarEntry.getName().endsWith(".json")) {
try {
JsonElement element = gson.fromJson(new InputStreamReader(tarStream, StandardCharsets.UTF_8), JsonElement.class);
parsedEntries.put(tarEntry.getName(), element);
} catch (JsonParseException exception) {
parseExceptions.put(tarEntry.getName(), exception);
}
}
}
}
JsonElement manifestJson = parsedEntries.get(MANIFEST_JSON);
if (manifestJson == null) {
JsonParseException parseException = parseExceptions.get(MANIFEST_JSON);
if (parseException != null) {
throw parseException;
}
return null;
}
ImageArchiveManifestAdapter manifest = new ImageArchiveManifestAdapter(manifestJson);
for (ImageArchiveManifestEntry entry : manifest.getEntries()) {
JsonElement entryConfigJson = parsedEntries.get(entry.getConfig());
if (entryConfigJson != null && entryConfigJson.isJsonObject()) {
manifest.putConfig(entry.getConfig(), entryConfigJson.getAsJsonObject());
}
}
return manifest;
}
use of io.fabric8.maven.docker.model.ImageArchiveManifest in project docker-maven-plugin by fabric8io.
the class BuildService method readArchiveManifest.
private ImageArchiveManifest readArchiveManifest(File tarArchive) throws IOException, JsonParseException {
long time = System.currentTimeMillis();
ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(tarArchive);
log.info("%s: Read archive manifest in %s", tarArchive, EnvUtil.formatDurationTill(time));
// Show the results of reading the manifest to users trying to debug their configuration
if (log.isDebugEnabled()) {
for (ImageArchiveManifestEntry entry : manifest.getEntries()) {
log.debug("Entry ID: %s has %d repo tag(s)", entry.getId(), entry.getRepoTags().size());
for (String repoTag : entry.getRepoTags()) {
log.debug("Repo Tag: %s", repoTag);
}
}
}
return manifest;
}
use of io.fabric8.maven.docker.model.ImageArchiveManifest in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtilTest method findByRepoTagPatternEmptyManifest.
@Test
public void findByRepoTagPatternEmptyManifest() {
ImageArchiveManifest empty = new ImageArchiveManifestAdapter(new JsonArray());
Assert.assertNull(ImageArchiveUtil.findEntryByRepoTagPattern(".*", empty));
Assert.assertNull(ImageArchiveUtil.findEntryByRepoTagPattern(".*", null));
Assert.assertNull(ImageArchiveUtil.findEntryByRepoTagPattern((String) null, null));
}
Aggregations