@stream44.studio/t44-docker.com 0.1.0-rc.3
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.
- package/.dco-signatures +9 -0
- package/.github/workflows/dco.yaml +12 -0
- package/.github/workflows/gordian-open-integrity.yaml +13 -0
- package/.github/workflows/test.yaml +29 -0
- package/.o/GordianOpenIntegrity-CurrentLifehash.svg +1026 -0
- package/.o/GordianOpenIntegrity-InceptionLifehash.svg +1026 -0
- package/.o/GordianOpenIntegrity.yaml +21 -0
- package/.o/stream44.studio/assets/Icon-v1.svg +1170 -0
- package/.repo-identifier +1 -0
- package/DCO.md +34 -0
- package/LICENSE.txt +23 -0
- package/README.md +73 -0
- package/caps/Cli.test.ts +116 -0
- package/caps/Cli.ts +134 -0
- package/caps/Container.test.ts +168 -0
- package/caps/Container.ts +484 -0
- package/caps/ContainerContext.test.ts +78 -0
- package/caps/ContainerContext.ts +111 -0
- package/caps/Containers.test.ts +59 -0
- package/caps/Containers.ts +47 -0
- package/caps/Hub.test.ts +107 -0
- package/caps/Hub.ts +367 -0
- package/caps/Image/tpl/Dockerfile.alpine +40 -0
- package/caps/Image/tpl/Dockerfile.distroless +45 -0
- package/caps/Image/tpl/package.json +8 -0
- package/caps/Image.test.ts +269 -0
- package/caps/Image.ts +623 -0
- package/caps/ImageContext.test.ts +130 -0
- package/caps/ImageContext.ts +126 -0
- package/caps/Project.test.ts +267 -0
- package/caps/Project.ts +304 -0
- package/lib/waitForFetch.ts +95 -0
- package/package.json +19 -0
- package/structs/Hub/WorkspaceConnectionConfig.ts +53 -0
- package/tsconfig.json +28 -0
package/.repo-identifier
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
did:repo:e3a028ddf2e2061c350dc3146a22165b667d93eb
|
package/DCO.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Developer Certificate of Origin
|
|
2
|
+
Version 1.1
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
7
|
+
license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Developer's Certificate of Origin 1.1
|
|
11
|
+
|
|
12
|
+
By making a contribution to this project, I certify that:
|
|
13
|
+
|
|
14
|
+
(a) The contribution was created in whole or in part by me and I
|
|
15
|
+
have the right to submit it under the open source license
|
|
16
|
+
indicated in the file; or
|
|
17
|
+
|
|
18
|
+
(b) The contribution is based upon previous work that, to the best
|
|
19
|
+
of my knowledge, is covered under an appropriate open source
|
|
20
|
+
license and I have the right under that license to submit that
|
|
21
|
+
work with modifications, whether created in whole or in part
|
|
22
|
+
by me, under the same open source license (unless I am
|
|
23
|
+
permitted to submit under a different license), as indicated
|
|
24
|
+
in the file; or
|
|
25
|
+
|
|
26
|
+
(c) The contribution was provided directly to me by some other
|
|
27
|
+
person who certified (a), (b) or (c) and I have not modified
|
|
28
|
+
it.
|
|
29
|
+
|
|
30
|
+
(d) I understand and agree that this project and the contribution
|
|
31
|
+
are public and that a record of the contribution (including all
|
|
32
|
+
personal information I submit with it, including my sign-off) is
|
|
33
|
+
maintained indefinitely and may be redistributed consistent with
|
|
34
|
+
this project or the open source license(s) involved.
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Docker Tools for Terminal44 Workspace
|
|
4
|
+
|
|
5
|
+
Copyright 2026 Christoph Dorn - https://Christoph.diy
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
<table>
|
|
2
|
+
<tr>
|
|
3
|
+
<td><a href="https://Stream44.Studio"><img src=".o/stream44.studio/assets/Icon-v1.svg" width="42" height="42"></a></td>
|
|
4
|
+
<td><strong><a href="https://Stream44.Studio">Stream44 Studio</a></strong><br/>Open Development Project</td>
|
|
5
|
+
<td>Preview release for community feedback.<br/>Get in touch on <a href="https://discord.gg/9eBcQXEJAN">discord</a>.</td>
|
|
6
|
+
</tr>
|
|
7
|
+
</table>
|
|
8
|
+
|
|
9
|
+
⚠️ **Disclaimer:** Under active development. Code has not been audited. APIs and interfaces are subject to change!
|
|
10
|
+
|
|
11
|
+
`t44` Capsules for Docker [](https://github.com/Stream44/t44-docker.com/actions/workflows/test.yaml?query=branch%3Amain)
|
|
12
|
+
===
|
|
13
|
+
|
|
14
|
+
This project [encapsulates](https://github.com/Stream44/encapsulate) the `docker` command-line tool from [Docker](https://docker.com/) for use in [t44](https://github.com/Stream44/t44).
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Capsules: High Level
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
### `Project`
|
|
21
|
+
|
|
22
|
+
A curated API to build and run docker for a directory. Combines `Cli`, `Image` and `Container` capsules for a seamless developer experience.
|
|
23
|
+
|
|
24
|
+
Capsules: Low Level
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
### `Hub`
|
|
28
|
+
|
|
29
|
+
Utilities for working with [hub.docker.com](https://hub.docker.com).
|
|
30
|
+
|
|
31
|
+
### `Image`
|
|
32
|
+
|
|
33
|
+
Lifecycle methods for a **Docker Image**.
|
|
34
|
+
|
|
35
|
+
### `ImageContext`
|
|
36
|
+
|
|
37
|
+
Configurations for a spcific docker image.
|
|
38
|
+
|
|
39
|
+
### `Container`
|
|
40
|
+
|
|
41
|
+
Lifecycle methods for a **Docker Container**.
|
|
42
|
+
|
|
43
|
+
### `ContainerContext`
|
|
44
|
+
|
|
45
|
+
Configurations for a spcific docker container.
|
|
46
|
+
|
|
47
|
+
### `Cli`
|
|
48
|
+
|
|
49
|
+
Utility to wrap `docker` command-line tool.
|
|
50
|
+
|
|
51
|
+
### `Containers`
|
|
52
|
+
|
|
53
|
+
Utilities for working with containers in docker.
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Provenance
|
|
57
|
+
===
|
|
58
|
+
|
|
59
|
+
[](https://github.com/Stream44/t44-docker.com/actions/workflows/gordian-open-integrity.yaml?query=branch%3Amain) [](https://github.com/Stream44/t44-docker.com/actions/workflows/dco.yaml?query=branch%3Amain)
|
|
60
|
+
|
|
61
|
+
Repository DID: `did:repo:e3a028ddf2e2061c350dc3146a22165b667d93eb`
|
|
62
|
+
|
|
63
|
+
<table>
|
|
64
|
+
<tr>
|
|
65
|
+
<td><strong>Inception Mark</strong></td>
|
|
66
|
+
<td><img src=".o/GordianOpenIntegrity-InceptionLifehash.svg" width="64" height="64"></td>
|
|
67
|
+
<td><strong>Current Mark</strong></td>
|
|
68
|
+
<td><img src=".o/GordianOpenIntegrity-CurrentLifehash.svg" width="64" height="64"></td>
|
|
69
|
+
<td>Trust established using<br/><a href="https://github.com/Stream44/t44-blockchaincommons.com">Stream44/t44-BlockchainCommons.com</a></td>
|
|
70
|
+
</tr>
|
|
71
|
+
</table>
|
|
72
|
+
|
|
73
|
+
(c) 2026 [Christoph.diy](https://christoph.diy) • Code: [MIT](./LICENSE.txt) • Text: [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) • Created with [Stream44.Studio](https://Stream44.Studio)
|
package/caps/Cli.test.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env bun test
|
|
2
|
+
|
|
3
|
+
import * as bunTest from 'bun:test'
|
|
4
|
+
import { run } from 't44/standalone-rt'
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
test: { describe, it, expect },
|
|
8
|
+
cli,
|
|
9
|
+
} = await run(async ({ encapsulate, CapsulePropertyTypes, makeImportStack }: any) => {
|
|
10
|
+
const spine = await encapsulate({
|
|
11
|
+
'#@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0': {
|
|
12
|
+
'#@stream44.studio/encapsulate/structs/Capsule': {},
|
|
13
|
+
'#': {
|
|
14
|
+
test: {
|
|
15
|
+
type: CapsulePropertyTypes.Mapping,
|
|
16
|
+
value: 't44/caps/ProjectTest',
|
|
17
|
+
options: {
|
|
18
|
+
'#': {
|
|
19
|
+
bunTest,
|
|
20
|
+
env: {}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
cli: {
|
|
25
|
+
type: CapsulePropertyTypes.Mapping,
|
|
26
|
+
value: './Cli',
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}, {
|
|
31
|
+
importMeta: import.meta,
|
|
32
|
+
importStack: makeImportStack(),
|
|
33
|
+
capsuleName: '@stream44.studio/t44-docker.com/caps/Cli.test'
|
|
34
|
+
})
|
|
35
|
+
return { spine }
|
|
36
|
+
}, async ({ spine, apis }: any) => {
|
|
37
|
+
return apis[spine.capsuleSourceLineRef]
|
|
38
|
+
}, {
|
|
39
|
+
importMeta: import.meta
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('Cli Capsule', () => {
|
|
43
|
+
|
|
44
|
+
describe('Platform Utils', () => {
|
|
45
|
+
it('getCurrentPlatform should return valid platform', () => {
|
|
46
|
+
const platform = cli.getCurrentPlatform();
|
|
47
|
+
expect(['arm64', 'amd64']).toContain(platform);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('getCurrentPlatformArch should return valid arch key', () => {
|
|
51
|
+
const archKey = cli.getCurrentPlatformArch();
|
|
52
|
+
expect(['linux-arm64', 'linux-x64']).toContain(archKey);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('getCurrentPlatformArch should match getCurrentPlatform', () => {
|
|
56
|
+
const platform = cli.getCurrentPlatform();
|
|
57
|
+
const archKey = cli.getCurrentPlatformArch();
|
|
58
|
+
if (platform === 'arm64') {
|
|
59
|
+
expect(archKey).toBe('linux-arm64');
|
|
60
|
+
} else {
|
|
61
|
+
expect(archKey).toBe('linux-x64');
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('DOCKER_ARCHS', () => {
|
|
67
|
+
it('should have correct structure', () => {
|
|
68
|
+
expect(cli.DOCKER_ARCHS).toBeDefined();
|
|
69
|
+
expect(cli.DOCKER_ARCHS['linux-arm64']).toBeDefined();
|
|
70
|
+
expect(cli.DOCKER_ARCHS['linux-x64']).toBeDefined();
|
|
71
|
+
|
|
72
|
+
expect(cli.DOCKER_ARCHS['linux-arm64'].archDir).toBe('linux-arm64');
|
|
73
|
+
expect(cli.DOCKER_ARCHS['linux-arm64'].arch).toBe('arm64');
|
|
74
|
+
expect(cli.DOCKER_ARCHS['linux-arm64'].os).toBe('linux');
|
|
75
|
+
|
|
76
|
+
expect(cli.DOCKER_ARCHS['linux-x64'].archDir).toBe('linux-x64');
|
|
77
|
+
expect(cli.DOCKER_ARCHS['linux-x64'].arch).toBe('amd64');
|
|
78
|
+
expect(cli.DOCKER_ARCHS['linux-x64'].os).toBe('linux');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('keys should match archDir values', () => {
|
|
82
|
+
for (const [key, value] of Object.entries(cli.DOCKER_ARCHS) as any) {
|
|
83
|
+
expect(key).toBe(value.archDir);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('exec', () => {
|
|
89
|
+
it('should execute docker version', async () => {
|
|
90
|
+
const result = await cli.exec(['version', '--format', '{{.Client.Version}}']);
|
|
91
|
+
expect(result).toBeTruthy();
|
|
92
|
+
expect(typeof result).toBe('string');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should execute docker info format', async () => {
|
|
96
|
+
const result = await cli.exec(['info', '--format', '{{.OSType}}']);
|
|
97
|
+
expect(result).toBe('linux');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('tagImage', () => {
|
|
102
|
+
it('should tag an image', async () => {
|
|
103
|
+
// Pull a small image to test with
|
|
104
|
+
await cli.exec(['pull', 'hello-world']);
|
|
105
|
+
await cli.tagImage({
|
|
106
|
+
sourceImage: 'hello-world',
|
|
107
|
+
targetImage: 'test-cli-tag:latest',
|
|
108
|
+
});
|
|
109
|
+
// Verify the tag exists
|
|
110
|
+
const result = await cli.exec(['images', 'test-cli-tag:latest', '--format', '{{.Repository}}:{{.Tag}}']);
|
|
111
|
+
expect(result).toContain('test-cli-tag:latest');
|
|
112
|
+
// Cleanup
|
|
113
|
+
await cli.exec(['rmi', 'test-cli-tag:latest']);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
package/caps/Cli.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { $ } from 'bun'
|
|
2
|
+
import * as path from 'path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Docker architectures configuration
|
|
6
|
+
* Keys are the architecture directory names, values contain metadata
|
|
7
|
+
*/
|
|
8
|
+
const DOCKER_ARCHS = {
|
|
9
|
+
'linux-arm64': { archDir: 'linux-arm64', arch: 'arm64', os: 'linux' },
|
|
10
|
+
'linux-x64': { archDir: 'linux-x64', arch: 'amd64', os: 'linux' },
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
export async function capsule({
|
|
14
|
+
encapsulate,
|
|
15
|
+
CapsulePropertyTypes,
|
|
16
|
+
makeImportStack
|
|
17
|
+
}: {
|
|
18
|
+
encapsulate: any
|
|
19
|
+
CapsulePropertyTypes: any
|
|
20
|
+
makeImportStack: any
|
|
21
|
+
}) {
|
|
22
|
+
|
|
23
|
+
return encapsulate({
|
|
24
|
+
'#@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0': {
|
|
25
|
+
'#@stream44.studio/encapsulate/structs/Capsule': {},
|
|
26
|
+
'#': {
|
|
27
|
+
verbose: {
|
|
28
|
+
type: CapsulePropertyTypes.Literal,
|
|
29
|
+
value: false,
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
DOCKER_ARCHS: {
|
|
33
|
+
type: CapsulePropertyTypes.Constant,
|
|
34
|
+
value: DOCKER_ARCHS,
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get the current architecture platform
|
|
39
|
+
*/
|
|
40
|
+
getCurrentPlatform: {
|
|
41
|
+
type: CapsulePropertyTypes.Function,
|
|
42
|
+
value: function (this: any): 'arm64' | 'amd64' {
|
|
43
|
+
const arch = process.arch;
|
|
44
|
+
if (arch === 'arm64') {
|
|
45
|
+
return 'arm64';
|
|
46
|
+
}
|
|
47
|
+
return 'amd64';
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get the current platform's architecture key for DOCKER_ARCHS
|
|
53
|
+
*/
|
|
54
|
+
getCurrentPlatformArch: {
|
|
55
|
+
type: CapsulePropertyTypes.Function,
|
|
56
|
+
value: function (this: any): keyof typeof DOCKER_ARCHS {
|
|
57
|
+
const platform = this.getCurrentPlatform();
|
|
58
|
+
return platform === 'arm64' ? 'linux-arm64' : 'linux-x64';
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Core exec method — ALL Docker CLI calls go through here.
|
|
64
|
+
* Returns trimmed stdout text.
|
|
65
|
+
*/
|
|
66
|
+
exec: {
|
|
67
|
+
type: CapsulePropertyTypes.Function,
|
|
68
|
+
value: async function (this: any, args: string[], options?: { stdin?: string; retry?: number | boolean; retryDelayMs?: number }): Promise<string> {
|
|
69
|
+
const maxAttempts = options?.retry === true ? 3 : (options?.retry || 1);
|
|
70
|
+
const delayMs = options?.retryDelayMs ?? 5000;
|
|
71
|
+
|
|
72
|
+
const attempt = async (): Promise<string> => {
|
|
73
|
+
if (this.verbose) {
|
|
74
|
+
console.log(`[docker] Executing: docker ${args.join(' ')}`);
|
|
75
|
+
}
|
|
76
|
+
if (options?.stdin !== undefined) {
|
|
77
|
+
const proc = Bun.spawn(['docker', ...args], {
|
|
78
|
+
stdin: 'pipe',
|
|
79
|
+
stdout: 'pipe',
|
|
80
|
+
stderr: 'pipe',
|
|
81
|
+
});
|
|
82
|
+
proc.stdin.write(options.stdin);
|
|
83
|
+
proc.stdin.end();
|
|
84
|
+
const [stdout, stderr] = await Promise.all([
|
|
85
|
+
new Response(proc.stdout).text(),
|
|
86
|
+
new Response(proc.stderr).text(),
|
|
87
|
+
]);
|
|
88
|
+
await proc.exited;
|
|
89
|
+
if (proc.exitCode !== 0) {
|
|
90
|
+
throw new Error(`docker ${args[0]} failed (exit ${proc.exitCode}): ${stderr}`);
|
|
91
|
+
}
|
|
92
|
+
return stdout.trim();
|
|
93
|
+
}
|
|
94
|
+
const result = await $`docker ${args}`.text();
|
|
95
|
+
return result.trim();
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let lastError: unknown;
|
|
99
|
+
for (let i = 1; i <= maxAttempts; i++) {
|
|
100
|
+
try {
|
|
101
|
+
return await attempt();
|
|
102
|
+
} catch (err) {
|
|
103
|
+
lastError = err;
|
|
104
|
+
if (i < maxAttempts) {
|
|
105
|
+
console.error(` ⚠️ docker ${args[0]} attempt ${i}/${maxAttempts} failed, retrying in ${delayMs / 1000}s...`);
|
|
106
|
+
await new Promise(r => setTimeout(r, delayMs));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
throw lastError;
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Tag a Docker image — shared by Image and Context capsules
|
|
116
|
+
*/
|
|
117
|
+
tagImage: {
|
|
118
|
+
type: CapsulePropertyTypes.Function,
|
|
119
|
+
value: async function (this: any, options: { sourceImage: string; targetImage: string }): Promise<string> {
|
|
120
|
+
const { sourceImage, targetImage } = options;
|
|
121
|
+
if (this.verbose) {
|
|
122
|
+
console.log(`[docker:tagImage] Tagging image: ${sourceImage} -> ${targetImage}`);
|
|
123
|
+
}
|
|
124
|
+
return await this.exec(['tag', sourceImage, targetImage]);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}, {
|
|
130
|
+
importMeta: import.meta,
|
|
131
|
+
importStack: makeImportStack(),
|
|
132
|
+
capsuleName: '@stream44.studio/t44-docker.com/caps/Cli',
|
|
133
|
+
})
|
|
134
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
#!/usr/bin/env bun test --timeout 120000
|
|
2
|
+
|
|
3
|
+
import * as bunTest from 'bun:test'
|
|
4
|
+
import { run } from 't44/standalone-rt'
|
|
5
|
+
import { join } from 'path'
|
|
6
|
+
import { mkdir, writeFile } from 'fs/promises'
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
test: { describe, it, expect, workbenchDir },
|
|
10
|
+
container,
|
|
11
|
+
image,
|
|
12
|
+
} = await run(async ({ encapsulate, CapsulePropertyTypes, makeImportStack }: any) => {
|
|
13
|
+
const spine = await encapsulate({
|
|
14
|
+
'#@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0': {
|
|
15
|
+
'#@stream44.studio/encapsulate/structs/Capsule': {},
|
|
16
|
+
'#': {
|
|
17
|
+
test: {
|
|
18
|
+
type: CapsulePropertyTypes.Mapping,
|
|
19
|
+
value: 't44/caps/ProjectTest',
|
|
20
|
+
options: {
|
|
21
|
+
'#': {
|
|
22
|
+
bunTest,
|
|
23
|
+
env: {}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
container: {
|
|
28
|
+
type: CapsulePropertyTypes.Mapping,
|
|
29
|
+
value: './Container',
|
|
30
|
+
},
|
|
31
|
+
image: {
|
|
32
|
+
type: CapsulePropertyTypes.Mapping,
|
|
33
|
+
value: './Image',
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}, {
|
|
38
|
+
importMeta: import.meta,
|
|
39
|
+
importStack: makeImportStack(),
|
|
40
|
+
capsuleName: '@stream44.studio/t44-docker.com/caps/Container.test'
|
|
41
|
+
})
|
|
42
|
+
return { spine }
|
|
43
|
+
}, async ({ spine, apis }: any) => {
|
|
44
|
+
return apis[spine.capsuleSourceLineRef]
|
|
45
|
+
}, {
|
|
46
|
+
importMeta: import.meta
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Create a minimal test app and build a Docker image for container tests
|
|
51
|
+
*/
|
|
52
|
+
async function buildTestImage(baseDir: string, org: string, repo: string): Promise<string> {
|
|
53
|
+
await mkdir(baseDir, { recursive: true });
|
|
54
|
+
|
|
55
|
+
await writeFile(join(baseDir, 'index.ts'), `
|
|
56
|
+
const server = Bun.serve({
|
|
57
|
+
port: 3000,
|
|
58
|
+
fetch(req) {
|
|
59
|
+
return new Response("Hello from container test!");
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
console.log("READY");
|
|
63
|
+
console.log(\`Server running on port \${server.port}\`);
|
|
64
|
+
`);
|
|
65
|
+
|
|
66
|
+
await writeFile(join(baseDir, 'package.json'), JSON.stringify({
|
|
67
|
+
name: 'container-test-app',
|
|
68
|
+
version: '0.1.0',
|
|
69
|
+
scripts: {
|
|
70
|
+
start: 'bun run index.ts'
|
|
71
|
+
}
|
|
72
|
+
}, null, 2));
|
|
73
|
+
|
|
74
|
+
image.context.organization = org;
|
|
75
|
+
image.context.repository = repo;
|
|
76
|
+
image.context.appBaseDir = baseDir;
|
|
77
|
+
image.context.buildContextBaseDir = join(baseDir, '.~o/t44-docker.com');
|
|
78
|
+
image.context.verbose = false;
|
|
79
|
+
|
|
80
|
+
const currentArch = image.cli.getCurrentPlatformArch();
|
|
81
|
+
const result = await image.buildVariant({ variant: 'alpine', arch: currentArch });
|
|
82
|
+
return result.imageTag;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
describe('Container Capsule', () => {
|
|
86
|
+
|
|
87
|
+
describe('run / stop / cleanup', () => {
|
|
88
|
+
it('should run, verify, and cleanup a container', async () => {
|
|
89
|
+
const appDir = join(workbenchDir, 'container-run-test');
|
|
90
|
+
const imageTag = await buildTestImage(appDir, 'test-docker-com', 'container-run-test');
|
|
91
|
+
|
|
92
|
+
// Configure container via derive() and pass to run()
|
|
93
|
+
const containerContext = container.context.derive({
|
|
94
|
+
image: imageTag,
|
|
95
|
+
name: `t44-docker-test-${Date.now()}`,
|
|
96
|
+
ports: [{ internal: 3000, external: 13579 }],
|
|
97
|
+
detach: true,
|
|
98
|
+
waitFor: 'READY',
|
|
99
|
+
waitTimeout: 30000,
|
|
100
|
+
verbose: false,
|
|
101
|
+
showOutput: false,
|
|
102
|
+
});
|
|
103
|
+
// Ensure no leftover container from a previous run
|
|
104
|
+
await container.ensureStopped(containerContext);
|
|
105
|
+
|
|
106
|
+
// Run — stores _containerId internally
|
|
107
|
+
const containerId = await container.run(containerContext);
|
|
108
|
+
expect(containerId).toBeTruthy();
|
|
109
|
+
|
|
110
|
+
// Verify running
|
|
111
|
+
const isRunning = await container.isRunning({
|
|
112
|
+
...containerContext,
|
|
113
|
+
retryDelayMs: 1000,
|
|
114
|
+
requestTimeoutMs: 5000,
|
|
115
|
+
timeoutMs: 30000,
|
|
116
|
+
});
|
|
117
|
+
expect(isRunning).toBe(true);
|
|
118
|
+
|
|
119
|
+
// List
|
|
120
|
+
const containers = await container.list(containerContext);
|
|
121
|
+
expect(containers.length).toBeGreaterThan(0);
|
|
122
|
+
|
|
123
|
+
// Cleanup
|
|
124
|
+
await container.cleanup(containerContext);
|
|
125
|
+
|
|
126
|
+
// Cleanup image
|
|
127
|
+
await image.removeImage({ image: imageTag, force: true }).catch(() => { });
|
|
128
|
+
}, 120000);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('ensureStopped', () => {
|
|
132
|
+
it('should remove existing container with same name', async () => {
|
|
133
|
+
const appDir = join(workbenchDir, 'container-ensure-test');
|
|
134
|
+
const imageTag = await buildTestImage(appDir, 'test-docker-com', 'container-ensure-test');
|
|
135
|
+
|
|
136
|
+
const containerName = `t44-docker-ensure-${Date.now()}`;
|
|
137
|
+
|
|
138
|
+
// Start a container via derive()
|
|
139
|
+
const containerContext = container.context.derive({
|
|
140
|
+
image: imageTag,
|
|
141
|
+
name: containerName,
|
|
142
|
+
ports: [{ internal: 3000, external: 13580 }],
|
|
143
|
+
detach: true,
|
|
144
|
+
waitFor: 'READY',
|
|
145
|
+
waitTimeout: 30000,
|
|
146
|
+
verbose: false,
|
|
147
|
+
showOutput: false,
|
|
148
|
+
});
|
|
149
|
+
const containerId = await container.run(containerContext);
|
|
150
|
+
expect(containerId).toBeTruthy();
|
|
151
|
+
|
|
152
|
+
// ensureStopped should remove it
|
|
153
|
+
await container.ensureStopped(containerContext);
|
|
154
|
+
|
|
155
|
+
// Verify it's gone
|
|
156
|
+
const output = await container.containers.list({
|
|
157
|
+
all: true,
|
|
158
|
+
filter: `name=${containerName}`,
|
|
159
|
+
format: '{{.Names}}',
|
|
160
|
+
});
|
|
161
|
+
const lines = (output as string).split('\n').filter((l: string) => l.trim() === containerName);
|
|
162
|
+
expect(lines.length).toBe(0);
|
|
163
|
+
|
|
164
|
+
// Cleanup image
|
|
165
|
+
await image.removeImage({ image: imageTag, force: true }).catch(() => { });
|
|
166
|
+
}, 120000);
|
|
167
|
+
});
|
|
168
|
+
});
|