docker/ts/docker.classes.service.ts

232 lines
6.3 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-09-12 12:45:36 +00:00
import { DockerSecret } from './docker.classes.secret';
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-12 12:45:36 +00:00
public static async getServiceByName(
dockerHost: DockerHost,
networkName: string
): Promise<DockerService> {
2019-09-08 17:22:20 +00:00
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-09-12 12:45:36 +00:00
plugins.smartlog.defaultLogger.log(
'info',
2019-09-13 14:57:21 +00:00
`now creating service ${serviceCreationDescriptor.name}`
2019-09-12 12:45:36 +00:00
);
2019-09-13 16:20:12 +00:00
2019-09-13 14:57:21 +00:00
// await serviceCreationDescriptor.image.pullLatestImageFromRegistry();
const serviceVersion = await serviceCreationDescriptor.image.getVersion();
2019-09-13 12:40:38 +00:00
const labels: interfaces.TLabels = {
...serviceCreationDescriptor.labels,
version: serviceVersion
};
2019-09-11 18:25:45 +00:00
2019-09-15 13:08:48 +00:00
const mounts: Array<{
/**
* the target inside the container
*/
Target: string;
/**
* The Source from which to mount the data (Volume or host path)
*/
Source: string;
Type: 'bind' | 'volume' | 'tmpfs' | 'npipe';
ReadOnly: boolean;
Consistency: 'default' | 'consistent' | 'cached' | 'delegated';
}> = [];
if (serviceCreationDescriptor.accessHostDockerSock) {
mounts.push({
Target: '/var/run/docker.sock',
Source: '/var/run/docker.sock',
Consistency: 'default',
ReadOnly: false,
Type: 'bind'
});
}
const networkArray: Array<{
Target: string;
Aliases: string[];
}> = [];
2019-08-16 16:21:55 +00:00
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-13 20:37:38 +00:00
const ports = [];
2019-09-13 20:43:29 +00:00
for (const port of serviceCreationDescriptor.ports) {
const portArray = port.split(':');
const hostPort = portArray[0];
const containerPort = portArray[1];
ports.push({
2019-09-15 13:08:48 +00:00
Protocol: 'tcp',
2019-09-20 14:29:43 +00:00
PublishedPort: parseInt(hostPort, 10),
TargetPort: parseInt(containerPort, 10)
2019-09-13 20:43:29 +00:00
});
}
2019-09-13 20:37:38 +00:00
2019-09-19 18:05:56 +00:00
// lets configure secrets
2019-09-12 12:45:36 +00:00
const secretArray: any[] = [];
for (const secret of serviceCreationDescriptor.secrets) {
secretArray.push({
File: {
2019-09-19 18:05:56 +00:00
Name: 'secret.json', // TODO: make sure that works with multiple secrets
2019-09-12 12:45:36 +00:00
UID: '33',
GID: '33',
Mode: 384
},
SecretID: secret.ID,
SecretName: secret.Spec.Name
});
}
2019-09-19 18:05:56 +00:00
// lets configure limits
const limits = {
MemoryBytes: 1000 * 1000000
};
if (serviceCreationDescriptor.resources) {
limits.MemoryBytes = serviceCreationDescriptor.resources.memorySizeMB * 1000000;
}
2019-09-08 17:22:20 +00:00
const response = await dockerHost.request('POST', '/services/create', {
2019-09-13 12:40:38 +00:00
Name: serviceCreationDescriptor.name,
2019-08-16 10:48:40 +00:00
TaskTemplate: {
ContainerSpec: {
2019-09-13 14:57:21 +00:00
Image: serviceCreationDescriptor.image.RepoTags[0],
2019-09-13 12:40:38 +00:00
Labels: labels,
2019-09-15 13:08:48 +00:00
Secrets: secretArray,
2019-10-05 13:56:46 +00:00
Mounts: mounts
2019-09-23 11:41:06 +00:00
/* DNSConfig: {
2019-09-22 15:42:28 +00:00
Nameservers: ['1.1.1.1']
2019-09-23 11:41:06 +00:00
} */
2019-09-12 12:45:36 +00:00
},
UpdateConfig: {
Parallelism: 0,
Delay: 0,
FailureAction: 'pause',
Monitor: 15000000000,
MaxFailureRatio: 0.15
2019-09-11 18:25:45 +00:00
},
2019-09-19 18:05:56 +00:00
ForceUpdate: 1,
Resources: {
Limits: limits
2019-10-05 13:56:46 +00:00
},
LogDriver: {
Name: 'json-file',
Options: {
'max-file': '3',
'max-size': '10M'
}
2019-09-19 18:05:56 +00:00
}
2019-08-16 10:48:40 +00:00
},
2019-09-13 20:31:03 +00:00
Labels: labels,
2019-09-13 20:37:38 +00:00
Networks: networkArray,
EndpointSpec: {
Ports: ports
}
2019-08-16 10:48:40 +00:00
});
2019-09-08 17:22:20 +00:00
2019-09-12 12:45:36 +00:00
const createdService = await DockerService.getServiceByName(
dockerHost,
2019-09-13 12:40:38 +00:00
serviceCreationDescriptor.name
2019-09-12 12:45:36 +00:00
);
2019-09-08 17:22:20 +00:00
return createdService;
2019-08-16 10:48:40 +00:00
}
// INSTANCE
2019-09-08 17:22:20 +00:00
public dockerHostRef: DockerHost;
2019-09-12 12:45:36 +00:00
2019-09-08 17:22:20 +00:00
public ID: string;
public Version: { Index: number };
public CreatedAt: string;
public UpdatedAt: string;
public Spec: {
Name: string;
2019-09-13 11:20:01 +00:00
Labels: interfaces.TLabels;
2019-09-11 18:25:45 +00:00
TaskTemplate: {
ContainerSpec: {
Image: string;
Isolation: string;
2019-09-12 12:45:36 +00:00
Secrets: Array<{
File: {
Name: string;
UID: string;
GID: string;
Mode: number;
};
SecretID: string;
SecretName: string;
}>;
};
2019-09-11 18:25:45 +00:00
ForceUpdate: 0;
2019-09-12 12:45:36 +00:00
};
2019-09-11 18:25:45 +00:00
Mode: {};
2019-09-12 12:45:36 +00:00
Networks: [any[]];
2019-09-08 17:22:20 +00:00
};
2019-09-12 12:45:36 +00:00
public Endpoint: { Spec: {}; VirtualIPs: [any[]] };
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 remove() {
await this.dockerHostRef.request('DELETE', `/services/${this.ID}`);
}
2019-09-11 18:25:45 +00:00
2019-09-12 12:45:36 +00:00
public async reReadFromDockerEngine() {
2019-09-11 18:25:45 +00:00
const dockerData = await this.dockerHostRef.request('GET', `/services/${this.ID}`);
2019-09-13 20:31:03 +00:00
// TODO: Better assign: Object.assign(this, dockerData);
2019-09-11 18:25:45 +00:00
}
2019-09-13 11:20:01 +00:00
public async needsUpdate(): Promise<boolean> {
2019-09-11 18:25:45 +00:00
// TODO: implement digest based update recognition
await this.reReadFromDockerEngine();
const dockerImage = await DockerImage.createFromRegistry(this.dockerHostRef, {
2019-09-12 12:45:36 +00:00
imageUrl: this.Spec.TaskTemplate.ContainerSpec.Image
2019-09-11 18:25:45 +00:00
});
const imageVersion = new plugins.smartversion.SmartVersion(dockerImage.Labels.version);
const serviceVersion = new plugins.smartversion.SmartVersion(this.Spec.Labels.version);
if (imageVersion.greaterThan(serviceVersion)) {
2019-09-13 11:20:01 +00:00
console.log(`service ${this.Spec.Name} needs to be updated`);
return true;
} else {
console.log(`service ${this.Spec.Name} is up to date.`);
}
}
2019-08-16 10:48:40 +00:00
}