Skip to content

Commit

Permalink
feat(MongoBinaryDownloadUrl): add ability to overwrite distro used
Browse files Browse the repository at this point in the history
fixes #753
  • Loading branch information
hasezoey committed Jul 7, 2023
1 parent 615b681 commit 826c72e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/api/config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ Valid Options are `x64`, `arm64`, ~~`ia32`~~([will be removed in 9.0](../guides/

[See here for what versions are available for what architectures](https://www.mongodb.com/download-center/community/releases/archive)

### DISTRO

| Environment Variable | PackageJson |
| :------------------: | :---------: |
| `MONGOMS_DISTRO` | `distro` |

Option `DISTRO` is used to overwrite the Distribution used instead of the detected one.

Only works for when [`PLATFORM`](#platform) (automatic or manually set) is `linux`.

Format is `distro-release`, both distro and release need to be always defined.

Example: `ubuntu-18.04`

[See here for what versions are available for what distros](https://www.mongodb.com/download-center/community/releases/archive)

### VERSION

| Environment Variable | PackageJson |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as semver from 'semver';
import { isNullOrUndefined } from './utils';
import { URL } from 'url';
import {
GenericMMSError,
KnownVersionIncompatibilityError,
UnknownArchitectureError,
UnknownPlatformError,
Expand Down Expand Up @@ -161,10 +162,14 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {

// the highest version for "i686" seems to be 3.3
if (this.arch !== 'i686') {
if (!this.os) {
if (!this.os && resolveConfig(ResolveConfigVariables.DISTRO)) {
this.os = await getOS();
}

if (resolveConfig(ResolveConfigVariables.DISTRO)) {
this.overwriteDistro();
}

osString = this.getLinuxOSVersionString(this.os as LinuxOS);
}

Expand All @@ -180,6 +185,38 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
return name;
}

/**
* Parse and apply config option DISTRO
*/
protected overwriteDistro() {
const env = resolveConfig(ResolveConfigVariables.DISTRO);

if (isNullOrUndefined(env)) {
return;
}

const split = env.split('-');

const distro = split[0];
const release = split[1];

if (isNullOrUndefined(distro)) {
throw new GenericMMSError('Expected DISTRO option to have a distro like "ubuntu-18.04"');
}

if (isNullOrUndefined(release)) {
throw new GenericMMSError(
'Expected DISTRO option to have a release like "ubuntu-18.04" (delimited by "-")'
);
}

this.os = {
os: 'linux',
dist: distro,
release: release,
} as LinuxOS;
}

/**
* Get the version string (with distro)
* @param os LinuxOS Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,41 @@ describe('MongoBinaryDownloadUrl', () => {
expect(du.getLegacyVersionString).toHaveBeenCalledTimes(1);
expect(ret).toBe('');
});

describe('wrap config options', () => {
const originalEnv = process.env;

beforeEach(() => {
process.env = originalEnv;
});

afterAll(() => {
process.env = originalEnv;
});

it('should apply config option DISTRO', async () => {
const du = new MongoBinaryDownloadUrl({
platform: 'linux',
arch: 'x64',
version: '5.0.0',
os: {
os: 'linux',
dist: 'unknown',
release: '0',
codename: 'unknown',
},
});
// @ts-expect-error "overwriteDistro" is protected
jest.spyOn(du, 'overwriteDistro');

process.env[envName(ResolveConfigVariables.DISTRO)] = 'ubuntu-18.04';

expect((await du.getArchiveNameLinux()).includes('ubuntu1804')).toBeTruthy();

// @ts-expect-error "overwriteDistro" is protected
expect(du.overwriteDistro).toBeCalledTimes(1);
});
});
});

describe('translateArch()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum ResolveConfigVariables {
USE_HTTP = 'USE_HTTP',
SYSTEM_BINARY_VERSION_CHECK = 'SYSTEM_BINARY_VERSION_CHECK',
USE_ARCHIVE_NAME_FOR_BINARY_NAME = 'USE_ARCHIVE_NAME_FOR_BINARY_NAME',
DISTRO = 'DISTRO',
}

/** The Prefix for Environmental values */
Expand Down

0 comments on commit 826c72e

Please sign in to comment.