🎥 Make videos programmatically with React
install.mjs and main.tf looking 🔥
Matt McGillivrayfe10a70ecff4
diff --git a/packages/cloudrun/haloInf.mp4 b/packages/cloudrun/haloInf.mp4deleted file mode 100644index 93e42edad0..0000000000Binary files a/packages/cloudrun/haloInf.mp4 and /dev/null differdiff --git a/packages/cloudrun/install.mjs b/packages/cloudrun/install.mjsindex 4558f22958..e02966a30d 100644--- a/packages/cloudrun/install.mjs+++ b/packages/cloudrun/install.mjs@@ -1,111 +1,201 @@// this will be executed with node-import { execSync, stdin } from "child_process";+import {execSync} from 'child_process';+import readline from 'readline';-// execSync("gcloud config get-value project", (error, stdout, stderr, {stdio: 'inherit'}) => {-// if (error) {-// console.log(`error: ${error.message}`);-// return;-// }+execSync(+ 'echo "\n\n\u001b[32;1m◀️◀️◀️ GCP project setup for Remotion Cloud Run ▶️▶️▶️\n\n\u001b[0m"',+ {stdio: 'inherit'}+);-// if (stderr) {-// console.log(`stderr: ${stderr}`);-// return;-// }+/****************************************+ * Prompt user for Remotion version+ ****************************************/-// console.log('setting TF_VAR_project_id');-// execSync(`export TF_VAR_project_id=${stdout}`)-// console.log('stdout');-// })+function versionPrompt() {+ return new Promise((resolve) => {+ // regex to ensure remotionVersion is in semver format+ const semverRegex = new RegExp(+ /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,+ 'i'+ );-execSync(- 'echo "\n\n\n\u001b[32;1m🎉 This will setup th current GCP project for Remotion Cloud Run.\n\u001b[0m"',- {stdio: 'inherit'}-);+ const rl = readline.createInterface({+ input: process.stdin,+ output: process.stdout,+ });++ rl.question(+ 'What version of Remotion do you want to use (format: 1.0.0)? \u001b[34;1m',+ async (answer) => {+ rl.close();++ if (semverRegex.test(answer.trim())) return resolve(answer.trim());++ console.log(+ `${answer.trim()} \u001b[0mis not a valid version number.\n`+ );+ const result = await versionPrompt();++ resolve(result);+ }+ );+ });+}-// Read input from the user-stdin.setEncoding('utf8');-console.log('What version of Remotion do you want to use? (format: 1.0.0)');-stdin.on('data', (input) => {- // Remove newlines and carriage returns from the input- input = input.trim();-- // Run a bash command with the input as an argument- exec(`echo "You entered: ${input}"`, (error, stdout, stderr) => {- if (error) {- console.error(`Error: ${error}`);- return;- }-- console.log(`Output: ${stdout}`);- console.error(`Errors: ${stderr}`);- });+const remotionVersion = await versionPrompt();++/****************************************+ * Retrieve and set current project ID+ ****************************************/++let projectID = '';++execSync('echo "\u001b[0m\nRetrieving current Project ID..."', {+ stdio: 'inherit',});-execSync(- 'echo "setting project_id in Terraform, based on current project."',- {stdio: 'inherit'}-);+projectID = execSync('gcloud config get-value project', {+ stdio: ['inherit', 'pipe', 'pipe'],+})+ .toString()+ .trim();-execSync(- 'export TF_VAR_project_id=$(gcloud config get-value project)',- {stdio: 'inherit'}-);+execSync(`echo "Project set to \u001b[34;1m${projectID}\u001b[0m\n"`, {+ stdio: 'inherit',+});-execSync(- 'echo "\u001b[32;1mproject_id set to $TF_VAR_project_id"',- {stdio: 'inherit'}-);+/****************************************+ * Check if Remotion Service Account already exists+ ****************************************/++execSync('echo "Checking if Remotion Service Account already exists..."', {+ stdio: 'inherit',+});-console.log('Running Terraform.')+const serviceAccountExists =+ execSync(+ `gcloud iam service-accounts list --filter="email:remotion-sa@${projectID}.iam.gserviceaccount.com" --project=${projectID}`,+ {+ stdio: ['inherit', 'pipe', 'pipe'],+ }+ )+ .toString()+ .trim().length > 0;++if (serviceAccountExists) {+ execSync(+ `echo "\u001b[34;1mremotion-sa@${projectID}.iam.gserviceaccount.com\u001b[0m found, and does not need to be created.\n"`,+ {+ stdio: 'inherit',+ }+ );+} else {+ execSync(+ `echo "No service account found, \u001b[34;1mremotion-sa@${projectID}.iam.gserviceaccount.com\u001b[0m will be created.\n"`,+ {+ stdio: 'inherit',+ }+ );+}++/****************************************+ * Check if Remotion IAM Role already exists+ ****************************************/++execSync('echo "Checking if Remotion IAM Role already exists..."', {+ stdio: 'inherit',+});-execSync(- 'terraform init',- {stdio: 'inherit'}-);+let iamRoleExists = false;+try {+ execSync(`gcloud iam roles describe RemotionSA --project=${projectID}`, {+ stdio: ['inherit', 'pipe', 'pipe'],+ })+ .toString()+ .trim();+ iamRoleExists = true;+ execSync(+ `echo "\u001b[34;1mRemotionSA\u001b[0m role found, and does not need to be created.\n"`,+ {+ stdio: 'inherit',+ }+ );+} catch {+ iamRoleExists = false;+ execSync(+ `echo "Role not found, \u001b[34;1mRemotionSA\u001b[0m will be created.\n"`,+ {+ stdio: 'inherit',+ }+ );+}++/****************************************+ * Terraform commands+ ****************************************/++console.log('◀️ Running Terraform ▶️');+const terraformVariables = `-var="remotion_version=${remotionVersion}" -var="project_id=${projectID}" -var="service_account_exists=${serviceAccountExists}"`;++execSync('terraform init', {stdio: 'inherit'});++if (iamRoleExists) {+ // If the role already exists, import the resource so that the permissions can be updated in place++ // If tfstate file exists, remove the role resource from state so that it can be imported fresh+ try {+ execSync('terraform state list', {stdio: 'pipe'});+ execSync(`terraform state rm google_project_iam_custom_role.remotion_sa`, {+ stdio: 'inherit',+ });+ } catch {}++ execSync(+ `terraform import ${terraformVariables} google_project_iam_custom_role.remotion_sa projects/${projectID}/roles/RemotionSA`,+ {stdio: 'inherit'}+ );+}execSync(- 'terraform plan',- {stdio: 'inherit'}+ `terraform plan ${terraformVariables} -out=remotion.tfplan -compact-warnings`,+ {stdio: 'inherit'});+// After the plan is complete, prompt the user to apply the plan or not++function applyPrompt() {+ return new Promise((resolve) => {+ const rl = readline.createInterface({+ input: process.stdin,+ output: process.stdout,+ });++ rl.question(+ '\n\u001b[32;1mDo you want to apply the above plan? \u001b[34;1m',+ async (answer) => {+ // reset terminal color+ rl.output.write('\u001b[0m');+ rl.close();++ if (['yes', 'y'].indexOf(answer.trim().toLowerCase()) >= 0)+ return resolve(true);++ if (['no', 'n'].indexOf(answer.trim().toLowerCase()) >= 0)+ return resolve(false);++ console.log('Invalid response.');+ const result = await applyPrompt();++ resolve(result);+ }+ );+ });+}++const applyPlan = await applyPrompt();-// try {-// const cmd = 'gcloud config get-value project';-// execSync(cmd, {-// encoding: "utf8"-// })-// } catch (error) {-// console.log(`Status Code: ${error.status} with '${error.message}'`);-// }--// exec("gcloud config get-value project", (error, stdout, stderr) => {-// if (error) {-// console.log(`error: ${error.message}`);-// return;-// }--// if (stderr) {-// console.log(`stderr: ${stderr}`);-// return;-// }--// console.log('setting TF_VAR_project_id');-// exec(`export TF_VAR_project_id=${stdout}`)-// console.log('stdout');-// });--// exec("terraform init", (error, stdout, stderr) => {-// if (error) {-// console.log(`error: ${error.message}`);-// return;-// }--// if (stderr) {-// console.log(`stderr: ${stderr}`);-// return;-// }--// console.log(stdout)--// console.log('Terraform initialised');-// })\ No newline at end of file+if (applyPlan) {+ execSync('terraform apply remotion.tfplan', {stdio: 'inherit'});+} else {+ console.log('Plan not applied, no changes made.');+}diff --git a/packages/cloudrun/main.tf b/packages/cloudrun/main.tfdeleted file mode 100644index 9a376a4264..0000000000--- a/packages/cloudrun/main.tf+++ /dev/null@@ -1,74 +0,0 @@-terraform {- required_providers {- google = {- source = "hashicorp/google"- version = "4.58.0"- }- }-- required_version = ">= 1.4.1"-}--variable "project_id" {- type = string- description = "The ID of the project in which the resources will be created."-}--variable "remotion_version" {- type = string- description = "The version of Remotion being deployed."-}---provider "google" {- project = var.project_id- region = "us-central1"- zone = "us-central1-c"-}--data "http" "permissions" {- url = "https://storage.googleapis.com/remotion-sa/${var.remotion_version}/sa-permissions.json"-}--locals {- # permissions = jsondecode(file("${path.module}/../src/shared/sa-permissions.json"))- cloudrun-permissions = jsondecode(data.http.permissions.body)-}--# Create an IAM role-resource "google_project_iam_custom_role" "remotion_sa" {- role_id = "RemotionSA"- title = "Remotion API Service Account"- description = "Allow the service account to manage necessary resources for Remotion Cloud Run rendering."- permissions = local.cloudrun-permissions.list-}--# Create a service account-resource "google_service_account" "remotion_sa" {- account_id = "remotion-sa"- display_name = "Remotion Service Account"-}--# Bind the IAM role to the service account-resource "google_project_iam_member" "remotion_sa" {- project = var.project_id- role = google_project_iam_custom_role.remotion_sa.id- member = "serviceAccount:${google_service_account.remotion_sa.email}"-}--# Enable Cloud Run API-resource "google_project_service" "cloud_run" {- project = var.project_id- service = "run.googleapis.com"-}--# Enable Cloud Resource Manager-resource "google_project_service" "cloud_resource_manager" {- project = var.project_id- service = "cloudresourcemanager.googleapis.com"-}--# Output the command to generate service account keys-output "service_account_key_generation_command" {- value = "gcloud iam service-accounts keys create key.json --iam-account=${google_service_account.remotion_sa.email}"-}diff --git a/packages/cloudrun/shellhelper.mjs b/packages/cloudrun/shellhelper.mjsdeleted file mode 100644index 811244c6fe..0000000000--- a/packages/cloudrun/shellhelper.mjs+++ /dev/null@@ -1,37 +0,0 @@--import { exec, spawn } from "child_process";--// execute a single shell command where "cmd" is a string-const shellExec = function(cmd, cb){- // this would be way easier on a shell/bash script :P- const parts = cmd.split(/\s+/g);- const p = spawn(parts[0], parts.slice(1), {stdio: 'inherit'});- p.on('exit', (code)=> {- let err = null;- if (code) {- err = new Error('command "'+ cmd +'" exited with wrong status code "'+ code +'"');- err.code = code;- err.cmd = cmd;- }-- if (cb) cb(err);- });-};---// execute multiple commands in series-// this could be replaced by any flow control lib-const shellSeries = function(cmds, cb){- const execNext = function(){- exec(cmds.shift(), (err)=> {- if (err) {- cb(err);- } else if (cmds.length) execNext();- else cb(null);- });- };-- execNext();-};--export {shellExec, shellSeries}\ No newline at end of filediff --git a/packages/cloudrun/terraform/main.tf b/packages/cloudrun/terraform/main.tfindex 9a376a4264..a700a425dc 100644--- a/packages/cloudrun/terraform/main.tf+++ b/packages/cloudrun/terraform/main.tf@@ -19,6 +19,11 @@ variable "remotion_version" {description = "The version of Remotion being deployed."}+variable "service_account_exists" {+ type = bool+ description = "If the service account already exists, no need to re-create."+}+provider "google" {project = var.project_id@@ -32,7 +37,8 @@ data "http" "permissions" {locals {# permissions = jsondecode(file("${path.module}/../src/shared/sa-permissions.json"))- cloudrun-permissions = jsondecode(data.http.permissions.body)+ cloudrun-permissions = jsondecode(data.http.permissions.response_body)+ service-account-email = "remotion-sa@${var.project_id}.iam.gserviceaccount.com"}# Create an IAM role@@ -45,15 +51,17 @@ resource "google_project_iam_custom_role" "remotion_sa" {# Create a service accountresource "google_service_account" "remotion_sa" {+ count = var.service_account_exists ? 0 : 1 // if the service account already exists, don't create itaccount_id = "remotion-sa"display_name = "Remotion Service Account"+ description = var.remotion_version}# Bind the IAM role to the service accountresource "google_project_iam_member" "remotion_sa" {project = var.project_idrole = google_project_iam_custom_role.remotion_sa.id- member = "serviceAccount:${google_service_account.remotion_sa.email}"+ member = "serviceAccount:${local.service-account-email}"}# Enable Cloud Run API@@ -70,5 +78,5 @@ resource "google_project_service" "cloud_resource_manager" {# Output the command to generate service account keysoutput "service_account_key_generation_command" {- value = "gcloud iam service-accounts keys create key.json --iam-account=${google_service_account.remotion_sa.email}"+ value = "gcloud iam service-accounts keys create key.json --iam-account=${local.service-account-email}"}diff --git a/packages/cloudrun/zoomJump.mp4 b/packages/cloudrun/zoomJump.mp4deleted file mode 100644index 4c2bfa1585..0000000000Binary files a/packages/cloudrun/zoomJump.mp4 and /dev/null differ