feat: add service log

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-12-28 15:06:07 +08:00
parent aed21b2ff6
commit 752e771285
12 changed files with 161 additions and 106 deletions

View File

@ -39,6 +39,8 @@ GET {{service}}/list
GET {{service}}/3gchxsmjld0fuex216w56jl5g
##### Service Remove
POST {{service}}/develop/remove
#####
POST {{service}}/swirl_swirl/restart
### Container

View File

@ -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');
});

View File

@ -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;
}
});

View File

@ -1 +0,0 @@
POST https://dayu-api.miaowoo.cc/service/swirl_swirl/restart

View File

@ -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);
}
}
}

View File

@ -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";
}
}

View File

@ -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<any>(api.getUri(`/services/${id}/update`, query), data)
}
export async function logs(id: string, opts: opts.service.LogsOpts = {}): Promise<http.ServerResponse> {
let data = {
follow: true,
stdout: true,
stderr: true,
tail: 10,
...opts
}
return await api.stream(`/services/${id}/logs`, data);
}
}

View File

@ -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"
}
}

View File

@ -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

View File

@ -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<T> implements vscode.TreeDataProvider<T> {
abstract getTreeItem(element: T): vscode.TreeItem | Thenable<vscode.TreeItem>;
abstract getChildren(element?: T | undefined): vscode.ProviderResult<T[]>;
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` }

View File

@ -28,7 +28,12 @@ export class DockerProvider extends BaseProvider<vscode.TreeItem> {
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<vscode.TreeItem> {
let list: string[] = value.data.list;
return list.map(s => {
return this.createTreeItem({
label: s
label: s.split('_')[1]
})
})
default: