Skip to content

Remove tmp-promise #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 35 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"rfc4648": "^1.3.0",
"stream-buffers": "^3.0.2",
"tar": "^6.1.11",
"tmp-promise": "^3.0.2",
"tslib": "^2.4.1",
"ws": "^8.11.0"
},
Expand Down
21 changes: 6 additions & 15 deletions src/cp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as fs from 'fs';
import { WritableStreamBuffer } from 'stream-buffers';
import * as tar from 'tar';
import * as tmp from 'tmp-promise';

import { KubeConfig } from './config';
import { Exec } from './exec';
import { generateTmpFileName } from './util';

export class Cp {
public execInstance: Exec;
Expand All @@ -18,7 +17,7 @@ export class Cp {
* @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
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
*/
public async cpFromPod(
namespace: string,
Expand All @@ -28,8 +27,7 @@ export class Cp {
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'zcf', '-'];
if (cwd) {
command.push('-C', cwd);
Expand Down Expand Up @@ -65,7 +63,7 @@ export class Cp {
* @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
* @param {string} [cwd] - The directory that is used as the parent in the host when uploading
*/
public async cpToPod(
namespace: string,
Expand All @@ -75,16 +73,9 @@ export class Cp {
tgtPath: string,
cwd?: string,
): Promise<void> {
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
const tmpFileName = await generateTmpFileName();
const command = ['tar', 'xf', '-', '-C', tgtPath];
await tar.c(
{
file: tmpFile.name,
cwd,
},
[srcPath],
);
await tar.c({ file: tmpFileName, cwd }, [srcPath]);
const readStream = fs.createReadStream(tmpFileName);
const errStream = new WritableStreamBuffer();
this.execInstance.exec(
Expand Down
24 changes: 24 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { tmpdir } from 'os';
import { randomUUID } from 'crypto';
import { promises as fs, constants as fsConstants } from 'fs';
import { CoreV1Api, V1Container, V1Pod } from './gen/api';

export async function podsForNode(api: CoreV1Api, nodeName: string): Promise<V1Pod[]> {
Expand Down Expand Up @@ -142,3 +145,24 @@ export function totalForResource(pod: V1Pod, resource: string): ResourceStatus {
});
return new ResourceStatus(reqTotal, limitTotal, resource);
}

export async function generateTmpFileName(): Promise<string> {
let tmpFileName: string;

let i = 0;
do {
tmpFileName = `${tmpdir()}/${randomUUID()}`;

try {
await fs.access(tmpFileName, fsConstants.W_OK);

console.warn('Tmp file already exists');
} catch (err) {
return tmpFileName;
}

i++;
} while (i < 10);

throw new Error('Cannot generate tmp file name');
}