Skip to content

Commit

Permalink
carefully parse /etc/os-release
Browse files Browse the repository at this point in the history
  • Loading branch information
aslushnikov committed Jul 17, 2020
1 parent b6639ed commit 733e6e5
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/server/validateDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export async function validateDependencies(browserPath: string, browser: Browser
return;
// Check Ubuntu version.
const missingPackages = new Set();
const osRelease = await readFileAsync('/etc/os-release', 'utf8').catch(e => '');
const [ubuntuName = '', ubuntuVersion = ''] = osRelease.trim().split('\n');
if (ubuntuName.toLowerCase().includes('ubuntu') && ubuntuVersion.includes('18.04')) {

const ubuntuVersion = await getUbuntuVersion();
if (ubuntuVersion === '18.04') {
// Translate missing dependencies to package names to install with apt.
for (const missingDep of missingDeps) {
const packageName = LIBRARY_TO_PACKAGE_NAME_UBUNTU_18_04[missingDep];
Expand All @@ -61,7 +61,7 @@ export async function validateDependencies(browserPath: string, browser: Browser
if (missingPackages.size) {
missingPackagesMessage = [
` Install missing packages with:`,
` apt-get install -y ${[...missingPackages].join('\\\n ')}`,
` apt-get install ${[...missingPackages].join('\\\n ')}`,
``,
``,
].join('\n');
Expand Down Expand Up @@ -122,6 +122,26 @@ function lddAsync(filePath: string): Promise<{stdout: string, stderr: string, co
});
}

async function getUbuntuVersion(): Promise<string> {
const osRelease = await readFileAsync('/etc/os-release', 'utf8').catch(e => '');
if (!osRelease)
return '';
const fields = new Map();
for (const line of osRelease.split('\n')) {
const tokens = line.split('=');
const name = tokens.shift();
let value = tokens.join('=').trim();
if (value.startsWith('"') && value.endsWith('"'))
value = value.substring(1, value.length - 1);
if (!name)
continue;
fields.set(name.toLowerCase(), value);
}
if (!fields.get('name') || fields.get('name').toLowerCase() !== 'ubuntu')
return '';
return fields.get('version_id') || '';
}

// This list is generated with https://gist.github.com/aslushnikov/2766200430228c3700537292fccad064
const LIBRARY_TO_PACKAGE_NAME_UBUNTU_18_04: { [s: string]: string} = {
'libEGL.so.1': 'libegl1',
Expand Down

0 comments on commit 733e6e5

Please sign in to comment.