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")));
}
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);
}
}
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();
}
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);
}
}
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);
}
}
Aggregations