-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
MongoBinaryDownloadUrl.ts
360 lines (329 loc) · 9.55 KB
/
MongoBinaryDownloadUrl.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
360
import getOS, { AnyOS, LinuxOS } from './getos';
import { execSync } from 'child_process';
import resolveConfig from './resolve-config';
export interface MongoBinaryDownloadUrlOpts {
version: string;
platform: string;
arch: string;
os?: AnyOS;
}
export default class MongoBinaryDownloadUrl {
platform: string;
arch: string;
version: string;
os: AnyOS | undefined;
constructor({ platform, arch, version, os }: MongoBinaryDownloadUrlOpts) {
this.version = version;
this.platform = this.translatePlatform(platform);
this.arch = this.translateArch(arch, this.platform);
this.os = os;
}
/**
* Assemble the URL to download
* Calls all the necessary functions to determine the URL
*/
async getDownloadUrl(): Promise<string> {
const archive = await this.getArchiveName();
const downloadUrl = resolveConfig('DOWNLOAD_URL');
if (downloadUrl) return downloadUrl;
const mirror = resolveConfig('DOWNLOAD_MIRROR') ?? 'https://fastdl.mongodb.org';
return `${mirror}/${this.platform}/${archive}`;
}
/**
* Get the archive
* Version independent
*/
async getArchiveName(): Promise<string> {
switch (this.platform) {
case 'osx':
return this.getArchiveNameOsx();
case 'win32':
return this.getArchiveNameWin();
case 'linux':
default:
return this.getArchiveNameLinux();
}
}
/**
* Get the archive for Windows
* (from: https://www.mongodb.org/dl/win32)
*/
async getArchiveNameWin(): Promise<string> {
let name = `mongodb-${this.platform}`;
name += `-${this.arch}`;
if (this.version.indexOf('4.2') === 0) {
name += '-2012plus';
} else {
name += '-2008plus-ssl';
}
name += `-${this.version}.zip`;
return name;
}
/**
* Get the archive for OSX (Mac)
* (from: https://www.mongodb.org/dl/osx)
*/
async getArchiveNameOsx(): Promise<string> {
let name = `mongodb-osx`;
if (
!(
this.version.indexOf('3.0') === 0 ||
this.version.indexOf('2.') === 0 ||
this.version.indexOf('1.') === 0
)
) {
name += '-ssl';
}
if (this.version.indexOf('4.2') === 0) {
name = `mongodb-macos`;
}
name += `-${this.arch}`;
name += `-${this.version}.tgz`;
return name;
}
/**
* Get the archive for Linux
* (from: https://www.mongodb.org/dl/linux)
*/
async getArchiveNameLinux(): Promise<string> {
let name = `mongodb-linux`;
name += `-${this.arch}`;
let osString: string | undefined;
if (this.arch !== 'i686') {
if (!this.os) {
this.os = await getOS();
}
osString = this.getLinuxOSVersionString(this.os as LinuxOS);
}
if (osString) {
name += `-${osString}`;
}
name += `-${this.version}.tgz`;
return name;
}
/**
* Get the version string (with distro)
* @param os LinuxOS Object
*/
getLinuxOSVersionString(os: LinuxOS): string {
if (/ubuntu/i.test(os.dist)) {
return this.getUbuntuVersionString(os);
} else if (/elementary OS/i.test(os.dist)) {
return this.getElementaryOSVersionString(os);
} else if (/suse/i.test(os.dist)) {
return this.getSuseVersionString(os);
} else if (/rhel/i.test(os.dist) || /centos/i.test(os.dist) || /scientific/i.test(os.dist)) {
return this.getRhelVersionString(os);
} else if (/fedora/i.test(os.dist)) {
return this.getFedoraVersionString(os);
} else if (/debian/i.test(os.dist)) {
return this.getDebianVersionString(os);
} else if (/mint/i.test(os.dist)) {
return this.getMintVersionString(os);
} else if (/arch/i.test(os.dist)) {
console.warn('There is no offical build of MongoDB for ArchLinux!');
} else if (/alpine/i.test(os.dist)) {
console.warn('There is no offical build of MongoDB for Alpine!');
} else if (/unkown/i.test(os.dist)) {
// "unkown" is likely to happen if no release file / command could be found
console.warn(
'Couldnt parse dist infomation, please report this to https://github.com/nodkz/mongodb-memory-server/issues'
);
} else {
// warn if no case for the *parsed* distro is found
console.warn(`Unknown linux distro ${os.dist}`);
}
// warn for the fallback
console.warn(`Falling back to legacy MongoDB build!`);
return this.getLegacyVersionString(os);
}
/**
* Get the version string for Debain
* @param os LinuxOS Object
*/
getDebianVersionString(os: LinuxOS): string {
let name = 'debian';
const release: number = parseFloat(os.release);
if (release >= 10 || os.release === 'unstable') {
name += '10';
} else if (release >= 9) {
name += '92';
} else if (release >= 8.1) {
name += '81';
} else if (release >= 7.1) {
name += '71';
}
return name;
}
/**
* Get the version string for Fedora
* @param os LinuxOS Object
*/
getFedoraVersionString(os: LinuxOS): string {
let name = 'rhel';
const fedoraVer: number = parseInt(os.release, 10);
if (fedoraVer > 18) {
name += '70';
} else if (fedoraVer < 19 && fedoraVer >= 12) {
name += '62';
} else if (fedoraVer < 12 && fedoraVer >= 6) {
name += '55';
}
return name;
}
/**
* Get the version string for Red Hat Enterprise Linux
* @param os LinuxOS Object
*/
getRhelVersionString(os: LinuxOS): string {
let name = 'rhel';
const { release } = os;
if (release) {
if (/^7/.test(release)) {
name += '70';
} else if (/^6/.test(release)) {
name += '62';
} else if (/^5/.test(release)) {
name += '55';
}
}
return name;
}
/**
* Get the version string for ElementaryOS
* @param os LinuxOS Object
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getElementaryOSVersionString(os: LinuxOS): string {
// Elementary specific - get used ubuntu version
const ubuntuVersion = execSync('/usr/bin/lsb_release -u -rs');
try {
// confirm it is actually a version, otherwise throw an error
parseFloat(ubuntuVersion.toString());
return `ubuntu${ubuntuVersion
.toString()
.replace('.', '')
.trim()}`;
} catch (err) {
console.error('ElementaryOS "lsb_relese -u -rs" couldnt be executed!');
throw err;
}
}
/**
* Get the version string for Linux Mint
* @param os LinuxOS Object
*/
getMintVersionString(os: LinuxOS): string {
let name = 'ubuntu';
const mintMajorVer = parseInt(os.release ? os.release.split('.')[0] : os.release);
if (mintMajorVer < 17) {
throw new Error('Mint Versions under 17 are not supported!');
}
switch (mintMajorVer) {
case 17:
name += '1404';
break;
case 18:
name += '1604';
break;
case 19:
default:
// a default to support versions > 19
name += '1804';
break;
}
return name;
}
/**
* Linux Fallback
* @param os LinuxOS Object
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getLegacyVersionString(os: AnyOS): string {
return '';
}
/**
* Get the version string for Suse / OpenSuse
* @param os LinuxOS Object
*/
getSuseVersionString(os: LinuxOS): string {
const releaseMatch: RegExpMatchArray | null = os.release.match(/(^11|^12)/);
return releaseMatch ? `suse${releaseMatch[0]}` : '';
}
/**
* Get the version string for Ubuntu
* @param os LinuxOS Object
*/
getUbuntuVersionString(os: LinuxOS): string {
let name = 'ubuntu';
const ubuntuVer: string[] = os.release ? os.release.split('.') : [];
const majorVer: number = parseInt(ubuntuVer[0], 10);
// const minorVer: string = ubuntuVer[1];
if (os.release === '12.04') {
name += '1204';
} else if (os.release === '14.04') {
name += '1404';
} else if (os.release === '14.10') {
name += '1410-clang';
} else if (majorVer === 14) {
name += '1404';
} else if (os.release === '16.04') {
name += '1604';
} else if (majorVer === 16) {
name += '1604';
} else if (majorVer >= 18) {
if (this.version && this.version.indexOf('3.') === 0) {
// For MongoDB 3.x using 1604 binaries, download distro does not have builds for Ubuntu 1804
// https://www.mongodb.org/dl/linux/x86_64-ubuntu1604
name += '1604';
} else {
// See fulllist of versions https://www.mongodb.org/dl/linux/x86_64-ubuntu1804
name += '1804';
}
} else {
name += '1404';
}
return name;
}
/**
* Translate input platform to mongodb useable platfrom
* @example
* darwin -> osx
* @param platform The Platform to translate
*/
translatePlatform(platform: string): string {
switch (platform) {
case 'darwin':
return 'osx';
case 'win32':
return 'win32';
case 'linux':
case 'elementary OS':
return 'linux';
case 'sunos':
return 'sunos5';
default:
throw new Error(`unsupported OS ${platform}`);
}
}
/**
* Translate input arch to mongodb useable arch
* @example
* x64 -> x86_64
* @param platform The Platform to translate
*/
translateArch(arch: string, mongoPlatform: string): string {
if (arch === 'ia32') {
if (mongoPlatform === 'linux') {
return 'i686';
} else if (mongoPlatform === 'win32') {
return 'i386';
}
throw new Error('unsupported architecture');
} else if (arch === 'x64') {
return 'x86_64';
} else {
throw new Error('unsupported architecture, ia32 and x64 are the only valid options');
}
}
}