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) {
|
2019-09-08 17:22:20 +00:00
|
|
|
const dockerService = new DockerService(dockerHost);
|
|
|
|
Object.assign(dockerService, serviceObject);
|
|
|
|
services.push(dockerService);
|
2019-08-16 10:48:40 +00:00
|
|
|
}
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
2019-09-08 17:22:20 +00:00
|
|
|
public static async getServiceByName(dockerHost: DockerHost, networkName: string): Promise<DockerService> {
|
|
|
|
const allServices = await DockerService.getServices(dockerHost);
|
|
|
|
const wantedService = allServices.find(service => {
|
|
|
|
return service.Spec.Name === networkName;
|
|
|
|
});
|
|
|
|
return wantedService;
|
|
|
|
}
|
|
|
|
|
2019-08-16 10:48:40 +00:00
|
|
|
/**
|
|
|
|
* creates a service
|
|
|
|
*/
|
2019-08-16 10:48:56 +00:00
|
|
|
public static async createService(
|
|
|
|
dockerHost: DockerHost,
|
|
|
|
serviceCreationDescriptor: interfaces.IServiceCreationDescriptor
|
2019-09-08 17:22:20 +00:00
|
|
|
): Promise<DockerService> {
|
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-09-11 18:25:45 +00:00
|
|
|
const serviceVersion = serviceImage.Labels.version;
|
|
|
|
serviceCreationDescriptor.Labels.version = serviceVersion;
|
|
|
|
|
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-09-08 17:22:20 +00:00
|
|
|
const response = await dockerHost.request('POST', '/services/create', {
|
2019-08-16 10:48:40 +00:00
|
|
|
Name: serviceCreationDescriptor.Name,
|
|
|
|
TaskTemplate: {
|
|
|
|
ContainerSpec: {
|
|
|
|
Image: serviceCreationDescriptor.Image,
|
|
|
|
Labels: serviceCreationDescriptor.Labels
|
2019-09-11 18:25:45 +00:00
|
|
|
},
|
|
|
|
ForceUpdate: 1
|
2019-08-16 10:48:40 +00:00
|
|
|
},
|
2019-08-16 16:21:55 +00:00
|
|
|
Labels: serviceCreationDescriptor.Labels,
|
|
|
|
Networks: networkArray
|
2019-08-16 10:48:40 +00:00
|
|
|
});
|
2019-09-08 17:22:20 +00:00
|
|
|
|
|
|
|
const createdService = await DockerService.getServiceByName(dockerHost, serviceCreationDescriptor.Name);
|
|
|
|
return createdService;
|
2019-08-16 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
2019-09-08 17:22:20 +00:00
|
|
|
public dockerHostRef: DockerHost;
|
|
|
|
|
|
|
|
public ID: string;
|
|
|
|
public Version: { Index: number };
|
|
|
|
public CreatedAt: string;
|
|
|
|
public UpdatedAt: string;
|
|
|
|
public Spec: {
|
|
|
|
Name: string;
|
2019-09-11 18:25:45 +00:00
|
|
|
Labels: interfaces.TLabels; // ZBD
|
|
|
|
TaskTemplate: {
|
|
|
|
ContainerSpec: {
|
|
|
|
Image: string;
|
|
|
|
Isolation: string;
|
|
|
|
}
|
|
|
|
ForceUpdate: 0;
|
|
|
|
},
|
|
|
|
Mode: {};
|
2019-09-08 17:22:20 +00:00
|
|
|
Networks: [any[]]
|
|
|
|
};
|
|
|
|
public Endpoint: { Spec: {}, VirtualIPs: [any[]] };
|
2019-08-16 10:48:40 +00:00
|
|
|
|
2019-09-08 17:22:20 +00:00
|
|
|
|
|
|
|
constructor(dockerHostArg: DockerHost) {
|
|
|
|
this.dockerHostRef = dockerHostArg;
|
2019-08-16 10:48:40 +00:00
|
|
|
}
|
2019-08-16 19:07:59 +00:00
|
|
|
|
2019-09-08 17:22:20 +00:00
|
|
|
public async update() {
|
2019-09-11 18:25:45 +00:00
|
|
|
const labels: interfaces.TLabels = {
|
|
|
|
...this.Spec.Labels,
|
|
|
|
|
|
|
|
};
|
|
|
|
const dockerData = await this.dockerHostRef.request('POST', `/servces/${this.ID}/update?version=${this.Version.Index}`, {
|
|
|
|
Name: this.Spec.Name,
|
|
|
|
TaskTemplate: {
|
|
|
|
ContainerSpec: {
|
|
|
|
Image: this.Spec.TaskTemplate.ContainerSpec.Image,
|
|
|
|
Labels: labels
|
|
|
|
},
|
|
|
|
ForceUpdate: 1
|
|
|
|
},
|
|
|
|
Labels: labels,
|
|
|
|
});
|
|
|
|
Object.assign(this, dockerData);
|
2019-08-16 19:07:59 +00:00
|
|
|
}
|
2019-09-08 17:22:20 +00:00
|
|
|
|
|
|
|
public async remove() {
|
|
|
|
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
|
|
|
|
}
|
2019-09-11 18:25:45 +00:00
|
|
|
|
|
|
|
public async reReadFromDockerEngine () {
|
|
|
|
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
|
|
|
|
Object.assign(this, dockerData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async updateFromRegistry () {
|
|
|
|
// TODO: implement digest based update recognition
|
|
|
|
|
|
|
|
await this.reReadFromDockerEngine();
|
|
|
|
const dockerImage = await DockerImage.createFromRegistry(this.dockerHostRef, {
|
|
|
|
imageUrl: this.Spec.TaskTemplate.ContainerSpec.Image
|
|
|
|
});
|
|
|
|
|
|
|
|
const imageVersion = new plugins.smartversion.SmartVersion(dockerImage.Labels.version);
|
|
|
|
const serviceVersion = new plugins.smartversion.SmartVersion(this.Spec.Labels.version);
|
|
|
|
if (imageVersion.greaterThan(serviceVersion)) {
|
|
|
|
console.log('service needs to be updated');
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-08-16 10:48:40 +00:00
|
|
|
}
|