feat: add config and exec

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-12-31 18:35:00 +08:00
parent 752e771285
commit 84c304a26a
23 changed files with 203 additions and 38 deletions

View File

@ -50,6 +50,12 @@ POST {{service}}/swirl_swirl/restart
@id=6365041d2e52c5896dd14a5450920e482dddc33e6addaa07fab413bcda78d723
GET {{container}}/{{id}}/logs
### Config
@config={{url}}/configs
##### Config
GET {{config}}/
### Dashboard
@dashboard={{url}}/dashboard

View File

@ -66,17 +66,5 @@ switch (query.action) {
}
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

@ -0,0 +1,14 @@
import * as docker from '@dayu/docker-api'
import { controller, get, requestParam } from '@cc-server/binding'
@controller('/config')
class ConfigController {
@get('/list')
public async list() {
return await docker.config.list({ label: "com.docker.stack.namespace=nacos-cloud" });
}
@get('/:id')
public async info(@requestParam('id') id: string) {
return await docker.config.inspect(id);
}
}

View File

@ -1,10 +1,10 @@
import * as docker from '@dayu/docker-api'
import { io, interfaces, namespace, listener, Message } from '@cc-server/ws'
import { controller, httpGet, requestParam } from '@cc-server/binding'
import { controller, get, post, requestParam } from '@cc-server/binding'
@controller('/container')
class ContainerController {
@httpGet('/list')
@get('/list')
public async list() {
return await docker.container.list({
filters: JSON.stringify({
@ -12,7 +12,21 @@ class ContainerController {
})
});
}
@httpGet('/:id')
@get('/:id')
public async info(@requestParam('id') id: string) {
return await docker.container;
}
}
@controller('/exec')
class ContainerExecController {
@post('/:id')
public async create(@requestParam('id') id: string) {
return await docker.container.exec.create(id, {
})
}
@get('/:id')
public async info(@requestParam('id') id: string) {
return await docker.container;
}

View File

@ -46,4 +46,9 @@ class StackController {
networks: networks.map(network => network.Name),
}
}
@post('/create')
public async create() {
}
}

View File

@ -9,3 +9,19 @@ export type Options = StringMap
export type Config = StringMap
export type Labels = StringMap
export type Ports = StringMap | ObjectMap
export interface Version {
Index: number;
}
export interface Meta {
Version: Version;
CreatedAt: string;
UpdatedAt: string;
}
export interface Annotations {
Name: string;
Labels: Labels;
}
export interface Driver {
Name: string;
Options: Options;
}

View File

@ -1,5 +1,5 @@
export declare namespace query {
export interface FilterOpt {
export interface Filter<T = any> {
filters?: string;
}
export interface LabelOpt {

View File

@ -0,0 +1,8 @@
export declare namespace config {
export interface FilterOpt {
id?: string;
label?: string;
name?: string;
names?: string;
}
}

View File

@ -1,7 +1,7 @@
import * as common from './common'
export declare namespace container {
export interface ListOpts extends common.query.FilterOpt {
export interface ListOpts extends common.query.Filter {
all?: boolean;
limit?: number;
size?: boolean;
@ -15,4 +15,26 @@ export declare namespace container {
timestamps?: boolean;
tail?: number | "all";
}
export namespace exec {
export interface Create {
AttachStdin?: boolean;
AttachStdout?: boolean;
AttachStderr?: boolean;
DetachKeys?: string;
Tty?: boolean;
Cmd?: string[];
Env?: string[];
Privileged?: boolean;
User?: string;
WorkingDir?: string;
}
export interface Start {
Detach?: boolean;
Tty?: boolean;
}
export interface Resize {
h?: number;
w?: number;
}
}
}

View File

@ -0,0 +1,9 @@
export function toJSON(filter: any) {
let filters: any = {}
for (const key in filter) {
let temp: any = {}
temp[`${filter[key]}`] = true
filters[`${key}`] = temp
}
return JSON.stringify(filters);
}

View File

@ -1,5 +1,6 @@
export * from './swarm'
export * from './common'
export * from './config'
export * from './network'
export * from './service'
export * from './container'

View File

@ -1,6 +1,6 @@
import * as common from './common'
export declare namespace network {
export interface ListOpts extends common.query.FilterOpt {
export interface ListOpts extends common.query.Filter {
}
}

View File

@ -1,6 +1,6 @@
import * as common from './common'
export declare namespace node {
export interface ListOpts extends common.query.FilterOpt {
export interface ListOpts extends common.query.Filter {
}
}

View File

@ -1,7 +1,8 @@
import * as common from './common'
export declare namespace service {
export interface ListOpts extends common.query.FilterOpt {
export interface FilterOpt {
}
export interface LogsOpts {
details?: boolean;

View File

@ -0,0 +1,11 @@
import { Meta, Annotations } from '../common'
export declare namespace config {
export interface Spec extends Annotations {
Data: string;
}
export interface Config extends Meta {
ID: string;
Spec: Spec;
}
}

View File

@ -302,4 +302,36 @@ export declare namespace container {
ContainersDeleted: string[];
SpaceReclaimed: number;
}
export namespace exec {
export interface CreateResult {
Id: string;
}
export interface StartResult {
}
export interface ResizeResult {
}
export interface ProcessConfig {
arguments: string[];
entrypoint: string;
privileged: boolean;
tty: boolean;
user: string;
}
export interface ExecJson {
CanRemove: boolean;
ContainerID: string;
DetachKeys: string;
ExitCode: number;
ID: string;
OpenStderr: boolean;
OpenStdin: boolean;
OpenStdout: boolean;
ProcessConfig: ProcessConfig;
Running: boolean;
Pid: number;
}
}
}

View File

@ -2,6 +2,7 @@ export * from './node'
export * from './task'
export * from './image'
export * from './swarm'
export * from './config'
export * from './volume'
export * from './system'
export * from './network'

View File

@ -1,4 +1,4 @@
import { Labels, Options, StringMap } from '../common'
import { Labels, Options, StringMap, Annotations } from '../common'
export declare namespace network {
export interface Config {
@ -48,8 +48,7 @@ export declare namespace network {
Tasks: Task;
}
export interface NetworkResource {
Name: string;
export interface NetworkResource extends Annotations {
Id: string;
Created: string;
Scope: string;
@ -63,7 +62,6 @@ export declare namespace network {
ConfigOnly: boolean;
Containers?: Containers;
Options: Options;
Labels: Labels;
Peers: network.PeerInfo[];
Services: network.ServiceInfo[];
}

View File

@ -1,11 +1,6 @@
import { Version, Labels, Meta } from '../common'
export declare namespace swarm {
export interface Version {
Index: number;
}
export interface Labels {
}
export interface Orchestration {
TaskHistoryRetentionLimit?: number;
}
@ -55,11 +50,8 @@ export declare namespace swarm {
Manager: string;
}
export interface Info {
export interface Info extends Meta {
ID: string;
Version: Version;
CreatedAt: string;
UpdatedAt: string;
Spec: Spec;
TLSInfo: TLSInfo;
RootRotationInProgress: boolean;

View File

@ -0,0 +1,18 @@
import * as api from '../utils/api'
import * as opts from '../api/opts'
import * as types from '../api/types'
import * as filterUtil from '../api/opts/filter'
export namespace config {
export async function list(filter?: opts.config.FilterOpt) {
return await api.get<types.config.Config[]>('/configs', {
filters: filterUtil.toJSON(filter)
});
}
export async function inspect(id: string) {
return await api.get(`/configs/${id}`)
}
export async function create() {
return await api.post<{}>('/configs/create')
}
}

View File

@ -26,4 +26,29 @@ export namespace container {
}
return await api.stream(`/containers/${id}/logs`, data);
}
export namespace exec {
export function create(id: string, opts: opts.container.exec.Create = {}): Promise<types.container.exec.CreateResult> {
let request = {
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
DetachKeys: 'ctrl-d',
Tty: true,
Cmd: '/bin/sh',
...opts
}
request.AttachStderr = true
return api.post<types.container.exec.CreateResult>(`/containers/${id}/exec`, request)
}
export function start(id: string, opts: opts.container.exec.Start = {}) {
return api.post<types.container.exec.StartResult>(`/exec/${id}/start`, opts)
}
export function resize(id: string, opts: opts.container.exec.Resize = {}) {
return api.post<types.container.exec.ResizeResult>(`/exec/${id}/resize`, opts)
}
export function inspect(id: string) {
return api.get<types.container.exec.ExecJson>(`/exec/${id}/json`)
}
}
}

View File

@ -2,6 +2,7 @@ export * from './node'
export * from './task'
export * from './image'
export * from './swarm'
export * from './config'
export * from './volume'
export * from './system'
export * from './network'

View File

@ -1,11 +1,14 @@
import * as api from '../utils/api';
import * as opts from '../api/opts';
import * as types from '../api/types';
import * as filterUtil from '../api/opts/filter'
import * as http from 'http'
export namespace service {
export async function list(filters?: opts.service.ListOpts) {
return await api.get<types.service.Service[]>('/services', filters);
export async function list(filter?: opts.service.FilterOpt) {
return await api.get<types.service.Service[]>('/services', {
filters: filterUtil.toJSON(filter)
});
}
export async function create() {