🎥 Make videos programmatically with React
wip ensuring region accounted for with sites
Matt McGillivraye32dd79578a0
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 5fbe34904b..5066380c70 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -6,6 +6,7 @@},"cSpell.words": ["gsutil",+ "Overwriteable","rmall"]}diff --git a/packages/cloudrun/src/api/get-buckets.ts b/packages/cloudrun/src/api/get-buckets.tsindex 82c72c897d..d25a3bd74e 100644--- a/packages/cloudrun/src/api/get-buckets.ts+++ b/packages/cloudrun/src/api/get-buckets.ts@@ -1,23 +1,28 @@import type {Bucket} from '@google-cloud/storage';+import type {GcpRegion} from '../pricing/gcp-regions';import {REMOTION_BUCKET_PREFIX} from '../shared/constants';import {getCloudStorageClient} from './helpers/get-cloud-storage-client';-export const getRemotionStorageBuckets = async (): Promise<{+export const getRemotionStorageBuckets = async (+ region: GcpRegion | 'all regions'+): Promise<{remotionBuckets: Bucket[];}> => {- // List buckets in this projectconst cloudStorageClient = getCloudStorageClient();const [buckets] = await cloudStorageClient.getBuckets();if (!buckets) {return {remotionBuckets: []};}- // Lambda equivalent code was filtering buckets by region.- // Instead, I am just returning all buckets that get listed.- // Not sure if I need to filter them by project ID.-- const remotionBuckets = buckets.filter((b) =>+ let remotionBuckets = buckets.filter((b) =>b.name?.startsWith(REMOTION_BUCKET_PREFIX));++ if (region !== 'all regions') {+ remotionBuckets = buckets.filter(+ (b) => b.metadata.location === region.toUpperCase()+ );+ }+return {remotionBuckets};};diff --git a/packages/cloudrun/src/api/get-or-create-bucket.ts b/packages/cloudrun/src/api/get-or-create-bucket.tsindex d13b3c1ae3..d620429c48 100644--- a/packages/cloudrun/src/api/get-or-create-bucket.ts+++ b/packages/cloudrun/src/api/get-or-create-bucket.ts@@ -19,16 +19,16 @@ export type GetOrCreateBucketOutput = {bucketName: string;};/**- * @description Creates a bucket for Remotion Cloud Run in your GCP Project. If one already exists, it will get returned instead.+ * @description Creates a bucket for Remotion Cloud Run in your GCP Project, in a particular region. If one already exists, it will get returned instead.* @link https://remotion.dev/docs/lambda/getorcreatebucket* @param options.region The region in which you want your Storage bucket to reside in.* @returns {Promise<GetOrCreateBucketOutput>} An object containing the `bucketName`.*/export const getOrCreateBucket = async (- options?: GetOrCreateBucketInput+ options: GetOrCreateBucketInput): Promise<GetOrCreateBucketOutput> => {- // checking for existing bucket- const {remotionBuckets} = await getRemotionStorageBuckets();+ const {remotionBuckets} = await getRemotionStorageBuckets(options.region);+if (remotionBuckets.length > 1) {throw new Error(`You have multiple buckets (${remotionBuckets.map(diff --git a/packages/cloudrun/src/api/get-service-info.ts b/packages/cloudrun/src/api/get-service-info.tsindex 179886ae9a..85184578e5 100644--- a/packages/cloudrun/src/api/get-service-info.ts+++ b/packages/cloudrun/src/api/get-service-info.ts@@ -1,4 +1,4 @@-import type {GcpRegion} from '../client';+import type {GcpRegion} from '../pricing/gcp-regions';import {RENDER_SERVICE_PREFIX} from '../shared/constants';import {getCloudRunClient} from './helpers/get-cloud-run-client';diff --git a/packages/cloudrun/src/api/get-services.ts b/packages/cloudrun/src/api/get-services.tsindex 79d0ed4c84..a84f1b4781 100644--- a/packages/cloudrun/src/api/get-services.ts+++ b/packages/cloudrun/src/api/get-services.ts@@ -1,4 +1,4 @@-import type {GcpRegion} from '../client';+import type {GcpRegion} from '../pricing/gcp-regions';import {RENDER_SERVICE_PREFIX} from '../shared/constants';import {SERVICE_VERSION_STRING} from '../shared/service-version-string';import type {ServiceInfo} from './get-service-info';diff --git a/packages/cloudrun/src/api/get-sites.ts b/packages/cloudrun/src/api/get-sites.tsindex 1ffef644a3..a25e2ce912 100644--- a/packages/cloudrun/src/api/get-sites.ts+++ b/packages/cloudrun/src/api/get-sites.ts@@ -1,4 +1,4 @@-import type {GcpRegion} from '../client';+import type {GcpRegion} from '../pricing/gcp-regions';import {REMOTION_BUCKET_PREFIX} from '../shared/constants';import {getCloudStorageClient} from './helpers/get-cloud-storage-client';@@ -7,10 +7,7 @@ type Site = {bucketName: string;id: string;serveUrl: string;-};--export type GetSitesInput = {- bucketName?: string;+ bucketRegion: GcpRegion;};export type BucketWithLocation = {@@ -25,11 +22,14 @@ export type GetSitesOutput = {};/**- * @description Gets all the deployed sites for a certain GCP project.+ * @description Gets all the deployed sites for a certain GCP project, within the specified region.* @see [Documentation](https://remotion.dev/docs/cloudrun/getsites)+ * @param {GcpRegion} params.region The GCP region that you want to query for.* @returns {Promise<GetSitesOutput>} A Promise containing an object with `sites` and `bucket` keys. Consult documentation for details.*/-export const getSites = async (): Promise<GetSitesOutput> => {+export const getSites = async (+ region: GcpRegion | 'all regions'+): Promise<GetSitesOutput> => {const cloudStorageClient = getCloudStorageClient();const buckets: BucketWithLocation[] = [];const sites: Site[] = [];@@ -37,11 +37,15 @@ export const getSites = async (): Promise<GetSitesOutput> => {const [fetchedBuckets] = await cloudStorageClient.getBuckets();for (const bucket of fetchedBuckets) {- if (bucket.name?.startsWith(REMOTION_BUCKET_PREFIX)) {+ if (+ bucket.name?.startsWith(REMOTION_BUCKET_PREFIX) &&+ (region === 'all regions' ||+ bucket.metadata.location === region.toUpperCase())+ ) {const bucketObject = {name: bucket.name,creationDate: new Date(bucket.metadata?.timeCreated).getTime(),- region: bucket.metadata.location,+ region: bucket.metadata.location.toLowerCase(),};buckets.push(bucketObject);}@@ -60,6 +64,7 @@ export const getSites = async (): Promise<GetSitesOutput> => {id: sitePath[1],serveUrl: `https://storage.googleapis.com/${bucket.name}/${prefix}index.html`,lastModified: null,+ bucketRegion: bucket.region,});}}diff --git a/packages/cloudrun/src/cli/commands/sites/ls.ts b/packages/cloudrun/src/cli/commands/sites/ls.tsindex 37c169f01c..e0bd269b10 100644--- a/packages/cloudrun/src/cli/commands/sites/ls.ts+++ b/packages/cloudrun/src/cli/commands/sites/ls.ts@@ -1,6 +1,7 @@import {CliInternals} from '@remotion/cli';import {getSites} from '../../../api/get-sites';-import {dateString} from '../../helpers/date-string';+import {parsedCloudrunCli} from '../../args';+import {getGcpRegion} from '../../get-gcp-region';export const SITES_LS_SUBCOMMAND = 'ls';@@ -15,7 +16,14 @@ const logRow = (data: [string, string, string]) => {};export const sitesLsSubcommand = async () => {- const {sites, buckets} = await getSites();+ // check if --allRegions flag is provided+ // if so, list all sites+ // else, list only the sites that are in the current project+ const allRegions = parsedCloudrunCli['all-regions'];++ const region = allRegions ? 'all regions' : getGcpRegion();++ const {sites, buckets} = await getSites(region);if (buckets.length > 1 && !CliInternals.quietFlagProvided()) {CliInternals.Log.warn(@@ -26,7 +34,7 @@ export const sitesLsSubcommand = async () => {const sitesPluralized = sites.length === 1 ? 'site' : 'sites';if (!CliInternals.quietFlagProvided()) {CliInternals.Log.info(- `${sites.length} ${sitesPluralized} in the ${process.env.REMOTION_GCP_PROJECT_ID} project.`+ `${sites.length} ${sitesPluralized} in ${region}, in the ${process.env.REMOTION_GCP_PROJECT_ID} project.`);}@@ -39,20 +47,18 @@ export const sitesLsSubcommand = async () => {return CliInternals.Log.info(sites.map((s) => s.id).join(' '));}- CliInternals.Log.info();- CliInternals.Log.info(- CliInternals.chalk.gray(logRow(['Site Name', 'Bucket', 'Last updated']))- );-- for (const site of sites) {+ if (sites.length > 0) {+ CliInternals.Log.info();CliInternals.Log.info(- logRow([- site.id,- site.bucketName,- site.lastModified ? dateString(new Date(site.lastModified)) : 'n/a',- ])+ CliInternals.chalk.gray(logRow(['Site Name', 'Bucket', 'Region'])));- CliInternals.Log.info(site.serveUrl);- CliInternals.Log.info();++ for (const site of sites) {+ CliInternals.Log.info(+ logRow([site.id, site.bucketName, site.bucketRegion])+ );+ CliInternals.Log.info(site.serveUrl);+ CliInternals.Log.info();+ }}};diff --git a/packages/cloudrun/src/cli/commands/sites/rm.ts b/packages/cloudrun/src/cli/commands/sites/rm.tsindex 7af556d1f9..6cc1bfc7e9 100644--- a/packages/cloudrun/src/cli/commands/sites/rm.ts+++ b/packages/cloudrun/src/cli/commands/sites/rm.ts@@ -1,10 +1,12 @@+import {CliInternals} from '@remotion/cli';import {deleteSite} from '../../../api/delete-site';-import {getOrCreateBucket} from '../../../api/get-or-create-bucket';import {getSites} from '../../../api/get-sites';import {confirmCli} from '../../helpers/confirm';import {quit} from '../../helpers/quit';import {Log} from '../../log';+export const SITES_RM_COMMAND = 'rm';+const LEFT_COL = 16;export const sitesRmSubcommand = async (args: string[]) => {if (args.length === 0) {@@ -19,19 +21,31 @@ export const sitesRmSubcommand = async (args: string[]) => {return;}- const deployedSites = await getSites();+ const deployedSites = await getSites('all regions');for (const siteName of args) {- const {bucketName} = await getOrCreateBucket();+ const infoOutput = CliInternals.createOverwriteableCliOutput({+ quiet: CliInternals.quietFlagProvided(),+ cancelSignal: null,+ });+ infoOutput.update('Getting site info...');const site = deployedSites.sites.find((s) => s.id === siteName.trim());if (!site) {- Log.error(- `No site ${siteName.trim()} was found in your bucket ${bucketName}.`- );- return quit(1);+ throw new Error(`${siteName.trim()} was not found.`);}+ infoOutput.update(+ [+ 'Site: '.padEnd(LEFT_COL, ' ') + ' ' + site.id,+ 'Bucket: '.padEnd(LEFT_COL, ' ') + ' ' + site.bucketName,+ 'Region: '.padEnd(LEFT_COL, ' ') + ' ' + site.bucketRegion,+ 'Serve Url: '.padEnd(LEFT_COL, ' ') + ' ' + site.serveUrl,+ ].join('\n')+ );+ Log.info();+ Log.info();+const confirmDelete = await confirmCli({delMessage: `Site ${site.id} in bucket ${site.bucketName}: Delete? (Y/n)`,allowForceFlag: true,@@ -43,7 +57,7 @@ export const sitesRmSubcommand = async (args: string[]) => {}await deleteSite({- bucketName,+ bucketName: site.bucketName,siteName,});diff --git a/packages/cloudrun/src/shared/validate-bucketname.ts b/packages/cloudrun/src/shared/validate-bucketname.tsindex 273e62f4ba..f0640b75af 100644--- a/packages/cloudrun/src/shared/validate-bucketname.ts+++ b/packages/cloudrun/src/shared/validate-bucketname.ts@@ -1,4 +1,4 @@-import type {GcpRegion} from '../client';+import type {GcpRegion} from '../pricing/gcp-regions';import {GCP_REGIONS} from '../regions';import {REMOTION_BUCKET_PREFIX} from './constants';import {randomHash} from './random-hash';diff --git a/packages/docs/docs/cloudrun/cli/sites.md b/packages/docs/docs/cloudrun/cli/sites.mdindex 604e9df998..cec41cb3d7 100644--- a/packages/docs/docs/cloudrun/cli/sites.md+++ b/packages/docs/docs/cloudrun/cli/sites.md@@ -43,7 +43,7 @@ Example output<pre>(1/3) [====================] Bundled video 3975ms<br/>(2/3) [====================] Created bucket 457ms<br/>-(3/3) [====================] Uploaded to S3 25118ms<br/>+(3/3) [====================] Uploaded to GCP Storage Bucket 25118ms<br/><br/>Deployed to GCP Cloud Storage!<br/>Serve URL: https://storage.googleapis.com/remotioncloudrun-12345/sites/mySite123/index.html<br/>@@ -53,7 +53,7 @@ Site Name: mySite123<br/>### `--region`-The [GCP region](/docs/cloudrun/region-selection) to select. Both project and service should be in this region.+The [GCP region](/docs/cloudrun/region-selection) to select. The service should also be in this same region to minimise latency.### `--site-name`@@ -92,11 +92,12 @@ Get a list of sites. The URL that is printed can be passed to the `render` commaExample output</summary><pre>-Site Name Bucket Last updated<br/>-pr6fwglz05 remotioncloudrun-abcdefg 2021-12-02<br/>-https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/pr6fwglz05/index.html<br/><br/>-testbed remotioncloudrun-abcdefg 2021-12-02 <br/>-https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/testbed/index.html<br/>+2 sites in us-east1, in the remotion-6 project.<br/><br/>+Site Name Bucket Region<br/>+another-site remotioncloudrun-abcdefg us-east1<br/>+https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/another-site/index.html<br/><br/>+test-site remotioncloudrun-abcdefg us-east1 <br/>+https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/test-site/index.html<br/></pre></details>@@ -117,21 +118,50 @@ pr6fwglz05 testbed<br/></pre></details>+### `--all-regions`,++Ignores region, returning sites across all regions for the project++```+npx remotion cloudrun sites ls --all-regions+```++<details>+<summary>+Example output+</summary>+<pre>+3 sites in all regions, in the remotion-6 project.<br/><br/>+Site Name Bucket Region<br/>+another-site remotioncloudrun-abcdefg us-east1<br/>+https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/another-site/index.html<br/><br/>+test-site remotioncloudrun-abcdefg us-east1 <br/>+https://storage.googleapis.com/remotioncloudrun-abcdefg/sites/test-site/index.html<br/><br/>+central-site remotioncloudrun-123456 us-central1 <br/>+https://storage.googleapis.com/remotioncloudrun-123456/sites/central-site/index.html<br/>+</pre>+</details>+## rmRemoves a site (or multiple) from Cloud Storage by it's ID.```bash-npx remotion cloudrun sites rm abcdef-npx remotion cloudrun sites rm abcdef my-project # multiple at once+npx remotion cloudrun sites rm central-site+npx remotion cloudrun sites rm central-site another-site # multiple at once```<details><summary>Example output</summary>-<pre>Site abcdef in bucket remotionlambda-gc1w0xbfzl: Delete? (Y/n): Y-<br/>Deleted site abcdef from bucket remotionlambda-gc1w0xbfzl.+<pre>+Site: central-site<br/>+Bucket: remotioncloudrun-123456<br/>+Region: us-central1<br/>+Serve Url: https://storage.googleapis.com/remotioncloudrun-123456/sites/central-site/index.html<br/><br/>+Site central-site in bucket remotioncloudrun-123456: Delete? (Y/n): Y<br/>+Deleted site central-site from bucket remotioncloudrun-123456.<br/></pre></details>@@ -141,7 +171,7 @@ Example outputRemoves a site without asking for confirmation.```-npx remotion cloudrun sites rm abcdef -y+npx remotion cloudrun sites rm central-site -y```## rmall