From 3e480da53933c945092cab3424cd383012bae6b9 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 27 Aug 2019 15:03:30 +0800 Subject: [PATCH] feat: add more docker api --- http/dayu.http | 15 +++++++++++++-- package.json | 2 +- packages/core/package.json | 3 ++- packages/core/src/controller/service.ts | 19 +++++++++++++++++++ packages/core/src/controller/stack.ts | 17 +++++++++++------ packages/core/src/index.ts | 2 +- packages/core/tsconfig.json | 11 +++++++++-- packages/docker-api/package.json | 2 +- packages/docker-api/src/api/opts/node.ts | 6 ++++++ packages/docker-api/src/api/opts/swarm.ts | 12 ++++++++++++ .../docker-api/src/api/types/container.ts | 5 +++++ packages/docker-api/src/client/container.ts | 14 ++++++++++---- packages/docker-api/src/client/service.ts | 7 +++++-- packages/docker-api/src/client/stack.ts | 0 packages/docker-api/src/client/swarm.ts | 16 ++++++++++++++++ packages/docker-api/src/utils/api.ts | 6 +----- packages/vscode/package.json | 3 ++- packages/vscode/src/extension.ts | 1 + packages/vscode/src/provider/docker.ts | 2 +- packages/vscode/src/provider/faas.ts | 2 +- 20 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 packages/core/src/controller/service.ts create mode 100644 packages/docker-api/src/api/opts/node.ts create mode 100644 packages/docker-api/src/client/stack.ts diff --git a/http/dayu.http b/http/dayu.http index 67e34b3..88a0961 100644 --- a/http/dayu.http +++ b/http/dayu.http @@ -1,4 +1,4 @@ -@url=https://dayu.n.yumc.pw +@url=https://dayu-api.miaowoo.cc ### System ##### Info @@ -25,11 +25,22 @@ POST {{swarm}}/init @stack={{url}}/stack ##### Stack List GET {{stack}}/list - +##### Stack Info +GET {{stack}}/faas ##### Stack Remove POST {{stack}}/develop/remove +### Service +@service={{url}}/service +##### Service List +GET {{service}}/list +##### Service Info +GET {{service}}/3gchxsmjld0fuex216w56jl5g +##### Service Remove +POST {{service}}/develop/remove + + ### Container @container={{url}}/container diff --git a/package.json b/package.json index a7f565f..a3761ab 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "license": "MIT", "scripts": { "clean": "npx lerna run clean", - "watch": "npx lerna run watch --concurrency 10", + "watch": "npx lerna run watch --parallel", "build": "npx lerna run build", "lp": "npx lerna publish" }, diff --git a/packages/core/package.json b/packages/core/package.json index 111667a..14e5b4f 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -26,6 +26,7 @@ "devDependencies": { "@types/express": "^4.17.0", "@types/socket.io": "^2.1.2", + "nodemon": "^1.19.1", "rimraf": "^2.6.3", "ts-node-dev": "^1.0.0-pre.40", "typescript": "^3.5.2" @@ -43,4 +44,4 @@ "delay": "1500", "ext": "js,json" } -} \ No newline at end of file +} diff --git a/packages/core/src/controller/service.ts b/packages/core/src/controller/service.ts new file mode 100644 index 0000000..5d902bc --- /dev/null +++ b/packages/core/src/controller/service.ts @@ -0,0 +1,19 @@ +import { controller, httpGet, requestParam } from 'inversify-express-utils'; +import * as docker from '@dayu/docker-api' + +@controller('/service') +class ServiceController { + @httpGet('/list') + public async list() { + let services = await docker.service.list(); + return services.map(s => ({ + id: s.ID, + name: s.Spec.Name + })); + } + + @httpGet('/:id') + public async details(@requestParam('id') id: string) { + return await docker.service.inspect(id) + } +} diff --git a/packages/core/src/controller/stack.ts b/packages/core/src/controller/stack.ts index ab8aa38..a57f50a 100644 --- a/packages/core/src/controller/stack.ts +++ b/packages/core/src/controller/stack.ts @@ -6,18 +6,25 @@ const STACK_LABEL = 'com.docker.stack.namespace'; @controller('/stack') class StackController { @httpGet('/list') - public async list() { + public async list(): Promise { let stacks: { [key: string]: string[] } = {}; + let result = []; let services = await docker.service.list(); for (const service of services) { let stackName = service.Spec.Labels[STACK_LABEL] if (stackName) { - let stack = stacks[stackName] || []; + let stack = stacks[stackName]; + if (!stack) { + result.push({ + name: stackName, + services: stack = [] + }) + stacks[stackName] = stack; + } stack.push(service.Spec.Name); - stacks[stackName] = stack; } } - return stacks; + return result; } @httpGet('/:stack') @@ -31,11 +38,9 @@ class StackController { } let services = await docker.service.list(filterOpt) let networks = await docker.network.list(filterOpt) - let containers = await docker.container.list(filterOpt); return { services: services.map(service => service.Spec.Name), networks: networks.map(network => network.Name), - containers: containers.map(container => container.Names[0].substring(1)) } } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 4806f58..fcc3538 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -19,4 +19,4 @@ for (let file of list) { } } -server.static().build().start(); +server.static().build().start(81); diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 7aae5d2..f754065 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -1,7 +1,14 @@ { - "extends": "../../tsconfig.json", "compilerOptions": { "baseUrl": "src", - "outDir": "dist" + "outDir": "dist", + "target": "es6", + "module": "commonjs", + "sourceMap": true, + "declaration": true, + "noImplicitAny": true, + "allowUnreachableCode": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true } } \ No newline at end of file diff --git a/packages/docker-api/package.json b/packages/docker-api/package.json index 30c0946..76152b2 100644 --- a/packages/docker-api/package.json +++ b/packages/docker-api/package.json @@ -17,7 +17,7 @@ "test": "echo \"Error: run tests from root\" && exit 1" }, "dependencies": { - "@cc-server/core": "^0.3.3", + "@cc-server/core": "^0.6.1", "axios": "^0.19.0" }, "devDependencies": { diff --git a/packages/docker-api/src/api/opts/node.ts b/packages/docker-api/src/api/opts/node.ts new file mode 100644 index 0000000..4074b2a --- /dev/null +++ b/packages/docker-api/src/api/opts/node.ts @@ -0,0 +1,6 @@ +import * as common from './common' + +export declare namespace node { + export interface ListOpts extends common.query.FilterOpt { + } +} \ No newline at end of file diff --git a/packages/docker-api/src/api/opts/swarm.ts b/packages/docker-api/src/api/opts/swarm.ts index e1f0250..55ab10e 100644 --- a/packages/docker-api/src/api/opts/swarm.ts +++ b/packages/docker-api/src/api/opts/swarm.ts @@ -3,6 +3,18 @@ import { Labels } from '../common' export declare namespace swarm { type NodeAvailability = string; + export interface UnlockOpts { + UnlockKey: string; + } + + export interface JoinOpts { + ListenAddr?: string; + AdvertiseAddr?: string; + DataPathAddr?: string; + RemoteAddrs?: string[]; + JoinToken?: string; + } + export interface InitOpts { ListenAddr?: string; AdvertiseAddr?: string; diff --git a/packages/docker-api/src/api/types/container.ts b/packages/docker-api/src/api/types/container.ts index eea46dd..7b57523 100644 --- a/packages/docker-api/src/api/types/container.ts +++ b/packages/docker-api/src/api/types/container.ts @@ -297,4 +297,9 @@ export declare namespace container { NetworkSettings: SummaryNetworkSettings; Mounts: Mount[]; } + + export interface ContainerPrune { + ContainersDeleted: string[]; + SpaceReclaimed: number; + } } diff --git a/packages/docker-api/src/client/container.ts b/packages/docker-api/src/client/container.ts index 94beb7d..1fb5116 100644 --- a/packages/docker-api/src/client/container.ts +++ b/packages/docker-api/src/client/container.ts @@ -8,16 +8,22 @@ export namespace container { return await api.get('/containers/json', filters) } - export async function info(id: string, query: { size: boolean } = { size: false }) { + export async function inspect(id: string, query: { size: boolean } = { size: false }) { return await api.get(`/containers/${id}/json`, query); } + export function prune() { + return api.post('/containers/prune'); + } + export async function logs(id: string, opts: opts.container.LogsOpts = {}): Promise { - return await api.stream(`/containers/${id}/logs`, Object.assign({ + let data = { follow: true, stdout: true, stderr: true, - tail: 10 - }, opts)); + tail: 10, + ...opts + } + return await api.stream(`/containers/${id}/logs`, data); } } diff --git a/packages/docker-api/src/client/service.ts b/packages/docker-api/src/client/service.ts index 0e12f11..fb728ff 100644 --- a/packages/docker-api/src/client/service.ts +++ b/packages/docker-api/src/client/service.ts @@ -7,6 +7,9 @@ export namespace service { return await api.get('/services', filters); } export async function create() { - + } -} \ No newline at end of file + export async function inspect(id: string, query: { insertDefaults: boolean } = { insertDefaults: false }) { + return await api.get(`/services/${id}`, query); + } +} diff --git a/packages/docker-api/src/client/stack.ts b/packages/docker-api/src/client/stack.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/docker-api/src/client/swarm.ts b/packages/docker-api/src/client/swarm.ts index 2684297..6ad3d8a 100644 --- a/packages/docker-api/src/client/swarm.ts +++ b/packages/docker-api/src/client/swarm.ts @@ -10,4 +10,20 @@ export namespace swarm { export async function init(opts: opts.swarm.InitOpts) { return await api.post('/swarm/init', opts); } + + export async function join(opts: opts.swarm.JoinOpts) { + return await api.post('/swarm/join', opts); + } + + export async function leave(force: boolean = false) { + return await api.post(`/swarm/leave?force=${force}`); + } + + export async function unlockkey() { + return await api.get(`/swarm/unlockkey`); + } + + export async function unlock(opts: opts.swarm.UnlockOpts) { + return await api.post(`/swarm/unlockkey`, opts); + } } \ No newline at end of file diff --git a/packages/docker-api/src/utils/api.ts b/packages/docker-api/src/utils/api.ts index 93b5c62..700ae4b 100644 --- a/packages/docker-api/src/utils/api.ts +++ b/packages/docker-api/src/utils/api.ts @@ -23,9 +23,9 @@ async function handle(method: Method, path: string, reqConfig?: AxiosRequestC let config: AxiosRequestConfig = { method, url: path, + ...reqConfig }; let startTime = Date.now(); - Object.assign(config, reqConfig) let response: AxiosResponse; try { response = await api.request(config); @@ -57,10 +57,6 @@ RESPONSE BODY : ${toString.call(response.data.pipe) === "[object Function]" ? HANDLE TIME : ${Date.now() - startTime}ms =======================================`); } - // console.log(`${method} ${path} HTTP/1.1 - // ${config.data ? JSON.stringify(config.data, null, 2) + '\n' : ''} - // HTTP/1.1 ${response.status} ${response.statusText} - // ${config.responseType != 'stream' ? JSON.stringify(response.data, null, 2) : config.responseType}`); } } diff --git a/packages/vscode/package.json b/packages/vscode/package.json index db57993..411af23 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -1,7 +1,7 @@ { "name": "@dayu/vscode", "displayName": "vscode", - "description": "", + "description": "Dayu VsCode Plugin", "publisher": "MiaoWoo", "version": "0.0.1", "engines": { @@ -56,6 +56,7 @@ } }, "scripts": { + "package": "vsce package", "vscode:prepublish": "yarn run compile", "compile": "tsc -p ./", "watch": "tsc -watch -p ./", diff --git a/packages/vscode/src/extension.ts b/packages/vscode/src/extension.ts index 60b3c3e..6d73a13 100644 --- a/packages/vscode/src/extension.ts +++ b/packages/vscode/src/extension.ts @@ -7,6 +7,7 @@ import { OpenFaasProvider, DockerProvider } from './provider' // this method is called when your extension is activated // your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { + console.log('init...') new DockerProvider(context); new OpenFaasProvider(context); } diff --git a/packages/vscode/src/provider/docker.ts b/packages/vscode/src/provider/docker.ts index 8dac3e7..6a3e8c3 100644 --- a/packages/vscode/src/provider/docker.ts +++ b/packages/vscode/src/provider/docker.ts @@ -28,7 +28,7 @@ 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://faas.n.yumc.pw?action=container&data=${value.data.id}`; + let url = `https://dayu-api.miaowoo.cc?action=container&data=${value.data.id}`; return vscode.commands.executeCommand("mini-browser.openUrl", url); }), vscode.window.registerTreeDataProvider('docker-explorer', this) diff --git a/packages/vscode/src/provider/faas.ts b/packages/vscode/src/provider/faas.ts index e602adf..784a24e 100644 --- a/packages/vscode/src/provider/faas.ts +++ b/packages/vscode/src/provider/faas.ts @@ -35,7 +35,7 @@ export class OpenFaasProvider extends BaseProvider { switch (value.type) { case Type.ROOT: let funcs = await faas.getFunctions(); - return funcs.map(f => { + return funcs.map((f: any) => { return this.createTreeItem({ label: f.name, tooltip: JSON.stringify(f, undefined, 2)