|
19 | 19 | import io.kubernetes.client.openapi.models.V1Pod;
|
20 | 20 | import io.kubernetes.client.util.exception.CopyNotSupportedException;
|
21 | 21 | import java.io.BufferedInputStream;
|
| 22 | +import java.io.BufferedReader; |
22 | 23 | import java.io.ByteArrayInputStream;
|
23 | 24 | import java.io.File;
|
24 | 25 | import java.io.FileInputStream;
|
25 | 26 | import java.io.FileOutputStream;
|
26 | 27 | import java.io.IOException;
|
27 | 28 | import java.io.InputStream;
|
| 29 | +import java.io.InputStreamReader; |
28 | 30 | import java.io.OutputStream;
|
29 | 31 | import java.nio.file.Path;
|
| 32 | +import java.nio.file.Paths; |
30 | 33 | import org.apache.commons.codec.binary.Base64InputStream;
|
31 | 34 | import org.apache.commons.codec.binary.Base64OutputStream;
|
32 | 35 | import org.apache.commons.compress.archivers.ArchiveEntry;
|
@@ -121,6 +124,35 @@ public void copyDirectoryFromPod(
|
121 | 124 | if (!isTarPresentInContainer(namespace, pod, container)) {
|
122 | 125 | throw new CopyNotSupportedException("Tar is not present in the target container");
|
123 | 126 | }
|
| 127 | + copyDirectoryFromPod(namespace, pod, container, srcPath, destination, true); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Copy directory from a pod to local. |
| 132 | + * |
| 133 | + * @param namespace |
| 134 | + * @param pod |
| 135 | + * @param container |
| 136 | + * @param srcPath |
| 137 | + * @param destination |
| 138 | + * @param enableTarCompressing: false if tar is not present in target container |
| 139 | + * @throws IOException |
| 140 | + * @throws ApiException |
| 141 | + */ |
| 142 | + public void copyDirectoryFromPod( |
| 143 | + String namespace, |
| 144 | + String pod, |
| 145 | + String container, |
| 146 | + String srcPath, |
| 147 | + Path destination, |
| 148 | + boolean enableTarCompressing) |
| 149 | + throws IOException, ApiException { |
| 150 | + if (!enableTarCompressing) { |
| 151 | + TreeNode tree = new TreeNode(true, srcPath, true); |
| 152 | + createDirectoryTree(tree, namespace, pod, container, srcPath); |
| 153 | + createDirectoryStructureFromTree(tree, namespace, pod, container, srcPath, destination); |
| 154 | + return; |
| 155 | + } |
124 | 156 | final Process proc =
|
125 | 157 | this.exec(
|
126 | 158 | namespace,
|
@@ -165,6 +197,125 @@ public void copyDirectoryFromPod(
|
165 | 197 | }
|
166 | 198 | }
|
167 | 199 |
|
| 200 | + // This creates directories and files using tree of files and directories under container |
| 201 | + private void createDirectoryStructureFromTree( |
| 202 | + TreeNode tree, |
| 203 | + String namespace, |
| 204 | + String pod, |
| 205 | + String container, |
| 206 | + String srcPath, |
| 207 | + Path destination) |
| 208 | + throws IOException, ApiException { |
| 209 | + // Code for creating invidual directory and files |
| 210 | + createDirectory(tree, destination); |
| 211 | + createFiles(tree, namespace, pod, container, srcPath, destination); |
| 212 | + } |
| 213 | + |
| 214 | + // Method to create files from directories/files tree in destination |
| 215 | + private void createFiles( |
| 216 | + TreeNode node, |
| 217 | + String namespace, |
| 218 | + String pod, |
| 219 | + String container, |
| 220 | + String srcPath, |
| 221 | + Path destination) |
| 222 | + throws IOException, ApiException { |
| 223 | + if (node == null) { |
| 224 | + return; |
| 225 | + } |
| 226 | + for (TreeNode childNode : node.getChildren()) { |
| 227 | + if (!childNode.isDir) { |
| 228 | + // Create file which is under 'node' |
| 229 | + String filePath = genericPathBuilder(destination.toString(), childNode.name); |
| 230 | + File f = new File(filePath); |
| 231 | + if (!f.createNewFile()) { |
| 232 | + throw new IOException("Failed to create file: " + f); |
| 233 | + } |
| 234 | + String modifiedSrcPath = genericPathBuilder(srcPath, childNode.name); |
| 235 | + try (InputStream is = copyFileFromPod(namespace, pod, modifiedSrcPath); |
| 236 | + OutputStream fs = new FileOutputStream(f)) { |
| 237 | + ByteStreams.copy(is, fs); |
| 238 | + fs.flush(); |
| 239 | + } |
| 240 | + } else { |
| 241 | + String newSrcPath = genericPathBuilder(srcPath, childNode.name); |
| 242 | + // TODO: Change this once the method genericPathBuilder is changed to varargs |
| 243 | + Path newDestination = Paths.get(destination.toString(), childNode.name); |
| 244 | + createFiles(childNode, namespace, pod, container, newSrcPath, newDestination); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + // Method to create directories in destination path |
| 250 | + private void createDirectory(TreeNode node, Path destination) throws IOException { |
| 251 | + if (node == null) { |
| 252 | + return; |
| 253 | + } |
| 254 | + if (!node.isRoot) { |
| 255 | + // String directoryPath = genericPathBuilder(destination.toString(), node.name); |
| 256 | + File f = new File(destination.toString()); |
| 257 | + // File f = new File(directoryPath); |
| 258 | + if (!f.mkdirs()) { |
| 259 | + throw new IOException("Failed to create directory: " + f); |
| 260 | + } |
| 261 | + } |
| 262 | + for (TreeNode childNode : node.getChildren()) { |
| 263 | + if (childNode.isDir) { |
| 264 | + String path = genericPathBuilder(destination.toString(), childNode.name); |
| 265 | + Path newPath = Paths.get(path); |
| 266 | + createDirectory(childNode, newPath); |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + // Method to create a tree of files and directory of location inside container |
| 272 | + private void createDirectoryTree( |
| 273 | + TreeNode node, String namespace, String pod, String container, String srcPath) |
| 274 | + throws IOException, ApiException { |
| 275 | + if (node.isDir) { |
| 276 | + final Process proc = |
| 277 | + this.exec( |
| 278 | + namespace, |
| 279 | + pod, |
| 280 | + new String[] {"sh", "-c", "ls -F " + srcPath}, |
| 281 | + container, |
| 282 | + false, |
| 283 | + false); |
| 284 | + |
| 285 | + try (InputStream is = proc.getInputStream(); |
| 286 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { |
| 287 | + String line = ""; |
| 288 | + while ((line = reader.readLine()) != null) { |
| 289 | + int len = line.length(); |
| 290 | + // line = stripFileSeparators(line); |
| 291 | + if (line.charAt(line.length() - 1) == '/') { |
| 292 | + TreeNode subDirTree = |
| 293 | + new TreeNode( |
| 294 | + true, |
| 295 | + line.substring(0, len - 1), |
| 296 | + false); // Stripping off '/' in the end of directory |
| 297 | + String path = genericPathBuilder(srcPath, subDirTree.name); |
| 298 | + createDirectoryTree(subDirTree, namespace, pod, container, path); |
| 299 | + node.getChildren().add(subDirTree); |
| 300 | + } else { |
| 301 | + node.getChildren().add(new TreeNode(false, line, false)); |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + /* |
| 309 | + Generic method to create path. |
| 310 | + TODO: Change this to varargs |
| 311 | + */ |
| 312 | + private String genericPathBuilder(String first, String second) { |
| 313 | + StringBuilder pathBuilder = new StringBuilder(first); |
| 314 | + pathBuilder.append(File.separator); |
| 315 | + pathBuilder.append(second); |
| 316 | + return pathBuilder.toString(); |
| 317 | + } |
| 318 | + |
168 | 319 | public static void copyFileFromPod(String namespace, String pod, String srcPath, Path dest)
|
169 | 320 | throws ApiException, IOException {
|
170 | 321 | Copy c = new Copy();
|
|
0 commit comments