-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.ts
554 lines (512 loc) · 17.4 KB
/
tasks.ts
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import * as Docker from 'dockerode';
import * as tar from 'tar-stream';
import { Task } from 'mahler';
import { logger } from '~/test-utils';
import type { Device, Service, ServiceStatus } from './state';
import { getContainerName, getImageName, getRegistryAndName } from './utils';
interface StatusError extends Error {
statusCode: number;
}
function isStatusError(x: unknown): x is StatusError {
return x instanceof Error && typeof (x as any).statusCode === 'number';
}
function isEqualConfig(s1: Service, s2: Service) {
return s1.image === s2.image && s1.command.join(' ') === s2.command.join(' ');
}
const docker = new Docker();
export const fetchImage = Task.of<Device>().from({
op: 'create',
lens: '/images/:imageTag',
effect: (image, { target }) => {
const { digest } = getRegistryAndName(target.name);
image._ = {
name: target.name,
};
if (digest) {
image._.contentHash = digest;
}
},
action: async (image, { target, imageTag, system: device }) => {
const { registry, digest } = getRegistryAndName(target.name);
const pack = tar.pack(); // pack is a stream
// we use a dockerfile to add image metadata
pack.entry(
{ name: 'Dockerfile' },
[
`FROM ${target.name}`,
`LABEL io.balena.image="${target.name}"`,
...(digest ? [`LABEL io.balena.content-hash="${digest}"`] : []),
].join('\n'),
);
pack.finalize();
await new Promise((resolve, reject) =>
docker
.buildImage(pack, {
t: imageTag,
// Add authentication to the registry if a key
// has been provided
...(registry &&
device.keys[registry] && {
authconfig: {
username: `d_${device.uuid}`,
password: device.keys[registry],
serverAddress: registry,
},
}),
} as Docker.ImageBuildOptions)
.then((stream) => {
stream.on('data', (b) => {
logger.debug(b.toString());
});
stream.on('error', reject);
stream.on('close', reject);
stream.on('end', resolve);
})
.catch(reject),
);
// Get the image using the name
const dockerImage = await docker.getImage(imageTag).inspect();
// try to delete the parent image
await docker
.getImage(target.name)
.remove()
.catch((e) => {
logger.warn(`could not remove image tag '${target.name}'`, e);
});
image._ = {
name: target.name,
dockerId: dockerImage.Id,
};
if (digest) {
image._.contentHash = digest;
}
},
description: ({ imageTag, target }) =>
`pull image '${target.name}' with tag '${imageTag}'`,
});
/**
* Pull an image from the registry, this task is applicable to
* the creation of a service, as pulling an image is only needed
* in that case.
*
* Condition: the image is not already present in the device
* Effect: add the image to the list of images
* Action: pull the image from the registry and set the image tag to match the app uuid and release before adding it to the list of images
*/
export const fetch = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
// Only pull the image if it's not already present
method: (_, ctx) =>
fetchImage({
imageTag: getImageName(ctx),
target: { name: ctx.target.image },
}),
description: (ctx) =>
`pull image '${ctx.target.image}' for service '${ctx.serviceName}' of app '${ctx.appUuid}'`,
});
/**
* Initialize an app
*
* The planner cannot infer what is an "empty" app, so we need to define
* an initializer task that creates an empty app object in the state.
*
* Because we are using the `create` operation, the task condition automatically
* checks that the object is not already present in the state.
*/
export const createApp = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid',
effect: (app, { target }) => {
app._ = {
name: target.name,
releases: {},
};
},
// Without a description the initializer will default to "create '/apps/a0'"
});
/**
* Initialize release
*
* The planner cannot infer what is an "empty" release, so we need to define
* an initializer task that creates an empty object in the state.
*
* Because we are using the `create` operation, the task condition automatically
* checks that the object is not already present in the state.
*/
export const createRelease = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid/releases/:releaseUuid',
// Return an empty release
effect: (release) => {
release._ = { services: {} };
},
// Without a function definition that defines what the parent 'State'
// object is, the compiler cannot infer the type of `ctx` so we just use
// any here
description: ({ releaseUuid, appUuid }) =>
`initialize release '${releaseUuid}' for app '${appUuid}'`,
});
/**
* Create a new service container from service data
*
* Condition: the service is not already present in the `services` object and the service image has already been downloaded
* Effect: add the service to the `services` object, with a `status` of `created`
* Action: create a new container using the docker API and set the `containerId` property of the service in the `services` object
*/
export const installService = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
condition: (_, { system, ...ctx }) =>
// The image has already been downloaded
getImageName(ctx) in system.images,
effect: (service, { target }) => {
service._ = {
image: target.image,
command: target.command || [],
status: 'created',
};
service._.containerId = 'deadbeef';
},
action: async (service, { target, ...ctx }) => {
const containerName = getContainerName(ctx);
// This is our way to update the internal agent state, we look for
// the service before we do anything, and if it exists, we update
// the state with the service metadata.
// Note that this will probably cause actions further on the plan to
// fail if the condition is that the container should have a `created` state.
const existing = await docker
.getContainer(containerName)
.inspect()
.catch((e) => {
if (isStatusError(e) && e.statusCode === 404) {
return null;
}
throw e;
});
if (existing != null) {
const status: ServiceStatus = (() => {
if (existing.State.Running) {
return 'running';
}
if (existing.State.ExitCode === 0) {
return 'stopped';
}
return 'created';
})();
const s: Service = {
// QUESTION: perhaps we need to check that the existing
// service image has the same image name in the label?
// that should only happen if someone is trying to mess with
// the engine state so we would need to abort
image: target.image,
startedAt: new Date(existing.State.StartedAt),
createdAt: new Date(existing.Created),
containerId: existing.Id,
status,
command: existing.Config.Cmd,
};
// If the service has a different config to what we are expecting
// the service start step will fail and we'll need to re-plan
service._ = s;
return;
}
const container = await docker.createContainer({
name: containerName,
Image: getImageName(ctx),
Cmd: target.command || [],
Labels: {
'io.balena.app-uuid': ctx.appUuid,
},
});
const { Id: containerId, Created } = await container.inspect();
service._ = {
image: target.image,
command: target.command || [],
status: 'created',
containerId,
createdAt: new Date(Created),
};
},
description: ({ serviceName, appUuid, releaseUuid }) =>
`create container for service '${serviceName}' of app '${appUuid}' and release '${releaseUuid}'`,
});
/**
* Start a service container
*
* Condition: the service container has been created (it has a container id), the container not running, the container configuration
* matches the target configuration, and there is no other service with the same name from another release running
* Effect: set the service status to `running`
* Action: start the container using the docker API
*/
export const startService = Task.of<Device>().from({
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
// Because we are dealing with releases, this has a more
// complex condition than the composer example
condition: (
service,
{ releaseUuid, appUuid, serviceName, system: device, target },
) => {
const { releases } = device.apps[appUuid]!;
// The task can be applied if the following conditions are met:
return (
// The container has been created
service.containerId != null &&
// The configuration of the existing container matches the target
isEqualConfig(service, target) &&
// The container is not running yet
service.status !== 'running' &&
// And if there is a service with the same name from other release, that
// service cannot be running
Object.keys(releases)
.filter((u) => u !== releaseUuid)
.every((u) => releases[u]!.services[serviceName]?.status !== 'running')
);
},
effect: (service) => {
service._.status = 'running';
},
action: async (service) => {
const container = docker.getContainer(service._.containerId!);
await container.start().catch((e) => {
if (isStatusError(e) && e.statusCode === 304) {
return;
}
throw e;
});
const { State } = await container.inspect();
// TODO: perhaps check if the container is actually running and fail if not?
service._.status = 'running';
service._.startedAt = new Date(State.StartedAt);
},
description: ({ serviceName, appUuid, releaseUuid }) =>
`start container for service '${serviceName}' of app '${appUuid}' and release '${releaseUuid}'`,
});
/**
* Delete service data from a release
*
* This is used to migrate a service between releases. It is only to be used within a method
*/
const deleteService = Task.of<Device>().from({
op: 'delete',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
effect: () => {
/* the operation already tells the framework to delete afterwards */
},
description: ({ serviceName, appUuid, releaseUuid }) =>
`remove metadata for service '${serviceName}' from release '${releaseUuid}' of app '${appUuid}'`,
});
/**
* Rename a service container between releases if the image and service configuration has not changed
*
* This is called only as part of the migrateService method so we skip the condition.
*
* Effect: copy the service from the source release to the target release
* Action: rename the container using the docker API
*/
const renameServiceContainer = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
condition: (
_,
{ appUuid, releaseUuid, serviceName, system: device, target },
) => {
const { releases } = device.apps[appUuid]!;
const [currentRelease] = Object.keys(releases).filter(
(u) => u !== releaseUuid,
);
if (currentRelease == null) {
return false;
}
const currService = releases[currentRelease]?.services[serviceName];
return (
currService != null &&
isEqualConfig(currService, target) &&
target.image === currService.image
);
},
effect: (service, { system: device, appUuid, serviceName, releaseUuid }) => {
const { releases } = device.apps[appUuid]!;
const currRelease = Object.keys(releases).find((u) => u !== releaseUuid)!;
const currService = releases[currRelease]?.services[serviceName];
// Move the service to the new release
service._ = currService;
},
action: async (
service,
{ system: device, appUuid, serviceName, releaseUuid },
) => {
const { releases } = device.apps[appUuid]!;
const currRelease = Object.keys(releases).find((u) => u !== releaseUuid)!;
const currService = releases[currRelease]!.services[serviceName]!;
// Rename the container
await docker.getContainer(currService.containerId!).rename({
name: getContainerName({ releaseUuid, serviceName }),
});
// Copy the container to the new release
service._ = currService;
},
description: (ctx) =>
`migrate unchanged service '${ctx.serviceName}' of app '${ctx.appUuid} to release '${ctx.releaseUuid}'`,
});
/**
* Rename a service container between releases if the image and service configuration has not changed
*
* This is a method as changes on two different releases need to be performed.
* The method will first rename the container and link the service on the target release. It then will
* remove the service metadata from the current release
*
* Condition: the service exists (it has a container id), the container configuration
* matches the target configuration, and source and target images are the same
*/
export const migrateService = Task.of<Device>().from({
op: 'create',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
expansion: 'sequential',
condition: (
_,
{ appUuid, releaseUuid, serviceName, system: device, target },
) => {
const { releases } = device.apps[appUuid]!;
const [currentRelease] = Object.keys(releases).filter(
(u) => u !== releaseUuid,
);
if (!currentRelease) {
return false;
}
const currService = releases[currentRelease]?.services[serviceName];
return (
currService != null &&
isEqualConfig(currService, target) &&
target.image === currService.image
);
},
method: (
_,
{ system: device, appUuid, serviceName, releaseUuid, target },
) => {
const { releases } = device.apps[appUuid]!;
const currRelease = Object.keys(releases).find((u) => u !== releaseUuid)!;
return [
renameServiceContainer({ target, appUuid, serviceName, releaseUuid }),
deleteService({ appUuid, releaseUuid: currRelease, serviceName }),
];
},
});
/**
* Stop a service container
*
* This task is applicable to `update` operations, e.g when purposely stopping a service,
* and `delete` operations, e.g. when uninstalling a release
*
* Condition: the service exists (it has a container id), the container is running, and
* if there are other services with the same name from other releases, their containers
* have already been created
* Effect: set the service status to `stopped`
* Action: stop the container using the docker API
*/
export const stopService = Task.of<Device>().from({
// Stop is applicable to a service delete or update
op: '*',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
condition: (
service,
{ system: device, appUuid, releaseUuid, serviceName },
) => {
const { releases } = device.apps[appUuid]!;
return (
service?.containerId != null &&
service?.status === 'running' &&
Object.keys(releases)
.filter((u) => u !== releaseUuid)
.every(
// If there are equivalent services from other releases they should at least have a container
(u) => releases[u]!.services[serviceName]?.containerId != null,
)
);
},
effect: (service) => {
service._.status = 'stopped';
},
action: async (service) => {
const container = docker.getContainer(service._.containerId!);
await container.stop().catch((e) => {
if (isStatusError(e)) {
if (e.statusCode !== 304 && e.statusCode !== 404) {
throw e;
}
} else {
throw e;
}
});
const { State } = await container.inspect();
service._.status = 'stopped';
service._.finishedAt = new Date(State.FinishedAt);
},
description: ({ serviceName, appUuid, releaseUuid }) =>
`stop container for service '${serviceName}' of app '${appUuid}' and release '${releaseUuid}'`,
});
/**
* Remove a service container
*
* This task is applicable to `update` operations, e.g. when recreating a container or `delete`
* operations, e.g. when removing a release
*
* Condition: the service exists (it has a container id), the container is not running
* Effect: remove the service from the device state
* Action: remove the container using the docker API
*/
export const uninstallService = Task.of<Device>().from({
op: '*',
lens: '/apps/:appUuid/releases/:releaseUuid/services/:serviceName',
condition: (service) =>
service?.containerId != null && service?.status !== 'running',
effect: (service) => {
service.delete();
},
action: async (service) => {
const container = docker.getContainer(service._.containerId!);
await container.remove({ v: true });
service.delete();
},
description: ({ serviceName, appUuid, releaseUuid }) =>
`remove container for service '${serviceName}' of app '${appUuid}' and release '${releaseUuid}'`,
});
/**
* Remove a release
*
* This only removes the release from the state object and has no effect in the system
*
* This task uses the Disposer task helper that already performs some checks and does the actual
* removal of the property from the state object. For that reason no `effect` is defined
*
* Condition: the release has not been deleted yet and the release has no services
* Effect: cleanup the release from the device state
*/
export const removeRelease = Task.of<Device>().from({
op: 'delete',
lens: '/apps/:appUuid/releases/:releaseUuid',
condition: (release) => Object.keys(release.services).length === 0,
effect: () => {
/* void */
},
description: (ctx) => `remove release '${ctx.releaseUuid}'`,
});
/**
* Remove an app
*
* This only removes the app from the state object and has no effect in the system
*
* This task uses the Disposer task helper that already performs some checks and does the actual
* removal of the property from the state object. For that reason no `effect` is defined
*
* Condition: the app has not been deleted yet and all releases have been uninstalled
* Effect: remove the app from the device state
*/
export const removeApp = Task.of<Device>().from({
op: 'delete',
lens: '/apps/:appUuid',
effect: () => void 0,
condition: (app) => Object.keys(app.releases).length === 0,
description: (ctx) => `remove app '${ctx.appUuid}'`,
});