Search in sources :

Example 31 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.

the class CompressedDirectoryTest method testFileWithEmptyDirectory.

@Test
public void testFileWithEmptyDirectory() throws Exception {
    Path tempDir = Files.createTempDirectory("dockerDirectoryEmptySubdirectory");
    tempDir.toFile().deleteOnExit();
    assertThat(new File(tempDir.toFile(), "emptySubDir").mkdir(), is(true));
    try (CompressedDirectory dir = CompressedDirectory.create(tempDir);
        BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, contains("emptySubDir/"));
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 32 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.

the class CompressedDirectoryTest method testFile.

@Test
public void testFile() throws Exception {
    // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path
    final URL dockerDirectory = Resources.getResource("dockerDirectory");
    try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI()));
        BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file()));
        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
        final List<String> names = new ArrayList<>();
        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String name = entry.getName();
            names.add(name);
        }
        assertThat(names, containsInAnyOrder("Dockerfile", "bin/", "bin/date.sh", "innerDir/", "innerDir/innerDockerfile"));
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) URL(java.net.URL) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 33 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.

the class DefaultDockerClientTest method integrationTest.

@Test
@SuppressWarnings("deprecation")
public void integrationTest() throws Exception {
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    assertThat(creation.warnings(), anyOf(is(empty()), is(nullValue())));
    assertThat(id, is(any(String.class)));
    // Inspect using container ID
    {
        final ContainerInfo info = sut.inspectContainer(id);
        assertThat(info.id(), equalTo(id));
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }
    // Inspect using container name
    {
        final ContainerInfo info = sut.inspectContainer(name);
        assertThat(info.config().image(), equalTo(config.image()));
        assertThat(info.config().cmd(), equalTo(config.cmd()));
    }
    // Start container
    sut.startContainer(id);
    final Path dockerDirectory = getResource("dockerSslDirectory");
    // to a directory in a container
    if (dockerApiVersionAtLeast("1.20")) {
        try {
            sut.copyToContainer(dockerDirectory, id, "/tmp");
        } catch (Exception e) {
            fail("error copying files to container");
        }
        // Copy the same files from container
        final ImmutableSet.Builder<String> filesDownloaded = ImmutableSet.builder();
        try (TarArchiveInputStream tarStream = new TarArchiveInputStream(dockerApiVersionLessThan("1.24") ? sut.copyContainer(id, "/tmp") : sut.archiveContainer(id, "/tmp"))) {
            TarArchiveEntry entry;
            while ((entry = tarStream.getNextTarEntry()) != null) {
                filesDownloaded.add(entry.getName());
            }
        }
        // Check that we got back what we put in
        final File folder = new File(dockerDirectory.toString());
        final File[] files = folder.listFiles();
        if (files != null) {
            for (final File file : files) {
                if (!file.isDirectory()) {
                    Boolean found = false;
                    for (final String fileDownloaded : filesDownloaded.build()) {
                        if (fileDownloaded.contains(file.getName())) {
                            found = true;
                        }
                    }
                    assertTrue(found);
                }
            }
        }
    }
    // Kill container
    sut.killContainer(id);
    try {
        // Remove the container
        sut.removeContainer(id);
    } catch (DockerRequestException e) {
        // CircleCI doesn't let you remove a container :(
        if (!CIRCLECI) {
            // Verify that the container is gone
            exception.expect(ContainerNotFoundException.class);
            sut.inspectContainer(id);
        }
    }
}
Also used : Path(java.nio.file.Path) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerException(com.spotify.docker.client.exceptions.DockerException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) ImagePushFailedException(com.spotify.docker.client.exceptions.ImagePushFailedException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) BadParamException(com.spotify.docker.client.exceptions.BadParamException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) UnsupportedApiVersionException(com.spotify.docker.client.exceptions.UnsupportedApiVersionException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) ExpectedException(org.junit.rules.ExpectedException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ImmutableSet(com.google.common.collect.ImmutableSet) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecretFile(com.spotify.docker.client.messages.swarm.SecretFile) File(java.io.File) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) Test(org.junit.Test)

Example 34 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-client by spotify.

the class DefaultDockerClientTest method testArchiveContainer.

@Test
public void testArchiveContainer() throws Exception {
    requireDockerApiVersionAtLeast("1.20", "copyToContainer");
    // Pull image
    sut.pull(BUSYBOX_LATEST);
    // Create container
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    final ImmutableSet.Builder<String> files = ImmutableSet.builder();
    try (final TarArchiveInputStream tarStream = new TarArchiveInputStream(sut.archiveContainer(id, "/bin"))) {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            files.add(entry.getName());
        }
    }
    // Check that some common files exist
    assertThat(files.build(), both(hasItem("bin/")).and(hasItem("bin/wc")));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) ImmutableSet(com.google.common.collect.ImmutableSet) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 35 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project bookkeeper by apache.

the class DockerUtils method dumpContainerLogDirToTarget.

public static void dumpContainerLogDirToTarget(DockerClient docker, String containerId, String path) {
    final int READ_BLOCK_SIZE = 10000;
    try (InputStream dockerStream = docker.copyArchiveFromContainerCmd(containerId, path).exec();
        TarArchiveInputStream stream = new TarArchiveInputStream(dockerStream)) {
        TarArchiveEntry entry = stream.getNextTarEntry();
        while (entry != null) {
            if (entry.isFile()) {
                File output = new File(getTargetDirectory(containerId), entry.getName().replace("/", "-"));
                try (FileOutputStream os = new FileOutputStream(output)) {
                    byte[] block = new byte[READ_BLOCK_SIZE];
                    int read = stream.read(block, 0, READ_BLOCK_SIZE);
                    while (read > -1) {
                        os.write(block, 0, read);
                        read = stream.read(block, 0, READ_BLOCK_SIZE);
                    }
                }
            }
            entry = stream.getNextTarEntry();
        }
    } catch (RuntimeException | IOException e) {
        LOG.error("Error reading bk logs from container {}", containerId, e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)200 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)99 File (java.io.File)86 FileInputStream (java.io.FileInputStream)55 IOException (java.io.IOException)55 FileOutputStream (java.io.FileOutputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)38 InputStream (java.io.InputStream)31 BufferedInputStream (java.io.BufferedInputStream)29 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 OutputStream (java.io.OutputStream)17 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)15 HashMap (java.util.HashMap)11 GZIPInputStream (java.util.zip.GZIPInputStream)11