/
selenoid.ts
146 lines (125 loc) · 5.06 KB
/
selenoid.ts
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
146
import path from 'path';
import { promisify } from 'util';
import { mkdir, writeFile, copyFile, lstatSync, existsSync } from 'fs';
import { Config, BrowserConfig } from '../../types';
import { downloadBinary, getCreeveyCache } from '../utils';
import { pullImages, runImage } from '../docker';
import { Octokit } from '@octokit/core';
import { subscribeOn } from '../messages';
import { isWorker } from 'cluster';
import { chmod, exec } from 'shelljs';
const mkdirAsync = promisify(mkdir);
const writeFileAsync = promisify(writeFile);
const copyFileAsync = promisify(copyFile);
async function createSelenoidConfig(browsers: BrowserConfig[], { useDocker }: { useDocker: boolean }): Promise<string> {
const selenoidConfig: {
[browser: string]: {
default: string;
versions: { [version: string]: { image: string | string[]; port: string; path: string } };
};
} = {};
const selenoidConfigDir = path.join(getCreeveyCache(), 'selenoid');
browsers.forEach(
({
browserName,
version = 'latest',
dockerImage = `selenoid/${browserName}:${version}`,
webdriverCommand = [],
}) => {
if (!selenoidConfig[browserName]) selenoidConfig[browserName] = { default: version, versions: {} };
if (!useDocker && webdriverCommand.length == 0)
throw new Error('Please specify "webdriverCommand" browser option with path to browser webdriver');
selenoidConfig[browserName].versions[version] = {
image: useDocker ? dockerImage : webdriverCommand,
port: '4444',
path: !useDocker || browserName == 'chrome' || browserName == 'opera' ? '/' : '/wd/hub',
};
},
);
await mkdirAsync(selenoidConfigDir, { recursive: true });
await writeFileAsync(path.join(selenoidConfigDir, 'browsers.json'), JSON.stringify(selenoidConfig));
return selenoidConfigDir;
}
async function downloadSelenoidBinary(destination: string): Promise<void> {
const platformNameMapping: Partial<Record<NodeJS.Platform, string>> = {
darwin: 'selenoid_darwin_amd64',
linux: 'selenoid_linux_amd64',
win32: 'selenoid_windows_amd64.exe',
};
const octokit = new Octokit();
const response = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
owner: 'aerokube',
repo: 'selenoid',
});
const { assets } = response.data;
const { browser_download_url: downloadUrl, size: binarySize } =
assets.find(({ name }) => platformNameMapping[process.platform] == name) ?? {};
if (existsSync(destination) && lstatSync(destination).size == binarySize) return;
if (!downloadUrl) {
throw new Error(
`Couldn't get download url for selenoid binary. Please download it manually from "https://github.com/aerokube/selenoid/releases/latest" and define "selenoidPath" option in the Creevey config`,
);
}
return downloadBinary(downloadUrl, destination);
}
export async function startSelenoidStandalone(config: Config, debug: boolean): Promise<void> {
config.gridUrl = 'http://localhost:4444/wd/hub';
if (isWorker) return;
const browsers = (Object.values(config.browsers) as BrowserConfig[]).filter((browser) => !browser.gridUrl);
const selenoidConfigDir = await createSelenoidConfig(browsers, { useDocker: false });
const binaryPath = path.join(selenoidConfigDir, process.platform == 'win32' ? 'selenoid.exe' : 'selenoid');
if (config.selenoidPath) {
await copyFileAsync(path.resolve(config.selenoidPath), binaryPath);
} else {
await downloadSelenoidBinary(binaryPath);
}
// TODO Download browser webdrivers
try {
if (process.platform != 'win32') chmod('+x', binaryPath);
} catch (_) {
/* noop */
}
const selenoidProcess = exec(`${binaryPath} -conf ./browsers.json -disable-docker`, {
async: true,
cwd: selenoidConfigDir,
});
if (debug) {
selenoidProcess.stdout?.pipe(process.stdout);
selenoidProcess.stderr?.pipe(process.stderr);
}
subscribeOn('shutdown', () => selenoidProcess.kill());
}
export async function startSelenoidContainer(config: Config, debug: boolean): Promise<string> {
const browsers = (Object.values(config.browsers) as BrowserConfig[]).filter((browser) => !browser.gridUrl);
const images: string[] = [];
let limit = 0;
browsers.forEach(
({
browserName,
version = 'latest',
limit: browserLimit = 1,
dockerImage = `selenoid/${browserName}:${version}`,
}) => {
limit += browserLimit;
images.push(dockerImage);
},
);
const selenoidImage = config.dockerImage;
const pullOptions = { auth: config.dockerAuth, platform: config.dockerImagePlatform };
if (config.pullImages) {
await pullImages([selenoidImage], pullOptions);
await pullImages(images, pullOptions);
}
// TODO Allow pass custom options
const selenoidOptions = {
ExposedPorts: { '4444/tcp': {} },
HostConfig: {
PortBindings: { '4444/tcp': [{ HostPort: '4444' }] },
Binds: [
'/var/run/docker.sock:/var/run/docker.sock',
`${await createSelenoidConfig(browsers, { useDocker: true })}:/etc/selenoid/:ro`,
],
},
};
return runImage(selenoidImage, ['-limit', String(limit)], selenoidOptions, debug);
}