From 927e2e0acc335b64ed8621c8d4f859acd36e1433 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 16 Aug 2019 14:46:48 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 13 ++++++++++- ts/docker.classes.image.ts | 42 +++++++++++++++++++++++++++--------- ts/docker.classes.service.ts | 6 ++++++ ts/interfaces/image.ts | 2 +- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/test/test.ts b/test/test.ts index 9d88688..9c61794 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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); @@ -62,4 +63,14 @@ tap.test('should list all services', async tools => { console.log(services); }); +tap.test('should create a service', async () => { + await DockerService.createService(testDockerHost, { + Image: 'nginx', + Labels: { + 'testlabel': 'hi' + }, + Name: 'testService' + }); +}); + tap.start(); diff --git a/ts/docker.classes.image.ts b/ts/docker.classes.image.ts index e19c33d..ddf6689 100644 --- a/ts/docker.classes.image.ts +++ b/ts/docker.classes.image.ts @@ -24,20 +24,45 @@ export class DockerImage { dockerHostArg: DockerHost, creationObject: interfaces.IImageCreationDescriptor ): Promise { + + // 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`); @@ -99,11 +124,8 @@ export class DockerImage { * pulls the latest version from the registry */ public async pullLatestImageFromRegistry(): Promise { - 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 diff --git a/ts/docker.classes.service.ts b/ts/docker.classes.service.ts index 4260f60..ed882a9 100644 --- a/ts/docker.classes.service.ts +++ b/ts/docker.classes.service.ts @@ -2,6 +2,7 @@ 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 @@ -21,6 +22,11 @@ export class DockerService { dockerHost: DockerHost, serviceCreationDescriptor: interfaces.IServiceCreationDescriptor ) { + // lets get the image + DockerImage.createFromRegistry(dockerHost, { + imageUrl: serviceCreationDescriptor.Image + }); + dockerHost.request('POST', '/services/create', { Name: serviceCreationDescriptor.Name, TaskTemplate: { diff --git a/ts/interfaces/image.ts b/ts/interfaces/image.ts index a07914a..5207910 100644 --- a/ts/interfaces/image.ts +++ b/ts/interfaces/image.ts @@ -1,4 +1,4 @@ export interface IImageCreationDescriptor { imageUrl: string; - tag: string; + imageTag?: string; }