Compare commits

..

22 Commits

Author SHA1 Message Date
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
13 changed files with 685 additions and 234 deletions

View File

@ -39,6 +39,8 @@ snyk:
# ==================== # ====================
testLTS: testLTS:
services:
- docker:18-dind
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
@ -48,7 +50,7 @@ testLTS:
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv - priv
testBuild: testBuild:
stage: test stage: test

686
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/docker", "name": "@mojoio/docker",
"version": "1.0.41", "version": "1.0.52",
"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,20 @@
}, },
"homepage": "https://gitlab.com/pushrocks/dockersock#README", "homepage": "https://gitlab.com/pushrocks/dockersock#README",
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.10", "@pushrocks/lik": "^3.0.11",
"@pushrocks/smartlog": "^2.0.19", "@pushrocks/smartlog": "^2.0.19",
"@pushrocks/smartnetwork": "^1.1.14",
"@pushrocks/smartpromise": "^3.0.2", "@pushrocks/smartpromise": "^3.0.2",
"@pushrocks/smartrequest": "^1.1.16", "@pushrocks/smartrequest": "^1.1.23",
"rxjs": "^6.5.2" "rxjs": "^6.5.3"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.11", "@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24", "@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.11", "@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.1", "@types/node": "^12.7.4",
"tslint": "^5.18.0", "tslint": "^5.19.0",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
}, },
"files": [ "files": [

View File

@ -1,5 +1,6 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as docker from '../ts/index'; import * as docker from '../ts/index';
import { DockerService } from '../ts/index';
let testDockerHost: docker.DockerHost; let testDockerHost: docker.DockerHost;
@ -8,6 +9,10 @@ tap.test('should create a new Dockersock instance', async () => {
return expect(testDockerHost).to.be.instanceof(docker.DockerHost); return expect(testDockerHost).to.be.instanceof(docker.DockerHost);
}); });
tap.test('should create a docker swarm', async () => {
});
// Containers // Containers
tap.test('should list containers', async () => { tap.test('should list containers', async () => {
const containers = await testDockerHost.getContainers(); const containers = await testDockerHost.getContainers();
@ -37,7 +42,7 @@ tap.test('should remove a network', async () => {
tap.test('should pull an image from imagetag', async () => { tap.test('should pull an image from imagetag', async () => {
const image = await docker.DockerImage.createFromRegistry(testDockerHost, { const image = await docker.DockerImage.createFromRegistry(testDockerHost, {
imageUrl: 'hosttoday/ht-docker-node', imageUrl: 'hosttoday/ht-docker-node',
tag: 'alpine' imageTag: 'alpine'
}); });
expect(image).to.be.instanceOf(docker.DockerImage); expect(image).to.be.instanceOf(docker.DockerImage);
console.log(image); console.log(image);
@ -52,4 +57,29 @@ tap.test('should return a change Observable', 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.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(); tap.start();

View File

@ -49,7 +49,10 @@ export class DockerContainer {
if (response.statusCode < 300) { if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log('info', 'Container created successfully'); plugins.smartlog.defaultLogger.log('info', 'Container created successfully');
} else { } else {
plugins.smartlog.defaultLogger.log('error', 'There has been a problem when creating the container'); plugins.smartlog.defaultLogger.log(
'error',
'There has been a problem when creating the container'
);
} }
} }

View File

@ -12,8 +12,16 @@ export class DockerHost {
* 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) {
this.socketPath = pathArg; 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;
} }
/** /**
@ -21,9 +29,12 @@ export class DockerHost {
* @param userArg * @param userArg
* @param passArg * @param passArg
*/ */
public async auth(registryArg: string, userArg: string, passArg: string) { public async auth(registryUrl: string, userArg: string, passArg: string) {
// TODO: implement Docker Registry authentication const response = await this.request('POST', '/auth', {
await this.request('POST', ''); serveraddress: registryUrl,
username: userArg,
password: passArg,
});
} }
/** /**
@ -65,10 +76,22 @@ export class DockerHost {
/** /**
* activates docker swarm * activates docker swarm
*/ */
public async activateSwarm(addvertisementIpArg: string) { 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', { const response = await this.request('POST', '/swarm/init', {
ListenAddr: '0.0.0.0:2377', ListenAddr: '0.0.0.0:2377',
AdvertiseAddr: `${addvertisementIpArg}:2377`, AdvertiseAddr: addvertisementIp,
DataPathPort: 4789, DataPathPort: 4789,
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'], DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
SubnetSize: 24, SubnetSize: 24,

View File

@ -13,7 +13,7 @@ export class DockerImage {
return images; return images;
} }
public static async findImageByName (dockerHost: DockerHost, imageNameArg: string) { public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) {
const images = await this.getImages(dockerHost); const images = await this.getImages(dockerHost);
return images.find(image => { return images.find(image => {
return image.RepoTags.includes(imageNameArg); return image.RepoTags.includes(imageNameArg);
@ -24,20 +24,47 @@ export class DockerImage {
dockerHostArg: DockerHost, dockerHostArg: DockerHost,
creationObject: interfaces.IImageCreationDescriptor creationObject: interfaces.IImageCreationDescriptor
): Promise<DockerImage> { ): 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( const response = await dockerHostArg.request(
'POST', 'POST',
`/images/create?fromImage=${encodeURIComponent( `/images/create?fromImage=${encodeURIComponent(
creationObject.imageUrl imageUrlObject.imageUrl
)}&tag=${encodeURIComponent(creationObject.tag)}` )}&tag=${encodeURIComponent(imageUrlObject.imageTag)}`
); );
if (response.statusCode < 300) { if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
`Successfully pulled image ${creationObject.imageUrl} from the registry` `Successfully pulled image ${imageUrlObject.imageUrl} from the registry`
); );
const originTag = `${creationObject.imageUrl}:${creationObject.tag}`; const image = await DockerImage.findImageByName(dockerHostArg, imageUrlObject.imageOriginTag);
console.log(originTag)
const image = await DockerImage.findImageByName(dockerHostArg, originTag);
return image; return image;
} else { } else {
plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`); plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`);
@ -85,32 +112,17 @@ export class DockerImage {
}); });
} }
/** public tagImage(newTag) {}
* 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 * pulls the latest version from the registry
*/ */
public async pullLatestImageFromRegistry(): Promise<boolean> { 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, { const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
imageUrl: dockerImageUrl, imageUrl: this.RepoTags[0]
tag: dockerImageTag
}); });
Object.assign(this, updatedImage); Object.assign(this, updatedImage);
// TODO: Compare image digists before and after // TODO: Compare image digists before and after
return true; return true;
} }
} }

View File

@ -41,12 +41,15 @@ export class DockerNetwork {
Attachable: true, Attachable: true,
Ingress: false Ingress: false
}); });
if (response.statusCode < 300 ) { if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log('info', 'Created network successfully'); plugins.smartlog.defaultLogger.log('info', 'Created network successfully');
return await DockerNetwork.getNetworkByName(dockerHost, networkCreationDescriptor.Name); return await DockerNetwork.getNetworkByName(dockerHost, networkCreationDescriptor.Name);
} else { } else {
plugins.smartlog.defaultLogger.log('error', 'There has been an error creating the wanted network'); plugins.smartlog.defaultLogger.log(
return null 'error',
'There has been an error creating the wanted network'
);
return null;
} }
} }

View File

@ -2,5 +2,62 @@ 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';
import { DockerImage } from './docker.classes.image';
export class DockerService {} 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() {
}
}

View File

@ -1,12 +1,13 @@
// @pushrocks scope // @pushrocks scope
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smartlog from '@pushrocks/smartlog'; import * as smartlog from '@pushrocks/smartlog';
import * as smartnetwork from '@pushrocks/smartnetwork';
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(); smartlog.defaultLogger.enableConsole();
export { lik, smartlog, smartpromise, smartrequest }; export { lik, smartlog, smartnetwork, smartpromise, smartrequest };
// third party // third party
import * as rxjs from 'rxjs'; import * as rxjs from 'rxjs';

View File

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

View File

@ -3,3 +3,4 @@ export * from './image';
export * from './label'; export * from './label';
export * from './network'; export * from './network';
export * from './port'; export * from './port';
export * from './service';

10
ts/interfaces/service.ts Normal file
View 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;
}