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 Jun 9, 2023
1 parent fe76b56 commit f2b1c62
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`

[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,
UnknownLinuxDistro,
Expand Down Expand Up @@ -160,10 +161,14 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
* (from: https://www.mongodb.org/dl/linux)
*/
async getArchiveNameLinux(): Promise<string> {
if (!this.os) {
if (!this.os && resolveConfig(ResolveConfigVariables.DISTRO)) {
this.os = await getOS();
}

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

const osString: string = this.getLinuxOSVersionString(this.os as LinuxOS);

// this is below, to allow overwriting the arch (like arm64 to aarch64)
Expand All @@ -179,6 +184,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 @@ -1735,6 +1735,41 @@ describe('MongoBinaryDownloadUrl', () => {
expect(err.message).toStrictEqual('Unknown/unsupported linux "unknown" id_like\'s: []');
}
});

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 f2b1c62

Please sign in to comment.