@toa.io/operations 1.0.0-alpha.31 → 1.0.0-alpha.310

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +213 -0
  2. package/image/runtime.Dockerfile +14 -0
  3. package/package.json +10 -10
  4. package/readme.md +201 -0
  5. package/src/deployment/.deployment/.describe/components.js +1 -5
  6. package/src/deployment/.deployment/.describe/compositions.js +26 -7
  7. package/src/deployment/.deployment/.describe/dependencies.js +1 -6
  8. package/src/deployment/.deployment/.describe/events.js +63 -0
  9. package/src/deployment/.deployment/.describe/fold.js +42 -0
  10. package/src/deployment/.deployment/.describe/fold.test.js +108 -0
  11. package/src/deployment/.deployment/.describe/index.js +4 -13
  12. package/src/deployment/.deployment/.describe/mounts.js +17 -0
  13. package/src/deployment/.deployment/.describe/resources.js +129 -0
  14. package/src/deployment/.deployment/.describe/resources.test.js +129 -0
  15. package/src/deployment/.deployment/.describe/services.js +32 -6
  16. package/src/deployment/.deployment/.describe/services.test.js +78 -0
  17. package/src/deployment/.deployment/.describe/variables.js +5 -9
  18. package/src/deployment/.deployment/declare.js +1 -5
  19. package/src/deployment/.deployment/describe.js +153 -10
  20. package/src/deployment/.deployment/index.js +3 -9
  21. package/src/deployment/.deployment/merge.js +66 -6
  22. package/src/deployment/.deployment/merge.test.js +133 -0
  23. package/src/deployment/chart/templates/components.configmap.yaml +14 -0
  24. package/src/deployment/chart/templates/components.yaml +1 -1
  25. package/src/deployment/chart/templates/compositions.yaml +109 -4
  26. package/src/deployment/chart/templates/mono.yaml +195 -0
  27. package/src/deployment/chart/templates/services.yaml +97 -7
  28. package/src/deployment/chart/values.yaml +23 -1
  29. package/src/deployment/composition.js +8 -6
  30. package/src/deployment/deployment.js +147 -34
  31. package/src/deployment/drain.js +70 -0
  32. package/src/deployment/drain.test.js +64 -0
  33. package/src/deployment/factory.js +169 -37
  34. package/src/deployment/images/bundle.js +88 -0
  35. package/src/deployment/images/composition.Dockerfile +9 -17
  36. package/src/deployment/images/composition.js +9 -56
  37. package/src/deployment/images/dependencies.Dockerfile +23 -0
  38. package/src/deployment/images/dependencies.js +171 -0
  39. package/src/deployment/images/dependencies.test.js +268 -0
  40. package/src/deployment/images/factory.js +31 -15
  41. package/src/deployment/images/format.js +66 -0
  42. package/src/deployment/images/format.test.js +149 -0
  43. package/src/deployment/images/image.fixtures.js +17 -19
  44. package/src/deployment/images/image.js +79 -38
  45. package/src/deployment/images/image.test.js +11 -5
  46. package/src/deployment/images/index.js +1 -5
  47. package/src/deployment/images/mono.Dockerfile +11 -0
  48. package/src/deployment/images/mono.js +15 -0
  49. package/src/deployment/images/publish.js +91 -0
  50. package/src/deployment/images/runtime-base.test.js +97 -0
  51. package/src/deployment/images/service.Dockerfile +17 -5
  52. package/src/deployment/images/service.js +63 -14
  53. package/src/deployment/index.js +1 -5
  54. package/src/deployment/operator.js +22 -16
  55. package/src/deployment/operator.test.js +57 -7
  56. package/src/deployment/registry.js +210 -46
  57. package/src/deployment/registry.test.js +539 -0
  58. package/src/deployment/service.js +4 -6
  59. package/src/deployment/workspace.js +13 -8
  60. package/src/index.js +3 -2
  61. package/src/process.js +51 -13
  62. package/src/process.test.js +33 -0
  63. package/types/_deployment/composition.d.ts +2 -3
  64. package/types/_deployment/dependency.d.ts +13 -7
  65. package/types/_deployment/deployment.d.ts +7 -8
  66. package/types/_deployment/factory.d.ts +2 -4
  67. package/types/_deployment/images/factory.d.ts +6 -8
  68. package/types/_deployment/images/image.d.ts +16 -1
  69. package/types/_deployment/images/registry.d.ts +8 -11
  70. package/types/_deployment/operator.d.ts +10 -9
  71. package/types/_deployment/registry.d.ts +7 -4
  72. package/types/_deployment/service.d.ts +4 -3
  73. package/types/dependency.ts +79 -0
  74. package/types/index.ts +1 -0
  75. package/types/dependency.d.ts +0 -39
  76. package/types/index.d.ts +0 -1
@@ -0,0 +1,70 @@
1
+ import { setTimeout as sleep } from 'node:timers/promises'
2
+
3
+ /**
4
+ * Waits until no pod of the application is still terminating. `helm --wait` answers once the
5
+ * replacements are ready, while the replicas they replace are still draining, and a request
6
+ * made then may land on either.
7
+ *
8
+ * @param {toa.operations.Process} process
9
+ * @param {toa.deployment.installation.Options} options
10
+ * @returns {Promise<void>}
11
+ */
12
+ export async function drain(process, options) {
13
+ const timeout = options.timeout ?? DEFAULT_TIMEOUT
14
+ const deadline = Date.now() + duration(timeout)
15
+ const args = ['get', 'pods', '-o', 'json']
16
+
17
+ if (options.namespace !== undefined) args.push('-n', options.namespace)
18
+
19
+ let announced = false
20
+
21
+ for (;;) {
22
+ const output = await process.execute('kubectl', args, { silently: true })
23
+ const pods = JSON.parse(output).items.filter(terminating)
24
+
25
+ if (pods.length === 0) return
26
+
27
+ if (Date.now() >= deadline)
28
+ throw new Error(`${pods.length} replaced pod(s) still terminating after ${timeout}`)
29
+
30
+ if (!announced) {
31
+ console.log(`Waiting for ${pods.length} replaced pod(s) to terminate`)
32
+ announced = true
33
+ }
34
+
35
+ await sleep(INTERVAL)
36
+ }
37
+ }
38
+
39
+ /**
40
+ * A pod of the application on its way out. The chart labels every pod it renders under `toa/`,
41
+ * and nothing else in the namespace is waited for.
42
+ */
43
+ const terminating = (pod) =>
44
+ pod.metadata.deletionTimestamp !== undefined &&
45
+ Object.keys(pod.metadata.labels ?? {}).some((label) => label.startsWith('toa/'))
46
+
47
+ /**
48
+ * A helm duration, `12m` or `1h30m` or `90s`, in milliseconds.
49
+ *
50
+ * @param {string} value
51
+ * @returns {number}
52
+ */
53
+ export function duration(value) {
54
+ const matches = value.matchAll(/(\d+)(h|ms|m|s)/g)
55
+
56
+ let total = 0
57
+
58
+ for (const [, amount, unit] of matches) total += Number(amount) * UNITS[unit]
59
+
60
+ if (total === 0) throw new Error(`'${value}' is not a duration`)
61
+
62
+ return total
63
+ }
64
+
65
+ const UNITS = { h: 3_600_000, m: 60_000, s: 1_000, ms: 1 }
66
+
67
+ /** helm's own default for `--timeout` */
68
+ const DEFAULT_TIMEOUT = '5m'
69
+
70
+ const INTERVAL = 3_000
@@ -0,0 +1,64 @@
1
+ import { it, beforeEach, mock } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+
4
+ import { drain, duration } from './drain.js'
5
+
6
+ /** @type {Array<object[]>} what each poll answers */
7
+ let polls
8
+
9
+ /** @type {toa.operations.Process} */
10
+ let process
11
+
12
+ beforeEach(() => {
13
+ polls = []
14
+ process = /** @type {toa.operations.Process} */ {
15
+ execute: mock.fn(async () => JSON.stringify({ items: polls.shift() ?? [] }))
16
+ }
17
+ })
18
+
19
+ it('should answer once no pod of the application is terminating', async () => {
20
+ polls.push([pod('composition-a', true), pod('composition-b')], [pod('composition-b')])
21
+
22
+ await drain(process, { namespace: 'acme', timeout: '1m' })
23
+
24
+ assert.strictEqual(process.execute.mock.callCount(), 2)
25
+
26
+ const [, args, options] = process.execute.mock.calls[0].arguments
27
+
28
+ assert.deepStrictEqual(args, ['get', 'pods', '-o', 'json', '-n', 'acme'])
29
+ assert.deepStrictEqual(options, { silently: true })
30
+ })
31
+
32
+ it('should not wait for a pod that is not the application', async () => {
33
+ polls.push([pod('redis-0', true, {})])
34
+
35
+ await drain(process, {})
36
+
37
+ assert.strictEqual(process.execute.mock.callCount(), 1)
38
+ })
39
+
40
+ it('should give up at the timeout', async () => {
41
+ polls.push([pod('composition-a', true)], [pod('composition-a', true)])
42
+
43
+ await assert.rejects(drain(process, { timeout: '1ms' }), /still terminating after 1ms/)
44
+ })
45
+
46
+ it('should read a helm duration', () => {
47
+ assert.strictEqual(duration('12m'), 720_000)
48
+ assert.strictEqual(duration('1h30m'), 5_400_000)
49
+ assert.strictEqual(duration('90s'), 90_000)
50
+ assert.throws(() => duration('soon'), /not a duration/)
51
+ })
52
+
53
+ /**
54
+ * @param {string} name
55
+ * @param {boolean} [terminating]
56
+ * @param {object} [labels]
57
+ */
58
+ function pod(name, terminating = false, labels = { 'toa/composition': 'a' }) {
59
+ const metadata = { name, labels }
60
+
61
+ if (terminating) metadata.deletionTimestamp = '2026-01-01T00:00:00Z'
62
+
63
+ return { metadata }
64
+ }
@@ -1,77 +1,153 @@
1
- 'use strict'
2
-
3
- const { context: load } = require('@toa.io/norm')
4
- const { Process } = require('../process')
5
- const { Operator } = require('./operator')
6
- const { Factory: ImagesFactory } = require('./images')
7
- const { Deployment } = require('./deployment')
8
- const { Registry } = require('./registry')
9
- const { Composition } = require('./composition')
10
- const { Service } = require('./service')
11
-
12
- class Factory {
1
+ import { context as load, definition } from '@toa.io/norm'
2
+ import { Process } from '../process.js'
3
+ import { Operator } from './operator.js'
4
+ import { Factory as ImagesFactory } from './images/index.js'
5
+ import { Deployment } from './deployment.js'
6
+
7
+ import { Registry } from './registry.js'
8
+ import { Composition } from './composition.js'
9
+ import { Service } from './service.js'
10
+
11
+ export class Factory {
13
12
  #context
13
+ #mono
14
14
  #compositions
15
15
  #dependencies
16
16
  #registry
17
17
  #process
18
- #extensionComponents = []
18
+ #image
19
+
20
+ /** Which compositions run a given extension's service, rather than deploying it on its
21
+ * own. A service is stateless and already runs several replicas, so several compositions
22
+ * running one are replicas of it, behind the one Service that selects them all.
23
+ * @type {Map<string, string[]>} */
24
+ #claims
25
+
26
+ /** what this context does not deploy, by package reference
27
+ * @type {Set<string>} */
28
+ #evicted
19
29
 
20
- constructor (context) {
30
+ /** the extensions some composition lists, whether or not that composition is deployed
31
+ * @type {Set<string>} */
32
+ #listed
33
+
34
+ constructor(context, options = {}) {
21
35
  this.#context = context
36
+ this.#mono = options.mono === true
22
37
  this.#process = new Process()
23
38
 
24
- const imagesFactory = new ImagesFactory(context.name, context.runtime, context.registry)
39
+ const imagesFactory = new ImagesFactory(
40
+ context.name,
41
+ context.runtime,
42
+ context.registry
43
+ )
44
+
45
+ const compositions = deployed(context.compositions)
25
46
 
26
47
  this.#registry = new Registry(context.registry, imagesFactory, this.#process)
27
- this.#compositions = context.compositions.map((composition) => this.#composition(composition))
48
+ this.#claims = claims(compositions)
49
+ this.#listed = listed(context.compositions)
50
+ this.#evicted = new Set(context.evicted?.services ?? [])
28
51
  this.#dependencies = this.#getDependencies()
52
+ this.#compositions = []
53
+
54
+ if (this.#mono)
55
+ this.#image = this.#registry.mono({
56
+ components: context.components.filter(managed),
57
+ // mono runs every service, so it installs what every one of them brings
58
+ packages: context.packages
59
+ })
60
+ else
61
+ this.#compositions = compositions.map((composition) => this.#composition(composition))
29
62
  }
30
63
 
31
- operator () {
32
- const deployment = new Deployment(this.#context, this.#compositions, this.#dependencies, this.#process)
33
-
34
- return new Operator(deployment, this.#registry)
64
+ async operator() {
65
+ const deployment = new Deployment(
66
+ this.#context,
67
+ this.#compositions,
68
+ // the constructor cannot await, so what it started is settled here
69
+ await this.#dependencies,
70
+ this.#process,
71
+ this.#image
72
+ )
73
+
74
+ return new Operator(deployment, this.#registry, this.#context.environment)
35
75
  }
36
76
 
37
- registry () {
77
+ registry() {
38
78
  return this.#registry
39
79
  }
40
80
 
41
- #composition (composition) {
81
+ #composition(composition) {
42
82
  const image = this.#registry.composition(composition)
43
83
 
44
84
  return new Composition(composition, image)
45
85
  }
46
86
 
47
- #getDependencies () {
87
+ async #getDependencies() {
48
88
  /** @type {toa.deployment.Dependency[]} */
49
89
  const dependencies = []
50
90
 
51
91
  if (this.#context.dependencies === undefined) return dependencies
52
92
 
93
+ /** the claimed references that turned out to contribute a service */
94
+ const contributing = new Set()
95
+
53
96
  for (const [reference, instances] of Object.entries(this.#context.dependencies)) {
54
- const dependency = this.#getDependency(reference, instances)
97
+ const dependency = await this.#getDependency(reference, instances)
98
+
99
+ if (dependency === undefined) continue
55
100
 
56
- if (dependency !== undefined) dependencies.push(dependency)
101
+ if (dependency.services?.length > 0) contributing.add(reference)
102
+
103
+ dependencies.push(dependency)
57
104
  }
58
105
 
106
+ // this is the first point where every `deployment()` has run, so it is the first
107
+ // point where a reference that yields nothing can be told from one that yields a service
108
+ for (const [reference, compositions] of this.#claims)
109
+ if (!contributing.has(reference))
110
+ throw new Error(
111
+ `Composition '${compositions[0]}' lists '${reference}', ` +
112
+ 'which contributes no service.'
113
+ )
114
+
59
115
  return dependencies
60
116
  }
61
117
 
62
- #getDependency (path, instances) {
63
- const module = require(path)
64
- const pkg = require(path + '/package.json')
118
+ async #getDependency(reference, instances) {
119
+ const { name, module } = await definition(reference)
65
120
 
66
121
  if (module.deployment === undefined) return
67
122
 
68
- const annotation = this.#context.annotations?.[pkg.name]
123
+ // an evicted component is deployed by other means, and so is whatever only evicted
124
+ // components require; a service a composition lists is deployed for being listed
125
+ const managed = instances.filter(({ component }) => component.evicted !== true)
69
126
 
127
+ if (managed.length === 0 && instances.length > 0 && !this.#listed.has(reference)) return
128
+
129
+ const annotation = this.#context.annotations?.[name]
130
+
131
+ // every instance as well: what a definition keeps for all of its components, such as the
132
+ // configuration the values service serves, holds for an evicted one too
70
133
  /** @type {toa.deployment.dependency.Declaration} */
71
- const dependency = module.deployment(instances, annotation)
134
+ const dependency = module.deployment(managed, annotation, instances)
135
+
136
+ // mono claims every service, including one an extension added since this context was
137
+ // written, so its claim is the wildcard rather than a list
138
+ const workload = this.#mono ? [MONO] : this.#claims.get(reference)
139
+
140
+ // an evicted service is deployed nowhere, so nothing is prepared for it: no image, and
141
+ // no Deployment, Service or Ingress downstream
142
+ const declared = this.#evicted.has(reference) ? undefined : dependency.services
72
143
 
73
144
  /** @type {toa.deployment.Service[]} */
74
- const services = dependency.services?.map((service) => this.#service(path, service))
145
+ const services = declared?.map((service) =>
146
+ workload === undefined
147
+ ? this.#service(reference, service) // its own deployment, its own image
148
+ : // named the way `Service` would name it, since it skips that wrapper
149
+ { ...service, name: `${service.group}-${service.name}`, workload }
150
+ )
75
151
 
76
152
  return { ...dependency, services }
77
153
  }
@@ -81,17 +157,73 @@ class Factory {
81
157
  * @param service {toa.deployment.dependency.Service}
82
158
  * @returns {Service}
83
159
  */
84
- #service (path, service) {
85
- const image = this.#registry.service(path, service)
160
+ #service(path, service) {
161
+ // an extension that publishes its service's image is taken at its word: nothing is
162
+ // added to the registry, so nothing is prepared, probed, built or pushed for it
163
+ const published =
164
+ service.image !== undefined && this.#context.registry?.services === PUBLISHED
165
+
166
+ const image = published
167
+ ? { reference: `${service.image}:${this.#context.runtime.version}` }
168
+ : this.#registry.service(path, service)
86
169
 
87
170
  return new Service(service, image)
88
171
  }
89
172
 
90
- static async create (path, environment) {
91
- const context = await load(path, environment)
173
+ static async create(path, environment, options = {}) {
174
+ const context = await load(path, environment, options)
92
175
 
93
- return new Factory(context)
176
+ return new Factory(context, options)
94
177
  }
95
178
  }
96
179
 
97
- exports.Factory = Factory
180
+ const MONO = 'mono'
181
+
182
+ const PUBLISHED = 'published'
183
+
184
+ /**
185
+ * The compositions this context deploys: each without its evicted members, and none left with no
186
+ * members at all — the name it held is free again, and a service it listed falls back to a
187
+ * deployment of its own.
188
+ *
189
+ * @param {toa.norm.Composition[]} [compositions]
190
+ * @returns {toa.norm.Composition[]}
191
+ */
192
+ function deployed(compositions = []) {
193
+ return compositions
194
+ .map((composition) => ({
195
+ ...composition,
196
+ components: composition.components.filter(managed)
197
+ }))
198
+ .filter((composition) => composition.components.length > 0)
199
+ }
200
+
201
+ /** An evicted component is deployed by other means. */
202
+ function managed(component) {
203
+ return component.evicted !== true
204
+ }
205
+
206
+ /**
207
+ * @param {toa.norm.Composition[]} [compositions]
208
+ * @returns {Set<string>}
209
+ */
210
+ function listed(compositions = []) {
211
+ return new Set(compositions.flatMap((composition) => composition.services ?? []))
212
+ }
213
+
214
+ /**
215
+ * @param {toa.norm.Composition[]} compositions
216
+ * @returns {Map<string, string[]>}
217
+ */
218
+ function claims(compositions) {
219
+ const map = new Map()
220
+
221
+ for (const composition of compositions)
222
+ for (const reference of composition.services ?? []) {
223
+ if (!map.has(reference)) map.set(reference, [])
224
+
225
+ map.get(reference).push(composition.name)
226
+ }
227
+
228
+ return map
229
+ }
@@ -0,0 +1,88 @@
1
+ import { basename, join } from 'node:path'
2
+ import { cp } from 'node:fs/promises'
3
+ import { createHash } from 'node:crypto'
4
+
5
+ import { Image } from './image.js'
6
+ import { Dependencies } from './dependencies.js'
7
+ import { declare, normalized } from './format.js'
8
+
9
+ /**
10
+ * Components in one image: their sources laid over their dependencies, which are an image
11
+ * of their own (see `Dependencies`). What is built here is the sources layer alone.
12
+ *
13
+ * @implements {toa.deployment.images.Bundle}
14
+ * @abstract
15
+ */
16
+ export class Bundle extends Image {
17
+ /** @type {Dependencies} */
18
+ dependencies
19
+
20
+ /** @type {string | undefined} */
21
+ image
22
+
23
+ /** @type {toa.norm.Component[]} */
24
+ components
25
+
26
+ /** What the services this workload runs install, which no component of it declares.
27
+ * @type {Record<string, string> | undefined} */
28
+ packages
29
+
30
+ constructor(scope, runtime, registry, composition) {
31
+ super(scope, runtime, registry)
32
+
33
+ this.image = composition.image
34
+ this.components = composition.components
35
+ this.packages = composition.packages
36
+ this.dependencies = new Dependencies(scope, runtime, registry, this)
37
+ }
38
+
39
+ tag() {
40
+ this.dependencies.tag()
41
+ super.tag()
42
+ }
43
+
44
+ get version() {
45
+ const hash = createHash('sha256')
46
+
47
+ for (const component of this.components) {
48
+ hash.update(component.locator.id)
49
+ hash.update(component.version)
50
+ }
51
+
52
+ // laid over a different base is a different image, whatever the sources
53
+ hash.update(this.dependencies.version)
54
+
55
+ return hash.digest('hex').slice(0, 8)
56
+ }
57
+
58
+ get base() {
59
+ return this.dependencies.reference
60
+ }
61
+
62
+ /**
63
+ * What to say when the components ask for different base images.
64
+ *
65
+ * @abstract
66
+ * @returns {string}
67
+ */
68
+ conflict() {
69
+ throw new Error('Not implemented')
70
+ }
71
+
72
+ async prepare(root) {
73
+ const context = await super.prepare(root)
74
+
75
+ for (const component of this.components) {
76
+ const target = join(context, component.locator.label)
77
+
78
+ // what was installed in the workspace is not what the image installs
79
+ await cp(component.path, target, { recursive: true, filter: sources })
80
+ await declare(component.path, target, component.locator.label)
81
+ await normalized(component, target)
82
+ }
83
+
84
+ return context
85
+ }
86
+ }
87
+
88
+ const sources = (path) => basename(path) !== 'node_modules'
@@ -1,19 +1,11 @@
1
1
  FROM {{build.image}}
2
2
 
3
- {{build.arguments}}
4
-
5
- ENV NODE_ENV=production
6
- RUN if [ "{{runtime.registry}}" != "" ]; then npm set registry {{runtime.registry}}; fi
7
- RUN if [ "{{runtime.proxy}}" != "" ]; then npm set proxy {{runtime.proxy}}; fi
8
- RUN npm i -g @toa.io/runtime@{{runtime.version}} --omit=dev
9
-
10
- WORKDIR /composition
11
- COPY --chown=node:node . /composition
12
-
13
- {{build.run}}
14
-
15
- # run 'npm i' in each component
16
- RUN for entry in *; do if [ -f "$entry/package.json" ]; then (cd $entry && npm i --omit=dev); fi; done
17
-
18
- USER node
19
- CMD toa compose *
3
+ # the one instruction that touches the filesystem, and linked: the base is not pulled to lay
4
+ # the sources over it, and a push carries this layer and a manifest, not the base's layers.
5
+ # Owned by root like the dependencies beside them: a linked layer knows no user by name,
6
+ # and the runtime reads its sources, it does not write them
7
+ COPY --link . /composition
8
+
9
+ # no USER: the runtime drops to `node` itself, and only a process that started as root can
10
+ # close its environment under /proc — see runtime/runtime/bin/toa
11
+ CMD toa compose * --map {{map.file}}
@@ -1,70 +1,23 @@
1
- 'use strict'
1
+ import { join } from 'node:path'
2
2
 
3
- const { join } = require('node:path')
4
- const fs = require('fs-extra')
5
- const { createHash } = require('node:crypto')
3
+ import { Bundle } from './bundle.js'
6
4
 
7
- const { Image } = require('./image')
8
-
9
- class Composition extends Image {
10
- dockerfile = join(__dirname, 'composition.Dockerfile')
5
+ export class Composition extends Bundle {
6
+ dockerfile = join(import.meta.dirname, 'composition.Dockerfile')
11
7
 
12
8
  #name
13
- #image
14
- #components
15
9
 
16
- constructor (scope, runtime, registry, composition) {
17
- super(scope, runtime, registry)
10
+ constructor(scope, runtime, registry, composition) {
11
+ super(scope, runtime, registry, composition)
18
12
 
19
13
  this.#name = composition.name
20
- this.#image = composition.image
21
- this.#components = composition.components
22
14
  }
23
15
 
24
- get name () {
16
+ get name() {
25
17
  return 'composition-' + this.#name
26
18
  }
27
19
 
28
- get version () {
29
- const hash = createHash('sha256')
30
-
31
- for (const component of this.#components) {
32
- hash.update(component.locator.id)
33
- hash.update(component.version)
34
- }
35
-
36
- return hash.digest('hex').slice(0, 8)
37
- }
38
-
39
- get base () {
40
- if (this.#image !== undefined) {
41
- return this.#image
42
- }
43
-
44
- let image = null
45
-
46
- for (const component of this.#components) {
47
- const value = component.build?.image
48
-
49
- if (image !== null && image !== value) {
50
- throw new Error(`Composition '${this.#name}' requires different base images for its components. Specify base image for the composition in the context.`)
51
- }
52
-
53
- image = value
54
- }
55
-
56
- return image ?? undefined
57
- }
58
-
59
- async prepare (root) {
60
- const context = await super.prepare(root)
61
-
62
- for (const component of this.#components) {
63
- await fs.copy(component.path, join(context, component.locator.label))
64
- }
65
-
66
- return context
20
+ conflict() {
21
+ return `Composition '${this.#name}' requires different base images for its components. Specify base image for the composition in the context.`
67
22
  }
68
23
  }
69
-
70
- exports.Composition = Composition
@@ -0,0 +1,23 @@
1
+ FROM {{build.image}}
2
+
3
+ {{build.arguments}}
4
+
5
+ ENV NODE_ENV=production
6
+ RUN if [ "{{runtime.registry}}" != "" ]; then npm set registry {{runtime.registry}}; fi
7
+ RUN if [ "{{runtime.proxy}}" != "" ]; then npm set proxy {{runtime.proxy}}; fi
8
+
9
+ WORKDIR /composition
10
+ COPY --chown=node:node . /composition
11
+
12
+ {{build.run}}
13
+
14
+ # the context holds the manifests alone, so this is every dependency and none of the sources.
15
+ # `.packages` is a dotfile, so the glob does not reach it
16
+ RUN --mount=type=cache,target=/root/.npm,sharing=locked \
17
+ for entry in *; do if grep -qs '"dependencies"' "$entry/package.json"; then (cd $entry && npm i --omit=dev); fi; done
18
+
19
+ # what the extensions need for what these components declare: installed beside the extension
20
+ # that reads the declaration, in Toa's own install, because that is where Node resolves an
21
+ # extension's imports from — and one copy for the composition rather than one per component
22
+ RUN --mount=type=cache,target=/root/.npm,sharing=locked \
23
+ if [ -s .packages ]; then npm i --prefix /toa --omit=dev $(cat .packages); fi