docker/ts/docker.classes.image.ts

113 lines
3.3 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-15 16:50:13 +00:00
const response = await dockerHostArg.request(
'POST',
`/images/create?fromImage=${encodeURIComponent(
creationObject.imageUrl
)}&tag=${encodeURIComponent(creationObject.tag)}`
);
if (response.statusCode < 300) {
plugins.smartlog.defaultLogger.log(
'info',
`Successfully pulled image ${creationObject.imageUrl} from the registry`
);
2019-08-15 17:00:17 +00:00
const originTag = `${creationObject.imageUrl}:${creationObject.tag}`;
2019-08-16 10:48:56 +00:00
console.log(originTag);
2019-08-15 17:00:17 +00:00
const image = await DockerImage.findImageByName(dockerHostArg, originTag);
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 dockerImageUrl = this.RepoTags[0].split(':')[0];
const dockerImageTag = this.RepoTags[0].split(':')[1];
const updatedImage = await DockerImage.createFromRegistry(this.dockerHost, {
imageUrl: dockerImageUrl,
tag: dockerImageTag
});
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
}