Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
c496405818 | |||
020737e21b | |||
fe3560caac | |||
b2a7e67868 | |||
f772ca15ef | |||
71cfad146f | |||
43b1c13256 | |||
2c8b17f029 | |||
c6521d9160 | |||
72c74e44b5 | |||
2fb628213d | |||
373a4e2eac | |||
a202d05e9c | |||
6e97a7d83c | |||
04bb3b9ed0 | |||
29e502a32e | |||
75a118183e | |||
aa1fe594e8 | |||
cf761e19bf | |||
af7590c8de | |||
ee2e4fb856 | |||
ff5cecb07e | |||
e50b028c00 | |||
867dda8e7c | |||
d12d595f21 | |||
6117228ea4 | |||
b6b584f808 | |||
48fe6ce0a8 |
22
.gitignore
vendored
22
.gitignore
vendored
@ -1,4 +1,22 @@
|
|||||||
.yarn/
|
.nogit/
|
||||||
node_modules/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
@ -1,5 +1,5 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
@ -34,52 +34,38 @@ snyk:
|
|||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
sast:
|
|
||||||
stage: security
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
|
|
||||||
variables:
|
|
||||||
DOCKER_DRIVER: overlay2
|
|
||||||
allow_failure: true
|
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command npm run build
|
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
|
||||||
- docker run
|
|
||||||
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
|
|
||||||
--volume "$PWD:/code"
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
|
|
||||||
artifacts:
|
|
||||||
reports:
|
|
||||||
sast: gl-sast-report.json
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testSTABLE:
|
testLTS:
|
||||||
image: hosttoday/ht-docker-dbase:npmci
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- notpriv
|
||||||
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
@ -92,19 +78,11 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm install
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
@ -120,13 +98,15 @@ trigger:
|
|||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
|
services:
|
||||||
|
- docker:18-dind
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
60
README.md
60
README.md
@ -1,52 +1,38 @@
|
|||||||
# @mojoio/dockersock
|
# @mojoio/docker
|
||||||
|
unofficial docker engine api abstraction package written in TypeScript
|
||||||
|
|
||||||
easy communication with docker remote api from node, TypeScript ready
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/docker)
|
||||||
## Availabililty
|
* [gitlab.com (source)](https://gitlab.com/mojoio/docker)
|
||||||
|
* [github.com (source mirror)](https://github.com/mojoio/docker)
|
||||||
[](https://www.npmjs.com/package/dockersock)
|
* [docs (typedoc)](https://mojoio.gitlab.io/docker/)
|
||||||
[](https://GitLab.com/mojoio/dockersock)
|
|
||||||
[](https://github.com/mojoio/dockersock)
|
|
||||||
[](https://mojoio.gitlab.io/dockersock/)
|
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
[](https://gitlab.com/mojoio/docker/commits/master)
|
||||||
[](https://GitLab.com/mojoio/dockersock/commits/master)
|
[](https://gitlab.com/mojoio/docker/commits/master)
|
||||||
[](https://GitLab.com/mojoio/dockersock/commits/master)
|
[](https://www.npmjs.com/package/@mojoio/docker)
|
||||||
[](https://www.npmjs.com/package/dockersock)
|
[](https://snyk.io/test/npm/@mojoio/docker)
|
||||||
[](https://david-dm.org/mojoio/dockersock)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/mojoio/dockersock/master/dependencies/npm)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://www.bithound.io/github/mojoio/dockersock)
|
[](https://prettier.io/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
```TypeScript
|
```typescript
|
||||||
import {Dockersock} from "dockersock"; // require Dockersock class
|
import { DockerHost } from '@mojoio/docker'; // require Dockersock class
|
||||||
|
|
||||||
let myDockersock = new Dockersock(); // optional: you can pass a domain to the contructor, defaults to /var/run/docker.sock
|
const run = async () => {
|
||||||
|
const myDockerHost = new DockerHost(); // optional: you can pass a domain to the contructor, defaults to /var/run/docker.sock
|
||||||
|
|
||||||
myDockersock.listContainers() // promise, resolve gets container data
|
const containers = await myDockerHost.getContainers(); // promise, resolve with an array of DockerContainers
|
||||||
myDockersock.listContainersDetailed() // promise, resolve gets more detailed container data (by combining several requests internally)
|
};
|
||||||
myDockersock.listContainersRunning() // promise, resolve gets container data for currently running containers
|
|
||||||
myDockersock.listContainersStopped() // promise, resolve gets container data for stopped containers
|
|
||||||
|
|
||||||
myDockersock.startContainer({ // starts a already present container
|
|
||||||
name: "somecontainername"
|
|
||||||
})
|
|
||||||
|
|
||||||
myDockersock.newContainer({ // start new Container, equals "docker run" shell command
|
|
||||||
image: "someimagetag"
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://mojo.io)
|
[](https://maintainedby.lossless.com)
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
{
|
{
|
||||||
"npmdocker": {
|
"npmdocker": {
|
||||||
"baseImage": "hosttoday/ht-docker-node:npmci",
|
"baseImage": "hosttoday/ht-docker-node:npmci",
|
||||||
"command":
|
"command": "(ls -a && rm -r node_modules && yarn global add npmts && yarn install && npmts)",
|
||||||
"(ls -a && rm -r node_modules && yarn global add npmts && yarn install && npmts)",
|
|
||||||
"dockerSock": true
|
"dockerSock": true
|
||||||
},
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public",
|
"npmAccessLevel": "public",
|
||||||
"npmRegistryUrl": "registry.npmjs.org"
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "mojoio",
|
||||||
|
"gitrepo": "docker",
|
||||||
|
"shortDescription": "unofficial docker engine api abstraction package written in TypeScript",
|
||||||
|
"npmPackagename": "@mojoio/docker",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
888
package-lock.json
generated
888
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
39
package.json
39
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.29",
|
"version": "1.0.43",
|
||||||
"description": "easy communication with docker remote api from node, TypeScript ready",
|
"description": "easy communication with docker remote api from node, TypeScript ready",
|
||||||
"private": false,
|
"private": false,
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -27,19 +27,30 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.4",
|
"@pushrocks/lik": "^3.0.10",
|
||||||
"@pushrocks/smartlog": "^2.0.9",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@pushrocks/smartrequest": "^1.1.14",
|
"@pushrocks/smartrequest": "^1.1.16",
|
||||||
"rxjs": "^6.3.3"
|
"rxjs": "^6.5.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.4",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@gitzone/tsrun": "^1.1.17",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.18",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^3.0.11",
|
||||||
"@types/node": "^10.12.18",
|
"@types/node": "^12.7.1",
|
||||||
"tslint": "^5.12.0",
|
"tslint": "^5.18.0",
|
||||||
"tslint-config-prettier": "^1.17.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"dist_ts_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
50
test/test.ts
50
test/test.ts
@ -1,23 +1,49 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import { DockerHost } from '../ts/index';
|
import * as docker from '../ts/index';
|
||||||
|
|
||||||
let testDockerHost: DockerHost;
|
let testDockerHost: docker.DockerHost;
|
||||||
|
|
||||||
tap.test('should create a new Dockersock instance', async () => {
|
tap.test('should create a new Dockersock instance', async () => {
|
||||||
testDockerHost = new DockerHost();
|
testDockerHost = new docker.DockerHost();
|
||||||
return expect(testDockerHost).to.be.instanceof(DockerHost);
|
return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Containers
|
||||||
tap.test('should list containers', async () => {
|
tap.test('should list containers', async () => {
|
||||||
const containers = await testDockerHost.getContainers();
|
const containers = await testDockerHost.getContainers();
|
||||||
console.log(containers);
|
console.log(containers);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.skip.test('should pull an image from imagetag', async () => {
|
// Networks
|
||||||
// await testDockerHost.pullImage('hosttoday/ht-docker-node:npmci');
|
tap.test('should list networks', async () => {
|
||||||
|
const networks = await testDockerHost.getNetworks();
|
||||||
|
console.log(networks);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should return a change Objservable', async tools => {
|
tap.test('should create a network', async () => {
|
||||||
|
const newNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
|
||||||
|
Name: 'webgateway'
|
||||||
|
});
|
||||||
|
expect(newNetwork).to.be.instanceOf(docker.DockerNetwork);
|
||||||
|
expect(newNetwork.Name).to.equal('webgateway');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should remove a network', async () => {
|
||||||
|
const webgateway = await docker.DockerNetwork.getNetworkByName(testDockerHost, 'webgateway');
|
||||||
|
await webgateway.remove();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Images
|
||||||
|
tap.test('should pull an image from imagetag', async () => {
|
||||||
|
const image = await docker.DockerImage.createFromRegistry(testDockerHost, {
|
||||||
|
imageUrl: 'hosttoday/ht-docker-node',
|
||||||
|
tag: 'alpine'
|
||||||
|
});
|
||||||
|
expect(image).to.be.instanceOf(docker.DockerImage);
|
||||||
|
console.log(image);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should return a change Observable', async tools => {
|
||||||
const testObservable = await testDockerHost.getEventObservable();
|
const testObservable = await testDockerHost.getEventObservable();
|
||||||
const subscription = testObservable.subscribe(changeObject => {
|
const subscription = testObservable.subscribe(changeObject => {
|
||||||
console.log(changeObject);
|
console.log(changeObject);
|
||||||
@ -26,4 +52,14 @@ tap.test('should return a change Objservable', async tools => {
|
|||||||
subscription.unsubscribe();
|
subscription.unsubscribe();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// SERVICES
|
||||||
|
tap.test('should activate swarm mode', async () => {
|
||||||
|
await testDockerHost.activateSwarm();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should list all services', async tools => {
|
||||||
|
const services = await docker.DockerService.getServices(testDockerHost);
|
||||||
|
console.log(services);
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1 +0,0 @@
|
|||||||
import * as plugins from './dockersock.plugins';
|
|
@ -1,54 +1,78 @@
|
|||||||
import * as plugins from './dockersock.plugins';
|
import * as plugins from './docker.plugins';
|
||||||
import * as interfaces from './interfaces';
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
import { DockerHost } from './docker.classes.host';
|
import { DockerHost } from './docker.classes.host';
|
||||||
|
|
||||||
export class DockerContainer {
|
export class DockerContainer {
|
||||||
// ======
|
|
||||||
// STATIC
|
// STATIC
|
||||||
// ======
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get all containers
|
* get all containers
|
||||||
*/
|
*/
|
||||||
static async getContainers(dockerHostArg: DockerHost): Promise<DockerContainer[]> {
|
public static async getContainers(dockerHostArg: DockerHost): Promise<DockerContainer[]> {
|
||||||
const result: DockerContainer[] = [];
|
const result: DockerContainer[] = [];
|
||||||
const response = await dockerHostArg.request('GET', '/containers/json');
|
const response = await dockerHostArg.request('GET', '/containers/json');
|
||||||
|
|
||||||
|
// TODO: Think about getting the config by inpsecting the container
|
||||||
for (const containerResult of response.body) {
|
for (const containerResult of response.body) {
|
||||||
result.push(new DockerContainer(containerResult));
|
result.push(new DockerContainer(dockerHostArg, containerResult));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* gets an container by Id
|
||||||
* @param containerId
|
* @param containerId
|
||||||
*/
|
*/
|
||||||
static async getContainerById(containerId: string) {}
|
public static async getContainerById(containerId: string) {
|
||||||
static async create() {}
|
// TODO: implement get container by id
|
||||||
|
|
||||||
// ========
|
|
||||||
// INSTANCE
|
|
||||||
// ========
|
|
||||||
|
|
||||||
constructor(dockerContainerObjectArg: any) {
|
|
||||||
Object.keys(dockerContainerObjectArg).forEach(keyArg => {
|
|
||||||
this[keyArg] = dockerContainerObjectArg[keyArg];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Id: string;
|
/**
|
||||||
Names: string[];
|
* create a container
|
||||||
Image: string;
|
*/
|
||||||
ImageID: string;
|
public static async create(
|
||||||
Command: string;
|
dockerHost: DockerHost,
|
||||||
Created: number;
|
containerCreationDescriptor: interfaces.IContainerCreationDescriptor
|
||||||
Ports: interfaces.TPorts;
|
) {
|
||||||
Labels: interfaces.TLabels;
|
// check for unique hostname
|
||||||
State: string;
|
const existingContainers = await DockerContainer.getContainers(dockerHost);
|
||||||
Status: string;
|
const sameHostNameContainer = existingContainers.find(container => {
|
||||||
HostConfig: any;
|
// TODO implement HostName Detection;
|
||||||
NetworkSettings: {
|
return false;
|
||||||
|
});
|
||||||
|
const response = await dockerHost.request('POST', '/containers/create', {
|
||||||
|
Hostname: containerCreationDescriptor.Hostname,
|
||||||
|
Domainname: containerCreationDescriptor.Domainname,
|
||||||
|
User: 'root'
|
||||||
|
});
|
||||||
|
if (response.statusCode < 300) {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'Container created successfully');
|
||||||
|
} else {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'error',
|
||||||
|
'There has been a problem when creating the container'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
// references
|
||||||
|
public dockerHost: DockerHost;
|
||||||
|
|
||||||
|
// properties
|
||||||
|
public Id: string;
|
||||||
|
public Names: string[];
|
||||||
|
public Image: string;
|
||||||
|
public ImageID: string;
|
||||||
|
public Command: string;
|
||||||
|
public Created: number;
|
||||||
|
public Ports: interfaces.TPorts;
|
||||||
|
public Labels: interfaces.TLabels;
|
||||||
|
public State: string;
|
||||||
|
public Status: string;
|
||||||
|
public HostConfig: any;
|
||||||
|
public NetworkSettings: {
|
||||||
Networks: {
|
Networks: {
|
||||||
[key: string]: {
|
[key: string]: {
|
||||||
IPAMConfig: any;
|
IPAMConfig: any;
|
||||||
@ -67,5 +91,11 @@ export class DockerContainer {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Mounts: any;
|
public Mounts: any;
|
||||||
|
constructor(dockerHostArg: DockerHost, dockerContainerObjectArg: any) {
|
||||||
|
this.dockerHost = dockerHostArg;
|
||||||
|
Object.keys(dockerContainerObjectArg).forEach(keyArg => {
|
||||||
|
this[keyArg] = dockerContainerObjectArg[keyArg];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,19 @@
|
|||||||
import * as plugins from './dockersock.plugins';
|
import * as plugins from './docker.plugins';
|
||||||
import { DockerContainer } from './docker.classes.container';
|
import { DockerContainer } from './docker.classes.container';
|
||||||
|
import { DockerNetwork } from './docker.classes.network';
|
||||||
|
|
||||||
export class DockerHost {
|
export class DockerHost {
|
||||||
/**
|
/**
|
||||||
* the path where the docker sock can be found
|
* the path where the docker sock can be found
|
||||||
*/
|
*/
|
||||||
sockPath: string;
|
public socketPath: string;
|
||||||
|
|
||||||
/**
|
|
||||||
* keeping track of currently active requests to safely end this module at any time
|
|
||||||
*/
|
|
||||||
requestObjectmap = new plugins.lik.Objectmap<any>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the constructor to instantiate a new docker sock instance
|
* the constructor to instantiate a new docker sock instance
|
||||||
* @param pathArg
|
* @param pathArg
|
||||||
*/
|
*/
|
||||||
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
||||||
this.sockPath = pathArg;
|
this.socketPath = pathArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,25 +21,40 @@ export class DockerHost {
|
|||||||
* @param userArg
|
* @param userArg
|
||||||
* @param passArg
|
* @param passArg
|
||||||
*/
|
*/
|
||||||
auth(registryArg: string, userArg: string, passArg: string) {
|
public async auth(registryArg: string, userArg: string, passArg: string) {
|
||||||
let done = plugins.smartpromise.defer();
|
// TODO: implement Docker Registry authentication
|
||||||
this.request('POST', '');
|
await this.request('POST', '');
|
||||||
return done.promise;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all networks
|
||||||
|
*/
|
||||||
|
public async getNetworks() {
|
||||||
|
return await DockerNetwork.getNetworks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all containers
|
||||||
|
*/
|
||||||
|
public async getContainers() {
|
||||||
|
const containerArray = await DockerContainer.getContainers(this);
|
||||||
|
return containerArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async getContainers() {
|
public async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
|
||||||
const containerArray = await DockerContainer.getContainers(this);
|
|
||||||
return containerArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
|
|
||||||
const response = await this.requestStreaming('GET', '/events');
|
const response = await this.requestStreaming('GET', '/events');
|
||||||
return plugins.rxjs.Observable.create(observer => {
|
return plugins.rxjs.Observable.create(observer => {
|
||||||
response.on('data', data => {
|
response.on('data', data => {
|
||||||
observer.next(data.toString());
|
const eventString = data.toString();
|
||||||
|
try {
|
||||||
|
const eventObject = JSON.parse(eventString);
|
||||||
|
observer.next(eventObject);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
response.emit('end');
|
response.emit('end');
|
||||||
@ -51,11 +62,30 @@ export class DockerHost {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* activates docker swarm
|
||||||
|
*/
|
||||||
|
public async activateSwarm(addvertisementIpArg?: string) {
|
||||||
|
const response = await this.request('POST', '/swarm/init', {
|
||||||
|
ListenAddr: '0.0.0.0:2377',
|
||||||
|
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377` : undefined,
|
||||||
|
DataPathPort: 4789,
|
||||||
|
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
|
||||||
|
SubnetSize: 24,
|
||||||
|
ForceNewCluster: false
|
||||||
|
});
|
||||||
|
if (response.statusCode === 200) {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'created Swam succesfully');
|
||||||
|
} else {
|
||||||
|
plugins.smartlog.defaultLogger.log('error', 'could not initiate swarm');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fire a request
|
* fire a request
|
||||||
*/
|
*/
|
||||||
async request(methodArg: string, routeArg: string, dataArg = {}) {
|
public async request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||||
const requestUrl = `${this.sockPath}${routeArg}`;
|
const requestUrl = `${this.socketPath}${routeArg}`;
|
||||||
const response = await plugins.smartrequest.request(requestUrl, {
|
const response = await plugins.smartrequest.request(requestUrl, {
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
headers: {
|
headers: {
|
||||||
@ -64,11 +94,14 @@ export class DockerHost {
|
|||||||
},
|
},
|
||||||
requestBody: dataArg
|
requestBody: dataArg
|
||||||
});
|
});
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
console.log(response.body);
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
|
public async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
|
||||||
const requestUrl = `${this.sockPath}${routeArg}`;
|
const requestUrl = `${this.socketPath}${routeArg}`;
|
||||||
const response = await plugins.smartrequest.request(
|
const response = await plugins.smartrequest.request(
|
||||||
requestUrl,
|
requestUrl,
|
||||||
{
|
{
|
||||||
|
@ -1,14 +1,112 @@
|
|||||||
import * as plugins from './dockersock.plugins';
|
import * as plugins from './docker.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
import { DockerHost } from './docker.classes.host';
|
import { DockerHost } from './docker.classes.host';
|
||||||
|
|
||||||
export class DockerImage {
|
export class DockerImage {
|
||||||
|
// STATIC
|
||||||
|
public static async getImages(dockerHost: DockerHost) {
|
||||||
|
const images: DockerImage[] = [];
|
||||||
|
const response = await dockerHost.request('GET', '/images/json');
|
||||||
|
for (const imageObject of response.body) {
|
||||||
|
images.push(new DockerImage(dockerHost, imageObject));
|
||||||
|
}
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) {
|
||||||
|
const images = await this.getImages(dockerHost);
|
||||||
|
return images.find(image => {
|
||||||
|
return image.RepoTags.includes(imageNameArg);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async createFromRegistry(
|
||||||
|
dockerHostArg: DockerHost,
|
||||||
|
creationObject: interfaces.IImageCreationDescriptor
|
||||||
|
): Promise<DockerImage> {
|
||||||
|
const response = await dockerHostArg.request(
|
||||||
|
'POST',
|
||||||
|
`/images/create?fromImage=${encodeURIComponent(
|
||||||
|
creationObject.imageUrl
|
||||||
|
)}&tag=${encodeURIComponent(creationObject.tag)}`
|
||||||
|
);
|
||||||
|
if (response.statusCode < 300) {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'info',
|
||||||
|
`Successfully pulled image ${creationObject.imageUrl} from the registry`
|
||||||
|
);
|
||||||
|
const originTag = `${creationObject.imageUrl}:${creationObject.tag}`;
|
||||||
|
console.log(originTag);
|
||||||
|
const image = await DockerImage.findImageByName(dockerHostArg, originTag);
|
||||||
|
return image;
|
||||||
|
} else {
|
||||||
|
plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async tagImageByIdOrName(
|
||||||
|
dockerHost: DockerHost,
|
||||||
|
idOrNameArg: string,
|
||||||
|
newTagArg: string
|
||||||
|
) {
|
||||||
|
const response = await dockerHost.request(
|
||||||
|
'POST',
|
||||||
|
`/images/${encodeURIComponent(idOrNameArg)}/${encodeURIComponent(newTagArg)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async buildImage(dockerHostArg: DockerHost, dockerImageTag) {
|
||||||
|
// TODO: implement building an image
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
// references
|
||||||
|
public dockerHost: DockerHost;
|
||||||
|
|
||||||
|
// properties
|
||||||
/**
|
/**
|
||||||
* the tags for an image
|
* the tags for an image
|
||||||
*/
|
*/
|
||||||
tags: string[] = [];
|
public Containers: number;
|
||||||
|
public Created: number;
|
||||||
|
public Id: string;
|
||||||
|
public Labels: interfaces.TLabels;
|
||||||
|
public ParentId: string;
|
||||||
|
public RepoDigests: string[];
|
||||||
|
public RepoTags: string[];
|
||||||
|
public SharedSize: number;
|
||||||
|
public Size: number;
|
||||||
|
public VirtualSize: number;
|
||||||
|
|
||||||
static async createFromRegsitry(dockerHostArg: DockerHost): Promise<DockerImage> {
|
constructor(dockerHostArg, dockerImageObjectArg: any) {
|
||||||
const resultingImage = new DockerImage();
|
this.dockerHost = dockerHostArg;
|
||||||
return resultingImage;
|
Object.keys(dockerImageObjectArg).forEach(keyArg => {
|
||||||
|
this[keyArg] = dockerImageObjectArg[keyArg];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a boolean wether the image has a upstream image
|
||||||
|
*/
|
||||||
|
public isUpstreamImage(): boolean {
|
||||||
|
// TODO: implement isUpastreamImage
|
||||||
|
return this.RepoTags.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public tagImage(newTag) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pulls the latest version from the registry
|
||||||
|
*/
|
||||||
|
public async pullLatestImageFromRegistry(): Promise<boolean> {
|
||||||
|
const dockerImageUrl = this.RepoTags[0].split(':')[0];
|
||||||
|
const dockerImageTag = this.RepoTags[0].split(':')[1];
|
||||||
|
const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
|
||||||
|
imageUrl: dockerImageUrl,
|
||||||
|
tag: dockerImageTag
|
||||||
|
});
|
||||||
|
Object.assign(this, updatedImage);
|
||||||
|
// TODO: Compare image digists before and after
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
import * as plugins from './docker.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
import { DockerHost } from './docker.classes.host';
|
||||||
|
|
||||||
|
export class DockerNetwork {
|
||||||
|
public static async getNetworks(dockerHost: DockerHost): Promise<DockerNetwork[]> {
|
||||||
|
const dockerNetworks: DockerNetwork[] = [];
|
||||||
|
const response = await dockerHost.request('GET', '/networks');
|
||||||
|
for (const networkObject of response.body) {
|
||||||
|
dockerNetworks.push(new DockerNetwork(dockerHost, networkObject));
|
||||||
|
}
|
||||||
|
return dockerNetworks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getNetworkByName(dockerHost: DockerHost, dockerNetworkNameArg: string) {
|
||||||
|
const networks = await DockerNetwork.getNetworks(dockerHost);
|
||||||
|
return networks.find(dockerNetwork => dockerNetwork.Name === dockerNetworkNameArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async createNetwork(
|
||||||
|
dockerHost: DockerHost,
|
||||||
|
networkCreationDescriptor: interfaces.INetworkCreationDescriptor
|
||||||
|
): Promise<DockerNetwork> {
|
||||||
|
const response = await dockerHost.request('POST', '/networks/create', {
|
||||||
|
Name: networkCreationDescriptor.Name,
|
||||||
|
CheckDuplicate: true,
|
||||||
|
Driver: 'overlay',
|
||||||
|
EnableIPv6: false,
|
||||||
|
IPAM: {
|
||||||
|
Driver: 'default',
|
||||||
|
Config: [
|
||||||
|
{
|
||||||
|
Subnet: '172.20.10.0/16',
|
||||||
|
IPRange: '172.20.10.0/24',
|
||||||
|
Gateway: '172.20.10.11'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Internal: true,
|
||||||
|
Attachable: true,
|
||||||
|
Ingress: false
|
||||||
|
});
|
||||||
|
if (response.statusCode < 300) {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'Created network successfully');
|
||||||
|
return await DockerNetwork.getNetworkByName(dockerHost, networkCreationDescriptor.Name);
|
||||||
|
} else {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'error',
|
||||||
|
'There has been an error creating the wanted network'
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
// references
|
||||||
|
public dockerHost: DockerHost;
|
||||||
|
|
||||||
|
// properties
|
||||||
|
public Name: string;
|
||||||
|
public Id: string;
|
||||||
|
public Created: string;
|
||||||
|
public Scope: string;
|
||||||
|
public Driver: string;
|
||||||
|
public EnableIPv6: boolean;
|
||||||
|
public Internal: boolean;
|
||||||
|
public Attachable: boolean;
|
||||||
|
public Ingress: false;
|
||||||
|
public IPAM: {
|
||||||
|
Driver: 'default' | 'bridge' | 'overlay';
|
||||||
|
Config: [
|
||||||
|
{
|
||||||
|
Subnet: string;
|
||||||
|
IPRange: string;
|
||||||
|
Gateway: string;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(dockerHostArg: DockerHost, dockerNetworkObjectArg: any) {
|
||||||
|
this.dockerHost = dockerHostArg;
|
||||||
|
Object.keys(dockerNetworkObjectArg).forEach(keyArg => {
|
||||||
|
this[keyArg] = dockerNetworkObjectArg[keyArg];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removes the network
|
||||||
|
*/
|
||||||
|
public async remove() {
|
||||||
|
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
import * as plugins from './docker.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
import { DockerHost } from './docker.classes.host';
|
||||||
|
|
||||||
|
export class DockerService {
|
||||||
|
// STATIC
|
||||||
|
public static async getServices(dockerHost: DockerHost) {
|
||||||
|
const services: DockerService[] = [];
|
||||||
|
const response = await dockerHost.request('GET', '/services');
|
||||||
|
for (const serviceObject of response.body) {
|
||||||
|
services.push(new DockerService(dockerHost, serviceObject));
|
||||||
|
}
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a service
|
||||||
|
*/
|
||||||
|
public static async createService(
|
||||||
|
dockerHost: DockerHost,
|
||||||
|
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
||||||
|
) {
|
||||||
|
dockerHost.request('POST', '/services/create', {
|
||||||
|
Name: serviceCreationDescriptor.Name,
|
||||||
|
TaskTemplate: {
|
||||||
|
ContainerSpec: {
|
||||||
|
Image: serviceCreationDescriptor.Image,
|
||||||
|
Labels: serviceCreationDescriptor.Labels
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Labels: serviceCreationDescriptor.Labels
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public dockerHost: DockerHost;
|
||||||
|
|
||||||
|
constructor(dockerHostArg: DockerHost, serviceObject) {
|
||||||
|
this.dockerHost = dockerHostArg;
|
||||||
|
Object.assign(this, serviceObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,6 +4,8 @@ import * as smartlog from '@pushrocks/smartlog';
|
|||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
|
||||||
|
smartlog.defaultLogger.enableConsole();
|
||||||
|
|
||||||
export { lik, smartlog, smartpromise, smartrequest };
|
export { lik, smartlog, smartpromise, smartrequest };
|
||||||
|
|
||||||
// third party
|
// third party
|
@ -1 +1,5 @@
|
|||||||
export { DockerHost } from './docker.classes.host';
|
export * from './docker.classes.host';
|
||||||
|
export * from './docker.classes.container';
|
||||||
|
export * from './docker.classes.image';
|
||||||
|
export * from './docker.classes.network';
|
||||||
|
export * from './docker.classes.service';
|
||||||
|
7
ts/interfaces/container.ts
Normal file
7
ts/interfaces/container.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { DockerNetwork } from '../docker.classes.network';
|
||||||
|
|
||||||
|
export interface IContainerCreationDescriptor {
|
||||||
|
Hostname: string;
|
||||||
|
Domainname: string;
|
||||||
|
networks?: DockerNetwork[];
|
||||||
|
}
|
4
ts/interfaces/image.ts
Normal file
4
ts/interfaces/image.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export interface IImageCreationDescriptor {
|
||||||
|
imageUrl: string;
|
||||||
|
tag: string;
|
||||||
|
}
|
@ -1,2 +1,6 @@
|
|||||||
|
export * from './container';
|
||||||
|
export * from './image';
|
||||||
export * from './label';
|
export * from './label';
|
||||||
|
export * from './network';
|
||||||
export * from './port';
|
export * from './port';
|
||||||
|
export * from './service';
|
||||||
|
6
ts/interfaces/network.ts
Normal file
6
ts/interfaces/network.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* creates a new Network
|
||||||
|
*/
|
||||||
|
export interface INetworkCreationDescriptor {
|
||||||
|
Name: string;
|
||||||
|
}
|
7
ts/interfaces/service.ts
Normal file
7
ts/interfaces/service.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import * as interfaces from './';
|
||||||
|
|
||||||
|
export interface IServiceCreationDescriptor {
|
||||||
|
Name: string;
|
||||||
|
Image: string;
|
||||||
|
Labels: interfaces.TLabels;
|
||||||
|
}
|
Reference in New Issue
Block a user