-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents-v4.ts
More file actions
344 lines (271 loc) · 9.15 KB
/
components-v4.ts
File metadata and controls
344 lines (271 loc) · 9.15 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import fs from 'fs'
import yaml from 'yaml'
import { warning, debug } from '@actions/core'
import { ComponentConfiguration } from './models/component_configuration'
/**
* Reads, parses, and validates the component configuration from the action's inputs.
* @param {string} componentName The name of the component to read.
* @param {string[]} resources The resources to use for the component.
* @param {string} componentConfigurationPath The path to the component configuration file.
* @returns {ComponentConfiguration?} A tuple containing a boolean indicating success and the component configuration.
*/
export function getComponentConfiguration(
componentName: string,
resources: string[],
componentConfigurationPath: string,
): ComponentConfiguration | undefined {
const [name, version] = componentName.split(':')
debug(`Reading the component configuration for ${name} @ ${version}`)
if (!fs.existsSync(componentConfigurationPath)) {
warning(
`The component configuration file does not exist: ${componentConfigurationPath}`,
)
return undefined
}
const fileContents = fs.readFileSync(componentConfigurationPath, 'utf8')
// Before parsing yaml, we need to replace environment expressions
// e.g:
// meta:
// version: ${{ env.VERSION }}
// becomes
// meta:
// version: 1.0.0
process.env.VERSION = version
process.env.NOMAD_VERSION = process.env.VERSION
// Manage NOMAD_CPU and NOMAD_RAM
// If the component has resources defined, use them
// Otherwise, set them to 500MHz and 256MB
const resourcesForComponent = resources
.map<{ component: string; cpu: string; ram: string }>(resource => {
return {
component: resource.split(',')[0],
cpu: resource.split(',')[1].split(':')[0],
ram: resource.split(',')[1].split(':')[1],
}
})
.find(resource => resource.component === name)
if (resourcesForComponent) {
process.env.NOMAD_CPU = resourcesForComponent.cpu
process.env.NOMAD_RAM = resourcesForComponent.ram
} else {
process.env.NOMAD_CPU = '500'
process.env.NOMAD_RAM = '256'
}
const replacedContents = fileContents.replace(
/\${{ env.([A-Z_]+) }}/g,
(_, envVar) => {
const value = process.env[envVar]
if (!value) {
warning(`Environment variable ${envVar} is not set`)
return ''
}
return value
},
)
let componentConfiguration: ComponentConfiguration
try {
componentConfiguration = yaml.parse(replacedContents)
} catch (error) {
warning(`Failed to parse the component configuration file: ${error}`)
return undefined
}
if (!componentConfiguration) {
warning('The component configuration file is empty')
return undefined
}
if (
!componentConfiguration.component ||
!componentConfiguration.component.trim()
) {
warning(
'The component name is missing from the component configuration file',
)
return undefined
}
if (!componentConfiguration.deployment) {
warning(`The component '${componentName}' can not be deployed`)
return undefined
}
if (
componentConfiguration.deployment.count === undefined ||
isNaN(componentConfiguration.deployment.count)
) {
componentConfiguration.deployment.count = 1
}
if (
!componentConfiguration.deployment.job ||
!componentConfiguration.deployment.job.trim()
) {
componentConfiguration.deployment.job = componentName.split(':')[0]
}
if (
componentConfiguration.deployment.type === undefined ||
!componentConfiguration.deployment.type.trim()
) {
componentConfiguration.deployment.type = 'service'
}
if (!['service', 'system'].includes(componentConfiguration.deployment.type)) {
warning(
`The deployment type for the component '${componentName}' is invalid`,
)
return undefined
}
if (componentConfiguration.deployment.meta !== undefined) {
componentConfiguration.deployment.meta = new Map(
Object.entries(componentConfiguration.deployment.meta),
)
}
if (componentConfiguration.deployment.count < 1) {
warning(
`The deployment count for the component '${componentName}' must be greater than 0`,
)
return undefined
}
if (
componentConfiguration.deployment.containers === undefined ||
componentConfiguration.deployment.containers.length === 0
) {
warning(`The component '${componentName}' must have at least one container`)
return undefined
}
for (
let i = 0;
i < componentConfiguration.deployment.containers.length;
i++
) {
const container = componentConfiguration.deployment.containers[i]
if (!container.image || !container.image.trim()) {
warning(`The image for container ${i + 1} is missing`)
return undefined
}
if (container.resources !== undefined) {
if (
(container.resources.cpu === undefined ||
isNaN(container.resources.cpu)) &&
(container.resources.ram === undefined ||
isNaN(container.resources.ram))
) {
container.resources = undefined
}
}
if (container.network !== undefined) {
if (
container.network.mode === undefined ||
!container.network.mode.trim()
) {
container.network.mode = 'bridge'
}
if (
container.network.mode !== 'bridge' &&
container.network.mode !== 'host' &&
container.network.mode !== 'none'
) {
warning(`The network mode for container ${i + 1} is invalid`)
return undefined
}
if (container.network.ports !== undefined) {
container.network.ports = new Map(
Object.entries(container.network.ports),
)
for (const [, port] of container.network.ports) {
// Range check on static and to
if (
port.static !== undefined &&
(port.static < 0 || port.static > 65535)
) {
warning(`The static port for container ${i + 1} is invalid`)
return undefined
}
if (port.to !== undefined && (port.to < 0 || port.to > 65535)) {
warning(`The to port for container ${i + 1} is invalid`)
return undefined
}
}
}
}
if (container.services !== undefined) {
for (const service of container.services) {
if (!service.name || !service.name.trim()) {
warning(`The service name for container ${i + 1} is missing`)
return undefined
}
if (service.port !== undefined) {
if (!container.network?.ports?.has(service.port)) {
warning(`The service port for container ${i + 1} is undefined`)
return undefined
}
}
if (service.checks !== undefined) {
for (const check of service.checks) {
if (!check.type || !check.type.trim()) {
warning(`The check type for service ${service.name} is missing`)
return undefined
}
if (!['http', 'tcp', 'grpc'].includes(check.type)) {
warning(`The check type for service ${service.name} is invalid`)
return undefined
}
if (check.interval === undefined) {
check.interval = '5s'
}
if (check.timeout === undefined) {
check.timeout = '2s'
}
if (
check.port !== undefined &&
!container.network?.ports?.has(check.port)
) {
warning(`The check port for service ${service.name} is undefined`)
return undefined
}
}
}
}
}
if (container.volumes !== undefined) {
for (const volume of container.volumes) {
// In format: hostPath:containerPath
if (!/^.+?:.+?$/.test(volume)) {
warning(`The volume ${volume} for container ${i + 1} is invalid`)
return undefined
}
}
}
if (container.driver_opts !== undefined) {
container.driver_opts = new Map(Object.entries(container.driver_opts))
}
if (container.config_maps !== undefined) {
for (const configMap of container.config_maps) {
// In format: configMapName:containerPath
if (
configMap.destination === undefined ||
!configMap.destination.trim()
) {
warning(
`The config map destination for container ${i + 1} is missing`,
)
return undefined
}
if (configMap.env === undefined) {
configMap.env = true
}
if (configMap.env !== true && configMap.env !== false) {
warning(`The config map env for container ${i + 1} is invalid`)
return undefined
}
if (configMap.on_change === undefined) {
configMap.on_change = 'restart'
}
if (!['restart', 'noop'].includes(configMap.on_change)) {
warning(`The config map on change for container ${i + 1} is invalid`)
return undefined
}
if (configMap.data === undefined || !configMap.data.trim()) {
warning(`The config map data for container ${i + 1} is missing`)
return undefined
}
}
}
}
return componentConfiguration
}