/
docker.js
145 lines (130 loc) Β· 3.82 KB
/
docker.js
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
const debug = require('debug')('loki:chrome:docker');
const os = require('os');
const path = require('path');
const { execSync } = require('child_process');
const execa = require('execa');
const waitOn = require('wait-on');
const CDP = require('chrome-remote-interface');
const getRandomPort = require('get-port');
const { ensureDependencyAvailable } = require('../../dependency-detection');
const createChromeTarget = require('./create-chrome-target');
const getLocalIPAddress = () => {
const interfaces = os.networkInterfaces();
const ips = Object.keys(interfaces)
.map(key =>
interfaces[key]
.filter(({ family, internal }) => family === 'IPv4' && !internal)
.map(({ address }) => address)
)
.reduce((acc, current) => acc.concat(current), []);
return ips[0];
};
const waitOnCDPAvailable = port =>
new Promise((resolve, reject) => {
waitOn(
{
resources: [`tcp:localhost:${port}`],
delay: 50,
interval: 100,
timeout: 5000,
},
err => {
if (err) {
reject(err);
} else {
resolve();
}
}
);
});
function createChromeDockerTarget({
baseUrl = 'http://localhost:6006',
chromeDockerImage = 'yukinying/chrome-headless',
chromeFlags = ['--headless', '--disable-gpu', '--hide-scrollbars'],
}) {
let port;
let dockerId;
let dockerUrl = baseUrl;
const storybookUrl = baseUrl;
const dockerPath = 'docker';
const runArgs = ['run', '--rm', '-d', '-P'];
if (!process.env.CI) {
runArgs.push(`--security-opt=seccomp=${__dirname}/docker-seccomp.json`);
}
if (baseUrl.indexOf('http://localhost') === 0) {
const ip = getLocalIPAddress();
if (!ip) {
throw new Error(
'Unable to detect local IP address, try passing --host argument'
);
}
dockerUrl = baseUrl.replace('localhost', ip);
} else if (baseUrl.indexOf('file:') === 0) {
const staticPath = path.resolve(baseUrl.substr('file:'.length));
const staticMountPath = '/var/loki';
runArgs.push('-v');
runArgs.push(`${staticPath}:${staticMountPath}`);
dockerUrl = `file://${staticMountPath}`;
}
async function start() {
port = await getRandomPort();
ensureDependencyAvailable('docker');
const args = runArgs
.concat([
'-p',
`${port}:${port}`,
chromeDockerImage,
'--disable-datasaver-prompt',
'--no-first-run',
'--disable-extensions',
'--remote-debugging-address=0.0.0.0',
`--remote-debugging-port=${port}`,
])
.concat(chromeFlags);
debug(
`Launching chrome in docker with command "${dockerPath} ${args.join(
' '
)}"`
);
const { code, stdout, stderr } = await execa(dockerPath, args);
if (code === 0) {
await waitOnCDPAvailable(port);
dockerId = stdout;
debug(`Docker started with id ${dockerId}`);
} else {
throw new Error(`Failed starting docker, ${stderr}`);
}
}
async function stop() {
if (dockerId) {
debug(`Killing chrome docker instance with id ${dockerId}`);
await execa('docker', ['kill', dockerId]);
} else {
debug('No chrome docker instance to kill');
}
}
async function createNewDebuggerInstance() {
debug(`Launching new tab with debugger at port ${port}`);
const target = await CDP.New({ port });
debug(`Launched with target id ${target.id}`);
const client = await CDP({ port, target });
client.close = () => {
debug('New closing tab');
return CDP.Close({ port, id: target.id });
};
return client;
}
process.on('SIGINT', () => {
if (dockerId) {
execSync(`${dockerPath} kill ${dockerId}`);
}
});
return createChromeTarget(
start,
stop,
createNewDebuggerInstance,
dockerUrl,
storybookUrl
);
}
module.exports = createChromeDockerTarget;