@lde/task-runner-docker 0.0.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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # task-runner-docker
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build task-runner-docker` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test task-runner-docker` to execute the unit tests via [Jest](https://jestjs.io).
@@ -0,0 +1,17 @@
1
+ import { TaskRunner } from '@lde/task-runner';
2
+ import Docker, { Container } from 'dockerode';
3
+ export interface DockerTaskRunnerOptions {
4
+ image: string;
5
+ containerName?: string;
6
+ port?: number;
7
+ mountDir?: string;
8
+ docker?: Docker;
9
+ }
10
+ export declare class DockerTaskRunner implements TaskRunner<Container> {
11
+ private readonly options;
12
+ constructor(options: DockerTaskRunnerOptions);
13
+ wait(task: Container): Promise<string>;
14
+ run(command: string): Promise<Container>;
15
+ stop(task: Container): Promise<string>;
16
+ }
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,MAAM,EAAE,EAAE,SAAS,EAA0B,MAAM,WAAW,CAAC;AAEtE,MAAM,WAAW,uBAAuB;IACtC,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,gBAAiB,YAAW,UAAU,CAAC,SAAS,CAAC;IAC5D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAEb,OAAO,EAAE,uBAAuB;IAOtC,IAAI,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAmBtC,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAuExC,IAAI,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;CAS7C"}
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ import process from 'node:process';
2
+ import Docker from 'dockerode';
3
+ export class DockerTaskRunner {
4
+ options;
5
+ constructor(options) {
6
+ this.options = {
7
+ docker: new Docker(),
8
+ ...options,
9
+ };
10
+ }
11
+ async wait(task) {
12
+ const result = await task.wait();
13
+ const logs = (await task.logs({
14
+ stdout: true,
15
+ stderr: true,
16
+ follow: false,
17
+ })).toString();
18
+ if (result.StatusCode !== 0) {
19
+ throw new Error(`Task failed with status code ${result.StatusCode}: ${logs})`);
20
+ }
21
+ return logs;
22
+ }
23
+ async run(command) {
24
+ if (this.options.containerName) {
25
+ try {
26
+ await this.options.docker
27
+ .getContainer(this.options.containerName)
28
+ .remove({ force: true });
29
+ }
30
+ catch {
31
+ // Ignore if the container does not exist yet.
32
+ }
33
+ }
34
+ const pull = await this.options.docker.pull(this.options.image);
35
+ const err = await new Promise((resolve) => this.options.docker.modem.followProgress(pull, resolve));
36
+ if (err) {
37
+ throw err;
38
+ }
39
+ const containerOptions = {
40
+ Entrypoint: ['sh', '-c'],
41
+ Image: this.options.image,
42
+ Cmd: [command],
43
+ name: this.options.containerName,
44
+ User: `${process.getuid()}:${process.getgid()}`,
45
+ };
46
+ if (this.options.port) {
47
+ containerOptions.ExposedPorts = {
48
+ [`${this.options.port}/tcp`]: {},
49
+ };
50
+ containerOptions.HostConfig = {
51
+ PortBindings: {
52
+ [`${this.options.port}/tcp`]: [
53
+ {
54
+ HostPort: this.options.port.toString(),
55
+ },
56
+ ],
57
+ },
58
+ };
59
+ }
60
+ if (this.options.mountDir) {
61
+ containerOptions.HostConfig = {
62
+ ...containerOptions.HostConfig,
63
+ Binds: [`${this.options.mountDir}:/mount`],
64
+ };
65
+ containerOptions.WorkingDir = '/mount';
66
+ }
67
+ const container = await this.options.docker.createContainer(containerOptions);
68
+ await container.start();
69
+ const logStream = await container.logs({
70
+ follow: true,
71
+ stdout: true,
72
+ stderr: true,
73
+ tail: 100,
74
+ });
75
+ logStream.on('data', (data) => {
76
+ console.log(data.toString());
77
+ // process.stdout.write(chunk.toString());
78
+ });
79
+ return container;
80
+ }
81
+ async stop(task) {
82
+ const logs = await task.logs({
83
+ stdout: true,
84
+ stderr: true,
85
+ follow: false,
86
+ });
87
+ await task.remove({ force: true });
88
+ return logs.toString();
89
+ }
90
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@lde/task-runner-docker",
3
+ "version": "0.0.3",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "development": "./src/index.ts",
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!**/*.tsbuildinfo"
20
+ ],
21
+ "dependencies": {
22
+ "dockerode": "^4.0.7",
23
+ "tslib": "^2.3.0",
24
+ "@lde/task-runner": "0.0.3"
25
+ },
26
+ "devDependencies": {
27
+ "@types/dockerode": "^3.3.40"
28
+ }
29
+ }