-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcreateContainers.js
More file actions
253 lines (217 loc) · 8.43 KB
/
createContainers.js
File metadata and controls
253 lines (217 loc) · 8.43 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"use strict";
const BbPromise = require("bluebird");
const singleSource = require("../../shared/singleSource");
const secrets = require("../../shared/secrets");
const domainUtils = require("../../shared/domains");
const maxConcurrencyDeprecationWarning = `WARNING: maxConcurrency is deprecated and has been replaced by scalingOption of type: concurrentRequests.
Please update your serverless.yml file.`;
function adaptHealthCheckToAPI(healthCheck) {
if (!healthCheck) {
return null;
}
// We need to find the type of the health check (tcp, http, ...)
// If httpPath is provided, we default to http, otherwise we default to tcp
let type = healthCheck.httpPath ? "http" : "tcp";
if (healthCheck.type) {
type = healthCheck.type;
}
return {
failure_threshold: healthCheck.failureThreshold,
interval: healthCheck.interval,
...(type === "http" && { http: { path: healthCheck.httpPath || "/" } }),
...(type === "tcp" && { tcp: {} }),
};
}
const scalingOptionToAPIProperty = {
concurrentRequests: "concurrent_requests_threshold",
cpuUsage: "cpu_usage_threshold",
memoryUsage: "memory_usage_threshold",
};
function adaptScalingOptionToAPI(scalingOption) {
if (!scalingOption || !scalingOption.type) {
return null;
}
const property = scalingOptionToAPIProperty[scalingOption.type];
if (!property) {
throw new Error(
`scalingOption.type must be one of: ${Object.keys(
scalingOptionToAPIProperty
).join(", ")}`
);
}
return {
[property]: scalingOption.threshold,
};
}
module.exports = {
createContainers() {
return BbPromise.bind(this)
.then(() => this.listContainers(this.namespace.id))
.then(this.createOrUpdateContainers);
},
deleteContainersByIds(containersIdsToDelete) {
containersIdsToDelete.forEach((containerIdToDelete) => {
this.deleteContainer(containerIdToDelete).then((res) => {
this.serverless.cli.log(
`Container ${res.name} removed from config file, deleting it...`
);
this.waitForContainer(containerIdToDelete).then(() => {
this.serverless.cli.log(`Container ${res.name} deleted`);
});
});
});
},
applyDomainsContainer(containerId, customDomains) {
this.listDomainsContainer(containerId).then((domains) => {
const existingDomains = domainUtils.formatDomainsStructure(domains);
const domainsToCreate = domainUtils.getDomainsToCreate(
customDomains,
existingDomains
);
const domainsIdToDelete = domainUtils.getDomainsToDelete(
customDomains,
existingDomains
);
domainsToCreate.forEach((newDomain) => {
const createDomainParams = {
container_id: containerId,
hostname: newDomain,
};
this.createDomainAndLog(createDomainParams);
});
domainsIdToDelete.forEach((domainId) => {
this.deleteDomain(domainId).then((res) => {
this.serverless.cli.log(`Deleting domain ${res.hostname}`);
});
});
});
},
createOrUpdateContainers(foundContainers) {
const { containers } = this.provider.serverless.service.custom;
const deleteData = singleSource.getElementsToDelete(
this.serverless.configurationInput.singleSource,
foundContainers,
Object.keys(containers)
);
this.deleteContainersByIds(deleteData.elementsIdsToRemove);
const promises = deleteData.serviceNamesRet.map((containerName) => {
const container = Object.assign(containers[containerName], {
name: containerName,
});
const foundContainer = foundContainers.find(
(c) => c.name === container.name
);
if (foundContainer) {
// If the container is not in a final status, we need to wait
// for it to be reconciled before updating it, otherwise the update will fail.
return this.waitForContainer(foundContainer.id).then(() => {
return this.updateSingleContainer(container, foundContainer);
});
}
return this.createSingleContainer(container);
});
return Promise.all(promises).then((updatedContainers) => {
this.containers = updatedContainers;
});
},
createSingleContainer(container) {
const params = {
name: container.name,
environment_variables: container.env,
secret_environment_variables: secrets.convertObjectToModelSecretsArray(
container.secret
),
namespace_id: this.namespace.id,
description: container.description,
memory_limit: container.memoryLimit,
cpu_limit: container.cpuLimit,
min_scale: container.minScale,
max_scale: container.maxScale,
registry_image: container.registryImage
? container.registryImage
: `${this.namespace.registry_endpoint}/${container.name}:latest`,
max_concurrency: container.maxConcurrency,
timeout: container.timeout,
privacy: container.privacy,
port: container.port,
http_option: container.httpOption,
sandbox: container.sandbox,
health_check: adaptHealthCheckToAPI(container.healthCheck),
scaling_option: adaptScalingOptionToAPI(container.scalingOption),
private_network_id: container.privateNetworkId,
};
// Checking if there is custom_domains set on container creation.
if (container.custom_domains && container.custom_domains.length > 0) {
this.serverless.cli.log(
"WARNING: custom_domains are available on container update only. " +
"Redeploy your container to apply custom domains. Doc : https://www.scaleway.com/en/docs/compute/containers/how-to/add-a-custom-domain-to-a-container/"
);
}
// note about maxConcurrency deprecation
if (container.maxConcurrency) {
this.serverless.cli.log(maxConcurrencyDeprecationWarning);
}
this.serverless.cli.log(`Creating container ${container.name}...`);
return this.createContainer(params).then((createdContainer) => {
return this.deployContainer(createdContainer.id);
});
},
async updateSingleContainer(container, foundContainer) {
// Assign domains to the container before updating it, as it's not possible to manage domains
// while the container is updating or pending, and we already wait for the container
// to be in a final status before updating it.
// => This order of operation is simpler and does not require performing two separate waits.
this.applyDomainsContainer(foundContainer.id, container.custom_domains);
let privateNetworkId = container.privateNetworkId;
const hasToDeletePrivateNetwork =
foundContainer.private_network_id && !container.privateNetworkId;
if (hasToDeletePrivateNetwork) {
privateNetworkId = "";
}
const params = {
environment_variables: container.env,
secret_environment_variables: await secrets.mergeSecretEnvVars(
foundContainer.secret_environment_variables,
secrets.convertObjectToModelSecretsArray(container.secret),
this.serverless.cli
),
description: container.description,
memory_limit: container.memoryLimit,
cpu_limit: container.cpuLimit,
min_scale: container.minScale,
max_scale: container.maxScale,
registry_image: container.registryImage
? container.registryImage
: `${this.namespace.registry_endpoint}/${container.name}:latest`,
max_concurrency: container.maxConcurrency,
timeout: container.timeout,
privacy: container.privacy,
port: container.port,
http_option: container.httpOption,
sandbox: container.sandbox,
health_check: adaptHealthCheckToAPI(container.healthCheck),
scaling_option: adaptScalingOptionToAPI(container.scalingOption),
private_network_id: privateNetworkId,
};
// note about maxConcurrency deprecation
if (container.maxConcurrency) {
this.serverless.cli.log(maxConcurrencyDeprecationWarning);
}
this.serverless.cli.log(`Updating container ${container.name}...`);
return this.updateContainer(foundContainer.id, params).then(
(updatedContainer) => {
// If the container is updating, no need to do anything, a redeploy is already in progress.
if (
updatedContainer.status === "pending" ||
updatedContainer.status === "updating"
) {
return updatedContainer;
}
this.serverless.cli.log(
`Redeploying container ${container.name} to apply changes...`
);
return this.deployContainer(updatedContainer.id);
}
);
},
};