Search in sources :

Example 21 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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 22 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream 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)

Example 23 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project RecordManager2 by moravianlibrary.

the class TarGzUtils method extract.

public static void extract(File tarFile, File destFile) throws IOException {
    logger.info("Extracting file: " + tarFile.getName());
    if (destFile.exists())
        throw new FileAlreadyExistsException(destFile.getAbsolutePath());
    if (!destFile.mkdir())
        throw new IOException("Can't make directory for " + destFile.getAbsolutePath());
    TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));
    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    while (tarEntry != null) {
        // create a file with the same name as the tarEntry
        File destPath = new File(destFile, tarEntry.getName());
        if (tarEntry.isDirectory()) {
            if (!destPath.mkdirs())
                logger.info("Can't make directory for " + destPath.getAbsolutePath());
        } else {
            if (!destPath.createNewFile())
                logger.info("Name of file already exists " + destPath.getAbsolutePath());
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            byte[] btoRead = new byte[1024];
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }
            bout.close();
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 24 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project halyard by spinnaker.

the class BackupService method untarHalconfig.

private void untarHalconfig(String halconfigDir, String halconfigTar) {
    FileInputStream tarInput = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    try {
        tarInput = new FileInputStream(new File(halconfigTar));
        tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", tarInput);
    } catch (IOException | ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to open backup: " + e.getMessage(), e);
    }
    try {
        ArchiveEntry archiveEntry = tarArchiveInputStream.getNextEntry();
        while (archiveEntry != null) {
            String entryName = archiveEntry.getName();
            Path outputPath = Paths.get(halconfigDir, entryName);
            File outputFile = outputPath.toFile();
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            if (archiveEntry.isDirectory()) {
                outputFile.mkdir();
            } else {
                Files.copy(tarArchiveInputStream, outputPath, REPLACE_EXISTING);
            }
            archiveEntry = tarArchiveInputStream.getNextEntry();
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read archive entry: " + e.getMessage(), e);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) Path(java.nio.file.Path) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 25 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project halyard by spinnaker.

the class RegistryBackedArchiveProfileBuilder method build.

public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath, SpinnakerArtifact artifact, String archiveName) {
    String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact);
    InputStream is;
    try {
        is = profileRegistry.readArchiveProfile(artifact.getName(), version, archiveName);
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Error retrieving contents of archive " + archiveName + ".tar.gz", e);
    }
    TarArchiveInputStream tis;
    try {
        tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    } catch (ArchiveException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e);
    }
    try {
        List<Profile> result = new ArrayList<>();
        ArchiveEntry profileEntry = tis.getNextEntry();
        while (profileEntry != null) {
            if (profileEntry.isDirectory()) {
                profileEntry = tis.getNextEntry();
                continue;
            }
            String entryName = profileEntry.getName();
            String profileName = String.join("/", artifact.getName(), archiveName, entryName);
            String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString();
            String contents = IOUtils.toString(tis);
            result.add((new ProfileFactory() {

                @Override
                protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) {
                    profile.setContents(profile.getBaseContents());
                }

                @Override
                protected Profile getBaseProfile(String name, String version, String outputFile) {
                    return new Profile(name, version, outputFile, contents);
                }

                @Override
                protected boolean showEditWarning() {
                    return false;
                }

                @Override
                protected ArtifactService getArtifactService() {
                    return artifactService;
                }

                @Override
                public SpinnakerArtifact getArtifact() {
                    return artifact;
                }

                @Override
                protected String commentPrefix() {
                    return null;
                }
            }).getProfile(profileName, outputPath, deploymentConfiguration, null));
            profileEntry = tis.getNextEntry();
        }
        return result;
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e);
    }
}
Also used : SpinnakerArtifact(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerArtifact) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) ArrayList(java.util.ArrayList) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) IOException(java.io.IOException) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)131 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)98 File (java.io.File)51 IOException (java.io.IOException)49 FileInputStream (java.io.FileInputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)45 InputStream (java.io.InputStream)34 FileOutputStream (java.io.FileOutputStream)33 BufferedInputStream (java.io.BufferedInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 Path (java.nio.file.Path)16 OutputStream (java.io.OutputStream)15 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)7