🎥 Make videos programmatically with React
added getServices function + docs
Matt McGillivraybacf918fa2cd
diff --git a/packages/cloudrun/src/api/get-services.ts b/packages/cloudrun/src/api/get-services.tsnew file mode 100644index 0000000000..5435c6b60b--- /dev/null+++ b/packages/cloudrun/src/api/get-services.ts@@ -0,0 +1,69 @@+import type {GcpRegion} from '../client';+import {RENDER_SERVICE_PREFIX} from '../shared/constants';+import {SERVICE_VERSION_STRING} from '../shared/service-version-string';+import {getCloudRunClient} from './helpers/get-cloud-run-client';+import type {IService} from './helpers/IService';++export type GetServicesInput = {+ region: GcpRegion;+ compatibleOnly: boolean;+};++export type ServiceInfo = {+ serviceName: string;+ timeoutInSeconds: number;+ memoryLimit: string;+ cpuLimit: string;+ remotionVersion: string | null;+};++/**+ * @description Lists Remotion Cloud Run render services deployed to GCP Cloud Run.+ * @see [Documentation](https://remotion.dev/docs/cloudrun/getservices)+ * @param options.region The region of which the services should be listed.+ * @param options.compatibleOnly Whether only services compatible with the installed version of Remotion Cloud Run should be returned.+ * @returns {Promise<ServiceInfo[]>} An array with the objects containing information about the deployed services.+ */++export const getServices = async (+ options: GetServicesInput+): Promise<ServiceInfo[]> => {+ const cloudRunClient = getCloudRunClient();++ const parent = `projects/${process.env.REMOTION_GCP_PROJECT_ID}/locations/${options.region}`;++ const [services] = await cloudRunClient.listServices({+ parent,+ });++ let remotionServices: IService[] = [];++ if (options.compatibleOnly) {+ remotionServices = services.filter((s) => {+ return s.name?.startsWith(+ `${parent}/services/${RENDER_SERVICE_PREFIX}--${SERVICE_VERSION_STRING}--`+ );+ });+ } else {+ remotionServices = services.filter((s) => {+ return s.name?.startsWith(+ `${parent}/services/${RENDER_SERVICE_PREFIX}--`+ );+ });+ }++ return remotionServices.map((service): ServiceInfo => {+ return {+ serviceName: service.name?.replace(parent + '/services/', '') as string,+ timeoutInSeconds: service.template?.timeout?.seconds as number,+ memoryLimit: service.template?.containers?.[0].resources?.limits+ ?.memory as string,+ cpuLimit: service.template?.containers?.[0].resources?.limits+ ?.cpu as string,+ remotionVersion: service.name+ ?.replace(parent + '/services/' + RENDER_SERVICE_PREFIX + '--', '')+ .split('--')[0]+ .replace(/-/g, '.') as string,+ };+ });+};diff --git a/packages/cloudrun/src/cli/args.ts b/packages/cloudrun/src/cli/args.tsindex 122d225062..2d5dc11afb 100644--- a/packages/cloudrun/src/cli/args.ts+++ b/packages/cloudrun/src/cli/args.ts@@ -2,7 +2,7 @@ import {CliInternals} from '@remotion/cli';import type {GcpRegion} from '../pricing/gcp-regions';-type CloudrunCommandLineOptions = {+type servicesCommandLineOptions = {help: boolean;region: GcpRegion;['project-id']: string;@@ -20,7 +20,7 @@ type CloudrunCommandLineOptions = {};export const parsedCloudrunCli =- CliInternals.minimist<CloudrunCommandLineOptions>(process.argv.slice(2), {+ CliInternals.minimist<servicesCommandLineOptions>(process.argv.slice(2), {boolean: CliInternals.BooleanFlags,});diff --git a/packages/cloudrun/src/cli/commands/services/index.ts b/packages/cloudrun/src/cli/commands/services/index.tsindex 3a9de6d1f1..ed929f451a 100644--- a/packages/cloudrun/src/cli/commands/services/index.ts+++ b/packages/cloudrun/src/cli/commands/services/index.ts@@ -3,23 +3,33 @@ import {BINARY_NAME} from '../../../shared/constants';import {quit} from '../../helpers/quit';import {Log} from '../../log';import {cloudRunDeploySubcommand, CLOUD_RUN_DEPLOY_SUBCOMMAND} from './deploy';+import {servicesLsCommand, SERVICES_LS_SUBCOMMAND} from './ls';-export const CLOUD_RUN_COMMAND = 'service';+export const SERVICES_COMMAND = 'services';const printCloudRunHelp = () => {- Log.info(`${BINARY_NAME} ${CLOUD_RUN_COMMAND} <subcommand>`);+ Log.info(`${BINARY_NAME} ${SERVICES_COMMAND} <subcommand>`);Log.info();Log.info('Available subcommands:');- Log.info('');- Log.info(- `${BINARY_NAME} ${CLOUD_RUN_COMMAND} ${CLOUD_RUN_DEPLOY_SUBCOMMAND}`+ CliInternals.Log.info('');+ CliInternals.Log.info(+ `${BINARY_NAME} ${SERVICES_COMMAND} ${SERVICES_LS_SUBCOMMAND}`+ );+ CliInternals.Log.info(+ CliInternals.chalk.gray('Lists the services currently deployed'));+ Log.info('');+ Log.info(`${BINARY_NAME} ${SERVICES_COMMAND} ${CLOUD_RUN_DEPLOY_SUBCOMMAND}`);Log.info(CliInternals.chalk.gray('Deploy a new Cloud Run service'));};-// TODO: Add LS, RM, RMALL subcommands+// TODO: Add RM, RMALL subcommands++export const servicesCommand = (args: string[]) => {+ if (args[0] === SERVICES_LS_SUBCOMMAND) {+ return servicesLsCommand();+ }-export const cloudRunCommand = (args: string[]) => {if (args[0] === CLOUD_RUN_DEPLOY_SUBCOMMAND) {return cloudRunDeploySubcommand();}diff --git a/packages/cloudrun/src/cli/commands/services/ls.ts b/packages/cloudrun/src/cli/commands/services/ls.tsnew file mode 100644index 0000000000..a6ede35186--- /dev/null+++ b/packages/cloudrun/src/cli/commands/services/ls.ts@@ -0,0 +1,68 @@+import {CliInternals} from '@remotion/cli';+import {getServices} from '../../../api/get-services';+import {getGcpRegion} from '../../get-gcp-region';++const NAME_COLS = 70;+const RESOURCE_LIMIT_COLS = 15;+const DISK_COLS = 15;+const TIMEOUT_COLS = 15;+const VERSION_COLS = 15;++export const SERVICES_LS_SUBCOMMAND = 'ls';++export const servicesLsCommand = async () => {+ const region = getGcpRegion();+ const fetchingOutput = CliInternals.createOverwriteableCliOutput({+ quiet: CliInternals.quietFlagProvided(),+ cancelSignal: null,+ });+ fetchingOutput.update('Getting services...');++ const services = await getServices({+ region,+ compatibleOnly: false,+ });++ if (CliInternals.quietFlagProvided()) {+ if (services.length === 0) {+ CliInternals.Log.info('()');+ return;+ }++ CliInternals.Log.info(services.map((f) => f.serviceName).join(' '));+ return;+ }++ fetchingOutput.update('Getting service info...');++ const pluralized = services.length === 1 ? 'service' : 'services';+ fetchingOutput.update(+ `${services.length} ${pluralized} in the ${region} region`+ );+ CliInternals.Log.info();+ CliInternals.Log.info(+ CliInternals.chalk.gray(+ [+ 'Name'.padEnd(NAME_COLS, ' '),+ 'Version'.padEnd(VERSION_COLS, ' '),+ 'CPU Limit'.padEnd(RESOURCE_LIMIT_COLS, ' '),+ 'Memory Limit'.padEnd(RESOURCE_LIMIT_COLS, ' '),+ 'Timeout (sec)'.padEnd(TIMEOUT_COLS, ' '),+ ].join('')+ )+ );++ for (const datapoint of services) {+ CliInternals.Log.info(+ [+ datapoint.serviceName.padEnd(NAME_COLS, ' '),+ datapoint.remotionVersion+ ? datapoint.remotionVersion.padEnd(VERSION_COLS, ' ')+ : 'Error'.padEnd(VERSION_COLS, ' '),+ String(datapoint.cpuLimit).padEnd(DISK_COLS, ' '),+ String(datapoint.memoryLimit).padEnd(RESOURCE_LIMIT_COLS, ' '),+ String(datapoint.timeoutInSeconds).padEnd(TIMEOUT_COLS, ' '),+ ].join('')+ );+ }+};diff --git a/packages/cloudrun/src/cli/help.ts b/packages/cloudrun/src/cli/help.tsindex 6a0332177a..a9a9cc11f2 100644--- a/packages/cloudrun/src/cli/help.ts+++ b/packages/cloudrun/src/cli/help.ts@@ -1,7 +1,7 @@import {CliInternals} from '@remotion/cli';import {BINARY_NAME} from '../shared/constants';import {RENDER_COMMAND} from './commands/render';-import {CLOUD_RUN_COMMAND} from './commands/services';+import {SERVICES_COMMAND} from './commands/services';import {SITES_COMMAND} from './commands/sites';import {Log} from './log';@@ -28,7 +28,7 @@ export const printHelp = () => {Log.info(CliInternals.chalk.gray('Deploy and manage Remotion projects.'));Log.info();- Log.info(`${BINARY_NAME} ${CLOUD_RUN_COMMAND}`);+ Log.info(`${BINARY_NAME} ${SERVICES_COMMAND}`);Log.info(CliInternals.chalk.gray('Deploy and manage Cloud Run services on GCP.'));diff --git a/packages/cloudrun/src/cli/index.ts b/packages/cloudrun/src/cli/index.tsindex 4963cfe609..2d343c5841 100644--- a/packages/cloudrun/src/cli/index.ts+++ b/packages/cloudrun/src/cli/index.ts@@ -6,7 +6,7 @@ import {import {policiesCommand, POLICIES_COMMAND} from './commands/permissions';import {regionsCommand, REGIONS_COMMAND} from './commands/regions';import {renderCommand, RENDER_COMMAND} from './commands/render';-import {cloudRunCommand, CLOUD_RUN_COMMAND} from './commands/services';+import {servicesCommand, SERVICES_COMMAND} from './commands/services';import {sitesCommand, SITES_COMMAND} from './commands/sites';import {printHelp} from './help';import {quit} from './helpers/quit';@@ -22,8 +22,8 @@ const matchCommand = (args: string[], remotionRoot: string) => {return renderCommand(args.slice(1), remotionRoot);}- if (args[0] === CLOUD_RUN_COMMAND) {- return cloudRunCommand(args.slice(1));+ if (args[0] === SERVICES_COMMAND) {+ return servicesCommand(args.slice(1));}if (args[0] === SITES_COMMAND) {diff --git a/packages/cloudrun/src/index.ts b/packages/cloudrun/src/index.tsindex d69b562a15..4585ea1b25 100644--- a/packages/cloudrun/src/index.ts+++ b/packages/cloudrun/src/index.ts@@ -5,6 +5,7 @@ import type {import {deployService} from './api/deploy-service';import {deploySite} from './api/deploy-site';import {getOrCreateBucket} from './api/get-or-create-bucket';+import {getServices} from './api/get-services';import {renderMediaOnCloudrun} from './api/render-media-on-cloudrun';import {renderStillOnCloudrun} from './api/render-still-on-cloudrun';import {CloudrunInternals} from './internals';@@ -13,6 +14,7 @@ export {CloudrunInternals,deployService,deploySite,+ getServices,getOrCreateBucket,renderMediaOnCloudrun,renderStillOnCloudrun,diff --git a/packages/cloudrun/src/shared/constants.ts b/packages/cloudrun/src/shared/constants.tsindex 67e1ca4443..7bddddae6b 100644--- a/packages/cloudrun/src/shared/constants.ts+++ b/packages/cloudrun/src/shared/constants.ts@@ -4,6 +4,7 @@ export const DEFAULT_REGION: GcpRegion = 'us-east1';export const BINARY_NAME = 'remotion cloudrun';export const REMOTION_BUCKET_PREFIX = 'remotioncloudrun-';+export const RENDER_SERVICE_PREFIX = 'remotion';export const getSitesKey = (siteId: string) => `sites/${siteId}`;diff --git a/packages/cloudrun/src/shared/generate-service-name.ts b/packages/cloudrun/src/shared/generate-service-name.tsindex 03257b38b9..2a99ac4cf7 100644--- a/packages/cloudrun/src/shared/generate-service-name.ts+++ b/packages/cloudrun/src/shared/generate-service-name.ts@@ -1,3 +1,4 @@+import {RENDER_SERVICE_PREFIX} from './constants';import {SERVICE_VERSION_STRING} from './service-version-string';export const generateServiceName = ({@@ -15,8 +16,8 @@ export const generateServiceName = ({.toLowerCase();return [- 'remotion',- `${SERVICE_VERSION_STRING}`,+ RENDER_SERVICE_PREFIX,+ SERVICE_VERSION_STRING,`mem${sanitisedMem}`,`cpu${sanitisedCPU}`,].join('--');diff --git a/packages/docs/docs/cloudrun/api.md b/packages/docs/docs/cloudrun/api.mdnew file mode 100644index 0000000000..32821ff1a1--- /dev/null+++ b/packages/docs/docs/cloudrun/api.md@@ -0,0 +1,60 @@+---+image: /generated/articles-docs-lambda-api.png+title: "@remotion/lambda"+crumb: "Render videos without servers"+---++import Tabs from '@theme/Tabs';+import TabItem from '@theme/TabItem';+import {TableOfContents} from '../../components/TableOfContents/lambda';++<Tabs+defaultValue="npm"+values={[+{ label: 'npm', value: 'npm', },+{ label: 'yarn', value: 'yarn', },+{ label: 'pnpm', value: 'pnpm', },+]+}>+<TabItem value="npm">++```bash+npm i @remotion/lambda+```++ </TabItem>++ <TabItem value="pnpm">++```bash+pnpm i @remotion/lambda+```++ </TabItem>+ <TabItem value="yarn">++```bash+yarn add @remotion/lambda+```++ </TabItem>++</Tabs>++Also update **all the other Remotion packages** to have the same version: `remotion`, `@remotion/cli` and others.++:::note+Make sure no package version number has a `^` character in front of it as it can lead to a version conflict.+:::++**See the [setup guide](/docs/lambda/setup) for complete instructions on how to get started.**++## APIs++The following Node.JS are available:++<TableOfContents />++## CLI++See [here](/docs/lambda/cli) for a list of CLI commands.diff --git a/packages/docs/docs/cloudrun/getservices.md b/packages/docs/docs/cloudrun/getservices.mdnew file mode 100644index 0000000000..a3ebf3b552--- /dev/null+++ b/packages/docs/docs/cloudrun/getservices.md@@ -0,0 +1,84 @@+---+image: /generated/articles-docs-cloudrun-getservices.png+title: getServices()+id: getservices+slug: /cloudrun/getservices+crumb: "Cloudrun API"+---++Retrieves a list of services that Remotion deployed to GCP Cloud Run in a certain region.++The parameter `compatibleOnly` determines whether only services that are compatible with the installed version of Remotion Cloud Run should be returned.++:::note+The Cloud Run service is versioned and the version of the service must match the version of the `@remotion/cloudrun` package. So if you upgrade Remotion, you should deploy a new service or otherwise you might get an empty array from this function.+:::++To get information about only a single service, use [`getServiceInfo()`](/docs/cloudrun/getserviceinfo).++If you are sure that a service exists, you can also guess the name of it using [`speculateServiceName()`](/docs/cloudrun/speculateservicename) and save an API call to Cloud Run.++## Example++```ts twoslash+// @module: esnext+// @target: es2017++import { getServices } from "@remotion/cloudrun";++const info = await getServices({+ region: "us-east1",+ compatibleOnly: true,+});++for (const service of info) {+ console.log(service.serviceName); // "remotion--3-3-82--mem512mi--cpu1-0"+ console.log(service.timeoutInSeconds); // 300+ console.log(service.memoryLimit); // 512Mi+ console.log(service.cpuLimit); // 1.0+ console.log(service.remotionVersion); // "3.3.82"+}+```++## Argument++An object containing the following properties:++### `region`++The [GCP region](/docs/cloudrun/region-selection) that you would like to query.++### `compatibleOnly`++If `true`, only services that match the version of the current Remotion Cloudrun package are returned. If `false`, all Remotion services are returned.++## Return value++A promise resolving to an **array of objects** with the following properties:++### `serviceName`++The name of the service.++### `memoryLimit`++The upper bound on the amount of RAM that the Cloud Run service can consume.++### `cpuLimit`++The maximum number of CPU cores that the Cloud Run service can use to process requests.++### `remotionVersion`++The Remotion version of the service. Remotion is versioning the Cloud Run service and a render can only be triggered from a version of `@remotion/cloudrun` that is matching the one of the service.++### `timeoutInSeconds`++The timeout that has been assigned to the Cloud Run service.++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/cloudrun/src/api/get-services.ts)+- [`getServiceInfo()`](/docs/cloudrun/getserviceinfo)+- [`deployService()`](/docs/cloudrun/deployservice)+- [`deleteService()`](/docs/cloudrun/deleteservice)diff --git a/packages/docs/docs/cloudrun/setup.md b/packages/docs/docs/cloudrun/setup.mdindex 29f76b2c7f..10be13e2dd 100644--- a/packages/docs/docs/cloudrun/setup.md+++ b/packages/docs/docs/cloudrun/setup.md@@ -146,7 +146,7 @@ values={[Deploy a service that can render videos into your GCP project by executing the following command:```bash-npx remotion cloudrun service deploy+npx remotion cloudrun services deploy```</TabItem>diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 5429d6afca..139f306055 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -218,6 +218,22 @@ module.exports = {"lambda/validatewebhooksignature",],},+ {+ type: "category",+ label: "@remotion/cloudrun",+ link: {+ type: "doc",+ id: "cloudrun/api",+ },+ items: [+ {+ type: "link",+ href: "/docs/cloudrun/cli",+ label: "CLI Reference",+ },+ "cloudrun/getservices",+ ],+ },{type: "category",label: "@remotion/three",