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 { 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,14 @@ tap.test('should list all services', async tools => {
|
|||||||
console.log(services);
|
console.log(services);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should create a service', async () => {
|
||||||
|
await DockerService.createService(testDockerHost, {
|
||||||
|
Image: 'nginx',
|
||||||
|
Labels: {
|
||||||
|
'testlabel': 'hi'
|
||||||
|
},
|
||||||
|
Name: 'testService'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -24,20 +24,45 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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`);
|
||||||
@ -99,11 +124,8 @@ export class DockerImage {
|
|||||||
* 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
|
||||||
|
@ -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
|
||||||
@ -21,6 +22,11 @@ export class DockerService {
|
|||||||
dockerHost: DockerHost,
|
dockerHost: DockerHost,
|
||||||
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
||||||
) {
|
) {
|
||||||
|
// lets get the image
|
||||||
|
DockerImage.createFromRegistry(dockerHost, {
|
||||||
|
imageUrl: serviceCreationDescriptor.Image
|
||||||
|
});
|
||||||
|
|
||||||
dockerHost.request('POST', '/services/create', {
|
dockerHost.request('POST', '/services/create', {
|
||||||
Name: serviceCreationDescriptor.Name,
|
Name: serviceCreationDescriptor.Name,
|
||||||
TaskTemplate: {
|
TaskTemplate: {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export interface IImageCreationDescriptor {
|
export interface IImageCreationDescriptor {
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
tag: string;
|
imageTag?: string;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user