-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
dependencies.ts
359 lines (328 loc) · 14.6 KB
/
dependencies.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs';
import path from 'path';
import * as os from 'os';
import childProcess from 'child_process';
import * as utils from '../../utils';
import { spawnAsync } from '../../utils/spawnAsync';
import { hostPlatform } from '../../utils/hostPlatform';
import { buildPlaywrightCLICommand } from '.';
import { deps } from './nativeDeps';
import { getPlaywrightVersion } from '../../common/userAgent';
const BIN_DIRECTORY = path.join(__dirname, '..', '..', '..', 'bin');
const packageJSON = require('../../../package.json');
const dockerVersionFilePath = '/ms-playwright/.docker-info';
export async function writeDockerVersion(dockerImageNameTemplate: string) {
await fs.promises.mkdir(path.dirname(dockerVersionFilePath), { recursive: true });
await fs.promises.writeFile(dockerVersionFilePath, JSON.stringify({
driverVersion: packageJSON.version,
dockerImageName: dockerImageNameTemplate.replace('%version%', packageJSON.version),
}, null, 2), 'utf8');
// Make sure version file is globally accessible.
await fs.promises.chmod(dockerVersionFilePath, 0o777);
}
async function readDockerVersion(): Promise<null | { driverVersion: string, dockerImageName: string }> {
return await fs.promises.readFile(dockerVersionFilePath, 'utf8')
.then(text => JSON.parse(text))
.catch(e => null);
}
const checkExecutable = (filePath: string) => fs.promises.access(filePath, fs.constants.X_OK).then(() => true).catch(e => false);
function isSupportedWindowsVersion(): boolean {
if (os.platform() !== 'win32' || os.arch() !== 'x64')
return false;
const [major, minor] = os.release().split('.').map(token => parseInt(token, 10));
// This is based on: https://stackoverflow.com/questions/42524606/how-to-get-windows-version-using-node-js/44916050#44916050
// The table with versions is taken from: https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexw#remarks
// Windows 7 is not supported and is encoded as `6.1`.
return major > 6 || (major === 6 && minor > 1);
}
export type DependencyGroup = 'chromium' | 'firefox' | 'webkit' | 'tools';
export async function installDependenciesWindows(targets: Set<DependencyGroup>, dryRun: boolean): Promise<void> {
if (targets.has('chromium')) {
const command = 'powershell.exe';
const args = ['-ExecutionPolicy', 'Bypass', '-File', path.join(BIN_DIRECTORY, 'install_media_pack.ps1')];
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
const { code } = await spawnAsync(command, args, { cwd: BIN_DIRECTORY, stdio: 'inherit' });
if (code !== 0)
throw new Error('Failed to install windows dependencies!');
}
}
export async function installDependenciesLinux(targets: Set<DependencyGroup>, dryRun: boolean) {
const libraries: string[] = [];
for (const target of targets) {
const info = deps[hostPlatform];
if (!info) {
console.warn('Cannot install dependencies for this linux distribution!'); // eslint-disable-line no-console
return;
}
libraries.push(...info[target]);
}
const uniqueLibraries = Array.from(new Set(libraries));
if (!dryRun)
console.log('Installing Ubuntu dependencies...'); // eslint-disable-line no-console
const commands: string[] = [];
commands.push('apt-get update');
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
...uniqueLibraries,
].join(' '));
const { command, args, elevatedPermissions } = await transformCommandsForRoot(commands);
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
if (elevatedPermissions)
console.log('Switching to root user to install dependencies...'); // eslint-disable-line no-console
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
await new Promise<void>((resolve, reject) => {
child.on('exit', (code: number) => code === 0 ? resolve() : reject(new Error(`Installation process exited with code: ${code}`)));
child.on('error', reject);
});
}
export async function validateDependenciesWindows(windowsExeAndDllDirectories: string[]) {
const directoryPaths = windowsExeAndDllDirectories;
const lddPaths: string[] = [];
for (const directoryPath of directoryPaths)
lddPaths.push(...(await executablesOrSharedLibraries(directoryPath)));
const allMissingDeps = await Promise.all(lddPaths.map(lddPath => missingFileDependenciesWindows(lddPath)));
const missingDeps: Set<string> = new Set();
for (const deps of allMissingDeps) {
for (const dep of deps)
missingDeps.add(dep);
}
if (!missingDeps.size)
return;
let isCrtMissing = false;
let isMediaFoundationMissing = false;
for (const dep of missingDeps) {
if (dep.startsWith('api-ms-win-crt') || dep === 'vcruntime140.dll' || dep === 'vcruntime140_1.dll' || dep === 'msvcp140.dll')
isCrtMissing = true;
else if (dep === 'mf.dll' || dep === 'mfplat.dll' || dep === 'msmpeg2vdec.dll' || dep === 'evr.dll' || dep === 'avrt.dll')
isMediaFoundationMissing = true;
}
const details = [];
if (isCrtMissing) {
details.push(
`Some of the Universal C Runtime files cannot be found on the system. You can fix`,
`that by installing Microsoft Visual C++ Redistributable for Visual Studio from:`,
`https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads`,
``);
}
if (isMediaFoundationMissing) {
details.push(
`Some of the Media Foundation files cannot be found on the system. If you are`,
`on Windows Server try fixing this by running the following command in PowerShell`,
`as Administrator:`,
``,
` Install-WindowsFeature Server-Media-Foundation`,
``,
`For Windows N editions visit:`,
`https://support.microsoft.com/en-us/help/3145500/media-feature-pack-list-for-windows-n-editions`,
``);
}
details.push(
`Full list of missing libraries:`,
` ${[...missingDeps].join('\n ')}`,
``);
const message = `Host system is missing dependencies!\n\n${details.join('\n')}`;
if (isSupportedWindowsVersion()) {
throw new Error(message);
} else {
// eslint-disable-next-line no-console
console.warn(`WARNING: running on unsupported windows version!`);
// eslint-disable-next-line no-console
console.warn(message);
}
}
export async function validateDependenciesLinux(sdkLanguage: string, linuxLddDirectories: string[], dlOpenLibraries: string[]) {
const directoryPaths = linuxLddDirectories;
const lddPaths: string[] = [];
for (const directoryPath of directoryPaths)
lddPaths.push(...(await executablesOrSharedLibraries(directoryPath)));
const missingDepsPerFile = await Promise.all(lddPaths.map(lddPath => missingFileDependencies(lddPath, directoryPaths)));
const missingDeps: Set<string> = new Set();
for (const deps of missingDepsPerFile) {
for (const dep of deps)
missingDeps.add(dep);
}
for (const dep of (await missingDLOPENLibraries(dlOpenLibraries)))
missingDeps.add(dep);
if (!missingDeps.size)
return;
const allMissingDeps = new Set(missingDeps);
// Check Ubuntu version.
const missingPackages = new Set();
const libraryToPackageNameMapping = {
...(deps[hostPlatform]?.lib2package || {}),
...MANUAL_LIBRARY_TO_PACKAGE_NAME_UBUNTU,
};
// Translate missing dependencies to package names to install with apt.
for (const missingDep of missingDeps) {
const packageName = libraryToPackageNameMapping[missingDep];
if (packageName) {
missingPackages.add(packageName);
missingDeps.delete(missingDep);
}
}
const maybeSudo = (process.getuid() !== 0) && os.platform() !== 'win32' ? 'sudo ' : '';
const dockerInfo = await readDockerVersion();
const errorLines = [
`Host system is missing dependencies to run browsers.`,
];
// Ignore patch versions when comparing docker container version and Playwright version:
// we **NEVER** roll browsers in patch releases, so native dependencies do not change.
if (dockerInfo && !dockerInfo.driverVersion.startsWith(getPlaywrightVersion(true /* majorMinorOnly */) + '.')) {
// We are running in a docker container with unmatching version.
// In this case, we know how to install dependencies in it.
const pwVersion = getPlaywrightVersion();
const requiredDockerImage = dockerInfo.dockerImageName.replace(dockerInfo.driverVersion, pwVersion);
errorLines.push(...[
`This is most likely due to docker image version not matching Playwright version:`,
`- Playwright: ${pwVersion}`,
`- Docker: ${dockerInfo.driverVersion}`,
``,
`Either:`,
`- (recommended) use docker image "${requiredDockerImage}"`,
`- (alternative 1) run the following command inside docker to install missing dependencies:`,
``,
` ${maybeSudo}${buildPlaywrightCLICommand(sdkLanguage, 'install-deps')}`,
``,
`- (alternative 2) use Aptitude inside docker:`,
``,
` ${maybeSudo}apt-get install ${[...missingPackages].join('\\\n ')}`,
``,
`<3 Playwright Team`,
]);
} else if (missingPackages.size && !missingDeps.size) {
// Only known dependencies are missing for browsers.
// Suggest installation with a Playwright CLI.
errorLines.push(...[
`Please install them with the following command:`,
``,
` ${maybeSudo}${buildPlaywrightCLICommand(sdkLanguage, 'install-deps')}`,
``,
`Alternatively, use Aptitude:`,
` ${maybeSudo}apt-get install ${[...missingPackages].join('\\\n ')}`,
``,
`<3 Playwright Team`,
]);
} else {
// Unhappy path: we either run on unknown distribution, or we failed to resolve all missing
// libraries to package names.
// Print missing libraries only:
errorLines.push(...[
`Missing libraries:`,
...[...allMissingDeps].map(dep => ' ' + dep),
]);
}
throw new Error('\n' + utils.wrapInASCIIBox(errorLines.join('\n'), 1));
}
function isSharedLib(basename: string) {
switch (os.platform()) {
case 'linux':
return basename.endsWith('.so') || basename.includes('.so.');
case 'win32':
return basename.endsWith('.dll');
default:
return false;
}
}
async function executablesOrSharedLibraries(directoryPath: string): Promise<string[]> {
if (!fs.existsSync(directoryPath))
return [];
const allPaths = (await fs.promises.readdir(directoryPath)).map(file => path.resolve(directoryPath, file));
const allStats = await Promise.all(allPaths.map(aPath => fs.promises.stat(aPath)));
const filePaths = allPaths.filter((aPath, index) => (allStats[index] as any).isFile());
const executablersOrLibraries = (await Promise.all(filePaths.map(async filePath => {
const basename = path.basename(filePath).toLowerCase();
if (isSharedLib(basename))
return filePath;
if (await checkExecutable(filePath))
return filePath;
return false;
}))).filter(Boolean);
return executablersOrLibraries as string[];
}
async function missingFileDependenciesWindows(filePath: string): Promise<Array<string>> {
const executable = path.join(__dirname, '..', '..', '..', 'bin', 'PrintDeps.exe');
const dirname = path.dirname(filePath);
const { stdout, code } = await spawnAsync(executable, [filePath], {
cwd: dirname,
env: {
...process.env,
LD_LIBRARY_PATH: process.env.LD_LIBRARY_PATH ? `${process.env.LD_LIBRARY_PATH}:${dirname}` : dirname,
},
});
if (code !== 0)
return [];
const missingDeps = stdout.split('\n').map(line => line.trim()).filter(line => line.endsWith('not found') && line.includes('=>')).map(line => line.split('=>')[0].trim().toLowerCase());
return missingDeps;
}
async function missingFileDependencies(filePath: string, extraLDPaths: string[]): Promise<Array<string>> {
const dirname = path.dirname(filePath);
let LD_LIBRARY_PATH = extraLDPaths.join(':');
if (process.env.LD_LIBRARY_PATH)
LD_LIBRARY_PATH = `${process.env.LD_LIBRARY_PATH}:${LD_LIBRARY_PATH}`;
const { stdout, code } = await spawnAsync('ldd', [filePath], {
cwd: dirname,
env: {
...process.env,
LD_LIBRARY_PATH,
},
});
if (code !== 0)
return [];
const missingDeps = stdout.split('\n').map(line => line.trim()).filter(line => line.endsWith('not found') && line.includes('=>')).map(line => line.split('=>')[0].trim());
return missingDeps;
}
async function missingDLOPENLibraries(libraries: string[]): Promise<string[]> {
if (!libraries.length)
return [];
// NOTE: Using full-qualified path to `ldconfig` since `/sbin` is not part of the
// default PATH in CRON.
// @see https://github.com/microsoft/playwright/issues/3397
const { stdout, code, error } = await spawnAsync('/sbin/ldconfig', ['-p'], {});
if (code !== 0 || error)
return [];
const isLibraryAvailable = (library: string) => stdout.toLowerCase().includes(library.toLowerCase());
return libraries.filter(library => !isLibraryAvailable(library));
}
const MANUAL_LIBRARY_TO_PACKAGE_NAME_UBUNTU: { [s: string]: string} = {
// libgstlibav.so (the only actual library provided by gstreamer1.0-libav) is not
// in the ldconfig cache, so we detect the actual library required for playing h.264
// and if it's missing recommend installing missing gstreamer lib.
// gstreamer1.0-libav -> libavcodec57 -> libx264-152
'libx264.so': 'gstreamer1.0-libav',
};
function quoteProcessArgs(args: string[]): string[] {
return args.map(arg => {
if (arg.includes(' '))
return `"${arg}"`;
return arg;
});
}
export async function transformCommandsForRoot(commands: string[]): Promise<{ command: string, args: string[], elevatedPermissions: boolean}> {
const isRoot = process.getuid() === 0;
if (isRoot)
return { command: 'sh', args: ['-c', `${commands.join('&& ')}`], elevatedPermissions: false };
const sudoExists = await spawnAsync('which', ['sudo']);
if (sudoExists.code === 0)
return { command: 'sudo', args: ['--', 'sh', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
return { command: 'su', args: ['root', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
}