From 733e6e5c43ccdc266cb51d7fa3e6eb18fe111b73 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 17 Jul 2020 16:17:11 -0700 Subject: [PATCH] carefully parse /etc/os-release --- src/server/validateDependencies.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/server/validateDependencies.ts b/src/server/validateDependencies.ts index 7dbb0c4289afd9..e4055f233b5dce 100644 --- a/src/server/validateDependencies.ts +++ b/src/server/validateDependencies.ts @@ -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]; @@ -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'); @@ -122,6 +122,26 @@ function lddAsync(filePath: string): Promise<{stdout: string, stderr: string, co }); } +async function getUbuntuVersion(): Promise { + 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',