Search in sources :

Example 11 with ImageArchiveManifest

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;
}
Also used : Matcher(java.util.regex.Matcher) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with ImageArchiveManifest

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;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 13 with ImageArchiveManifest

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;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ImageArchiveManifestAdapter(io.fabric8.maven.docker.model.ImageArchiveManifestAdapter) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) LinkedHashMap(java.util.LinkedHashMap) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry)

Example 14 with ImageArchiveManifest

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;
}
Also used : ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry)

Example 15 with ImageArchiveManifest

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));
}
Also used : ImageArchiveManifestAdapter(io.fabric8.maven.docker.model.ImageArchiveManifestAdapter) JsonArray(com.google.gson.JsonArray) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) Test(org.junit.Test)

Aggregations

ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)22 Test (org.junit.Test)20 ImageArchiveManifestAdapter (io.fabric8.maven.docker.model.ImageArchiveManifestAdapter)13 ImageArchiveManifestEntry (io.fabric8.maven.docker.model.ImageArchiveManifestEntry)10 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)5 JsonArray (com.google.gson.JsonArray)3 Gson (com.google.gson.Gson)2 JsonParseException (com.google.gson.JsonParseException)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Matcher (java.util.regex.Matcher)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1