|
1 | 1 | import type { ReleaseOpts } from "./main.ts";
|
2 |
| -import { assertExists } from "@std/assert/exists"; |
3 |
| -import $, { CommandBuilder } from "dax"; |
4 |
| -import { assert } from "@std/assert/assert"; |
5 |
| -import { resolve } from "@std/path"; |
| 2 | +import { $ } from "execa"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import * as fs from "node:fs/promises"; |
| 5 | + |
| 6 | +function assert(condition: any, message?: string): asserts condition { |
| 7 | + if (!condition) { |
| 8 | + throw new Error(message || "Assertion failed"); |
| 9 | + } |
| 10 | +} |
6 | 11 |
|
7 | 12 | export async function updateArtifacts(opts: ReleaseOpts) {
|
8 | 13 | // Get credentials and set them in the environment
|
9 |
| - const awsAccessKeyId = |
10 |
| - Deno.env.get("R2_RELEASES_ACCESS_KEY_ID") ?? |
11 |
| - (await $`op read "op://Engineering/rivet-releases R2 Upload/username"`.text()); |
12 |
| - const awsSecretAccessKey = |
13 |
| - Deno.env.get("R2_RELEASES_SECRET_ACCESS_KEY") ?? |
14 |
| - (await $`op read "op://Engineering/rivet-releases R2 Upload/password"`.text()); |
| 14 | + let awsAccessKeyId = process.env.R2_RELEASES_ACCESS_KEY_ID; |
| 15 | + if (!awsAccessKeyId) { |
| 16 | + const result = await $`op read "op://Engineering/rivet-releases R2 Upload/username"`; |
| 17 | + awsAccessKeyId = result.stdout.trim(); |
| 18 | + } |
| 19 | + let awsSecretAccessKey = process.env.R2_RELEASES_SECRET_ACCESS_KEY; |
| 20 | + if (!awsSecretAccessKey) { |
| 21 | + const result = await $`op read "op://Engineering/rivet-releases R2 Upload/password"`; |
| 22 | + awsSecretAccessKey = result.stdout.trim(); |
| 23 | + } |
15 | 24 |
|
16 | 25 | const endpointUrl = "https://2a94c6a0ced8d35ea63cddc86c2681e7.r2.cloudflarestorage.com";
|
17 | 26 |
|
18 |
| - // Create AWS CLI command builder with credentials |
19 |
| - const awsCommand = new CommandBuilder().env({ |
| 27 | + // Create AWS environment for commands |
| 28 | + const awsEnv = { |
20 | 29 | AWS_ACCESS_KEY_ID: awsAccessKeyId,
|
21 | 30 | AWS_SECRET_ACCESS_KEY: awsSecretAccessKey,
|
22 | 31 | AWS_DEFAULT_REGION: "auto",
|
23 |
| - }); |
| 32 | + }; |
24 | 33 |
|
25 | 34 | // List all files under rivet/{commit}/
|
26 | 35 | const commitPrefix = `rivet/${opts.commit}/`;
|
27 |
| - $.logStep("Listing Original Files", commitPrefix); |
28 |
| - const commitFiles = await awsCommand |
29 |
| - .command( |
30 |
| - `aws s3api list-objects --bucket rivet-releases --prefix ${commitPrefix} --endpoint-url ${endpointUrl}`, |
31 |
| - ) |
32 |
| - .json(); |
| 36 | + console.log(`==> Listing Original Files: ${commitPrefix}`); |
| 37 | + const listResult = await $({ env: awsEnv, shell: true })`aws s3api list-objects --bucket rivet-releases --prefix ${commitPrefix} --endpoint-url ${endpointUrl}`; |
| 38 | + const commitFiles = JSON.parse(listResult.stdout); |
33 | 39 | assert(
|
34 | 40 | Array.isArray(commitFiles?.Contents) && commitFiles.Contents.length > 0,
|
35 | 41 | `No files found under rivet/${opts.commit}/`,
|
36 | 42 | );
|
37 | 43 |
|
38 | 44 | // Copy files to version directory
|
39 | 45 | const versionTarget = `rivet/${opts.version}/`;
|
40 |
| - await copyFiles(awsCommand, commitPrefix, versionTarget, endpointUrl); |
41 |
| - await generateInstallScripts(awsCommand, opts, opts.version, endpointUrl); |
| 46 | + await copyFiles(awsEnv, commitPrefix, versionTarget, endpointUrl); |
| 47 | + await generateInstallScripts(awsEnv, opts, opts.version, endpointUrl); |
42 | 48 |
|
43 | 49 | // If this is the latest version, copy to latest directory
|
44 | 50 | if (opts.latest) {
|
45 |
| - await copyFiles(awsCommand, commitPrefix, "rivet/latest/", endpointUrl); |
46 |
| - await generateInstallScripts(awsCommand, opts, "latest", endpointUrl); |
| 51 | + await copyFiles(awsEnv, commitPrefix, "rivet/latest/", endpointUrl); |
| 52 | + await generateInstallScripts(awsEnv, opts, "latest", endpointUrl); |
47 | 53 | }
|
48 | 54 | }
|
49 | 55 |
|
50 | 56 | async function copyFiles(
|
51 |
| - awsCommand: CommandBuilder, |
| 57 | + awsEnv: Record<string, string>, |
52 | 58 | sourcePrefix: string,
|
53 | 59 | targetPrefix: string,
|
54 | 60 | endpointUrl: string,
|
55 | 61 | ) {
|
56 |
| - $.logStep("Copying Files", targetPrefix); |
57 |
| - await $.logGroup(async () => { |
58 |
| - // Delete existing files in target directory using --recursive |
59 |
| - $.logStep("Deleting existing files in", targetPrefix); |
60 |
| - await awsCommand |
61 |
| - .command(`aws s3 rm s3://rivet-releases/${targetPrefix} --recursive --endpoint-url ${endpointUrl}`) |
62 |
| - .spawn(); |
| 62 | + console.log(`==> Copying Files: ${targetPrefix}`); |
| 63 | + // Delete existing files in target directory using --recursive |
| 64 | + console.log(`Deleting existing files in ${targetPrefix}`); |
| 65 | + await $({ env: awsEnv, shell: true })`aws s3 rm s3://rivet-releases/${targetPrefix} --recursive --endpoint-url ${endpointUrl}`; |
63 | 66 |
|
64 |
| - // Copy new files using --recursive |
65 |
| - $.logStep("Copying files from", sourcePrefix, "to", targetPrefix); |
66 |
| - await awsCommand |
67 |
| - .command( |
68 |
| - `aws s3 cp s3://rivet-releases/${sourcePrefix} s3://rivet-releases/${targetPrefix} --recursive --copy-props none --endpoint-url ${endpointUrl}`, |
69 |
| - ) |
70 |
| - .spawn(); |
71 |
| - }); |
| 67 | + // Copy new files using --recursive |
| 68 | + console.log(`Copying files from ${sourcePrefix} to ${targetPrefix}`); |
| 69 | + await $({ env: awsEnv, shell: true })`aws s3 cp s3://rivet-releases/${sourcePrefix} s3://rivet-releases/${targetPrefix} --recursive --copy-props none --endpoint-url ${endpointUrl}`; |
72 | 70 | }
|
73 | 71 |
|
74 | 72 | async function generateInstallScripts(
|
75 |
| - awsCommand: CommandBuilder, |
| 73 | + awsEnv: Record<string, string>, |
76 | 74 | opts: ReleaseOpts,
|
77 | 75 | version: string,
|
78 | 76 | endpointUrl: string,
|
79 | 77 | ) {
|
80 | 78 | const installScriptPaths = [
|
81 |
| - resolve(opts.root, "scripts/release/static/install.sh"), |
82 |
| - resolve(opts.root, "scripts/release/static/install.ps1"), |
| 79 | + path.resolve(opts.root, "scripts/release/static/install.sh"), |
| 80 | + path.resolve(opts.root, "scripts/release/static/install.ps1"), |
83 | 81 | ];
|
84 | 82 |
|
85 | 83 | for (const scriptPath of installScriptPaths) {
|
86 |
| - let scriptContent = await Deno.readTextFile(scriptPath); |
| 84 | + let scriptContent = await fs.readFile(scriptPath, 'utf-8'); |
87 | 85 | scriptContent = scriptContent.replace(/__VERSION__/g, version);
|
88 | 86 |
|
89 | 87 | const uploadKey = `rivet/${version}/${scriptPath.split("/").pop() ?? ""}`;
|
90 | 88 |
|
91 | 89 | // Upload the install script to S3
|
92 |
| - $.logStep("Uploading Install Script", uploadKey); |
93 |
| - await awsCommand |
94 |
| - .command(`aws s3 cp - s3://rivet-releases/${uploadKey} --endpoint-url ${endpointUrl}`) |
95 |
| - .stdinText(scriptContent) |
96 |
| - .spawn(); |
| 90 | + console.log(`==> Uploading Install Script: ${uploadKey}`); |
| 91 | + await $({ env: awsEnv, input: scriptContent, shell: true })`aws s3 cp - s3://rivet-releases/${uploadKey} --endpoint-url ${endpointUrl}`; |
97 | 92 | }
|
98 | 93 | }
|
0 commit comments