fix(core): update
This commit is contained in:
parent
c496405818
commit
927e2e0acc
13
test/test.ts
13
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();
|
||||
|
@ -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`);
|
||||
@ -99,11 +124,8 @@ export class DockerImage {
|
||||
* 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
|
||||
|
@ -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: {
|
||||
|
@ -1,4 +1,4 @@
|
||||
export interface IImageCreationDescriptor {
|
||||
imageUrl: string;
|
||||
tag: string;
|
||||
imageTag?: string;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user