Search in sources :

Example 1 with ImageArchiveManifestEntry

use of io.fabric8.maven.docker.model.ImageArchiveManifestEntry in project docker-maven-plugin by fabric8io.

the class ImageArchiveUtilTest method findEntriesByRepoTagPatternNonEmptyManifest.

@Test
public void findEntriesByRepoTagPatternNonEmptyManifest() {
    ImageArchiveManifest nonEmpty = new ImageArchiveManifestAdapter(createBasicManifestJson());
    Map<String, ImageArchiveManifestEntry> entries;
    entries = ImageArchiveUtil.findEntriesByRepoTagPattern("does/not:match", nonEmpty);
    Assert.assertNotNull(entries);
    Assert.assertTrue(entries.isEmpty());
    // Anchored pattern
    entries = ImageArchiveUtil.findEntriesByRepoTagPattern("^test/image$", nonEmpty);
    Assert.assertNotNull(entries);
    Assert.assertTrue(entries.isEmpty());
}
Also used : ImageArchiveManifestAdapter(io.fabric8.maven.docker.model.ImageArchiveManifestAdapter) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry) Test(org.junit.Test)

Example 2 with ImageArchiveManifestEntry

use of io.fabric8.maven.docker.model.ImageArchiveManifestEntry in project docker-maven-plugin by fabric8io.

the class ImageArchiveUtilTest method findEntriesByRepoTagPatternSuccessfully.

@Test
public void findEntriesByRepoTagPatternSuccessfully() {
    ImageArchiveManifest nonEmpty = new ImageArchiveManifestAdapter(createBasicManifestJson());
    Map<String, ImageArchiveManifestEntry> entries;
    // Complete match
    entries = ImageArchiveUtil.findEntriesByRepoTagPattern("test/image:latest", nonEmpty);
    Assert.assertNotNull(entries);
    Assert.assertNotNull(entries.get("test/image:latest"));
    Assert.assertTrue(entries.get("test/image:latest").getRepoTags().contains("test/image:latest"));
    // Unanchored match
    entries = ImageArchiveUtil.findEntriesByRepoTagPattern("test/image", nonEmpty);
    Assert.assertNotNull(entries);
    Assert.assertNotNull(entries.get("test/image:latest"));
    Assert.assertTrue(entries.get("test/image:latest").getRepoTags().contains("test/image:latest"));
    // Initial anchor
    entries = ImageArchiveUtil.findEntriesByRepoTagPattern("^test/image", nonEmpty);
    Assert.assertNotNull(entries);
    Assert.assertNotNull(entries.get("test/image:latest"));
    Assert.assertTrue(entries.get("test/image:latest").getRepoTags().contains("test/image:latest"));
}
Also used : ImageArchiveManifestAdapter(io.fabric8.maven.docker.model.ImageArchiveManifestAdapter) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry) Test(org.junit.Test)

Example 3 with ImageArchiveManifestEntry

use of io.fabric8.maven.docker.model.ImageArchiveManifestEntry 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 4 with ImageArchiveManifestEntry

use of io.fabric8.maven.docker.model.ImageArchiveManifestEntry 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 5 with ImageArchiveManifestEntry

use of io.fabric8.maven.docker.model.ImageArchiveManifestEntry 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)

Aggregations

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