-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
MongoBinaryDownloadUrl.js
202 lines (176 loc) · 5.16 KB
/
MongoBinaryDownloadUrl.js
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
/* @flow */
/* eslint-disable class-methods-use-this */
import getos from 'getos';
type OS = {
dist: string,
release?: string,
};
const DOWNLOAD_URI = 'https://downloads.mongodb.org';
export type MongoBinaryDownloadUrlOpts = {
version: string,
platform: string,
arch: string,
os?: ?OS, // getos() result
};
export default class MongoBinaryDownloadUrl {
platform: string;
arch: string;
version: string;
os: ?OS;
constructor({ platform, arch, version, os }: MongoBinaryDownloadUrlOpts) {
this.platform = this.translatePlatform(platform);
this.arch = this.translateArch(arch, this.platform);
this.version = version;
this.os = os;
}
async getDownloadUrl(): Promise<string> {
const archive = await this.getArchiveName();
return `${DOWNLOAD_URI}/${this.platform}/${archive}`;
}
async getArchiveName(): Promise<string> {
let name = `mongodb-${this.platform}-${this.arch}`;
let osString;
if (this.platform === 'linux' && this.arch !== 'i686') {
if (!this.os) this.os = await this.getos();
osString = this.getLinuxOSVersionString(this.os);
}
if (osString) {
name += `-${osString}`;
}
name += `-${this.version}.${this.getArchiveType()}`;
return name;
}
getArchiveType(): string {
return this.platform === 'win32' ? 'zip' : 'tgz';
}
async getos(): Promise<OS> {
return new Promise((resolve, reject) => {
getos((e: any, os: any) => {
if (e) reject(e);
resolve(os);
});
});
}
getLinuxOSVersionString(os: OS): 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);
}
throw new Error(`Cannot determine version string for ${JSON.stringify(os)}`);
}
getDebianVersionString(os: OS): string {
let name: string = 'debian';
const release: number = parseFloat(os.release);
if (release >= 8.1) {
name += '81';
} else if (release >= 7.1) {
name += '71';
}
return name;
}
getFedoraVersionString(os: OS): string {
let name: string = '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;
}
getRhelVersionString(os: OS): string {
let name: string = '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;
}
// eslint-disable-next-line no-unused-vars
getElementaryOSVersionString(os: OS): string {
return 'ubuntu1404';
}
// eslint-disable-next-line no-unused-vars
getMintVersionString(os: OS): string {
// unfortunately getos doesn't return version for Mint
return 'ubuntu1404';
}
getSuseVersionString(os: any): string {
const [release]: [string | null] = os.release.match(/(^11|^12)/) || [null];
if (release) {
return `suse${release}`;
}
return '';
}
getUbuntuVersionString(os: OS): string {
let name: string = '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 {
name += '1404';
}
return name;
}
translatePlatform(platform: string): string {
switch (platform) {
case 'darwin':
return 'osx';
case 'win32':
return 'win32';
case 'linux':
return 'linux';
case 'elementary OS':
return 'linux';
case 'sunos':
return 'sunos5';
default:
throw new Error(`unsupported OS ${platform}`);
}
}
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');
}
}
}