From 67b410e44873d4cb884d73bca173633c53cb3184 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 3 Jan 2020 14:33:39 +0800 Subject: [PATCH] feat: update docker api invoke Signed-off-by: MiaoWoo --- packages/core/src/controller/dashboard.ts | 4 ++-- packages/core/src/controller/docker/config.ts | 2 +- .../core/src/controller/docker/container.ts | 2 +- packages/core/src/controller/docker/node.ts | 2 +- .../core/src/controller/docker/service.ts | 2 +- packages/core/src/controller/docker/stack.ts | 6 +++--- packages/core/src/controller/docker/swarm.ts | 2 +- packages/core/src/controller/docker/system.ts | 2 +- packages/core/src/controller/docker/task.ts | 20 +++++++++++++++++- packages/core/src/controller/ext/group.ts | 2 +- packages/core/src/index.ts | 4 ++-- packages/vscode/package.json | 12 +++++++++++ packages/vscode/src/provider/docker.ts | 21 +++++++++++++++++++ 13 files changed, 66 insertions(+), 15 deletions(-) diff --git a/packages/core/src/controller/dashboard.ts b/packages/core/src/controller/dashboard.ts index 819f8fe..0dd68ac 100644 --- a/packages/core/src/controller/dashboard.ts +++ b/packages/core/src/controller/dashboard.ts @@ -1,4 +1,4 @@ -import * as docker from '@dayu/docker-api' +import docker from '@dayu/docker-api' import { controller, httpGet } from "@cc-server/binding"; const STACK_LABEL = 'com.docker.stack.namespace'; @@ -28,7 +28,7 @@ class DashboardController { stacks: Array.from(new Set(stacks)), services, networks: networks.map(n => n.Name), - containers: containers.map(c => c.Names), + containers: containers.map(c => c.Names[0].substr(1)), images: images.map(i => i.Id), volumes: volumes.Volumes.map(v => v.Name) } diff --git a/packages/core/src/controller/docker/config.ts b/packages/core/src/controller/docker/config.ts index 8e841fb..9e45ab4 100644 --- a/packages/core/src/controller/docker/config.ts +++ b/packages/core/src/controller/docker/config.ts @@ -1,4 +1,4 @@ -import * as docker from '@dayu/docker-api' +import docker from '@dayu/docker-api' import { controller, get, requestParam } from '@cc-server/binding' @controller('/config') diff --git a/packages/core/src/controller/docker/container.ts b/packages/core/src/controller/docker/container.ts index dff46c5..11e7140 100644 --- a/packages/core/src/controller/docker/container.ts +++ b/packages/core/src/controller/docker/container.ts @@ -1,4 +1,4 @@ -import * as docker from '@dayu/docker-api' +import docker from '@dayu/docker-api' import { io, interfaces, namespace, listener, Message } from '@cc-server/ws' import { controller, get, post, requestParam } from '@cc-server/binding' diff --git a/packages/core/src/controller/docker/node.ts b/packages/core/src/controller/docker/node.ts index f1091c1..3b4cda6 100644 --- a/packages/core/src/controller/docker/node.ts +++ b/packages/core/src/controller/docker/node.ts @@ -1,5 +1,5 @@ +import docker from '@dayu/docker-api' import { controller, httpGet, httpPost } from 'inversify-express-utils'; -import * as docker from '@dayu/docker-api' @controller('/node') class NodeController { diff --git a/packages/core/src/controller/docker/service.ts b/packages/core/src/controller/docker/service.ts index 6110779..1110eaa 100644 --- a/packages/core/src/controller/docker/service.ts +++ b/packages/core/src/controller/docker/service.ts @@ -1,4 +1,4 @@ -import * as docker from '@dayu/docker-api' +import docker from '@dayu/docker-api' import { namespace, listener, interfaces, io, Message } from '@cc-server/ws' import { controller, post, get, requestParam, queryParam } from '@cc-server/binding'; diff --git a/packages/core/src/controller/docker/stack.ts b/packages/core/src/controller/docker/stack.ts index 9429bb9..7bda994 100644 --- a/packages/core/src/controller/docker/stack.ts +++ b/packages/core/src/controller/docker/stack.ts @@ -1,5 +1,5 @@ +import docker from '@dayu/docker-api' import { controller, get, post, requestParam } from '@cc-server/binding'; -import * as docker from '@dayu/docker-api' const STACK_LABEL = 'com.docker.stack.namespace'; @@ -26,7 +26,7 @@ class StackController { } return { status: 0, - data: result + data: { rows: result } }; } @@ -49,6 +49,6 @@ class StackController { @post('/create') public async create() { - + } } diff --git a/packages/core/src/controller/docker/swarm.ts b/packages/core/src/controller/docker/swarm.ts index 7e95105..607982a 100644 --- a/packages/core/src/controller/docker/swarm.ts +++ b/packages/core/src/controller/docker/swarm.ts @@ -1,5 +1,5 @@ +import docker from '@dayu/docker-api' import { controller, httpGet, httpPost } from 'inversify-express-utils'; -import * as docker from '@dayu/docker-api' @controller('/swarm') class SwarmController { diff --git a/packages/core/src/controller/docker/system.ts b/packages/core/src/controller/docker/system.ts index 8a82b93..ba62a47 100644 --- a/packages/core/src/controller/docker/system.ts +++ b/packages/core/src/controller/docker/system.ts @@ -1,5 +1,5 @@ +import docker from '@dayu/docker-api' import { controller, httpGet, httpPost } from 'inversify-express-utils'; -import * as docker from '@dayu/docker-api' import { io, namespace, listener, interfaces, Message } from '@cc-server/ws'; @controller('') diff --git a/packages/core/src/controller/docker/task.ts b/packages/core/src/controller/docker/task.ts index 2dc92dc..7d88001 100644 --- a/packages/core/src/controller/docker/task.ts +++ b/packages/core/src/controller/docker/task.ts @@ -1,5 +1,6 @@ +import docker from '@dayu/docker-api' +import { namespace, listener, interfaces, io, Message } from '@cc-server/ws' import { controller, post, get, requestParam, queryParam } from '@cc-server/binding'; -import * as docker from '@dayu/docker-api' @controller('/task') class TaskController { @@ -19,3 +20,20 @@ class TaskController { }; } } + +@namespace("/task") +class TaskNamespace extends interfaces.Namespace { + @listener() + async logs(socket: io.Socket, data: any) { + try { + let stream = await docker.task.logs(data.id, data); + this.defer(socket, () => stream.connection.destroy()); + stream.on('data', (chunk: ArrayBuffer) => { + let log = Buffer.from(chunk.slice(8, chunk.byteLength - 1)).toString(); + socket.send(log); + }) + } catch (ex) { + return new Message(ex.message); + } + } +} \ No newline at end of file diff --git a/packages/core/src/controller/ext/group.ts b/packages/core/src/controller/ext/group.ts index 0668985..847940b 100644 --- a/packages/core/src/controller/ext/group.ts +++ b/packages/core/src/controller/ext/group.ts @@ -1,5 +1,5 @@ +import docker from '@dayu/docker-api' import { controller, get, post, requestParam } from '@cc-server/binding'; -import * as docker from '@dayu/docker-api' const GROUP_LABEL = 'pw.yumc.group.name' diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 225bd32..717cf82 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -10,8 +10,8 @@ import * as path from 'path' // process.env.DOCKER_HOST = '/var/run/docker.sock' // process.env.DOCKER_HOST = 'https://dscli.miaowoo.cc' // process.env.DOCKER_HOST = 'http://172.20.0.90:2378' -// process.env.DOCKER_HOST = 'https://dcli.yumc.pw' -process.env.DOCKER_HOST = 'http://172.16.200.12:8376' +process.env.DOCKER_HOST = 'https://dcli.yumc.pw' +// process.env.DOCKER_HOST = 'http://172.16.200.12:8376' let CC_MONGO_URL = process.env.CC_MONGO_URL let CC_MONGO_DB = process.env.CC_MONGO_DB // if (process.env.local) { diff --git a/packages/vscode/package.json b/packages/vscode/package.json index bfdf15c..3c8264a 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -33,6 +33,13 @@ ] }, "commands": [ + { + "command": "dayu.task.logs", + "title": "View Task Logs", + "icon": { + "dark": "src/images/browser.svg" + } + }, { "command": "dayu.container.logs", "title": "View Container Logs", @@ -55,6 +62,11 @@ "when": "view == docker-explorer && viewItem =~ /.*\"type\":\"CONTAINER\".*/", "group": "inline" }, + { + "command": "dayu.task.logs", + "when": "view == docker-explorer && viewItem =~ /.*\"type\":\"TASK\".*/", + "group": "inline" + }, { "command": "dayu.service.logs", "when": "view == docker-explorer && viewItem =~ /.*\"type\":\"SERVICE\".*/", diff --git a/packages/vscode/src/provider/docker.ts b/packages/vscode/src/provider/docker.ts index 47cf60d..a388e62 100644 --- a/packages/vscode/src/provider/docker.ts +++ b/packages/vscode/src/provider/docker.ts @@ -13,6 +13,8 @@ enum Type { NETWORK = "NETWORK", STACKS = "STACKS", STACK = "STACK", + TASKS = "TASKS", + TASK = "TASK", NODES = "NODES", NODE = "NODE" } @@ -26,6 +28,11 @@ export class DockerProvider extends BaseProvider { constructor(context: vscode.ExtensionContext) { super(); context.subscriptions.push( + vscode.commands.registerCommand('dayu.task.logs', (item: vscode.TreeItem) => { + let value: ItemContextValue = JSON.parse(item.contextValue); + let url = `https://dayu-api.miaowoo.cc/logs/?action=task&data=${value.data.id}`; + return vscode.commands.executeCommand("mini-browser.openUrl", url); + }), vscode.commands.registerCommand('dayu.container.logs', (item: vscode.TreeItem) => { let value: ItemContextValue = JSON.parse(item.contextValue); let url = `https://dayu-api.miaowoo.cc/logs/?action=container&data=${value.data.id}`; @@ -81,6 +88,20 @@ export class DockerProvider extends BaseProvider { tooltip: JSON.stringify(n, undefined, 2) }) }) + case Type.TASKS: + let tasks = await docker.task.list(); + return tasks.map(n => { + return this.createTreeItem({ + label: n.ID, + context: { + type: Type.NODE, + data: { + id: n.ID + } + }, + tooltip: JSON.stringify(n, undefined, 2) + }) + }) case Type.CONTAINERS: let containers = await docker.container.list(); return containers.map(c => {