1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.getCurrentLinuxDistro = void 0;
|
7 | const child_process_1 = __importDefault(require("child_process"));
|
8 | const fs_1 = require("fs");
|
9 | const util_1 = require("util");
|
10 | const debug_1 = __importDefault(require("debug"));
|
11 | const debug = debug_1.default('mongodb-download-url:linux-distro');
|
12 | const execFile = util_1.promisify(child_process_1.default.execFile);
|
13 | let osRelease;
|
14 | async function getCurrentLinuxDistro() {
|
15 | var _a, _b;
|
16 | if (process.env.DISTRO_ID) {
|
17 | const distroId = process.env.DISTRO_ID.split('-')[0].split('_')[0];
|
18 | debug('Using environment-provided linux distro ID', distroId);
|
19 | let match;
|
20 | if (match = distroId.match(/^ubuntu(\d\d)(\d\d)$/)) {
|
21 | return listDistroIds({ id: 'ubuntu', version: match[1] });
|
22 | }
|
23 | else if (match = distroId.match(/^debian([8-9])\d$/)) {
|
24 | return listDistroIds({ id: 'debian', version: match[1] });
|
25 | }
|
26 | else if (match = distroId.match(/^debian([1-7]\d)$/)) {
|
27 | return listDistroIds({ id: 'debian', version: match[1] });
|
28 | }
|
29 | else if (match = distroId.match(/^suse(\d+)$/)) {
|
30 | return listDistroIds({ id: 'suse', version: match[1] });
|
31 | }
|
32 | else if (match = distroId.match(/^rhel(\d+)$/)) {
|
33 | return listDistroIds({ id: 'redhatenterprise', version: match[1] });
|
34 | }
|
35 | else if (['amazon', 'amzn64', 'amazon1'].includes(distroId)) {
|
36 | return listDistroIds({ id: 'amazon', version: '2018.03' });
|
37 | }
|
38 | return [{ value: distroId, priority: 100 }];
|
39 | }
|
40 | let osReleaseId;
|
41 | try {
|
42 | osRelease !== null && osRelease !== void 0 ? osRelease : (osRelease = await fs_1.promises.readFile('/etc/os-release', 'utf8'));
|
43 | debug('loaded /etc/os-release');
|
44 | }
|
45 | catch (err) {
|
46 | if (err.code === 'ENOENT') {
|
47 | osRelease = '';
|
48 | }
|
49 | }
|
50 | if (osRelease) {
|
51 | const id = osReleaseId = (_a = osRelease.match(/^ID="?(.+?)"?$/m)) === null || _a === void 0 ? void 0 : _a[1];
|
52 | const version = (_b = osRelease.match(/^VERSION_ID="?(.+?)"?$/m)) === null || _b === void 0 ? void 0 : _b[1];
|
53 | if (id && version) {
|
54 | debug('got os-release info', { id, version });
|
55 | const results = listDistroIds({ id, version });
|
56 | if (results.length > 0) {
|
57 | return results;
|
58 | }
|
59 | }
|
60 | }
|
61 | const { id, version } = await lsbReleaseInfo();
|
62 | const results = listDistroIds({ id, version });
|
63 | if (results.length > 0) {
|
64 | return results;
|
65 | }
|
66 | throw new Error(`Could not figure out current Linux distro (${id}, ${osReleaseId})`);
|
67 | }
|
68 | exports.getCurrentLinuxDistro = getCurrentLinuxDistro;
|
69 | function listDistroIds({ id, version }) {
|
70 | const results = [];
|
71 | switch (id.toLowerCase()) {
|
72 | case 'ubuntu': {
|
73 | const major = +version.split('.')[0];
|
74 | if (major >= 12)
|
75 | results.push({ value: 'ubuntu1204', priority: 100 });
|
76 | if (major >= 14)
|
77 | results.push({ value: 'ubuntu1404', priority: 200 });
|
78 | if (major >= 16)
|
79 | results.push({ value: 'ubuntu1604', priority: 300 });
|
80 | if (major >= 18)
|
81 | results.push({ value: 'ubuntu1804', priority: 400 });
|
82 | if (major >= 20)
|
83 | results.push({ value: 'ubuntu2004', priority: 500 });
|
84 | if (major > 20)
|
85 | results.push({ value: 'ubuntu' + version.replace('.', ''), priority: 600 });
|
86 | return results;
|
87 | }
|
88 | case 'debian': {
|
89 | if (+version >= 8)
|
90 | results.push({ value: 'debian81', priority: 100 });
|
91 | if (+version >= 9)
|
92 | results.push({ value: 'debian92', priority: 200 });
|
93 | if (+version >= 10)
|
94 | results.push({ value: 'debian10', priority: 300 });
|
95 | if (+version > 10)
|
96 | results.push({ value: 'debian' + version, priority: 400 });
|
97 | return results;
|
98 | }
|
99 | case 'suse':
|
100 | case 'sles':
|
101 | return [{ value: 'suse' + version.split('.')[0], priority: 100 }];
|
102 | case 'amzn':
|
103 | case 'amzn64':
|
104 | case 'amazon':
|
105 | if (version.match(/^201[0-9]\./)) {
|
106 | return [{ value: 'amazon', priority: 100 }, { value: 'amzn64', priority: 100 }];
|
107 | }
|
108 | else {
|
109 | return [{ value: 'amazon' + version.replace('.', ''), priority: 100 }];
|
110 | }
|
111 | case 'centos':
|
112 | return [{ value: 'rhel' + version + '0', priority: 100 }];
|
113 | case 'redhatenterprise':
|
114 | case 'redhatenterpriseserver': {
|
115 | const want = +version.replace('.', '');
|
116 | const known = [55, 57, 62, 67, 70, 71, 72, 80, 81, 82];
|
117 | const allowedVersions = known.filter(v => v <= want);
|
118 | return allowedVersions.map((v, i) => ({ value: 'rhel' + v, priority: (i + 1) * 100 }));
|
119 | }
|
120 | }
|
121 | return [];
|
122 | }
|
123 | async function lsbReleaseInfo() {
|
124 | const [id, version] = await Promise.all([
|
125 | (async () => {
|
126 | return (await execFile('lsb_release', ['-si'], { encoding: 'utf8' })).stdout.trim();
|
127 | })(),
|
128 | (async () => {
|
129 | return (await execFile('lsb_release', ['-sr'], { encoding: 'utf8' })).stdout.trim();
|
130 | })()
|
131 | ]);
|
132 | debug('got lsb info', { id, version });
|
133 | return { id, version };
|
134 | }
|
135 |
|
\ | No newline at end of file |