-
Notifications
You must be signed in to change notification settings - Fork 17
feat: DSC support for mongodb runner #822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mpobrien
wants to merge
5
commits into
mongodb-js:main
Choose a base branch
from
mpobrien:07-01-dsc_support_for_mongodb-runner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| /* eslint-disable no-console */ | ||
| import fetch from 'node-fetch'; | ||
| import * as tar from 'tar'; | ||
| import { createHash } from 'crypto'; | ||
| import { promisify } from 'util'; | ||
| import { promises as fs, createWriteStream } from 'fs'; | ||
| import path from 'path'; | ||
| import decompress from 'decompress'; | ||
| import { pipeline } from 'stream'; | ||
| import { pipeline, Transform } from 'stream'; | ||
| import getDownloadURL from 'mongodb-download-url'; | ||
| import type { | ||
| Options as DownloadOptions, | ||
|
|
@@ -31,6 +32,11 @@ export type MongoDBDownloaderOptions = { | |
| useLockfile: boolean; | ||
| /** The options to pass to the download URL lookup. */ | ||
| downloadOptions?: DownloadOptions; | ||
| /** | ||
| * A direct URL to a MongoDB tarball to download. If set, `version` and | ||
| * `downloadOptions` are ignored and no download URL lookup is performed. | ||
| */ | ||
| downloadUrl?: string; | ||
| }; | ||
|
|
||
| export class MongoDBDownloader { | ||
|
|
@@ -39,6 +45,7 @@ export class MongoDBDownloader { | |
| version = '*', | ||
| directory, | ||
| useLockfile, | ||
| downloadUrl, | ||
| }: MongoDBDownloaderOptions): Promise<DownloadResult> { | ||
| await fs.mkdir(directory, { recursive: true }); | ||
| const isWindows = ['win32', 'windows'].includes( | ||
|
|
@@ -58,12 +65,20 @@ export class MongoDBDownloader { | |
| versionName = versionName + (isEnterprise ? '-enterprise' : '-community'); | ||
| } | ||
|
|
||
| const downloadTarget = path.resolve( | ||
| directory, | ||
| `mongodb-${process.platform}-${process.env.DISTRO_ID || 'none'}-${ | ||
| process.arch | ||
| }-${versionName}`.replace(/[^a-zA-Z0-9_-]/g, ''), | ||
| ); | ||
| const downloadTarget = downloadUrl | ||
| ? path.resolve( | ||
| directory, | ||
| `mongodb-custom-${createHash('sha256') | ||
| .update(downloadUrl) | ||
| .digest('hex') | ||
| .slice(0, 16)}`, | ||
| ) | ||
| : path.resolve( | ||
| directory, | ||
| `mongodb-${process.platform}-${process.env.DISTRO_ID || 'none'}-${ | ||
| process.arch | ||
| }-${versionName}`.replace(/[^a-zA-Z0-9_-]/g, ''), | ||
| ); | ||
| const bindir = path.resolve( | ||
| downloadTarget, | ||
| isCryptLibrary && !isWindows ? 'lib' : 'bin', | ||
|
|
@@ -99,11 +114,13 @@ export class MongoDBDownloader { | |
| } | ||
|
|
||
| await fs.mkdir(downloadTarget, { recursive: true }); | ||
| const artifactInfo = await this.lookupDownloadUrl({ | ||
| targetVersion: version, | ||
| enterprise: isEnterprise, | ||
| options: downloadOptions, | ||
| }); | ||
| const artifactInfo = downloadUrl | ||
| ? ({ url: downloadUrl } as DownloadArtifactInfo) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dangerous assertion that makes the public interface here return a value that doesn't match the provided type at all and can cause consumer code to crash completely |
||
| : await this.lookupDownloadUrl({ | ||
| targetVersion: version, | ||
| enterprise: isEnterprise, | ||
| options: downloadOptions, | ||
| }); | ||
| const { url } = artifactInfo; | ||
| debug(`Downloading ${url} into ${downloadTarget}...`); | ||
|
|
||
|
|
@@ -139,11 +156,35 @@ export class MongoDBDownloader { | |
| const response = await fetch(url, { | ||
| highWaterMark: MongoDBDownloader.HWM, | ||
| } as Parameters<typeof fetch>[1]); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to download ${url}: ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| const totalBytes = +(response.headers.get('content-length') ?? ''); | ||
| const totalMB = totalBytes ? (totalBytes / 1048576).toFixed(1) : null; | ||
| debug(`Download started`, { url, totalMB }); | ||
| let downloadedBytes = 0; | ||
| let lastProgressLog = Date.now(); | ||
| const progress = new Transform({ | ||
| transform(chunk: Buffer, _encoding, callback) { | ||
| downloadedBytes += chunk.length; | ||
| if (Date.now() - lastProgressLog >= 3000) { | ||
| lastProgressLog = Date.now(); | ||
| const downloadedMB = (downloadedBytes / 1048576).toFixed(1); | ||
| debug( | ||
| `Downloading: ${downloadedMB}MB${totalMB ? ` / ${totalMB}MB` : ''}`, | ||
| ); | ||
| } | ||
| callback(null, chunk); | ||
| }, | ||
| }); | ||
| if (/\.tgz$|\.tar(\.[^.]+)?$/.exec(url)) { | ||
| // the server's tarballs can contain hard links, which the (unmaintained?) | ||
| // `download` package is unable to handle (https://github.com/kevva/decompress/issues/93) | ||
| await promisify(pipeline)( | ||
| response.body, | ||
| progress, | ||
| tar.x({ cwd: downloadTarget, strip: isCryptLibrary ? 0 : 1 }), | ||
| ); | ||
| } else { | ||
|
|
@@ -153,6 +194,7 @@ export class MongoDBDownloader { | |
| ); | ||
| await promisify(pipeline)( | ||
| response.body, | ||
| progress, | ||
| createWriteStream(filename, { highWaterMark: MongoDBDownloader.HWM }), | ||
| ); | ||
| debug(`Written file ${url} to ${filename}, extracting...`); | ||
|
|
@@ -230,32 +272,15 @@ async function withoutLock<T>( | |
| const downloader = new MongoDBDownloader(); | ||
|
|
||
| /** Download mongod + mongos with version info and return version info and the path to a directory containing them. */ | ||
| export async function downloadMongoDbWithVersionInfo({ | ||
| downloadOptions = {}, | ||
| version = '*', | ||
| directory, | ||
| useLockfile, | ||
| }: MongoDBDownloaderOptions): Promise<DownloadResult> { | ||
| return await downloader.downloadMongoDbWithVersionInfo({ | ||
| downloadOptions, | ||
| version, | ||
| directory, | ||
| useLockfile, | ||
| }); | ||
| export async function downloadMongoDbWithVersionInfo( | ||
| options: MongoDBDownloaderOptions, | ||
| ): Promise<DownloadResult> { | ||
| return await downloader.downloadMongoDbWithVersionInfo(options); | ||
| } | ||
| /** Download mongod + mongos and return the path to a directory containing them. */ | ||
| export async function downloadMongoDb({ | ||
| downloadOptions = {}, | ||
| version = '*', | ||
| directory, | ||
| useLockfile, | ||
| }: MongoDBDownloaderOptions): Promise<string> { | ||
| return ( | ||
| await downloader.downloadMongoDbWithVersionInfo({ | ||
| downloadOptions, | ||
| version, | ||
| directory, | ||
| useLockfile, | ||
| }) | ||
| ).downloadedBinDir; | ||
| export async function downloadMongoDb( | ||
| options: MongoDBDownloaderOptions, | ||
| ): Promise<string> { | ||
| return (await downloader.downloadMongoDbWithVersionInfo(options)) | ||
| .downloadedBinDir; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should think about some different approach for adding this functionality in the downloader: the issue I see here is that while downloadOptions will be ignored, we will still consider them somewhat when returning the DownloadResult metadata, which from the consumer perspective might put you in a weird state where what you download doesn't match the returned options at all