Skip to content

Commit 200cb0c

Browse files
authored
Upgrade runner-binaries-syncer to aws sdk v3 (#7078)
`yarn test` is broken on main, you can see this in https://github.com/pytorch/test-infra/blob/a32b8f647ed2df0e93a167e518cf92f5855671ce/terraform-aws-github-runner/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/Makefile#L15 Testing: Stole some environment variables from the lambda and mangled the key to upload to a dummy key then ran `yarn build; cd dist;node -e 'require("./index").handler();' > t.log` Saw that it uploaded a file, and skipped some because they didn't need to be uploaded. The one that was uploaded was arm64, which I'm thinking was uploaded manually since it lacks a tag on s3. I had to add an `await` since it wasn't working, which I think is a bug in the original code
1 parent 9306b81 commit 200cb0c

File tree

3 files changed

+1171
-261
lines changed

3 files changed

+1171
-261
lines changed

terraform-aws-github-runner/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
"@types/request": "^2.48.4",
2222
"@typescript-eslint/eslint-plugin": "^4.17.0",
2323
"@typescript-eslint/parser": "^4.17.0",
24-
"@zeit/ncc": "^0.22.1",
25-
"aws-sdk": "^2.863.0",
2624
"eslint": "^7.22.0",
2725
"jest": "^26.6.3",
2826
"prettier": "^2.4.1",
@@ -31,6 +29,9 @@
3129
"typescript": "^4.2.3"
3230
},
3331
"dependencies": {
32+
"@aws-sdk/client-s3": "^3.879.0",
33+
"@aws-sdk/lib-storage": "^3.879.0",
34+
"@vercel/ncc": "^0.38.3",
3435
"request": "^2.88.2",
3536
"yn": "^4.0.0"
3637
}

terraform-aws-github-runner/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/handler.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Octokit } from '@octokit/rest';
22
import { PassThrough } from 'stream';
33
import request from 'request';
4-
import { S3 } from 'aws-sdk';
5-
import AWS from 'aws-sdk';
4+
import { S3, Tag } from '@aws-sdk/client-s3';
5+
import { Upload } from '@aws-sdk/lib-storage';
66
import yn from 'yn';
77

88
const versionKey = 'name';
@@ -14,14 +14,12 @@ interface CacheObject {
1414

1515
async function getCachedVersion(s3: S3, cacheObject: CacheObject): Promise<string | undefined> {
1616
try {
17-
const objectTagging = await s3
18-
.getObjectTagging({
19-
Bucket: cacheObject.bucket,
20-
Key: cacheObject.key,
21-
})
22-
.promise();
23-
const versions = objectTagging.TagSet?.filter((t: S3.Tag) => t.Key === versionKey);
24-
return versions.length === 1 ? versions[0].Value : undefined;
17+
const objectTagging = await s3.getObjectTagging({
18+
Bucket: cacheObject.bucket,
19+
Key: cacheObject.key,
20+
});
21+
const versions = objectTagging.TagSet?.filter((t: Tag) => t.Key === versionKey);
22+
return versions?.length === 1 ? versions[0].Value : undefined;
2523
} catch (e) {
2624
console.debug('No tags found');
2725
return undefined;
@@ -65,12 +63,16 @@ async function getReleaseAsset(
6563

6664
async function uploadToS3(s3: S3, cacheObject: CacheObject, actionRunnerReleaseAsset: ReleaseAsset): Promise<void> {
6765
const writeStream = new PassThrough();
68-
s3.upload({
69-
Bucket: cacheObject.bucket,
70-
Key: cacheObject.key,
71-
Tagging: versionKey + '=' + actionRunnerReleaseAsset.name,
72-
Body: writeStream,
73-
}).promise();
66+
const upload = new Upload({
67+
client: s3,
68+
params: {
69+
Bucket: cacheObject.bucket,
70+
Key: cacheObject.key,
71+
Tagging: `${versionKey}=${actionRunnerReleaseAsset.name}`,
72+
Body: writeStream,
73+
},
74+
});
75+
const uploadPromise = upload.done();
7476

7577
await new Promise<void>((resolve, reject) => {
7678
console.debug('Start downloading %s and uploading to S3.', actionRunnerReleaseAsset.name);
@@ -87,10 +89,11 @@ async function uploadToS3(s3: S3, cacheObject: CacheObject, actionRunnerReleaseA
8789
}).catch((error) => {
8890
console.error(`Exception: ${error}`);
8991
});
92+
await uploadPromise;
9093
}
9194

9295
export const handle = async (): Promise<void> => {
93-
const s3 = new AWS.S3();
96+
const s3 = new S3();
9497

9598
const fetchPrereleaseBinaries = yn(process.env.GITHUB_RUNNER_ALLOW_PRERELEASE_BINARIES, { default: false });
9699
const distributions = [
@@ -131,7 +134,7 @@ export const handle = async (): Promise<void> => {
131134
const currentVersion = await getCachedVersion(s3, cacheObject);
132135
console.debug('latest: ' + currentVersion);
133136
if (currentVersion === undefined || currentVersion != actionRunnerReleaseAsset.name) {
134-
uploadToS3(s3, cacheObject, actionRunnerReleaseAsset);
137+
await uploadToS3(s3, cacheObject, actionRunnerReleaseAsset);
135138
} else {
136139
console.debug('Distribution is up-to-date, no action.');
137140
}

0 commit comments

Comments
 (0)