docker/ts/docker.classes.image.ts

137 lines
4.0 KiB
TypeScript
Raw Normal View History

2019-08-15 16:50:13 +00:00
import * as plugins from './docker.plugins';
import * as interfaces from './interfaces';
2018-07-16 21:52:50 +00:00
import { DockerHost } from './docker.classes.host';
export class DockerImage {
2019-08-14 12:19:45 +00:00
// STATIC
2019-08-15 16:50:13 +00:00
public static async getImages(dockerHost: DockerHost) {
const images: DockerImage[] = [];
const response = await dockerHost.request('GET', '/images/json');
for (const imageObject of response.body) {
images.push(new DockerImage(dockerHost, imageObject));
}
return images;
}
2019-08-16 10:48:56 +00:00
public static async findImageByName(dockerHost: DockerHost, imageNameArg: string) {
2019-08-15 17:00:17 +00:00
const images = await this.getImages(dockerHost);
return images.find(image => {
return image.RepoTags.includes(imageNameArg);
});
}
2019-08-14 18:56:57 +00:00
public static async createFromRegistry(
dockerHostArg: DockerHost,
2019-08-15 16:50:13 +00:00
creationObject: interfaces.IImageCreationDescriptor
2019-08-14 18:56:57 +00:00
): Promise<DockerImage> {
2019-08-16 12:46:48 +00:00
// 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;
}
2019-08-16 16:21:55 +00:00
} else if (!imageUrlObject.imageTag) {
imageUrlObject.imageTag = 'latest';
2019-08-16 12:46:48 +00:00
}
imageUrlObject.imageOriginTag = `${imageUrlObject.imageUrl}:${imageUrlObject.imageTag}`;
// lets actually create the image
2019-08-15 16:50:13 +00:00
const response = await dockerHostArg.request(
'POST',
`/images/create?fromImage=${encodeURIComponent(
2019-08-16 12:46:48 +00:00
imageUrlObject.imageUrl
)}&tag=${encodeURIComponent(imageUrlObject.imageTag)}`
2019-08-15 16:50:13 +00:00
);
if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log(
'info',
2019-08-16 12:46:48 +00:00
`Successfully pulled image ${imageUrlObject.imageUrl} from the registry`
2019-08-15 16:50:13 +00:00
);
2019-08-16 12:46:48 +00:00
const image = await DockerImage.findImageByName(dockerHostArg, imageUrlObject.imageOriginTag);
2019-08-15 16:50:13 +00:00
return image;
} else {
plugins.smartlog.defaultLogger.log('error', `Failed at the attempt of creating a new image`);
}
}
2019-08-14 12:19:45 +00:00
2019-08-15 16:50:13 +00:00
public static async tagImageByIdOrName(
dockerHost: DockerHost,
idOrNameArg: string,
newTagArg: string
) {
const response = await dockerHost.request(
'POST',
`/images/${encodeURIComponent(idOrNameArg)}/${encodeURIComponent(newTagArg)}`
);
2019-08-14 12:19:45 +00:00
}
2019-08-15 16:50:13 +00:00
public static async buildImage(dockerHostArg: DockerHost, dockerImageTag) {
// TODO: implement building an image
}
2019-08-14 12:19:45 +00:00
// INSTANCE
2019-08-15 16:50:13 +00:00
// references
public dockerHost: DockerHost;
// properties
2018-07-16 21:52:50 +00:00
/**
* the tags for an image
*/
2019-08-15 16:50:13 +00:00
public Containers: number;
public Created: number;
public Id: string;
public Labels: interfaces.TLabels;
public ParentId: string;
public RepoDigests: string[];
public RepoTags: string[];
public SharedSize: number;
public Size: number;
public VirtualSize: number;
constructor(dockerHostArg, dockerImageObjectArg: any) {
this.dockerHost = dockerHostArg;
Object.keys(dockerImageObjectArg).forEach(keyArg => {
this[keyArg] = dockerImageObjectArg[keyArg];
});
}
2018-07-16 21:52:50 +00:00
2019-08-14 12:19:45 +00:00
/**
* returns a boolean wether the image has a upstream image
*/
2019-08-14 18:56:57 +00:00
public isUpstreamImage(): boolean {
2019-08-14 12:19:45 +00:00
// TODO: implement isUpastreamImage
2019-08-15 16:50:13 +00:00
return this.RepoTags.length > 0;
2019-08-14 18:56:57 +00:00
}
2019-08-14 12:19:45 +00:00
2019-08-16 10:48:56 +00:00
public tagImage(newTag) {}
2019-08-15 17:00:17 +00:00
2019-08-14 12:19:45 +00:00
/**
2019-08-15 16:50:13 +00:00
* pulls the latest version from the registry
2019-08-14 12:19:45 +00:00
*/
2019-08-14 18:56:57 +00:00
public async pullLatestImageFromRegistry(): Promise<boolean> {
2019-08-15 16:50:13 +00:00
const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
2019-08-16 12:46:48 +00:00
imageUrl: this.RepoTags[0]
2019-08-15 16:50:13 +00:00
});
Object.assign(this, updatedImage);
// TODO: Compare image digists before and after
2019-08-14 12:19:45 +00:00
return true;
2018-07-16 21:52:50 +00:00
}
2018-07-17 06:39:37 +00:00
}