-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathcp.ts
121 lines (118 loc) · 4.57 KB
/
cp.ts
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
import * as fs from 'fs';
import { WritableStreamBuffer } from 'stream-buffers';
import * as tar from 'tar';
import { KubeConfig } from './config';
import { Exec } from './exec';
import { generateTmpFileName } from './util';
export class Cp {
public execInstance: Exec;
public constructor(config: KubeConfig, execInstance?: Exec) {
this.execInstance = execInstance || new Exec(config);
}
/**
* @param {string} namespace - The namespace of the pod to exec the command inside.
* @param {string} podName - The name of the pod to exec the command inside.
* @param {string} containerName - The name of the container in the pod to exec the command inside.
* @param {string} srcPath - The source path in the pod
* @param {string} tgtPath - The target path in local
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
*/
public async cpFromPod(
namespace: string,
podName: string,
containerName: string,
srcPath: string,
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'zcf', '-'];
if (cwd) {
command.push('-C', cwd);
}
command.push(srcPath);
const writerStream = fs.createWriteStream(tmpFileName);
const errStream = new WritableStreamBuffer();
return new Promise<void>((resolve, reject) => {
this.execInstance
.exec(
namespace,
podName,
containerName,
command,
writerStream,
errStream,
null,
false,
async ({ status }) => {
try {
writerStream.close();
if (status === 'Failure' || errStream.size()) {
return reject(
new Error(
`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`,
),
);
}
await tar.x({
file: tmpFileName,
cwd: tgtPath,
});
resolve();
} catch (e) {
reject(e);
}
},
)
.catch(reject);
});
}
/**
* @param {string} namespace - The namespace of the pod to exec the command inside.
* @param {string} podName - The name of the pod to exec the command inside.
* @param {string} containerName - The name of the container in the pod to exec the command inside.
* @param {string} srcPath - The source path in local
* @param {string} tgtPath - The target path in the pod
* @param {string} [cwd] - The directory that is used as the parent in the host when uploading
*/
public async cpToPod(
namespace: string,
podName: string,
containerName: string,
srcPath: string,
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'xf', '-', '-C', tgtPath];
await tar.c({ file: tmpFileName, cwd }, [srcPath]);
const readStream = fs.createReadStream(tmpFileName);
const errStream = new WritableStreamBuffer();
return new Promise<void>((resolve, reject) => {
this.execInstance
.exec(
namespace,
podName,
containerName,
command,
null,
errStream,
readStream,
false,
async ({ status }) => {
await fs.promises.unlink(tmpFileName);
if (status === 'Failure' || errStream.size()) {
reject(
new Error(
`Error from cpToPod - details: \n ${errStream.getContentsAsString()}`,
),
);
} else {
resolve();
}
},
)
.catch(reject);
});
}
}