forked from kubernetes-client/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy.java
More file actions
157 lines (143 loc) · 5.49 KB
/
Copy pathCopy.java
File metadata and controls
157 lines (143 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2017, 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client;
import com.google.common.io.ByteStreams;
import io.kubernetes.client.models.V1Pod;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Copy extends Exec {
private static final Logger log = LoggerFactory.getLogger(Copy.class);
/** Simple Copy constructor, uses default configuration */
public Copy() {
super(Configuration.getDefaultApiClient());
}
/**
* Copy Constructor
*
* @param apiClient The api client to use.
*/
public Copy(ApiClient apiClient) {
super(apiClient);
}
public InputStream copyFileFromPod(String namespace, String pod, String srcPath)
throws ApiException, IOException {
return copyFileFromPod(namespace, pod, null, srcPath);
}
public InputStream copyFileFromPod(V1Pod pod, String srcPath) throws ApiException, IOException {
return copyFileFromPod(pod, null, srcPath);
}
public InputStream copyFileFromPod(V1Pod pod, String container, String srcPath)
throws ApiException, IOException {
return copyFileFromPod(
pod.getMetadata().getNamespace(), pod.getMetadata().getName(), container, srcPath);
}
public InputStream copyFileFromPod(String namespace, String pod, String container, String srcPath)
throws ApiException, IOException {
Process proc =
this.exec(
namespace,
pod,
new String[] {"sh", "-c", "cat " + srcPath + " | base64"},
container,
false,
false);
return new Base64InputStream(proc.getInputStream());
}
public void copyFileFromPod(
String namespace, String name, String container, String srcPath, Path destination)
throws ApiException, IOException {
try (InputStream is = copyFileFromPod(namespace, name, srcPath);
FileOutputStream fos = new FileOutputStream(destination.toFile())) {
ByteStreams.copy(is, fos);
fos.flush();
}
}
public void copyDirectoryFromPod(V1Pod pod, String srcPath, Path destination)
throws ApiException, IOException {
copyDirectoryFromPod(pod, null, srcPath, destination);
}
public void copyDirectoryFromPod(V1Pod pod, String container, String srcPath, Path destination)
throws ApiException, IOException {
copyDirectoryFromPod(
pod.getMetadata().getNamespace(),
pod.getMetadata().getName(),
container,
srcPath,
destination);
}
public void copyDirectoryFromPod(String namespace, String pod, String srcPath, Path destination)
throws ApiException, IOException {
copyDirectoryFromPod(namespace, pod, null, srcPath, destination);
}
public void copyDirectoryFromPod(
String namespace, String pod, String container, String srcPath, Path destination)
throws ApiException, IOException {
// TODO: Test that 'tar' is present in the container?
final Process proc =
this.exec(
namespace,
pod,
new String[] {"sh", "-c", "tar cf - " + srcPath + " | base64"},
container,
false,
false);
InputStream is = new Base64InputStream(new BufferedInputStream(proc.getInputStream()));
try (ArchiveInputStream archive = new TarArchiveInputStream(is)) {
// TODO Use new GzipCompressorInputStream(is))) here {
for (ArchiveEntry entry = archive.getNextEntry();
entry != null;
entry = archive.getNextEntry()) {
if (!archive.canReadEntryData(entry)) {
log.error("Can't read: " + entry);
continue;
}
File f = new File(destination.toFile(), entry.getName());
if (entry.isDirectory()) {
if (!f.isDirectory() && !f.mkdirs()) {
throw new IOException("create directory failed: " + f);
}
} else {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("create directory failed: " + parent);
}
try (OutputStream fs = new FileOutputStream(f)) {
System.out.println("Writing: " + f.getCanonicalPath());
ByteStreams.copy(archive, fs);
fs.flush();
}
}
}
}
}
public static void copyFileFromPod(String namespace, String pod, String srcPath, Path dest)
throws ApiException, IOException {
Copy c = new Copy();
InputStream is = c.copyFileFromPod(namespace, pod, null, srcPath);
FileOutputStream os = new FileOutputStream(dest.toFile());
ByteStreams.copy(is, os);
os.flush();
os.close();
}
}