-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-spec.ts
More file actions
129 lines (121 loc) · 5.17 KB
/
Copy pathservice-spec.ts
File metadata and controls
129 lines (121 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import assert from "assert";
import {SwarmAppConfig} from "./swarm-app-config.js";
import {HashedConfigs} from "./hashed-config.js";
import {DockerResources} from "./docker-api.js";
import {ServiceSpec} from "dockerode";
import {sortObjectKeys} from "./object.js";
import {assertTaskTemplateContainerTaskSpec} from "./asserts.js";
export function sortServiceSpec (s: ServiceSpec | undefined) {
assertTaskTemplateContainerTaskSpec(s);
if (s.Labels) {
s.Labels = sortObjectKeys(s.Labels);
}
if (s.TaskTemplate.ContainerSpec.Labels) {
s.TaskTemplate.ContainerSpec.Labels = sortObjectKeys(s.TaskTemplate.ContainerSpec.Labels);
}
s.TaskTemplate.ContainerSpec.Env?.sort((a, b) => {
return a.localeCompare(b);
});
s.TaskTemplate.ContainerSpec.Configs?.sort((a, b) => {
if (!a.File?.Name) return 0;
if (!b.File?.Name) return 0;
return a.File.Name.localeCompare(b.File.Name);
});
}
interface InitServiceSpecOpts {
appName: string;
serviceName: string;
config: SwarmAppConfig;
hashedConfigs: HashedConfigs;
current?: DockerResources;
}
export function initServiceSpec ({appName, serviceName, config, hashedConfigs, current}: InitServiceSpecOpts): ServiceSpec & {version?: number} {
assert(config.service_specs[serviceName], "config.service_specs[serviceName] must be non-null");
const serviceConfig = config.service_specs[serviceName];
let env;
if (serviceConfig.environment) {
env = Object.entries(serviceConfig.environment ?? {}).map(([k, v]) => `${k}=${v}`).sort((a, b) => a.localeCompare(b));
}
let configs;
const byServiceName = hashedConfigs.filterByServiceName(serviceName);
if (byServiceName.length > 0) {
configs = byServiceName.map(({targetPath, hash}) => {
return {
File: {Name: targetPath, UID: "0", GID: "0", Mode: 0},
ConfigID: current?.configs.find((c) => c.Spec?.Name === hash)?.ID,
ConfigName: hash,
};
});
}
let updateConfig;
if (serviceConfig.update_config) {
updateConfig = {
Parallelism: serviceConfig.update_config.parallelism,
FailureAction: "pause",
MaxFailureRatio: 0,
Order: serviceConfig.update_config.order,
};
}
const serviceSpec: ServiceSpec & {version?: number; TaskTemplate: {ContainerSpec: {HealthCheck: {StartInterval: number | undefined}}}} = {
version: 0,
Name: `${appName}_${serviceName}`,
Labels: serviceConfig.service_labels,
TaskTemplate: {
ContainerSpec: {
Image: serviceConfig.image,
Labels: serviceConfig.container_labels,
Command: serviceConfig.entrypoint,
Args: serviceConfig.command,
Env: env,
StopSignal: serviceConfig.stop_signal,
StopGracePeriod: serviceConfig.stop_grace_period,
User: serviceConfig.user,
Configs: configs,
Isolation: "default",
Dir: serviceConfig.dir,
HealthCheck: {
Test: serviceConfig.health_check?.test,
Interval: serviceConfig.health_check?.interval,
Timeout: serviceConfig.health_check?.timeout,
StartPeriod: serviceConfig.health_check?.start_period,
StartInterval: serviceConfig.health_check?.start_interval,
Retries: serviceConfig.health_check?.retries,
},
Mounts: Object.entries(serviceConfig.mounts ?? {}).map(([t, m]) => {
return {
Target: t,
Source: m.source,
Type: m.type,
ReadOnly: m.readonly,
};
}),
},
Placement: {
Constraints: serviceConfig.placement?.constraints,
Preferences: serviceConfig.placement?.preferences?.map((p) => {
return {Spread: {SpreadDescriptor: p.spread}};
}),
MaxReplicas: serviceConfig.placement?.max_replicas_per_node,
},
Networks: serviceConfig.networks?.map((networkKey) => {
assert(config.networks != null, "config.networks cannot be empty here");
const network = config.networks[networkKey];
assert(network != null, `config.networks[${networkKey}] cannot be empty here`);
const foundNetwork = current?.networks.find((n) => n.Name === network.name);
return {Target: foundNetwork?.Id};
}),
ForceUpdate: 0,
Runtime: "container",
},
Mode: {Replicated: {Replicas: serviceConfig.replicas ?? 1}},
UpdateConfig: updateConfig,
EndpointSpec: {
Mode: "vip",
Ports: serviceConfig.endpoint_spec?.ports.map((p) => {
return {Protocol: p.protocol, TargetPort: p.target_port, PublishedPort: p.published_port, PublishMode: p.publish_mode};
}),
},
};
sortServiceSpec(serviceSpec);
return serviceSpec;
}