From 752e7712859c7a3892db85f2f3b4f6a2da78b1e8 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 28 Dec 2019 15:06:07 +0800 Subject: [PATCH] feat: add service log Signed-off-by: MiaoWoo --- http/dayu.http | 2 + packages/core/public/event/js/index.js | 81 ------------------ .../core/public/{event => logs}/index.html | 0 packages/core/public/logs/js/index.js | 82 +++++++++++++++++++ packages/core/resources/docker.http | 1 - .../core/src/controller/docker/service.ts | 36 ++++++-- packages/docker-api/src/api/opts/service.ts | 10 +++ packages/docker-api/src/client/service.ts | 11 +++ packages/vscode/package.json | 24 ++++-- packages/vscode/src/extension.ts | 7 +- packages/vscode/src/provider/base.ts | 4 +- packages/vscode/src/provider/docker.ts | 9 +- 12 files changed, 161 insertions(+), 106 deletions(-) delete mode 100644 packages/core/public/event/js/index.js rename packages/core/public/{event => logs}/index.html (100%) create mode 100644 packages/core/public/logs/js/index.js delete mode 100644 packages/core/resources/docker.http diff --git a/http/dayu.http b/http/dayu.http index 88a0961..1f5ff16 100644 --- a/http/dayu.http +++ b/http/dayu.http @@ -39,6 +39,8 @@ GET {{service}}/list GET {{service}}/3gchxsmjld0fuex216w56jl5g ##### Service Remove POST {{service}}/develop/remove +##### +POST {{service}}/swirl_swirl/restart ### Container diff --git a/packages/core/public/event/js/index.js b/packages/core/public/event/js/index.js deleted file mode 100644 index a757b7e..0000000 --- a/packages/core/public/event/js/index.js +++ /dev/null @@ -1,81 +0,0 @@ -let command = ''; -Terminal.applyAddon(fit); -Terminal.applyAddon(attach); -Terminal.applyAddon(fullscreen); -var term = new Terminal({ - experimentalCharAtlas: 'dynamic', - cursorBlink: false, -}); -term.open(document.getElementById('terminal')); -term.toggleFullScreen(); -term.fit(); - -window.onresize = function() { - term.fit(); -} - -let query = {} -location.search.substring(1).split("&").forEach(q => { - let qy = q.split("=", 2); - query[qy[0]] = qy[1] -}) - -console.log(query); - -var system = io('/', { - path: '/ws', - transports: ['websocket'] -}); - -system.on('connect', () => { - system.emit('events', {}) -}) - -system.on('message', data => { - term.write(data.toString() + '\r\n'); -}); - -var container = io('/container', { - path: '/ws', - transports: ['websocket'] -}); -container.on('connect', () => { - term.writeln('connect') - if (query.action) { - term.writeln(`Recover Action: ${query.action} Data: ${query.data}`) - switch (query.action) { - case "container": - container.emit('logs', { - id: query.data, - // since: Date.now() / 1000 - 60 * 15, - // until: Date.now() / 1000, - // stderr: false, - tail: "200" - }) - break; - default: - } - } -}); -term.on('data', async data => { - if (data == '\t') { - return; - } - term.write(data); - if (data == '\r') { - term.write('\n'); - container.emit('logs', { - id: command - }) - command = ''; - } else { - command += data; - } -}); -container.on('message', data => { - term.write(data.toString() + '\r\n'); -}); -container.on('disconnect', () => { - term.reset(); - term.writeln('disconnect'); -}); diff --git a/packages/core/public/event/index.html b/packages/core/public/logs/index.html similarity index 100% rename from packages/core/public/event/index.html rename to packages/core/public/logs/index.html diff --git a/packages/core/public/logs/js/index.js b/packages/core/public/logs/js/index.js new file mode 100644 index 0000000..97e511d --- /dev/null +++ b/packages/core/public/logs/js/index.js @@ -0,0 +1,82 @@ +let command = ''; +Terminal.applyAddon(fit); +Terminal.applyAddon(attach); +Terminal.applyAddon(fullscreen); +var term = new Terminal({ + experimentalCharAtlas: 'dynamic', + cursorBlink: false, +}); +term.open(document.getElementById('terminal')); +term.toggleFullScreen(); +term.fit(); + +window.onresize = function() { + term.fit(); +} + +let query = {} +location.search.substring(1).split("&").forEach(q => { + let qy = q.split("=", 2); + query[qy[0]] = qy[1] +}) + +function connectServer(namespace, id) { + var server = io(namespace, { + path: '/ws', + transports: ['websocket'] + }); + server.on('connect', () => { + term.writeln('connect') + server.emit('logs', { + id: id, + tail: "200" + }) + }); + server.on('message', data => { + term.write(data.toString() + '\r\n'); + }); + server.on('disconnect', () => { + term.reset(); + term.writeln('disconnect'); + }); +} + +console.log(query); + +term.writeln(`Recover Action: ${query.action} Data: ${query.data}`) + +switch (query.action) { + case "container": + case "service": + connectServer(`/${query.action}`, query.data) + break; + default: + var system = io('/', { + path: '/ws', + transports: ['websocket'] + }); + + system.on('connect', () => { + system.emit('events', {}) + }) + + system.on('message', data => { + term.write(data.toString() + '\r\n'); + }); +} + +term.on('data', async data => { + if (data == '\t') { + return; + } + term.write(data); + if (data == '\r') { + term.write('\n'); + container.emit('logs', { + id: command + }) + command = ''; + } else { + command += data; + } +}); diff --git a/packages/core/resources/docker.http b/packages/core/resources/docker.http deleted file mode 100644 index 4730790..0000000 --- a/packages/core/resources/docker.http +++ /dev/null @@ -1 +0,0 @@ -POST https://dayu-api.miaowoo.cc/service/swirl_swirl/restart diff --git a/packages/core/src/controller/docker/service.ts b/packages/core/src/controller/docker/service.ts index 764e3da..6110779 100644 --- a/packages/core/src/controller/docker/service.ts +++ b/packages/core/src/controller/docker/service.ts @@ -1,21 +1,22 @@ -import { controller, post, get, requestParam, queryParam } from '@cc-server/binding'; import * as 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'; @controller('/service') class ServiceController { @get('/list') public async list(@queryParam('page') page: number, @queryParam('perPage') perPage: number, ) { let services = await docker.service.list(); + let rows = services.map(s => ({ + "service-name": s.Spec.Name, + image: s.Spec.TaskTemplate.ContainerSpec.Image.split('@')[0], + updated_at: s.UpdatedAt, + mode: s.Spec.Mode, + update_status: s.UpdateStatus + })) return { status: 0, - msg: '', - data: services.map(s => ({ - "service-name": s.Spec.Name, - image: s.Spec.TaskTemplate.ContainerSpec.Image.split('@')[0], - updated_at: s.UpdatedAt, - mode: s.Spec.Mode, - update_status: s.UpdateStatus - })) + data: { rows } }; } @@ -73,3 +74,20 @@ class ServiceController { } } } + +@namespace("/service") +class ServiceNamespace extends interfaces.Namespace { + @listener() + async logs(socket: io.Socket, data: any) { + try { + let stream = await docker.service.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); + } + } +} diff --git a/packages/docker-api/src/api/opts/service.ts b/packages/docker-api/src/api/opts/service.ts index ca19846..f35161b 100644 --- a/packages/docker-api/src/api/opts/service.ts +++ b/packages/docker-api/src/api/opts/service.ts @@ -3,4 +3,14 @@ import * as common from './common' export declare namespace service { export interface ListOpts extends common.query.FilterOpt { } + export interface LogsOpts { + details?: boolean; + follow?: boolean; + stdout?: boolean; + stderr?: boolean; + since?: number; + until?: number; + timestamps?: boolean; + tail?: number | "all"; + } } \ No newline at end of file diff --git a/packages/docker-api/src/client/service.ts b/packages/docker-api/src/client/service.ts index a4d4d9d..293a3fb 100644 --- a/packages/docker-api/src/client/service.ts +++ b/packages/docker-api/src/client/service.ts @@ -1,6 +1,7 @@ import * as api from '../utils/api'; import * as opts from '../api/opts'; import * as types from '../api/types'; +import * as http from 'http' export namespace service { export async function list(filters?: opts.service.ListOpts) { @@ -15,4 +16,14 @@ export namespace service { export async function update(id: string, query: { version: number, registryAuthFrom?: string, rollback?: string }, data: any) { return await api.post(api.getUri(`/services/${id}/update`, query), data) } + export async function logs(id: string, opts: opts.service.LogsOpts = {}): Promise { + let data = { + follow: true, + stdout: true, + stderr: true, + tail: 10, + ...opts + } + return await api.stream(`/services/${id}/logs`, data); + } } diff --git a/packages/vscode/package.json b/packages/vscode/package.json index 68ebcac..bfdf15c 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -11,8 +11,7 @@ "Other" ], "activationEvents": [ - "onView:docker-explorer", - "onView:openfaas-explorer" + "onView:docker-explorer" ], "main": "./dist/extension.js", "contributes": { @@ -30,10 +29,6 @@ { "id": "docker-explorer", "name": "Docker" - }, - { - "id": "openfaas-explorer", - "name": "OpenFaaS" } ] }, @@ -44,6 +39,13 @@ "icon": { "dark": "src/images/browser.svg" } + }, + { + "command": "dayu.service.logs", + "title": "View Service Logs", + "icon": { + "dark": "src/images/browser.svg" + } } ], "menus": { @@ -52,6 +54,11 @@ "command": "dayu.container.logs", "when": "view == docker-explorer && viewItem =~ /.*\"type\":\"CONTAINER\".*/", "group": "inline" + }, + { + "command": "dayu.service.logs", + "when": "view == docker-explorer && viewItem =~ /.*\"type\":\"SERVICE\".*/", + "group": "inline" } ] } @@ -67,13 +74,14 @@ "dependencies": { "@dayu/docker-api": "^0.0.1", "@dayu/faas": "^0.0.1", - "axios": "^0.19.0" + "axios": "^0.19.0", + "lerna": "^3.14.1" }, "devDependencies": { "@types/mocha": "^2.2.42", "@types/node": "^10.12.21", "tslint": "^5.12.1", "typescript": "^3.3.1", - "vscode": "^1.1.28" + "vscode": "^1.1.36" } } \ No newline at end of file diff --git a/packages/vscode/src/extension.ts b/packages/vscode/src/extension.ts index bc9c0e6..266f913 100644 --- a/packages/vscode/src/extension.ts +++ b/packages/vscode/src/extension.ts @@ -1,8 +1,9 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -process.env.DOCKER_HOST = 'https://dscli.miaowoo.cc'; -import { OpenFaasProvider, DockerProvider } from './provider'; +// process.env.DOCKER_HOST = 'https://dscli.miaowoo.cc'; +process.env.DOCKER_HOST = 'http://172.16.200.12:8376'; +import { DockerProvider } from './provider'; // this method is called when your extension is activated // your extension is activated the very first time the command is executed @@ -11,7 +12,7 @@ export function activate(context: vscode.ExtensionContext) { // tslint:disable-next-line: no-unused-expression new DockerProvider(context); // tslint:disable-next-line: no-unused-expression - new OpenFaasProvider(context); + // new OpenFaasProvider(context); } // this method is called when your extension is deactivated diff --git a/packages/vscode/src/provider/base.ts b/packages/vscode/src/provider/base.ts index 33abc15..44c809c 100644 --- a/packages/vscode/src/provider/base.ts +++ b/packages/vscode/src/provider/base.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode' -export class CteateItemOpt { +export class CreateItemOpt { label: string; context?: ItemContextValue; state?: vscode.TreeItemCollapsibleState = vscode.TreeItemCollapsibleState.None; @@ -18,7 +18,7 @@ export abstract class BaseProvider implements vscode.TreeDataProvider { abstract getTreeItem(element: T): vscode.TreeItem | Thenable; abstract getChildren(element?: T | undefined): vscode.ProviderResult; - createTreeItem(opts: CteateItemOpt) { + createTreeItem(opts: CreateItemOpt) { let item = new vscode.TreeItem(opts.label, opts.state); item.contextValue = JSON.stringify(opts.context); if (opts.icon) { item.iconPath = `src/images/${opts.icon}.svg` } diff --git a/packages/vscode/src/provider/docker.ts b/packages/vscode/src/provider/docker.ts index 6a3e8c3..47cf60d 100644 --- a/packages/vscode/src/provider/docker.ts +++ b/packages/vscode/src/provider/docker.ts @@ -28,7 +28,12 @@ export class DockerProvider extends BaseProvider { context.subscriptions.push( vscode.commands.registerCommand('dayu.container.logs', (item: vscode.TreeItem) => { let value: ItemContextValue = JSON.parse(item.contextValue); - let url = `https://dayu-api.miaowoo.cc?action=container&data=${value.data.id}`; + let url = `https://dayu-api.miaowoo.cc/logs/?action=container&data=${value.data.id}`; + return vscode.commands.executeCommand("mini-browser.openUrl", url); + }), + vscode.commands.registerCommand('dayu.service.logs', (item: vscode.TreeItem) => { + let value: ItemContextValue = JSON.parse(item.contextValue); + let url = `https://dayu-api.miaowoo.cc/logs/?action=service&data=${value.data.id}`; return vscode.commands.executeCommand("mini-browser.openUrl", url); }), vscode.window.registerTreeDataProvider('docker-explorer', this) @@ -146,7 +151,7 @@ export class DockerProvider extends BaseProvider { let list: string[] = value.data.list; return list.map(s => { return this.createTreeItem({ - label: s + label: s.split('_')[1] }) }) default: