docker/ts/docker.classes.service.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-08-15 16:50:13 +00:00
import * as plugins from './docker.plugins';
2019-08-14 12:19:45 +00:00
import * as interfaces from './interfaces';
import { DockerHost } from './docker.classes.host';
2019-08-16 12:46:48 +00:00
import { DockerImage } from './docker.classes.image';
2019-08-14 12:19:45 +00:00
2019-08-16 10:48:40 +00:00
export class DockerService {
// STATIC
public static async getServices(dockerHost: DockerHost) {
const services: DockerService[] = [];
const response = await dockerHost.request('GET', '/services');
for (const serviceObject of response.body) {
services.push(new DockerService(dockerHost, serviceObject));
}
return services;
}
/**
* creates a service
*/
2019-08-16 10:48:56 +00:00
public static async createService(
dockerHost: DockerHost,
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
) {
2019-08-16 12:46:48 +00:00
// lets get the image
2019-08-16 16:21:55 +00:00
plugins.smartlog.defaultLogger.log('info', `downloading image for service ${serviceCreationDescriptor.Name}`);
const serviceImage = await DockerImage.createFromRegistry(dockerHost, {
2019-08-16 12:46:48 +00:00
imageUrl: serviceCreationDescriptor.Image
});
2019-08-16 16:21:55 +00:00
const networkArray: any[] = [];
for (const network of serviceCreationDescriptor.networks) {
networkArray.push({
Target: network.Name,
2019-08-16 16:32:41 +00:00
Aliases: [serviceCreationDescriptor.networkAlias]
2019-08-16 16:21:55 +00:00
});
}
2019-08-16 10:48:40 +00:00
dockerHost.request('POST', '/services/create', {
Name: serviceCreationDescriptor.Name,
TaskTemplate: {
ContainerSpec: {
Image: serviceCreationDescriptor.Image,
Labels: serviceCreationDescriptor.Labels
}
},
2019-08-16 16:21:55 +00:00
Labels: serviceCreationDescriptor.Labels,
Networks: networkArray
2019-08-16 10:48:40 +00:00
});
}
// INSTANCE
public dockerHost: DockerHost;
constructor(dockerHostArg: DockerHost, serviceObject) {
this.dockerHost = dockerHostArg;
Object.assign(this, serviceObject);
}
2019-08-16 19:07:59 +00:00
update() {
// TODO: implemnt updating service
}
2019-08-16 10:48:40 +00:00
}