Compare commits

...

121 Commits

Author SHA1 Message Date
b182a379af 1.0.67 2019-09-13 22:37:38 +02:00
5c6c06dee6 fix(core): update 2019-09-13 22:37:38 +02:00
a48e1e035e 1.0.66 2019-09-13 22:31:03 +02:00
8836c06b56 fix(core): update 2019-09-13 22:31:03 +02:00
7af8e0739b 1.0.65 2019-09-13 22:09:35 +02:00
684185e951 fix(core): update 2019-09-13 22:09:35 +02:00
21e6fff3fb 1.0.64 2019-09-13 18:20:12 +02:00
83c49a6234 fix(core): update 2019-09-13 18:20:12 +02:00
ad67849d45 1.0.63 2019-09-13 18:16:22 +02:00
0e4e07a912 fix(core): update 2019-09-13 18:16:21 +02:00
1fbc09f557 1.0.62 2019-09-13 18:15:46 +02:00
d6201b864c fix(core): update 2019-09-13 18:15:45 +02:00
ea5e552192 1.0.61 2019-09-13 17:54:18 +02:00
1afe5c6e16 fix(core): update 2019-09-13 17:54:17 +02:00
eb0dc96dbd 1.0.60 2019-09-13 16:57:22 +02:00
55f45b1c3a fix(core): update 2019-09-13 16:57:21 +02:00
87ff0f01bb 1.0.59 2019-09-13 14:45:35 +02:00
dd1939d7b2 fix(core): update 2019-09-13 14:45:35 +02:00
5a2a5f1248 1.0.58 2019-09-13 14:40:38 +02:00
9767b8767a fix(core): update 2019-09-13 14:40:38 +02:00
546e139b46 1.0.57 2019-09-13 13:20:01 +02:00
28d70bb49f fix(core): update 2019-09-13 13:20:01 +02:00
b71f134abd 1.0.56 2019-09-12 14:52:56 +02:00
968b3c7449 fix(core): update 2019-09-12 14:52:55 +02:00
d9558f7843 1.0.55 2019-09-12 14:45:36 +02:00
19e67ffdcc fix(core): update 2019-09-12 14:45:36 +02:00
e983b66c28 1.0.54 2019-09-11 20:25:46 +02:00
c79f6a698f fix(core): update 2019-09-11 20:25:45 +02:00
0ef098e9c8 1.0.53 2019-09-08 19:22:20 +02:00
d56350ff28 fix(core): update 2019-09-08 19:22:20 +02:00
a4dc4e7950 1.0.52 2019-09-08 16:34:26 +02:00
424e911804 fix(core): update 2019-09-08 16:34:26 +02:00
b5c4727bae 1.0.51 2019-08-16 21:45:22 +02:00
b6f3fbf8a9 fix(core): update 2019-08-16 21:45:21 +02:00
7241e7a8fd 1.0.50 2019-08-16 21:34:36 +02:00
ae37148ece fix(core): update 2019-08-16 21:34:35 +02:00
65c37bdd6f 1.0.49 2019-08-16 21:21:31 +02:00
6acbe30e2e fix(core): update 2019-08-16 21:21:30 +02:00
eb6f7889d0 1.0.48 2019-08-16 21:10:03 +02:00
e39da5fee9 fix(core): update 2019-08-16 21:10:03 +02:00
b07628bb0b 1.0.47 2019-08-16 21:07:59 +02:00
5815f9b202 fix(core): update 2019-08-16 21:07:59 +02:00
846ea9997e 1.0.46 2019-08-16 18:32:42 +02:00
de54db33ad fix(core): update 2019-08-16 18:32:41 +02:00
314cb692ac 1.0.45 2019-08-16 18:21:55 +02:00
73f8ded3fe fix(core): update 2019-08-16 18:21:55 +02:00
a28b10ac51 1.0.44 2019-08-16 14:46:48 +02:00
927e2e0acc fix(core): update 2019-08-16 14:46:48 +02:00
c496405818 1.0.43 2019-08-16 12:48:57 +02:00
020737e21b fix(core): update 2019-08-16 12:48:56 +02:00
fe3560caac 1.0.42 2019-08-16 12:48:40 +02:00
b2a7e67868 fix(core): update 2019-08-16 12:48:40 +02:00
f772ca15ef 1.0.41 2019-08-15 19:04:10 +02:00
71cfad146f fix(core): update 2019-08-15 19:04:09 +02:00
43b1c13256 1.0.40 2019-08-15 19:02:44 +02:00
2c8b17f029 fix(core): update 2019-08-15 19:02:43 +02:00
c6521d9160 1.0.39 2019-08-15 19:00:17 +02:00
72c74e44b5 fix(core): update 2019-08-15 19:00:17 +02:00
2fb628213d 1.0.38 2019-08-15 18:50:13 +02:00
373a4e2eac fix(core): update 2019-08-15 18:50:13 +02:00
a202d05e9c 1.0.37 2019-08-14 23:21:54 +02:00
6e97a7d83c fix(core): update 2019-08-14 23:21:54 +02:00
04bb3b9ed0 1.0.36 2019-08-14 21:02:06 +02:00
29e502a32e fix(core): update 2019-08-14 21:02:06 +02:00
75a118183e 1.0.35 2019-08-14 20:58:10 +02:00
aa1fe594e8 fix(core): update 2019-08-14 20:58:10 +02:00
cf761e19bf 1.0.34 2019-08-14 20:57:43 +02:00
af7590c8de fix(core): update 2019-08-14 20:57:43 +02:00
ee2e4fb856 1.0.33 2019-08-14 20:56:57 +02:00
ff5cecb07e fix(core): update 2019-08-14 20:56:57 +02:00
e50b028c00 1.0.32 2019-08-14 14:19:46 +02:00
867dda8e7c fix(core): update 2019-08-14 14:19:45 +02:00
d12d595f21 1.0.31 2019-01-18 02:42:13 +01:00
6117228ea4 fix(core): update 2019-01-18 02:42:13 +01:00
b6b584f808 1.0.30 2019-01-10 00:43:46 +01:00
48fe6ce0a8 fix(core): update 2019-01-10 00:43:45 +01:00
55acb39071 1.0.29 2019-01-10 00:37:32 +01:00
f7b17a4684 fix(core): update 2019-01-10 00:37:32 +01:00
d95f6c8e7f 1.0.28 2019-01-10 00:31:49 +01:00
001cdadb6b fix(core): update 2019-01-10 00:31:48 +01:00
82655623d1 1.0.27 2019-01-10 00:28:13 +01:00
ea117bc391 fix(core): update 2019-01-10 00:28:12 +01:00
57594728df 1.0.26 2019-01-10 00:24:35 +01:00
8430a4d6e0 fix(core): update 2019-01-10 00:24:35 +01:00
7f38868510 1.0.25 2018-07-17 08:39:38 +02:00
7a80718d27 fix(core): update 2018-07-17 08:39:37 +02:00
1d0c4c3cf9 1.0.24 2018-07-16 23:52:50 +02:00
a037cba454 fix(core): shift to new style 2018-07-16 23:52:50 +02:00
7009e07ecc 1.0.23 2017-07-16 13:45:08 +02:00
7b8ef33e21 update creation of node_modules within npmdocker 2017-07-16 13:44:59 +02:00
3011e599e8 1.0.22 2017-07-16 12:04:53 +02:00
8adb636db3 1.0.21 2017-07-16 12:04:38 +02:00
dd35e5d7d2 update 2017-07-16 12:04:35 +02:00
91d7f583e2 1.0.20 2017-04-02 15:30:58 +02:00
a77ed42e9f now working with npmdocker and the npmts 7.x.x 2017-04-02 15:30:43 +02:00
499c6cc3b7 update ci 2017-04-02 12:12:18 +02:00
19283fb695 update ci 2017-04-02 12:11:21 +02:00
b2d84b1399 update npmextra to have npmts and npmdocker available in ci 2017-04-02 12:08:36 +02:00
7cc2374130 improve README 2017-04-02 12:05:29 +02:00
526bb5bbd0 update 2017-03-26 13:02:20 +02:00
14b2105a49 1.0.19 2016-08-03 12:21:26 +02:00
37efeffe69 update dependencies 2016-08-03 12:21:07 +02:00
943f46d620 update type versions 2016-08-03 12:19:54 +02:00
cfb7c1ce06 1.0.18 2016-07-31 23:07:14 +02:00
489a47b9bf now waiting for response to be stored before ending streaming request 2016-07-31 23:07:09 +02:00
28fa5349d7 cosmetic fix 2016-07-31 22:47:22 +02:00
cc82b3af04 1.0.17 2016-07-31 21:05:57 +02:00
6a8751afd2 now has possibility to end streaming requests 2016-07-31 21:05:41 +02:00
56612f9ac9 1.0.16 2016-07-29 20:41:30 +02:00
2a1fcd8cdb fixed request for change observable 2016-07-29 20:41:27 +02:00
09911328b2 1.0.15 2016-07-28 19:41:27 +02:00
548e32419e improve README 2016-07-28 19:41:14 +02:00
2b2b06b48b Merge branch 'master' of gitlab.com:pushrocks/dockersock 2016-07-28 19:38:40 +02:00
37028a9b1f add npmdocker 2016-07-28 19:38:26 +02:00
3cba5844d8 fix dep versions 2016-07-18 04:07:52 +02:00
a3ceb68eab add request typings 2016-07-18 04:06:56 +02:00
be659a2dbd 1.0.14 2016-07-18 02:50:34 +02:00
144f963d8a fixed request for newer docker 2016-07-18 02:50:28 +02:00
0c7cd35448 prepare for npmdocker 2016-07-18 01:35:04 +02:00
4e3446cb2f now encoding URI 2016-07-18 00:54:41 +02:00
cec9555908 update 2016-07-17 23:09:54 +02:00
39 changed files with 3350 additions and 595 deletions

21
.gitignore vendored
View File

@ -1,3 +1,22 @@
docs/
.nogit/
# artifacts
coverage/
public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,53 +1,119 @@
image: hosttoday/ht-docker-dbase
services:
- docker:dind
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- build
- test
- release
- trigger
- security
- test
- release
- metadata
before_script:
- npmci prepare docker-gitlab
build:
stage: build
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npm install
- npm test
- npmci build docker
- npmci git mirror
tags:
- lossless
- priv
- docker
- notpriv
test:
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# test stage
# ====================
testStable:
stage: test
script:
- npmci test docker
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- priv
- docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
image: hosttoday/ht-docker-node:npmts
stage: release
script:
- npmci publish npm
- npmci node install stable
- npmci npm publish
only:
- tags
- tags
tags:
- lossless
- priv
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
trigger:
stage: trigger
stage: metadata
script:
- npmci trigger
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:stable-dind
stage: metadata
script:
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- docker
- notpriv
only:
- tags
tags:
- lossless
- priv
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

29
.vscode/launch.json vendored Normal file
View 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"
}
]
}

View File

@ -1,6 +0,0 @@
FROM hosttoday/ht-docker-node:npmts
RUN mkdir app-node
COPY ./ /app-node/
WORKDIR /app-node
ENV CI true
CMD ["npmts"]

View File

@ -1,28 +1,38 @@
# dockersock
easy communication with docker from node, TypeScript ready
# @mojoio/docker
unofficial docker engine api abstraction package written in TypeScript
## Status
[![build status](https://gitlab.com/pushrocks/dockersock/badges/master/build.svg)](https://gitlab.com/pushrocks/dockersock/commits/master)
## 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
[![build status](https://gitlab.com/mojoio/docker/badges/master/build.svg)](https://gitlab.com/mojoio/docker/commits/master)
[![coverage report](https://gitlab.com/mojoio/docker/badges/master/coverage.svg)](https://gitlab.com/mojoio/docker/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@mojoio/docker.svg)](https://www.npmjs.com/package/@mojoio/docker)
[![Known Vulnerabilities](https://snyk.io/test/npm/@mojoio/docker/badge.svg)](https://snyk.io/test/npm/@mojoio/docker)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
We recommend the use of TypeScript for best Intellisense.
```TypeScript
import {Dockersock} from "dockersock"; // require Dockersock class
Use TypeScript for best in class instellisense.
let myDockersock = new Dockersock(); // optional: you can pass a domain to the contructor, defaults to /var/run/docker.sock
```typescript
import { DockerHost } from '@mojoio/docker'; // require Dockersock class
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
const run = async () => {
const myDockerHost = new DockerHost(); // optional: you can pass a domain to the contructor, defaults to /var/run/docker.sock
myDockersock.startContainer({ // starts a already present container
name: "somecontainername"
})
const containers = await myDockerHost.getContainers(); // promise, resolve with an array of DockerContainers
};
```
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.
```
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -1,26 +0,0 @@
/// <reference types="q" />
import "typings-global";
import * as plugins from "./dockersock.plugins";
import { Observable } from "rxjs";
export declare class Dockersock {
sockPath: string;
constructor(pathArg?: string);
auth(userArg: string, passArg: string): plugins.q.Promise<{}>;
listContainers(): plugins.q.Promise<{}>;
listContainersDetailed(): plugins.q.Promise<{}>;
listContainersRunning(): plugins.q.Promise<{}>;
listContainersStopped(): plugins.q.Promise<{}>;
listImages(): plugins.q.Promise<{}>;
listImagesDangling(): plugins.q.Promise<{}>;
pullImage(imageLabel: string): plugins.q.Promise<{}>;
createContainer(optionsArg: any, pullFirstArg?: boolean): plugins.q.Promise<{}>;
getContainerId(): void;
startContainer(containerNameArg: any): plugins.q.Promise<{}>;
stopContainer(containerNameArg: any): plugins.q.Promise<{}>;
removeContainer(containerNameArg: any): plugins.q.Promise<{}>;
clean(): plugins.q.Promise<{}>;
callOnChange(cb: Function): void;
getChangeObservable(): Observable<{}>;
request(methodArg: string, routeArg: string, queryArg?: string, dataArg?: {}): plugins.q.Promise<{}>;
requestStream(methodArg: any, routeArg: any, endArg?: boolean): plugins.q.Promise<{}>;
}

File diff suppressed because one or more lines are too long

View File

@ -1,5 +0,0 @@
import "typings-global";
export import beautylog = require("beautylog");
export import q = require("q");
export declare let request: any;
export import rxjs = require("rxjs");

View File

@ -1,7 +0,0 @@
"use strict";
require("typings-global");
exports.beautylog = require("beautylog");
exports.q = require("q");
exports.request = require("request");
exports.rxjs = require("rxjs");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9ja2Vyc29jay5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZG9ja2Vyc29jay5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFDVixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2pDLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNwQixlQUFPLEdBQUcsT0FBTyxDQUFDLFNBQVMsQ0FBQyxDQUFDO0FBQzFCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQyJ9

2
dist/index.d.ts vendored
View File

@ -1,2 +0,0 @@
import "typings-global";
export { Dockersock } from "./dockersock.classes.dockersock";

5
dist/index.js vendored
View File

@ -1,5 +0,0 @@
"use strict";
require("typings-global");
var dockersock_classes_dockersock_1 = require("./dockersock.classes.dockersock");
exports.Dockersock = dockersock_classes_dockersock_1.Dockersock;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBQ3hCLDhDQUF5QixpQ0FBaUMsQ0FBQztBQUFuRCxnRUFBa0QifQ==

View File

@ -1,18 +1,18 @@
nginx1 :
image: "nginx"
environment:
VIRTUAL_HOST: "test100.labkomp.de"
VIRTUAL_HOST: "test100.bleu.de"
restart: always
container_name: nginx1
nginx2:
image: nginx
environment:
VIRTUAL_HOST: "test101.labkomp.de"
VIRTUAL_HOST: "test101.bleu.de"
restart: always
container_name: nginx2
npmts-test-container:
image: "npmts-test-image"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
nginx3:
image: nginx
environment:
VIRTUAL_HOST: "test101.bleu.de"
restart: always
container_name: npmts-test-container
container_name: nginx3

22
npmextra.json Normal file
View File

@ -0,0 +1,22 @@
{
"npmdocker": {
"baseImage": "hosttoday/ht-docker-node:npmci",
"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"
}
}
}

View File

@ -1,4 +0,0 @@
{
"mode":"default",
"coverageTreshold":10
}

2057
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,13 @@
{
"name": "dockersock",
"version": "1.0.13",
"description": "easy communication with docker from node, TypeScript ready",
"name": "@mojoio/docker",
"version": "1.0.67",
"description": "easy communication with docker remote api from node, TypeScript ready",
"private": false,
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "npmts --notest && npm run build && npm run startdocker && npm run cleanup",
"build": "docker build -t npmts-test-image .",
"startdocker": "docker run -v /var/run/docker.sock:/var/run/docker.sock --name npmts-test-container npmts-test-image",
"cleanup": "docker rm npmts-test-container && docker rmi npmts-test-image"
"test": "tstest test/",
"build": "tsbuild"
},
"repository": {
"type": "git",
@ -28,16 +27,36 @@
},
"homepage": "https://gitlab.com/pushrocks/dockersock#README",
"dependencies": {
"@types/q": "*",
"beautylog": "^5.0.14",
"q": "^1.4.1",
"request": "^2.73.0",
"rxjs": "^5.0.0-beta.10",
"typings-global": "^1.0.6"
"@pushrocks/lik": "^3.0.11",
"@pushrocks/smartfile": "^7.0.4",
"@pushrocks/smartjson": "^3.0.8",
"@pushrocks/smartlog": "^2.0.19",
"@pushrocks/smartnetwork": "^1.1.14",
"@pushrocks/smartpath": "^4.0.1",
"@pushrocks/smartpromise": "^3.0.2",
"@pushrocks/smartrequest": "^1.1.26",
"@pushrocks/smartstring": "^3.0.10",
"@pushrocks/smartversion": "^2.0.4",
"rxjs": "^6.5.3"
},
"devDependencies": {
"npmts-g": "^5.2.6",
"should": "^9.0.2",
"typings-test": "^1.0.1"
}
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24",
"@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"
]
}

11
scripts/testauth.ts Normal file
View File

@ -0,0 +1,11 @@
import * as docker from '../ts';
import * as smartstring from '@pushrocks/smartstring';
const run = async () => {
const dockerHost = new docker.DockerHost();
await docker.DockerImage.createFromRegistry(dockerHost, {
imageUrl: 'registry.gitlab.com/servezone/private/cloudly:latest'
});
};
run();

2
test/test.d.ts vendored
View File

@ -1,2 +0,0 @@
import "typings-test";
import "should";

View File

@ -1,36 +0,0 @@
"use strict";
require("typings-test");
require("should");
const index_1 = require("../dist/index");
describe("dockersock", function () {
describe(".Dockersock()", function () {
let testDockersock;
it("should create a new Dockersock instance", function () {
testDockersock = new index_1.Dockersock();
testDockersock.should.be.instanceof(index_1.Dockersock);
});
it("should list containers", function (done) {
testDockersock.listContainers()
.then((dataArg) => {
console.log(dataArg);
done();
});
});
it("should list detailed containers", function (done) {
this.timeout(5000);
testDockersock.listContainersDetailed()
.then((dataArg) => {
console.log(dataArg);
done();
});
});
it("should pull an image from imagetag", function (done) {
this.timeout(60000);
testDockersock.pullImage("hosttoday%2Fht-docker-dbase")
.then((dataArg) => {
done();
}, done);
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQix3QkFBeUIsZUFFekIsQ0FBQyxDQUZ1QztBQUV4QyxRQUFRLENBQUMsWUFBWSxFQUFDO0lBQ2xCLFFBQVEsQ0FBQyxlQUFlLEVBQUM7UUFDckIsSUFBSSxjQUF5QixDQUFDO1FBQzlCLEVBQUUsQ0FBQyx5Q0FBeUMsRUFBQztZQUN6QyxjQUFjLEdBQUcsSUFBSSxrQkFBVSxFQUFFLENBQUM7WUFDbEMsY0FBYyxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLGtCQUFVLENBQUMsQ0FBQztRQUNwRCxDQUFDLENBQUMsQ0FBQztRQUNILEVBQUUsQ0FBQyx3QkFBd0IsRUFBQyxVQUFTLElBQUk7WUFDckMsY0FBYyxDQUFDLGNBQWMsRUFBRTtpQkFDMUIsSUFBSSxDQUFDLENBQUMsT0FBTztnQkFDVixPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDO2dCQUNyQixJQUFJLEVBQUUsQ0FBQztZQUNYLENBQUMsQ0FBQyxDQUFDO1FBQ1gsQ0FBQyxDQUFDLENBQUM7UUFDSCxFQUFFLENBQUMsaUNBQWlDLEVBQUMsVUFBUyxJQUFJO1lBQzlDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDbkIsY0FBYyxDQUFDLHNCQUFzQixFQUFFO2lCQUNsQyxJQUFJLENBQUMsQ0FBQyxPQUFPO2dCQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsT0FBTyxDQUFDLENBQUM7Z0JBQ3JCLElBQUksRUFBRSxDQUFDO1lBQ1gsQ0FBQyxDQUFDLENBQUM7UUFDWCxDQUFDLENBQUMsQ0FBQztRQUNILEVBQUUsQ0FBQyxvQ0FBb0MsRUFBQyxVQUFTLElBQUk7WUFDakQsSUFBSSxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQztZQUNwQixjQUFjLENBQUMsU0FBUyxDQUFDLDZCQUE2QixDQUFDO2lCQUNsRCxJQUFJLENBQUMsQ0FBQyxPQUFPO2dCQUNWLElBQUksRUFBRSxDQUFDO1lBQ1gsQ0FBQyxFQUFDLElBQUksQ0FBQyxDQUFDO1FBQ2hCLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUM7QUFDUCxDQUFDLENBQUMsQ0FBQyJ9

View File

@ -1,36 +1,118 @@
import "typings-test";
import "should";
import { expect, tap } from '@pushrocks/tapbundle';
import * as docker from '../ts/index';
import {Dockersock} from "../dist/index"
let testDockerHost: docker.DockerHost;
describe("dockersock",function(){
describe(".Dockersock()",function(){
let testDockersock:Dockersock;
it("should create a new Dockersock instance",function(){
testDockersock = new Dockersock();
testDockersock.should.be.instanceof(Dockersock);
});
it("should list containers",function(done){
testDockersock.listContainers()
.then((dataArg)=>{
console.log(dataArg);
done();
});
});
it("should list detailed containers",function(done){
this.timeout(5000);
testDockersock.listContainersDetailed()
.then((dataArg)=>{
console.log(dataArg);
done();
});
});
it("should pull an image from imagetag",function(done){
this.timeout(60000);
testDockersock.pullImage("hosttoday%2Fht-docker-dbase")
.then((dataArg)=>{
done();
},done);
})
});
});
tap.test('should create a new Dockersock instance', async () => {
testDockerHost = new docker.DockerHost('http://unix:/var/run/docker.sock:');
return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
});
tap.test('should create a docker swarm', async () => {
await testDockerHost.activateSwarm();
});
// Containers
tap.test('should list containers', async () => {
const containers = await testDockerHost.getContainers();
console.log(containers);
});
// Networks
tap.test('should list networks', async () => {
const networks = await testDockerHost.getNetworks();
console.log(networks);
});
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);
});
await tools.delayFor(2000);
subscription.unsubscribe();
});
// SECRETS
tap.test('should create a secret', async () => {
const mySecret = await docker.DockerSecret.createSecret(testDockerHost, {
name: 'testSecret',
version: '1.0.3',
contentArg: `{ "hi": "wow"}`,
labels: {}
});
console.log(mySecret);
});
tap.test('should remove a secret by name', async () => {
const mySecret = await docker.DockerSecret.getSecretByName(testDockerHost, 'testSecret');
await mySecret.remove();
});
// SERVICES
tap.test('should activate swarm mode', async () => {
await testDockerHost.activateSwarm();
});
tap.test('should list all services', async tools => {
const services = await testDockerHost.getServices();
console.log(services);
});
tap.test('should create a service', async () => {
const testNetwork = await docker.DockerNetwork.createNetwork(testDockerHost, {
Name: 'testNetwork'
});
const testSecret = await docker.DockerSecret.createSecret(testDockerHost, {
name: 'serviceSecret',
version: '0.0.1',
labels: {},
contentArg: '{"hi": "wow"}'
});
const testImage = await docker.DockerImage.createFromRegistry(testDockerHost, {
imageUrl: 'nginx:latest'
});
const testService = await docker.DockerService.createService(testDockerHost, {
image: testImage,
labels: {
testlabel: 'hi'
},
name: 'testService',
networks: [testNetwork],
networkAlias: 'testService',
secrets: [testSecret],
ports: []
});
await testSecret.update(`{"updated": "socool"}`);
await testService.update();
await testService.remove();
await testNetwork.remove();
await testSecret.remove();
});
tap.start();

View File

@ -0,0 +1,101 @@
import * as plugins from './docker.plugins';
import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host';
export class DockerContainer {
// STATIC
/**
* get all containers
*/
public static async getContainers(dockerHostArg: DockerHost): Promise<DockerContainer[]> {
const result: DockerContainer[] = [];
const response = await dockerHostArg.request('GET', '/containers/json');
// TODO: Think about getting the config by inpsecting the container
for (const containerResult of response.body) {
result.push(new DockerContainer(dockerHostArg, containerResult));
}
return result;
}
/**
* gets an container by Id
* @param containerId
*/
public static async getContainerById(containerId: string) {
// TODO: implement get container by id
}
/**
* create a container
*/
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'
);
}
}
// 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;
Links: any;
Aliases: any;
NetworkID: string;
EndpointID: string;
Gateway: string;
IPAddress: string;
IPPrefixLen: number;
IPv6Gateway: string;
GlobalIPv6Address: string;
GlobalIPv6PrefixLen: number;
MacAddress: string;
DriverOpts: any;
};
};
};
public Mounts: any;
constructor(dockerHostArg: DockerHost, dockerContainerObjectArg: any) {
this.dockerHost = dockerHostArg;
Object.keys(dockerContainerObjectArg).forEach(keyArg => {
this[keyArg] = dockerContainerObjectArg[keyArg];
});
}
}

188
ts/docker.classes.host.ts Normal file
View File

@ -0,0 +1,188 @@
import * as plugins from './docker.plugins';
import { DockerContainer } from './docker.classes.container';
import { DockerNetwork } from './docker.classes.network';
import { DockerService } from './docker.classes.service';
export interface IAuthData {
serveraddress: string;
username: string;
password: string;
}
export class DockerHost {
/**
* the path where the docker sock can be found
*/
public socketPath: string;
private registryToken: string = '';
/**
* the constructor to instantiate a new docker sock instance
* @param 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;
}
/**
* authenticate against a registry
* @param userArg
* @param passArg
*/
public async auth(authData: IAuthData) {
const response = await this.request('POST', '/auth', authData);
if (response.body.Status !== 'Login Succeeded') {
console.log(`Login failed with ${response.body.Status}`);
throw new Error(response.body.Status);
}
console.log(response.body.Status);
this.registryToken = plugins.smartstring.base64.encode(
plugins.smartjson.Smartjson.stringify(authData, {})
);
}
/**
* gets the token from the .docker/config.json file for GitLab registry
*/
public async getGitlabComTokenFromDockerConfig() {
const dockerConfigPath = plugins.smartpath.get.home('~/.docker/config.json');
const configObject = plugins.smartfile.fs.toObjectSync(dockerConfigPath);
const gitlabAuthBase64 = configObject.auths['registry.gitlab.com'].auth;
const gitlabAuth: string = plugins.smartstring.base64.decode(gitlabAuthBase64);
const gitlabAuthArray = gitlabAuth.split(':');
await this.auth({
username: gitlabAuthArray[0],
password: gitlabAuthArray[1],
serveraddress: 'registry.gitlab.com'
})
}
/**
* 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;
}
/**
* gets all services
*/
public async getServices() {
const serviceArray = await DockerService.getServices(this);
return serviceArray;
}
/**
*
*/
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 => {
const eventString = data.toString();
try {
const eventObject = JSON.parse(eventString);
observer.next(eventObject);
} catch (e) {
console.log(e);
}
});
return () => {
response.emit('end');
};
});
}
/**
* 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
*/
public async request(methodArg: string, routeArg: string, dataArg = {}) {
const requestUrl = `${this.socketPath}${routeArg}`;
const response = await plugins.smartrequest.request(requestUrl, {
method: methodArg,
headers: {
'Content-Type': 'application/json',
'X-Registry-Auth': this.registryToken,
Host: 'docker.sock'
},
requestBody: dataArg,
keepAlive: false
});
if (response.statusCode !== 200) {
console.log(response.body);
}
return response;
}
public async requestStreaming(methodArg: string, routeArg: string, dataArg = {}) {
const requestUrl = `${this.socketPath}${routeArg}`;
const response = await plugins.smartrequest.request(
requestUrl,
{
method: methodArg,
headers: {
'Content-Type': 'application/json',
'X-Registry-Auth': this.registryToken,
Host: 'docker.sock'
},
requestBody: null,
keepAlive: false
},
true
);
console.log(response.statusCode);
console.log(response.body);
return response;
}
}

142
ts/docker.classes.image.ts Normal file
View File

@ -0,0 +1,142 @@
import * as plugins from './docker.plugins';
import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host';
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);
const result = images.find(image => {
if (image.RepoTags) {
return image.RepoTags.includes(imageNameArg);
} else {
return false;
}
});
return result;
}
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.imageUrl = imageUrl;
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 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;
constructor(dockerHostArg, dockerImageObjectArg: any) {
this.dockerHost = dockerHostArg;
Object.keys(dockerImageObjectArg).forEach(keyArg => {
this[keyArg] = dockerImageObjectArg[keyArg];
});
}
/**
* tag an image
* @param newTag
*/
public async tagImage(newTag) {
throw new Error('.tagImage is not yet implemented');
}
/**
* 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;
}
// get stuff
public async getVersion() {
return this.Labels.version;
}
}

View File

@ -0,0 +1,93 @@
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) {
const dockerNetwork = new DockerNetwork(dockerHost);
Object.assign(dockerNetwork, networkObject);
dockerNetworks.push(dockerNetwork);
}
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) {
this.dockerHost = dockerHostArg;
}
/**
* removes the network
*/
public async remove() {
const response = await this.dockerHost.request('DELETE', `/networks/${this.Id}`);
}
}

View File

@ -0,0 +1,92 @@
import * as plugins from './docker.plugins';
import { DockerHost } from './docker.classes.host';
// interfaces
import * as interfaces from './interfaces';
export class DockerSecret {
// STATIC
public static async getSecrets(dockerHostArg: DockerHost) {
const response = await dockerHostArg.request('GET', '/secrets');
const secrets: DockerSecret[] = [];
for (const secret of response.body) {
const dockerSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(dockerSecretInstance, secret);
secrets.push(dockerSecretInstance);
}
return secrets;
}
public static async getSecretByID(dockerHostArg: DockerHost, idArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.ID === idArg);
}
public static async getSecretByName(dockerHostArg: DockerHost, nameArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.Spec.Name === nameArg);
}
public static async createSecret(
dockerHostArg: DockerHost,
secretDescriptor: interfaces.ISecretCreationDescriptor
) {
const labels: interfaces.TLabels = {
...secretDescriptor.labels,
version: secretDescriptor.version
};
const response = await dockerHostArg.request('POST', '/secrets/create', {
Name: secretDescriptor.name,
Labels: labels,
Data: plugins.smartstring.base64.encode(secretDescriptor.contentArg)
});
const newSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(newSecretInstance, response.body);
Object.assign(
newSecretInstance,
await DockerSecret.getSecretByID(dockerHostArg, newSecretInstance.ID)
);
return newSecretInstance;
}
// INSTANCE
public ID: string;
public Spec: {
Name: string;
Labels: interfaces.TLabels;
};
public Version: {
Index: string;
};
public dockerHost: DockerHost;
constructor(dockerHostArg: DockerHost) {
this.dockerHost = dockerHostArg;
}
/**
* updates a secret
*/
public async update(contentArg: string) {
const route = `/secrets/${this.ID}/update?=version=${this.Version.Index}`;
const response = await this.dockerHost.request(
'POST',
`/secrets/${this.ID}/update?version=${this.Version.Index}`,
{
Name: this.Spec.Name,
Labels: this.Spec.Labels,
Data: plugins.smartstring.base64.encode(contentArg)
}
);
}
public async remove() {
await this.dockerHost.request('DELETE', `/secrets/${this.ID}`);
}
// get things
public async getVersion() {
return this.Spec.Labels.version;
}
}

View File

@ -0,0 +1,195 @@
import * as plugins from './docker.plugins';
import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host';
import { DockerImage } from './docker.classes.image';
import { DockerSecret } from './docker.classes.secret';
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) {
const dockerService = new DockerService(dockerHost);
Object.assign(dockerService, serviceObject);
services.push(dockerService);
}
return services;
}
public static async getServiceByName(
dockerHost: DockerHost,
networkName: string
): Promise<DockerService> {
const allServices = await DockerService.getServices(dockerHost);
const wantedService = allServices.find(service => {
return service.Spec.Name === networkName;
});
return wantedService;
}
/**
* creates a service
*/
public static async createService(
dockerHost: DockerHost,
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
): Promise<DockerService> {
// lets get the image
plugins.smartlog.defaultLogger.log(
'info',
`now creating service ${serviceCreationDescriptor.name}`
);
// await serviceCreationDescriptor.image.pullLatestImageFromRegistry();
const serviceVersion = await serviceCreationDescriptor.image.getVersion();
const labels: interfaces.TLabels = {
...serviceCreationDescriptor.labels,
version: serviceVersion
};
const networkArray: any[] = [];
for (const network of serviceCreationDescriptor.networks) {
networkArray.push({
Target: network.Name,
Aliases: [serviceCreationDescriptor.networkAlias]
});
}
const ports = [];
const secretArray: any[] = [];
for (const secret of serviceCreationDescriptor.secrets) {
secretArray.push({
File: {
Name: 'secret.json',
UID: '33',
GID: '33',
Mode: 384
},
SecretID: secret.ID,
SecretName: secret.Spec.Name
});
}
const response = await dockerHost.request('POST', '/services/create', {
Name: serviceCreationDescriptor.name,
TaskTemplate: {
ContainerSpec: {
Image: serviceCreationDescriptor.image.RepoTags[0],
Labels: labels,
Secrets: secretArray
},
UpdateConfig: {
Parallelism: 0,
Delay: 0,
FailureAction: 'pause',
Monitor: 15000000000,
MaxFailureRatio: 0.15
},
ForceUpdate: 1
},
Labels: labels,
Networks: networkArray,
EndpointSpec: {
Ports: ports
}
});
const createdService = await DockerService.getServiceByName(
dockerHost,
serviceCreationDescriptor.name
);
return createdService;
}
// INSTANCE
public dockerHostRef: DockerHost;
public ID: string;
public Version: { Index: number };
public CreatedAt: string;
public UpdatedAt: string;
public Spec: {
Name: string;
Labels: interfaces.TLabels;
TaskTemplate: {
ContainerSpec: {
Image: string;
Isolation: string;
Secrets: Array<{
File: {
Name: string;
UID: string;
GID: string;
Mode: number;
};
SecretID: string;
SecretName: string;
}>;
};
ForceUpdate: 0;
};
Mode: {};
Networks: [any[]];
};
public Endpoint: { Spec: {}; VirtualIPs: [any[]] };
constructor(dockerHostArg: DockerHost) {
this.dockerHostRef = dockerHostArg;
}
public async update() {
const labels: interfaces.TLabels = {
...this.Spec.Labels,
version: 'x.x.x'
};
const dockerData = await this.dockerHostRef.request(
'POST',
`/services/${this.ID}/update?version=${this.Version.Index}`,
{
Name: this.Spec.Name,
TaskTemplate: this.Spec.TaskTemplate,
Labels: labels,
Networks: this.Spec.Networks
}
);
Object.assign(this, dockerData);
}
public async remove() {
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
}
public async reReadFromDockerEngine() {
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
// TODO: Better assign: Object.assign(this, dockerData);
}
public async needsUpdate(): Promise<boolean> {
// TODO: implement digest based update recognition
await this.reReadFromDockerEngine();
const dockerImage = await DockerImage.createFromRegistry(this.dockerHostRef, {
imageUrl: this.Spec.TaskTemplate.ContainerSpec.Image
});
const imageVersion = new plugins.smartversion.SmartVersion(dockerImage.Labels.version);
const serviceVersion = new plugins.smartversion.SmartVersion(this.Spec.Labels.version);
if (imageVersion.greaterThan(serviceVersion)) {
console.log(`service ${this.Spec.Name} needs to be updated`);
return true;
} else {
console.log(`service ${this.Spec.Name} is up to date.`);
}
}
public async updateFromRegistry() {
if (await this.needsUpdate()) {
this.update();
}
}
}

36
ts/docker.plugins.ts Normal file
View File

@ -0,0 +1,36 @@
// node native path
import * as path from 'path';
export { path };
// @pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartfile from '@pushrocks/smartfile';
import * as smartjson from '@pushrocks/smartjson';
import * as smartlog from '@pushrocks/smartlog';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartstring from '@pushrocks/smartstring';
import * as smartversion from '@pushrocks/smartversion';
smartlog.defaultLogger.enableConsole();
export {
lik,
smartfile,
smartjson,
smartlog,
smartnetwork,
smartpath,
smartpromise,
smartrequest,
smartstring,
smartversion
};
// third party
import * as rxjs from 'rxjs';
export { rxjs };

View File

@ -1,184 +0,0 @@
import "typings-global"
import * as plugins from "./dockersock.plugins";
import {Observable} from "rxjs";
export class Dockersock {
sockPath:string;
constructor(pathArg:string = "http://unix:/var/run/docker.sock:"){
this.sockPath = pathArg;
}
// methods
auth(userArg:string,passArg:string){
let done = plugins.q.defer();
this.request("POST","");
return done.promise;
}
listContainers() {
let done = plugins.q.defer();
this.request("GET","/containers")
.then(done.resolve);
return done.promise;
};
listContainersDetailed() {
let done = plugins.q.defer();
let detailedDataObject = [];
this.listContainers()
.then((dataArg) => {
let recursiveCounter = 0;
let makeDetailed = () => {
if(typeof dataArg[recursiveCounter] != "undefined"){
this.request("GET","/containers/" + dataArg[recursiveCounter].Id)
.then((dataArg2) => {
detailedDataObject.push(dataArg2);
recursiveCounter++;
// recursive call
makeDetailed();
});
} else {
done.resolve(detailedDataObject);
}
};
makeDetailed();
});
return done.promise;
};
listContainersRunning() {
let done = plugins.q.defer();
return done.promise;
}
listContainersStopped() {
let done = plugins.q.defer();
return done.promise;
}
listImages() {
return this.request("GET","/images","?all=true");
}
listImagesDangling(){
return this.request("GET","/images","?dangling=true");
}
pullImage(imageLabel:string){
return this.requestStream("POST","/images/create?fromImage=" + imageLabel);
};
createContainer(optionsArg,pullFirstArg:boolean = true){
let done = plugins.q.defer();
let create = () => {
return this.request("POST","/containers/create","",optionsArg);
}
if(pullFirstArg){
this.pullImage(optionsArg.Image)
.then(create)
.then(done.resolve);
} else {
create()
.then(done.resolve)
}
return done.promise;
};
getContainerId(){
};
startContainer(containerNameArg){
return this.request("POST","/containers/"+ containerNameArg +"/start");
};
stopContainer(containerNameArg){
return this.request("POST","/containers/"+ containerNameArg +"/stop");
};
removeContainer(containerNameArg){
return this.request("DELETE","/containers/" + containerNameArg + "?v=1");
};
clean() {
let done = plugins.q.defer();
return done.promise;
};
callOnChange(cb:Function){
let cbPromise;
let changeBuffered:boolean = false; // when cb is running then buffer any consequent change
let requestStream = plugins.request.get(this.sockPath + "/events");
requestStream.on("response",(response) => {
if(response.statusCode == 200){
plugins.beautylog.ok("request returned status 200, so we are good!");
} else {
plugins.beautylog.error("request returned error: " + response.statusCode);
}
});
requestStream.on("data",(data:Buffer) => {
let status = JSON.parse(data.toString()).status;
plugins.beautylog.logReduced(status);
if(typeof cbPromise == "undefined" || cbPromise.state == "pending"){
cbPromise = cb();
} else if (changeBuffered) {
changeBuffered = true;
cbPromise.then(() => {
changeBuffered = false;
cbPromise = cb();
});
}
});
requestStream.on("end",()=> {
});
};
getChangeObservable(){
let requestStream = plugins.request.get(this.sockPath + "/events");
requestStream.on("response",(response) => {
if(response.statusCode == 200){
plugins.beautylog.ok("request returned status 200, so we are good!");
} else {
plugins.beautylog.error("request returned error: " + response.statusCode);
}
});
let changeObservable = Observable.fromEvent(requestStream,"data");
requestStream.on("end",()=> {
});
return changeObservable;
}
request(methodArg:string,routeArg:string,queryArg:string = "", dataArg = {}){
let done = plugins.q.defer();
let jsonArg:string = JSON.stringify(dataArg);
let suffix:string = "";
if(methodArg == "GET") suffix = "/json";
let options = {
method:methodArg,
url:this.sockPath + routeArg + suffix + queryArg,
headers:{
"Content-Type":"application/json"
},
body:jsonArg
};
plugins.request(options,(err, res, body) => {
if (!err && res.statusCode == 200) {
var responseObj = JSON.parse(body);
done.resolve(responseObj);
} else {
console.log(err);
console.log(res);
done.reject(err);
};
});
return done.promise;
}
requestStream(methodArg,routeArg,endArg:boolean = true){
let done = plugins.q.defer();
if(methodArg == "POST"){
let requestStream = plugins.request.post(this.sockPath + routeArg);
requestStream.on("response",(response) => {
if(response.statusCode == 200){
plugins.beautylog.ok("request returned status 200, so we are good!");
} else {
plugins.beautylog.error("request returned error: " + response.statusCode);
done.reject(response);
}
});
requestStream.on("data",(data:Buffer) => {
let status = JSON.parse(data.toString()).status;
plugins.beautylog.logReduced(status);
});
requestStream.on("end",()=> {
done.resolve();
});
}
return done.promise;
}
}

View File

@ -1,5 +0,0 @@
import "typings-global";
export import beautylog = require("beautylog");
export import q = require("q");
export let request = require("request");
export import rxjs = require("rxjs");

View File

@ -1,2 +1,6 @@
import "typings-global";
export {Dockersock} from "./dockersock.classes.dockersock"
export * from './docker.classes.host';
export * from './docker.classes.container';
export * from './docker.classes.image';
export * from './docker.classes.network';
export * from './docker.classes.secret';
export * from './docker.classes.service';

View 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
View File

@ -0,0 +1,4 @@
export interface IImageCreationDescriptor {
imageUrl: string;
imageTag?: string;
}

7
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1,7 @@
export * from './container';
export * from './image';
export * from './label';
export * from './network';
export * from './port';
export * from './secret';
export * from './service';

2
ts/interfaces/label.ts Normal file
View File

@ -0,0 +1,2 @@
// tslint:disable-next-line: interface-over-type-literal
export type TLabels = { [key: string]: string };

6
ts/interfaces/network.ts Normal file
View File

@ -0,0 +1,6 @@
/**
* creates a new Network
*/
export interface INetworkCreationDescriptor {
Name: string;
}

6
ts/interfaces/port.ts Normal file
View File

@ -0,0 +1,6 @@
export interface IPort {
PrivatePort: 80;
Type: 'tcp';
}
export type TPorts = IPort[];

8
ts/interfaces/secret.ts Normal file
View File

@ -0,0 +1,8 @@
import * as interfaces from './';
export interface ISecretCreationDescriptor {
name: string;
version: string;
contentArg: any;
labels: interfaces.TLabels;
}

14
ts/interfaces/service.ts Normal file
View File

@ -0,0 +1,14 @@
import * as interfaces from './';
import { DockerNetwork } from '../docker.classes.network';
import { DockerSecret } from '../docker.classes.secret';
import { DockerImage } from '../docker.classes.image';
export interface IServiceCreationDescriptor {
name: string;
image: DockerImage;
labels: interfaces.TLabels;
networks: DockerNetwork[];
networkAlias: string;
secrets: DockerSecret[];
ports: string[];
}

17
tslint.json Normal file
View File

@ -0,0 +1,17 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}