@miwt/docker-tools
v0.0.6
Published
node api for docker
Downloads
7
Readme
example:
import os from 'node:os';
import {
composeArgs,
withArg,
getSSHDirInWin,
getWorkSpaceInWin,
miotDockerContainerName,
miotDockerImageName,
miotDockerImageRef,
miotDockerSSH,
miotDockerWorkspace,
DockerClientBase,
RunContainerCommandOptions,
RunContainerMount,
CancellationTokenLike,
DockerSpawnRef,
runDockerCommand,
} from '@miwt/docker-tools';
export class DockerClient extends DockerClientBase {
/**
* The ID of the Docker client
*/
public static ClientId = 'com.xiaomi.aiot.ide.miot.docker';
dockerOutput: OutputChannelWithPipe;
public constructor(
private scope: ServiceScope,
commandName = 'docker',
displayName = 'Docker',
description = 'Runs container commands using the Docker CLI',
) {
super(DockerClient.ClientId, commandName, displayName, description);
this.dockerOutput = this.scope.outputChannels.get('docker')!;
this.dockerOutput.outputChannel.show();
}
getWorkspaceMount(): RunContainerMount {
const workspace = getWorkSpaceInWin();
return {
type: 'bind',
readOnly: false,
source: workspace,
destination: miotDockerWorkspace,
};
}
getSSHMount(): RunContainerMount {
const sshDir = getSSHDirInWin();
return {
type: 'bind',
readOnly: false,
source: sshDir,
destination: miotDockerSSH,
};
}
async runMiotContainer(options?: Partial<RunContainerCommandOptions>): Promise<string | string[] | undefined> {
const containerList = await super.listContainers({
all: true,
stdOutPipe: this.dockerOutput.pipe,
stdErrPipe: this.dockerOutput.pipe,
});
const miotContaniner = containerList.find((t) => t.name === miotDockerContainerName);
if (miotContaniner?.state === 'running') {
return;
}
if (miotContaniner) {
return super.restartContainers({
container: [miotContaniner.id],
stdOutPipe: this.dockerOutput.pipe,
stdErrPipe: this.dockerOutput.pipe,
});
}
return super.runContainer({
...options,
stdOutPipe: this.dockerOutput.pipe,
stdErrPipe: this.dockerOutput.pipe,
imageRef: miotDockerImageRef,
name: miotDockerContainerName,
mounts: [this.getSSHMount(), this.getWorkspaceMount()],
detached: true,
interactive: true,
});
}
async ensureContainerRunning() {
/** 启动 docker */
if (!(await this.isMiotContainerRunning())) {
await this.runMiotContainer({});
}
}
async isMiotContainerRunning() {
const runningList = await super.listContainers({
running: true,
stdErrPipe: this.dockerOutput.pipe,
stdOutPipe: this.dockerOutput.pipe,
});
return runningList.findIndex((t) => t.name === miotDockerContainerName) > 0;
}
async stopMiotContainer() {
if (await this.isMiotContainerRunning())
super.stopContainers({
container: [miotDockerImageName],
});
}
async pullMiotImage(cancellationToken?: vscodeCancelToken) {
return super.pullImage({
imageRef: miotDockerImageRef,
stdOutPipe: this.dockerOutput.pipe,
stdErrPipe: this.dockerOutput.pipe,
cancellationToken,
});
}
//#region
#dockerSpawn: DockerSpawnRef | undefined;
get dockerSpawn() {
if (!this.#dockerSpawn || this.#dockerSpawn.childProcess.killed) {
this.#dockerSpawn = runDockerCommand({
command: 'docker',
args: composeArgs(withArg('exec', '-i', miotDockerContainerName, '/bin/bash'))(),
stdOutPipe: this.dockerOutput.pipe,
stdErrPipe: this.dockerOutput.pipe,
}) as DockerSpawnRef;
}
return this.#dockerSpawn;
}
async runCmdInDocker(cmd: string) {
this.dockerSpawn.wirte(cmd);
}
async cloneGitRepo(opt: {
args: string[];
projectName: string;
pipe?: NodeJS.WritableStream;
cancellationToken?: CancellationTokenLike;
}) {
await runDockerCommand({
command: 'docker',
args: composeArgs(
withArg('exec', '-i', miotDockerContainerName, 'git', ...opt.args, `${miotDockerWorkspace}/${opt.projectName}`),
)(),
stdOutPipe: opt.pipe,
stdErrPipe: opt.pipe,
cancellationToken: opt.cancellationToken,
});
await runDockerCommand({
command: 'docker',
args: composeArgs(
withArg('exec', '-i', miotDockerContainerName, 'chmod', '777', `${miotDockerWorkspace}/${opt.projectName}`),
)(),
stdOutPipe: opt.pipe,
stdErrPipe: opt.pipe,
cancellationToken: opt.cancellationToken,
});
}
//endregion
dispose() {
this.#dockerSpawn?.dispose();
this.#dockerSpawn?.childProcess.kill();
}
}