Skip to content

Commit

Permalink
[1.x] Update re2 build for arm under node 10 (#1482) (#1510)
Browse files Browse the repository at this point in the history
This is a backport for the fix we made on 2.0. Please check
the Backport PR for more details. The difference between this
version and 2.0 is node bump.

From originial PR: We build and restore re2 for arm. With a license
built in, we zip the build artifact as .tar.gz. The original patchModule
method has a default extract method and path which causes issues
for extracting and using re2 arm build artifact. Therefore, we
modify the method in this PR by passing an overriddenExtractMethod
and an overriddenDestinationPath.

Backport PR: #1454
Issue Resolved: #1436

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
(cherry picked from commit d0f368a)
  • Loading branch information
opensearch-trigger-bot[bot] authored Apr 28, 2022
1 parent 49ef208 commit 208a9ae
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/dev/build/tasks/patch_native_modules_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface Package {
{
url: string;
sha256: string;
overriddenExtractMethod?: string;
overriddenDestinationPath?: string;
}
>;
}
Expand All @@ -63,8 +65,11 @@ const packages: Package[] = [
sha256: 'e743587bc96314edf10c3e659c03168bc374a5cd9a6623ee99d989251e331f28',
},
'linux-arm64': {
url: 'https://d1v1sj258etie.cloudfront.net/node-re2/1.15.4/linux-arm64-64.gz',
sha256: '19fa97f39d4965276382429bcd932dd696915f711663e7cef9b0a304b3e8e6f7',
url:
'https://d1v1sj258etie.cloudfront.net/node-re2/releases/download/1.15.4/linux-arm64-64.tar.gz',
sha256: '24edcdf45a09e69b6329385ab3ece24b424602a2656c8a297111d7aac174723b',
overriddenExtractMethod: 'untar',
overriddenDestinationPath: 'node_modules/re2/build/Release',
},
'win32-x64': {
url: 'https://github.com/uhop/node-re2/releases/download/1.15.4/win32-x64-64.gz',
Expand Down Expand Up @@ -100,7 +105,11 @@ async function patchModule(
const archive = pkg.archives[platformName];
const archiveName = path.basename(archive.url);
const downloadPath = config.resolveFromRepo(DOWNLOAD_DIRECTORY, pkg.name, archiveName);
const extractPath = build.resolvePathForPlatform(platform, pkg.destinationPath);
const extractMethod = archive.overriddenExtractMethod || pkg.extractMethod;
const extractPath = build.resolvePathForPlatform(
platform,
archive.overriddenDestinationPath || pkg.destinationPath
);
log.debug(`Patching ${pkg.name} binaries from ${archive.url} to ${extractPath}`);

await deleteAll([extractPath], log);
Expand All @@ -111,15 +120,15 @@ async function patchModule(
sha256: archive.sha256,
retries: 3,
});
switch (pkg.extractMethod) {
switch (extractMethod) {
case 'gunzip':
await gunzip(downloadPath, extractPath);
break;
case 'untar':
await untar(downloadPath, extractPath);
break;
default:
throw new Error(`Extract method of ${pkg.extractMethod} is not supported`);
throw new Error(`Extract method of ${extractMethod} is not supported`);
}
}

Expand Down
92 changes: 92 additions & 0 deletions src/dev/build/tasks/patch_native_modules_tasks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {
ToolingLog,
ToolingLogCollectingWriter,
createAnyInstanceSerializer,
createAbsolutePathSerializer,
} from '@osd/dev-utils';
import { Build, Config } from '../lib';
import { PatchNativeModules } from './patch_native_modules_task';

const log = new ToolingLog();
const testWriter = new ToolingLogCollectingWriter();
log.setWriters([testWriter]);
expect.addSnapshotSerializer(createAnyInstanceSerializer(Config));
expect.addSnapshotSerializer(createAnyInstanceSerializer(ToolingLog));
expect.addSnapshotSerializer(createAbsolutePathSerializer());

jest.mock('../lib/download');
jest.mock('../lib/fs', () => ({
...jest.requireActual('../lib/fs'),
untar: jest.fn(),
gunzip: jest.fn(),
}));

const { untar } = jest.requireMock('../lib/fs');
const { gunzip } = jest.requireMock('../lib/fs');
const { download } = jest.requireMock('../lib/download');

async function setup() {
const config = await Config.create({
isRelease: true,
targetAllPlatforms: false,
targetPlatforms: {
linux: false,
linuxArm: false,
darwin: false,
},
});

const build = new Build(config);

download.mockImplementation(() => {});
untar.mockImplementation(() => {});
gunzip.mockImplementation(() => {});

return { config, build };
}

beforeEach(() => {
testWriter.messages.length = 0;
jest.clearAllMocks();
});

it('patch native modules task downloads the correct platform package', async () => {
const { config, build } = await setup();
config.targetPlatforms.linuxArm = true;
await PatchNativeModules.run(config, log, build);
expect(download.mock.calls.length).toBe(1);
expect(download.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
"destination": <absolute path>/.native_modules/re2/linux-arm64-64.tar.gz,
"log": <ToolingLog>,
"retries": 3,
"sha256": "24edcdf45a09e69b6329385ab3ece24b424602a2656c8a297111d7aac174723b",
"url": "https://d1v1sj258etie.cloudfront.net/node-re2/releases/download/1.15.4/linux-arm64-64.tar.gz",
},
],
]
`);
});

it('for .tar.gz artifact, patch native modules task unzip it via untar', async () => {
const { config, build } = await setup();
config.targetPlatforms.linuxArm = true;
await PatchNativeModules.run(config, log, build);
expect(untar.mock.calls.length).toBe(1);
expect(gunzip.mock.calls.length).toBe(0);
});

it('for .gz artifact, patch native modules task unzip it via gunzip', async () => {
const { config, build } = await setup();
config.targetPlatforms.linux = true;
await PatchNativeModules.run(config, log, build);
expect(untar.mock.calls.length).toBe(0);
expect(gunzip.mock.calls.length).toBe(1);
});

0 comments on commit 208a9ae

Please sign in to comment.