-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathmicro.ts
More file actions
81 lines (74 loc) · 2.29 KB
/
micro.ts
File metadata and controls
81 lines (74 loc) · 2.29 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
import Docker from 'dockerode';
import { Writable } from 'stream';
export interface DockerWrapper {
url: string;
container: Docker.Container;
}
const docker = new Docker();
/**
* Start the micro Docker container to accept events.
* @param {boolean} [isRemote] For local runs, we use the local address, for remote, a custom host which has been set on the remote machine.
* @return {*}
*/
export const start = (isRemote?: boolean) => {
return docker
.createContainer({
Image: 'snowplow/snowplow-micro:2.0.0',
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: ['--collector-config', '/config/micro.conf', '--iglu', '/config/iglu.json'],
OpenStdin: false,
StdinOnce: false,
HostConfig: {
Binds: [`${process.cwd()}/test/micro-config:/config`],
PortBindings: {
'9090/tcp': [
{
HostIp: '',
HostPort: '',
},
],
},
},
ExposedPorts: {
'9090/tcp': {},
},
})
.then((c) => {
return c.start().then(() => {
const outs = new Writable({
write(chunk, _, callback) {
let found = /(REST interface bound|http4s.+started at)/.test(chunk.toString());
if (found) this.end();
callback();
},
});
c.attach({ stream: true, stdout: true, stderr: true }, (_, stream) => {
stream?.pipe(process.stdout);
stream?.pipe(outs);
});
return new Promise<DockerWrapper>((resolve) => {
outs.on('finish', () =>
c.inspect().then((info) => {
resolve({
container: c,
url: `${isRemote ? 'snowplow-js-tracker.local' : '127.0.0.1'}:${
info.NetworkSettings.Ports['9090/tcp'][0].HostPort
}`,
});
})
);
});
});
});
};
export const stop = (container: Docker.Container) => container.stop().then(() => container.remove());
export async function fetchResults() {
const dockerUrl = (await browser.sharedStore.get('dockerInstanceUrl')) as string;
if (!dockerUrl) {
throw 'dockerInstanceUrl not set in shared store.';
}
return (await fetch(`http://${dockerUrl}/micro/good`)).json();
}