diff --git a/http/docker.http b/http/docker.http index f2a4805..5ce9639 100644 --- a/http/docker.http +++ b/http/docker.http @@ -1,4 +1,4 @@ -@url=https://dcli.yumc.pw/v1.39 +@url=https://dscli.miaowoo.cc/v1.39 ### Info GET {{url}}/info @@ -25,12 +25,79 @@ GET {{container}}/json?filters={"label":{"com.docker.stack.namespace%3Dmonitor": ##### In GET {{container}}/{{cid}}/json -### Services -GET {{url}}/services?filters={"mode":["global"]} +### Services ?filters={"mode":["global"]} +GET {{url}}/services -### Create Services +##### Create Services POST {{url}}/services/create { - -} \ No newline at end of file + "Name": "analysis_tera-200706", + "Labels": { + "com.docker.stack.image": "miaowoo/tera:frp", + "com.docker.stack.namespace": "analysis" + }, + "TaskTemplate": { + "ContainerSpec": { + "Image": "miaowoo/tera:frp", + "Labels": { + "com.docker.stack.namespace": "analysis" + }, + "Env": [ + "FRP_NAME=tera-200706", + "FRP_TOKEN=Jtb2hwwfor", + "FRP_USER=MiaoWoo", + "PASSWD=Jtb2hwwfor", + "TERA_COUNT_MINING_CPU=8", + "TERA_NET_WORK_MODE_IP=60.12.241.181", + "TERA_NET_WORK_MODE_PORT=30019", + "TERA_REST_START_COUNT=100000", + "TERA_WALLET_MINING_ACCOUNT=200706", + "TERA_WATCHDOG_BADACCOUNT=2" + ], + "Privileges": { + "CredentialSpec": null, + "SELinuxContext": null + }, + "Init": true, + "Mounts": [ + { + "Type": "bind", + "Source": "/home/app/tera/DATA", + "Target": "/app/DATA" + } + ], + "Isolation": "default" + }, + "Resources": { + "Limits": { + "NanoCPUs": 9000000000, + "MemoryBytes": 38654705664 + }, + "Reservations": { + "NanoCPUs": 8000000000, + "MemoryBytes": 34359738368 + } + }, + "Placement": { + "Constraints": [ + "node.hostname == manager1" + ] + }, + "ForceUpdate": 0, + "Runtime": "container" + }, + "Mode": { + "Replicated": { + "Replicas": 1 + } + }, + "EndpointSpec": { + "Mode": "vip" + } +} + +### Delete Services +@id=t6yn64q53q1dmgzdr8td8crbl +DELETE {{url}}/services/{{id}} + diff --git a/packages/core/public/js/index.js b/packages/core/public/js/index.js index d21f903..a757b7e 100644 --- a/packages/core/public/js/index.js +++ b/packages/core/public/js/index.js @@ -22,17 +22,30 @@ location.search.substring(1).split("&").forEach(q => { console.log(query); -var socket = io('/container', { +var system = io('/', { path: '/ws', transports: ['websocket'] }); -socket.on('connect', () => { + +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": - socket.emit('logs', { + container.emit('logs', { id: query.data, // since: Date.now() / 1000 - 60 * 15, // until: Date.now() / 1000, @@ -51,7 +64,7 @@ term.on('data', async data => { term.write(data); if (data == '\r') { term.write('\n'); - socket.emit('logs', { + container.emit('logs', { id: command }) command = ''; @@ -59,10 +72,10 @@ term.on('data', async data => { command += data; } }); -socket.on('message', data => { +container.on('message', data => { term.write(data.toString() + '\r\n'); }); -socket.on('disconnect', () => { +container.on('disconnect', () => { term.reset(); term.writeln('disconnect'); }); diff --git a/packages/core/src/controller/dashboard.ts b/packages/core/src/controller/dashboard.ts index 5eade13..819f8fe 100644 --- a/packages/core/src/controller/dashboard.ts +++ b/packages/core/src/controller/dashboard.ts @@ -19,12 +19,18 @@ class DashboardController { let networks = await docker.network.list(); let nodes = await docker.node.list(); let tasks = await docker.task.list(); + let containers = await docker.container.list(); + let images = await docker.image.list(); + let volumes = await docker.volume.list(); return { nodes: nodes.map(n => n.Description.Hostname), tasks: tasks.map(t => t.ID), stacks: Array.from(new Set(stacks)), services, - networks: networks.map(n => n.Name) + networks: networks.map(n => n.Name), + containers: containers.map(c => c.Names), + images: images.map(i => i.Id), + volumes: volumes.Volumes.map(v => v.Name) } } } diff --git a/packages/core/src/controller/system.ts b/packages/core/src/controller/system.ts index bcb22ec..8a82b93 100644 --- a/packages/core/src/controller/system.ts +++ b/packages/core/src/controller/system.ts @@ -1,5 +1,6 @@ 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('') class SystemController { @@ -14,9 +15,32 @@ class SystemController { } @httpGet('/events') public async event() { - await docker.system.events((event) => { - if (!event) { return } - console.log(event) + let stream = await docker.system.events(); + stream.on('data', (chunk: ArrayBuffer) => { + let log = Buffer.from(chunk).toString(); + console.log(log); }) + return {} + } +} + +@namespace() +class SystemNamespace extends interfaces.Namespace { + @listener() + async events(socket: io.Socket) { + try { + socket.send('Starting listening docker event...'); + let stream = await docker.system.events(); + socket.send('Connect to docker deamon...'); + this.defer(socket, () => stream.connection.destroy()); + stream.on('data', (chunk: ArrayBuffer) => { + let log = Buffer.from(chunk).toString(); + console.log(log); + socket.send(log); + }) + } catch (ex) { + //console.error(ex); + return new Message(ex.message); + } } } \ No newline at end of file diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 0288df4..4806f58 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -3,7 +3,8 @@ import * as fs from 'fs' import * as path from 'path' //process.env.DOCKER_HOST = 'https://ndcli.yumc.pw' -process.env.DOCKER_HOST = '/var/run/docker.sock' +// process.env.DOCKER_HOST = '/var/run/docker.sock' +process.env.DOCKER_HOST = 'https://dscli.miaowoo.cc' let server = new CcServerBoot(); diff --git a/packages/docker-api/src/api/types/image.ts b/packages/docker-api/src/api/types/image.ts new file mode 100644 index 0000000..b45767b --- /dev/null +++ b/packages/docker-api/src/api/types/image.ts @@ -0,0 +1,17 @@ +import { Labels } from '../common' + +export declare namespace image { + export interface Image { + Containers: number; + Created: number; + Id: string; + Labels: Labels; + ParentId: string; + RepoDigests: string[]; + RepoTags: string[]; + SharedSize: number; + Size: number; + VirtualSize: number; + } +} + diff --git a/packages/docker-api/src/api/types/index.ts b/packages/docker-api/src/api/types/index.ts index e65346d..5de851a 100644 --- a/packages/docker-api/src/api/types/index.ts +++ b/packages/docker-api/src/api/types/index.ts @@ -1,6 +1,8 @@ export * from './node' export * from './task' +export * from './image' export * from './swarm' +export * from './volume' export * from './system' export * from './network' export * from './service' diff --git a/packages/docker-api/src/api/types/volume.ts b/packages/docker-api/src/api/types/volume.ts new file mode 100644 index 0000000..eaa0436 --- /dev/null +++ b/packages/docker-api/src/api/types/volume.ts @@ -0,0 +1,19 @@ +import { Labels, Options } from '../common' + +export declare namespace volume { + export interface Volume { + CreatedAt: Date; + Driver: string; + Labels: Labels; + Mountpoint: string; + Name: string; + Options: Options; + Scope: string; + } + + export interface VolumeJSON { + Volumes: Volume[]; + Warnings?: any; + } +} + diff --git a/packages/docker-api/src/client/image.ts b/packages/docker-api/src/client/image.ts new file mode 100644 index 0000000..4489527 --- /dev/null +++ b/packages/docker-api/src/client/image.ts @@ -0,0 +1,8 @@ +import * as api from '../utils/api' +import * as types from '../api/types' + +export namespace image { + export async function list() { + return await api.get('/images/json'); + } +} diff --git a/packages/docker-api/src/client/index.ts b/packages/docker-api/src/client/index.ts index e65346d..5de851a 100644 --- a/packages/docker-api/src/client/index.ts +++ b/packages/docker-api/src/client/index.ts @@ -1,6 +1,8 @@ export * from './node' export * from './task' +export * from './image' export * from './swarm' +export * from './volume' export * from './system' export * from './network' export * from './service' diff --git a/packages/docker-api/src/client/service.ts b/packages/docker-api/src/client/service.ts index b9a74a4..0e12f11 100644 --- a/packages/docker-api/src/client/service.ts +++ b/packages/docker-api/src/client/service.ts @@ -6,4 +6,7 @@ export namespace service { export async function list(filters?: opts.service.ListOpts) { return await api.get('/services', filters); } + export async function create() { + + } } \ No newline at end of file diff --git a/packages/docker-api/src/client/system.ts b/packages/docker-api/src/client/system.ts index 27b6151..4eae7a6 100644 --- a/packages/docker-api/src/client/system.ts +++ b/packages/docker-api/src/client/system.ts @@ -10,13 +10,7 @@ export namespace system { return await api.get('/version'); } - export async function events(cb: (events: object) => void) { - let stream = await api.stream('/events'); - stream.on('data', (chunk: ArrayBuffer) => { - cb(JSON.parse(Buffer.from(chunk).toString())) - }) - stream.on('end', () => { - cb(undefined); - }) + export async function events() { + return await api.stream('/events'); } } diff --git a/packages/docker-api/src/client/volume.ts b/packages/docker-api/src/client/volume.ts new file mode 100644 index 0000000..8f37f83 --- /dev/null +++ b/packages/docker-api/src/client/volume.ts @@ -0,0 +1,8 @@ +import * as api from '../utils/api' +import * as types from '../api/types' + +export namespace volume { + export async function list() { + return await api.get('/volumes'); + } +} \ 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 b0ade70..93b5c62 100644 --- a/packages/docker-api/src/utils/api.ts +++ b/packages/docker-api/src/utils/api.ts @@ -11,6 +11,10 @@ export async function post(path: string, data?: object): Promise { return await handle("POST", path, { data }); } +export async function del(path: string, data?: object): Promise { + return await handle("DELETE", path, { params: data }); +} + export async function stream(path: string, data?: object): Promise { return await handle("GET", path, { params: data, responseType: "stream" }); } @@ -64,8 +68,7 @@ function init() { const instanceConfig: AxiosRequestConfig = { headers: { 'Content-Type': 'application/json' - }, - timeout: 5000 + } } if (process.env.DOCKER_HOST.startsWith("/")) { instanceConfig.socketPath = process.env.DOCKER_HOST diff --git a/packages/vscode/package.json b/packages/vscode/package.json index 91426b7..db57993 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -2,6 +2,7 @@ "name": "@dayu/vscode", "displayName": "vscode", "description": "", + "publisher": "MiaoWoo", "version": "0.0.1", "engines": { "vscode": "^1.35.0" @@ -73,4 +74,4 @@ "typescript": "^3.3.1", "vscode": "^1.1.28" } -} +} \ No newline at end of file