Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
a4dc4e7950 | |||
424e911804 | |||
b5c4727bae | |||
b6f3fbf8a9 | |||
7241e7a8fd | |||
ae37148ece | |||
65c37bdd6f | |||
6acbe30e2e | |||
eb6f7889d0 | |||
e39da5fee9 | |||
b07628bb0b | |||
5815f9b202 | |||
846ea9997e | |||
de54db33ad | |||
314cb692ac | |||
73f8ded3fe | |||
a28b10ac51 | |||
927e2e0acc | |||
c496405818 | |||
020737e21b | |||
fe3560caac | |||
b2a7e67868 | |||
f772ca15ef | |||
71cfad146f | |||
43b1c13256 | |||
2c8b17f029 | |||
c6521d9160 | |||
72c74e44b5 | |||
2fb628213d | |||
373a4e2eac | |||
a202d05e9c | |||
6e97a7d83c | |||
04bb3b9ed0 | |||
29e502a32e | |||
75a118183e | |||
aa1fe594e8 | |||
cf761e19bf | |||
af7590c8de | |||
ee2e4fb856 | |||
ff5cecb07e |
22
.gitignore
vendored
22
.gitignore
vendored
@ -1,4 +1,22 @@
|
||||
.yarn/
|
||||
node_modules/
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
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
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
@ -34,43 +34,17 @@ snyk:
|
||||
- docker
|
||||
- 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
|
||||
# ====================
|
||||
|
||||
testSTABLE:
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
testLTS:
|
||||
services:
|
||||
- docker:dind
|
||||
- docker:18-dind
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
@ -78,10 +52,22 @@ testSTABLE:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
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:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci node install lts
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
@ -94,19 +80,11 @@ release:
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--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]
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
@ -122,13 +100,15 @@ trigger:
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
services:
|
||||
- docker:18-dind
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- docker
|
||||
- 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
|
||||
|
||||
[](https://www.npmjs.com/package/dockersock)
|
||||
[](https://GitLab.com/mojoio/dockersock)
|
||||
[](https://github.com/mojoio/dockersock)
|
||||
[](https://mojoio.gitlab.io/dockersock/)
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/docker)
|
||||
* [gitlab.com (source)](https://gitlab.com/mojoio/docker)
|
||||
* [github.com (source mirror)](https://github.com/mojoio/docker)
|
||||
* [docs (typedoc)](https://mojoio.gitlab.io/docker/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/mojoio/dockersock/commits/master)
|
||||
[](https://GitLab.com/mojoio/dockersock/commits/master)
|
||||
[](https://www.npmjs.com/package/dockersock)
|
||||
[](https://david-dm.org/mojoio/dockersock)
|
||||
[](https://www.bithound.io/github/mojoio/dockersock/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/mojoio/dockersock)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
[](https://gitlab.com/mojoio/docker/commits/master)
|
||||
[](https://gitlab.com/mojoio/docker/commits/master)
|
||||
[](https://www.npmjs.com/package/@mojoio/docker)
|
||||
[](https://snyk.io/test/npm/@mojoio/docker)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```TypeScript
|
||||
import {Dockersock} from "dockersock"; // require Dockersock class
|
||||
```typescript
|
||||
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
|
||||
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"
|
||||
})
|
||||
const containers = await myDockerHost.getContainers(); // promise, resolve with an array of DockerContainers
|
||||
};
|
||||
```
|
||||
|
||||
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)
|
||||
> | 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": {
|
||||
"baseImage": "hosttoday/ht-docker-node:npmci",
|
||||
"command":
|
||||
"(ls -a && rm -r node_modules && yarn global add npmts && yarn install && npmts)",
|
||||
"command": "(ls -a && rm -r node_modules && yarn global add npmts && yarn install && npmts)",
|
||||
"dockerSock": true
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
686
package-lock.json
generated
686
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/docker",
|
||||
"version": "1.0.32",
|
||||
"version": "1.0.52",
|
||||
"description": "easy communication with docker remote api from node, TypeScript ready",
|
||||
"private": false,
|
||||
"main": "dist/index.js",
|
||||
@ -27,19 +27,31 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^3.0.10",
|
||||
"@pushrocks/lik": "^3.0.11",
|
||||
"@pushrocks/smartlog": "^2.0.19",
|
||||
"@pushrocks/smartnetwork": "^1.1.14",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrequest": "^1.1.16",
|
||||
"rxjs": "^6.5.2"
|
||||
"@pushrocks/smartrequest": "^1.1.23",
|
||||
"rxjs": "^6.5.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/tapbundle": "^3.0.11",
|
||||
"@types/node": "^12.7.1",
|
||||
"tslint": "^5.18.0",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.4",
|
||||
"tslint": "^5.19.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
]
|
||||
}
|
||||
|
70
test/test.ts
70
test/test.ts
@ -1,23 +1,54 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { DockerHost } from '../ts/index';
|
||||
import * as docker from '../ts/index';
|
||||
import { DockerService } from '../ts/index';
|
||||
|
||||
let testDockerHost: DockerHost;
|
||||
let testDockerHost: docker.DockerHost;
|
||||
|
||||
tap.test('should create a new Dockersock instance', async () => {
|
||||
testDockerHost = new DockerHost();
|
||||
return expect(testDockerHost).to.be.instanceof(DockerHost);
|
||||
testDockerHost = new docker.DockerHost();
|
||||
return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
|
||||
});
|
||||
|
||||
tap.test('should create a docker swarm', async () => {
|
||||
|
||||
});
|
||||
|
||||
// Containers
|
||||
tap.test('should list containers', async () => {
|
||||
const containers = await testDockerHost.getContainers();
|
||||
console.log(containers);
|
||||
});
|
||||
|
||||
tap.skip.test('should pull an image from imagetag', async () => {
|
||||
// await testDockerHost.pullImage('hosttoday/ht-docker-node:npmci');
|
||||
// Networks
|
||||
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',
|
||||
imageTag: '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 subscription = testObservable.subscribe(changeObject => {
|
||||
console.log(changeObject);
|
||||
@ -26,4 +57,29 @@ tap.test('should return a change Objservable', async tools => {
|
||||
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.test('should create a service', async () => {
|
||||
const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
|
||||
Name: 'testNetwork'
|
||||
});
|
||||
await DockerService.createService(testDockerHost, {
|
||||
Image: 'nginx:latest',
|
||||
Labels: {
|
||||
'testlabel': 'hi'
|
||||
},
|
||||
Name: 'testService',
|
||||
networks: [testNetwork],
|
||||
networkAlias: 'testService'
|
||||
});
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1 +0,0 @@
|
||||
import * as plugins from './dockersock.plugins';
|
@ -1,12 +1,10 @@
|
||||
import * as plugins from './dockersock.plugins';
|
||||
import * as plugins from './docker.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { DockerHost } from './docker.classes.host';
|
||||
|
||||
export class DockerContainer {
|
||||
// ======
|
||||
// STATIC
|
||||
// ======
|
||||
|
||||
/**
|
||||
* get all containers
|
||||
@ -17,7 +15,7 @@ export class DockerContainer {
|
||||
|
||||
// TODO: Think about getting the config by inpsecting the container
|
||||
for (const containerResult of response.body) {
|
||||
result.push(new DockerContainer(containerResult));
|
||||
result.push(new DockerContainer(dockerHostArg, containerResult));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -26,37 +24,55 @@ export class DockerContainer {
|
||||
* gets an container by Id
|
||||
* @param containerId
|
||||
*/
|
||||
public static async getContainerById(containerId: string) {}
|
||||
public static async getContainerById(containerId: string) {
|
||||
// TODO: implement get container by id
|
||||
}
|
||||
|
||||
/**
|
||||
* create a container
|
||||
*/
|
||||
public static async create(creationSpecifier: interfaces.IContainerCreationSpecifier) {
|
||||
|
||||
}
|
||||
|
||||
// ========
|
||||
// INSTANCE
|
||||
// ========
|
||||
|
||||
constructor(dockerContainerObjectArg: any) {
|
||||
Object.keys(dockerContainerObjectArg).forEach(keyArg => {
|
||||
this[keyArg] = dockerContainerObjectArg[keyArg];
|
||||
public static async create(
|
||||
dockerHost: DockerHost,
|
||||
containerCreationDescriptor: interfaces.IContainerCreationDescriptor
|
||||
) {
|
||||
// check for unique hostname
|
||||
const existingContainers = await DockerContainer.getContainers(dockerHost);
|
||||
const sameHostNameContainer = existingContainers.find(container => {
|
||||
// TODO implement HostName Detection;
|
||||
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'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Id: string;
|
||||
Names: string[];
|
||||
Image: string;
|
||||
ImageID: string;
|
||||
Command: string;
|
||||
Created: number;
|
||||
Ports: interfaces.TPorts;
|
||||
Labels: interfaces.TLabels;
|
||||
State: string;
|
||||
Status: string;
|
||||
HostConfig: any;
|
||||
NetworkSettings: {
|
||||
// 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: {
|
||||
[key: string]: {
|
||||
IPAMConfig: any;
|
||||
@ -75,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,27 @@
|
||||
import * as plugins from './dockersock.plugins';
|
||||
import * as plugins from './docker.plugins';
|
||||
import { DockerContainer } from './docker.classes.container';
|
||||
import { DockerNetwork } from './docker.classes.network';
|
||||
|
||||
export class DockerHost {
|
||||
/**
|
||||
* the path where the docker sock can be found
|
||||
*/
|
||||
sockPath: string;
|
||||
|
||||
/**
|
||||
* keeping track of currently active requests to safely end this module at any time
|
||||
*/
|
||||
requestObjectmap = new plugins.lik.Objectmap<any>();
|
||||
public socketPath: string;
|
||||
|
||||
/**
|
||||
* the constructor to instantiate a new docker sock instance
|
||||
* @param pathArg
|
||||
*/
|
||||
constructor(pathArg: string = 'http://unix:/var/run/docker.sock:') {
|
||||
this.sockPath = pathArg;
|
||||
constructor(pathArg?: string) {
|
||||
let pathToUse: string;
|
||||
if (pathArg) {
|
||||
pathToUse = pathArg;
|
||||
} else if (process.env.CI) {
|
||||
pathToUse = 'http://docker:2375/';
|
||||
} else {
|
||||
pathToUse = 'http://unix:/var/run/docker.sock:';
|
||||
}
|
||||
this.socketPath = pathToUse;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -25,21 +29,33 @@ export class DockerHost {
|
||||
* @param userArg
|
||||
* @param passArg
|
||||
*/
|
||||
auth(registryArg: string, userArg: string, passArg: string) {
|
||||
let done = plugins.smartpromise.defer();
|
||||
this.request('POST', '');
|
||||
return done.promise;
|
||||
public async auth(registryUrl: string, userArg: string, passArg: string) {
|
||||
const response = await this.request('POST', '/auth', {
|
||||
serveraddress: registryUrl,
|
||||
username: userArg,
|
||||
password: passArg,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
const containerArray = await DockerContainer.getContainers(this);
|
||||
return containerArray;
|
||||
}
|
||||
|
||||
async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
|
||||
public async getEventObservable(): Promise<plugins.rxjs.Observable<any>> {
|
||||
const response = await this.requestStreaming('GET', '/events');
|
||||
return plugins.rxjs.Observable.create(observer => {
|
||||
response.on('data', data => {
|
||||
@ -57,11 +73,42 @@ export class DockerHost {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* activates docker swarm
|
||||
*/
|
||||
public async activateSwarm(addvertisementIpArg?: string) {
|
||||
// determine advertisement address
|
||||
let addvertisementIp: string;
|
||||
if (addvertisementIpArg) {
|
||||
addvertisementIp = addvertisementIpArg;
|
||||
} else {
|
||||
const smartnetworkInstance = new plugins.smartnetwork.SmartNetwork();
|
||||
const defaultGateway = await smartnetworkInstance.getDefaultGateway();
|
||||
if (defaultGateway) {
|
||||
addvertisementIp = defaultGateway.ipv4.address;
|
||||
}
|
||||
}
|
||||
|
||||
const response = await this.request('POST', '/swarm/init', {
|
||||
ListenAddr: '0.0.0.0:2377',
|
||||
AdvertiseAddr: addvertisementIp,
|
||||
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
|
||||
*/
|
||||
async request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
const requestUrl = `${this.sockPath}${routeArg}`;
|
||||
public async request(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
const requestUrl = `${this.socketPath}${routeArg}`;
|
||||
const response = await plugins.smartrequest.request(requestUrl, {
|
||||
method: methodArg,
|
||||
headers: {
|
||||
@ -70,11 +117,14 @@ export class DockerHost {
|
||||
},
|
||||
requestBody: dataArg
|
||||
});
|
||||
if (response.statusCode !== 200) {
|
||||
console.log(response.body);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
const requestUrl = `${this.sockPath}${routeArg}`;
|
||||
public async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
|
||||
const requestUrl = `${this.socketPath}${routeArg}`;
|
||||
const response = await plugins.smartrequest.request(
|
||||
requestUrl,
|
||||
{
|
||||
|
@ -1,37 +1,128 @@
|
||||
import * as plugins from './dockersock.plugins';
|
||||
import * as plugins from './docker.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import { DockerHost } from './docker.classes.host';
|
||||
|
||||
export class DockerImage {
|
||||
// STATIC
|
||||
public static async createFromRegistry(dockerHostArg: DockerHost, dockerImageTag): Promise<DockerImage> {
|
||||
const resultingImage = new DockerImage();
|
||||
|
||||
return resultingImage;
|
||||
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 createFromExistingImage(dockerHostArg: DockerHost, dockerImageTag) {}
|
||||
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> {
|
||||
|
||||
// lets create a sanatized imageUrlObject
|
||||
const imageUrlObject: {
|
||||
imageUrl: string;
|
||||
imageTag: string;
|
||||
imageOriginTag: string;
|
||||
} = {
|
||||
imageUrl: creationObject.imageUrl,
|
||||
imageTag: creationObject.imageTag,
|
||||
imageOriginTag: null
|
||||
};
|
||||
if (imageUrlObject.imageUrl.includes(':')) {
|
||||
const imageUrl = imageUrlObject.imageUrl.split(':')[0];
|
||||
const imageTag = imageUrlObject.imageUrl.split(':')[1];
|
||||
if (imageUrlObject.imageTag) {
|
||||
throw new Error(
|
||||
`imageUrl ${imageUrlObject.imageUrl} can't be tagged with ${
|
||||
imageUrlObject.imageTag
|
||||
} because it is already tagged with ${imageTag}`
|
||||
);
|
||||
} else {
|
||||
imageUrlObject.imageTag = imageTag;
|
||||
}
|
||||
} else if (!imageUrlObject.imageTag) {
|
||||
imageUrlObject.imageTag = 'latest';
|
||||
}
|
||||
imageUrlObject.imageOriginTag = `${imageUrlObject.imageUrl}:${imageUrlObject.imageTag}`;
|
||||
|
||||
// lets actually create the image
|
||||
const response = await dockerHostArg.request(
|
||||
'POST',
|
||||
`/images/create?fromImage=${encodeURIComponent(
|
||||
imageUrlObject.imageUrl
|
||||
)}&tag=${encodeURIComponent(imageUrlObject.imageTag)}`
|
||||
);
|
||||
if (response.statusCode < 300) {
|
||||
plugins.smartlog.defaultLogger.log(
|
||||
'info',
|
||||
`Successfully pulled image ${imageUrlObject.imageUrl} from the registry`
|
||||
);
|
||||
const image = await DockerImage.findImageByName(dockerHostArg, imageUrlObject.imageOriginTag);
|
||||
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
|
||||
*/
|
||||
public 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;
|
||||
|
||||
/**
|
||||
* returns a boolean wether the image has a upstream image
|
||||
*/
|
||||
public isUpstreamImage (): boolean {
|
||||
// TODO: implement isUpastreamImage
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public async pullLatestImageFromRegistry() : Promise<boolean> {
|
||||
// TODO: implement pullLatestImageFromRegistry
|
||||
return true;
|
||||
constructor(dockerHostArg, dockerImageObjectArg: any) {
|
||||
this.dockerHost = dockerHostArg;
|
||||
Object.keys(dockerImageObjectArg).forEach(keyArg => {
|
||||
this[keyArg] = dockerImageObjectArg[keyArg];
|
||||
});
|
||||
}
|
||||
|
||||
public tagImage(newTag) {}
|
||||
|
||||
/**
|
||||
* pulls the latest version from the registry
|
||||
*/
|
||||
public async pullLatestImageFromRegistry(): Promise<boolean> {
|
||||
const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
|
||||
imageUrl: this.RepoTags[0]
|
||||
});
|
||||
Object.assign(this, updatedImage);
|
||||
// TODO: Compare image digists before and after
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,94 @@
|
||||
import * as plugins from './dockersock.plugins';
|
||||
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}`);
|
||||
}
|
||||
}
|
@ -1,8 +1,63 @@
|
||||
import * as plugins from './dockersock.plugins';
|
||||
import * as plugins from './docker.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { DockerHost } from './docker.classes.host';
|
||||
import { DockerImage } from './docker.classes.image';
|
||||
|
||||
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
|
||||
) {
|
||||
// lets get the image
|
||||
plugins.smartlog.defaultLogger.log('info', `downloading image for service ${serviceCreationDescriptor.Name}`);
|
||||
const serviceImage = await DockerImage.createFromRegistry(dockerHost, {
|
||||
imageUrl: serviceCreationDescriptor.Image
|
||||
});
|
||||
|
||||
const networkArray: any[] = [];
|
||||
for (const network of serviceCreationDescriptor.networks) {
|
||||
networkArray.push({
|
||||
Target: network.Name,
|
||||
Aliases: [serviceCreationDescriptor.networkAlias]
|
||||
});
|
||||
}
|
||||
|
||||
dockerHost.request('POST', '/services/create', {
|
||||
Name: serviceCreationDescriptor.Name,
|
||||
TaskTemplate: {
|
||||
ContainerSpec: {
|
||||
Image: serviceCreationDescriptor.Image,
|
||||
Labels: serviceCreationDescriptor.Labels
|
||||
}
|
||||
},
|
||||
Labels: serviceCreationDescriptor.Labels,
|
||||
Networks: networkArray
|
||||
});
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public dockerHost: DockerHost;
|
||||
|
||||
constructor(dockerHostArg: DockerHost, serviceObject) {
|
||||
this.dockerHost = dockerHostArg;
|
||||
Object.assign(this, serviceObject);
|
||||
}
|
||||
|
||||
update() {
|
||||
|
||||
}
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
// @pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { lik, smartlog, smartpromise, smartrequest };
|
||||
smartlog.defaultLogger.enableConsole();
|
||||
|
||||
export { lik, smartlog, smartnetwork, smartpromise, smartrequest };
|
||||
|
||||
// third party
|
||||
import * as rxjs from 'rxjs';
|
@ -1,7 +1,7 @@
|
||||
import { DockerNetwork } from '../docker.classes.network';
|
||||
|
||||
export interface IContainerCreationSpecifier {
|
||||
hostname: string;
|
||||
domainName: string;
|
||||
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;
|
||||
imageTag?: string;
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
export * from './container';
|
||||
export * from './image';
|
||||
export * from './label';
|
||||
export * from './network';
|
||||
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;
|
||||
}
|
10
ts/interfaces/service.ts
Normal file
10
ts/interfaces/service.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import * as interfaces from './';
|
||||
import { DockerNetwork } from '../docker.classes.network';
|
||||
|
||||
export interface IServiceCreationDescriptor {
|
||||
Name: string;
|
||||
Image: string;
|
||||
Labels: interfaces.TLabels;
|
||||
networks: DockerNetwork[];
|
||||
networkAlias: string;
|
||||
}
|
Reference in New Issue
Block a user