Compare commits

..

6 Commits

Author SHA1 Message Date
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
11 changed files with 124 additions and 28 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/docker",
"version": "1.0.41",
"version": "1.0.44",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/docker",
"version": "1.0.41",
"version": "1.0.44",
"description": "easy communication with docker remote api from node, TypeScript ready",
"private": false,
"main": "dist/index.js",

View File

@ -1,5 +1,6 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as docker from '../ts/index';
import { DockerService } from '../ts/index';
let testDockerHost: docker.DockerHost;
@ -37,7 +38,7 @@ tap.test('should remove a network', async () => {
tap.test('should pull an image from imagetag', async () => {
const image = await docker.DockerImage.createFromRegistry(testDockerHost, {
imageUrl: 'hosttoday/ht-docker-node',
tag: 'alpine'
imageTag: 'alpine'
});
expect(image).to.be.instanceOf(docker.DockerImage);
console.log(image);
@ -52,4 +53,24 @@ tap.test('should return a change Observable', 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 () => {
await DockerService.createService(testDockerHost, {
Image: 'nginx',
Labels: {
'testlabel': 'hi'
},
Name: 'testService'
});
});
tap.start();

View File

@ -49,7 +49,10 @@ export class DockerContainer {
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');
plugins.smartlog.defaultLogger.log(
'error',
'There has been a problem when creating the container'
);
}
}

View File

@ -65,10 +65,10 @@ export class DockerHost {
/**
* activates docker swarm
*/
public async activateSwarm(addvertisementIpArg: string) {
public async activateSwarm(addvertisementIpArg?: string) {
const response = await this.request('POST', '/swarm/init', {
ListenAddr: '0.0.0.0:2377',
AdvertiseAddr: `${addvertisementIpArg}:2377`,
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377` : undefined,
DataPathPort: 4789,
DefaultAddrPool: ['10.10.0.0/8', '20.20.0.0/8'],
SubnetSize: 24,

View File

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

View File

@ -41,12 +41,15 @@ export class DockerNetwork {
Attachable: true,
Ingress: false
});
if (response.statusCode < 300 ) {
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
plugins.smartlog.defaultLogger.log(
'error',
'There has been an error creating the wanted network'
);
return null;
}
}

View File

@ -2,5 +2,48 @@ 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 {}
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
DockerImage.createFromRegistry(dockerHost, {
imageUrl: serviceCreationDescriptor.Image
});
dockerHost.request('POST', '/services/create', {
Name: serviceCreationDescriptor.Name,
TaskTemplate: {
ContainerSpec: {
Image: serviceCreationDescriptor.Image,
Labels: serviceCreationDescriptor.Labels
}
},
Labels: serviceCreationDescriptor.Labels
});
}
// INSTANCE
public dockerHost: DockerHost;
constructor(dockerHostArg: DockerHost, serviceObject) {
this.dockerHost = dockerHostArg;
Object.assign(this, serviceObject);
}
}

View File

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

View File

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

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

@ -0,0 +1,7 @@
import * as interfaces from './';
export interface IServiceCreationDescriptor {
Name: string;
Image: string;
Labels: interfaces.TLabels;
}