Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
846ea9997e | |||
de54db33ad | |||
314cb692ac | |||
73f8ded3fe | |||
a28b10ac51 | |||
927e2e0acc | |||
c496405818 | |||
020737e21b |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.42",
|
"version": "1.0.46",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/docker",
|
"name": "@mojoio/docker",
|
||||||
"version": "1.0.42",
|
"version": "1.0.46",
|
||||||
"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",
|
||||||
|
18
test/test.ts
18
test/test.ts
@ -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;
|
||||||
|
|
||||||
@ -37,7 +38,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);
|
||||||
@ -62,4 +63,19 @@ tap.test('should list all services', async tools => {
|
|||||||
console.log(services);
|
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();
|
||||||
|
@ -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'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ export class DockerHost {
|
|||||||
public async activateSwarm(addvertisementIpArg?: string) {
|
public async activateSwarm(addvertisementIpArg?: string) {
|
||||||
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 ? `${addvertisementIpArg}:2377`: undefined,
|
AdvertiseAddr: addvertisementIpArg ? `${addvertisementIpArg}:2377` : undefined,
|
||||||
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,
|
||||||
|
@ -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`);
|
||||||
@ -93,24 +120,17 @@ export class DockerImage {
|
|||||||
return this.RepoTags.length > 0;
|
return this.RepoTags.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public tagImage(newTag) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ 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
|
// STATIC
|
||||||
@ -14,11 +15,27 @@ export class DockerService {
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a service
|
* creates a service
|
||||||
*/
|
*/
|
||||||
public static async createService(dockerHost: DockerHost, serviceCreationDescriptor: interfaces.IServiceCreationDescriptor) {
|
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', {
|
dockerHost.request('POST', '/services/create', {
|
||||||
Name: serviceCreationDescriptor.Name,
|
Name: serviceCreationDescriptor.Name,
|
||||||
TaskTemplate: {
|
TaskTemplate: {
|
||||||
@ -27,7 +44,8 @@ export class DockerService {
|
|||||||
Labels: serviceCreationDescriptor.Labels
|
Labels: serviceCreationDescriptor.Labels
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Labels: serviceCreationDescriptor.Labels
|
Labels: serviceCreationDescriptor.Labels,
|
||||||
|
Networks: networkArray
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export interface IImageCreationDescriptor {
|
export interface IImageCreationDescriptor {
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
tag: string;
|
imageTag?: string;
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,3 @@ export * from './label';
|
|||||||
export * from './network';
|
export * from './network';
|
||||||
export * from './port';
|
export * from './port';
|
||||||
export * from './service';
|
export * from './service';
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
import * as interfaces from './'
|
import * as interfaces from './';
|
||||||
|
import { DockerNetwork } from '../docker.classes.network';
|
||||||
|
|
||||||
export interface IServiceCreationDescriptor {
|
export interface IServiceCreationDescriptor {
|
||||||
Name: string;
|
Name: string;
|
||||||
Image: string;
|
Image: string;
|
||||||
Labels: interfaces.TLabels;
|
Labels: interfaces.TLabels;
|
||||||
}
|
networks: DockerNetwork[];
|
||||||
|
networkAlias: string;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user