Feat: 对接 Docker 启动方式

This commit is contained in:
Suwings 2022-07-16 16:12:48 +08:00
parent 4bd3487abf
commit b6da9109c4
3 changed files with 22 additions and 5 deletions

View File

@ -1,4 +1,4 @@
{
"printWidth": 220,
"printWidth": 140,
"trailingComma": "none"
}

View File

@ -66,7 +66,12 @@ export default class FunctionDispatcher extends InstanceCommand {
}
// 是否启用 PTY 模式
const ptyProgramPath = path.normalize(path.join(process.cwd(), "lib"));
if (instance.config.terminalOption.pty && instance.config.terminalOption.ptyWindowCol && instance.config.terminalOption.ptyWindowRow) {
if (
instance.config.terminalOption.pty &&
instance.config.terminalOption.ptyWindowCol &&
instance.config.terminalOption.ptyWindowRow &&
instance.config.processType === "general"
) {
if (!fs.existsSync(ptyProgramPath)) throw new Error("无法启用 PTY 模式,因为 ./lib/pty 附属程序不存在");
instance.setPreset("start", new PtyStartCommand());
instance.setPreset("stop", new PtyStopCommand());

View File

@ -40,7 +40,7 @@ class StartupDockerProcessError extends Error {
}
}
// Docker 进程适配器
// 进程适配器
export class DockerProcessAdapter extends EventEmitter implements IInstanceProcess {
pid?: number | string;
@ -98,7 +98,8 @@ export default class DockerStartCommand extends InstanceCommand {
}
async exec(instance: Instance, source = "Unknown") {
if (!instance.config.startCommand || !instance.config.cwd || !instance.config.ie || !instance.config.oe) return instance.failure(new StartupDockerProcessError("启动命令,输入输出编码或工作目录为空值"));
if (!instance.config.startCommand || !instance.config.cwd || !instance.config.ie || !instance.config.oe)
return instance.failure(new StartupDockerProcessError("启动命令,输入输出编码或工作目录为空值"));
if (!fs.existsSync(instance.absoluteCwdPath())) return instance.failure(new StartupDockerProcessError("工作目录并不存在"));
try {
@ -205,6 +206,9 @@ export default class DockerStartCommand extends InstanceCommand {
logger.info(`类型: Docker 容器`);
logger.info("----------------");
// 是否使用 TTY 模式
const isTty = instance.config.terminalOption.pty;
// 开始 Docker 容器创建并运行
const docker = new Docker();
const container = await docker.createContainer({
@ -213,7 +217,7 @@ export default class DockerStartCommand extends InstanceCommand {
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Tty: isTty,
User: `${processUserUid()}:${processGroupGid()}`,
WorkingDir: "/workspace/",
Cmd: commandList,
@ -239,6 +243,14 @@ export default class DockerStartCommand extends InstanceCommand {
}
});
// 根据 PTY 参数设置窗口大小
if (isTty) {
const w = instance.config.terminalOption.ptyWindowCol;
const h = instance.config.terminalOption.ptyWindowCol;
container.resize({ h, w });
}
// Docker 对接到进程适配器
const processAdapter = new DockerProcessAdapter(container);
await processAdapter.start();