Loads the Linux operating system context.
()
| 470 | |
| 471 | /** Loads the Linux operating system context. */ |
| 472 | async function getLinuxInfo(): Promise<OsContext> { |
| 473 | // By default, we cannot assume anything about the distribution or Linux |
| 474 | // version. `os.release()` returns the kernel version and we assume a generic |
| 475 | // "Linux" name, which will be replaced down below. |
| 476 | const linuxInfo: OsContext = { |
| 477 | kernel_version: os.release(), |
| 478 | name: 'Linux', |
| 479 | }; |
| 480 | |
| 481 | try { |
| 482 | // We start guessing the distribution by listing files in the /etc |
| 483 | // directory. This is were most Linux distributions (except Knoppix) store |
| 484 | // release files with certain distribution-dependent meta data. We search |
| 485 | // for exactly one known file defined in `LINUX_DISTROS` and exit if none |
| 486 | // are found. In case there are more than one file, we just stick with the |
| 487 | // first one. |
| 488 | const etcFiles = await readDirAsync('/etc'); |
| 489 | const distroFile = LINUX_DISTROS.find(file => etcFiles.includes(file.name)); |
| 490 | if (!distroFile) { |
| 491 | return linuxInfo; |
| 492 | } |
| 493 | |
| 494 | // Once that file is known, load its contents. To make searching in those |
| 495 | // files easier, we lowercase the file contents. Since these files are |
| 496 | // usually quite small, this should not allocate too much memory and we only |
| 497 | // hold on to it for a very short amount of time. |
| 498 | const distroPath = join('/etc', distroFile.name); |
| 499 | const contents = (await readFileAsync(distroPath, { encoding: 'utf-8' })).toLowerCase(); |
| 500 | |
| 501 | // Some Linux distributions store their release information in the same file |
| 502 | // (e.g. RHEL and Centos). In those cases, we scan the file for an |
| 503 | // identifier, that basically consists of the first word of the linux |
| 504 | // distribution name (e.g. "red" for Red Hat). In case there is no match, we |
| 505 | // just assume the first distribution in our list. |
| 506 | const { distros } = distroFile; |
| 507 | linuxInfo.name = distros.find(d => contents.indexOf(getLinuxDistroId(d)) >= 0) || distros[0]; |
| 508 | |
| 509 | // Based on the found distribution, we can now compute the actual version |
| 510 | // number. This is different for every distribution, so several strategies |
| 511 | // are computed in `LINUX_VERSIONS`. |
| 512 | const id = getLinuxDistroId(linuxInfo.name); |
| 513 | linuxInfo.version = LINUX_VERSIONS[id]?.(contents); |
| 514 | } catch { |
| 515 | // ignore |
| 516 | } |
| 517 | |
| 518 | return linuxInfo; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Grabs some information about hosting provider based on best effort. |
no test coverage detected