-
-
Notifications
You must be signed in to change notification settings - Fork 961
/
deploy-service.ts
157 lines (143 loc) · 4.32 KB
/
deploy-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type {LogLevel} from '@remotion/renderer';
import {NoReactAPIs} from '@remotion/renderer/pure';
import {
DEFAULT_MAX_INSTANCES,
DEFAULT_MIN_INSTANCES,
DEFAULT_TIMEOUT,
} from '../shared/constants';
import {generateServiceName} from '../shared/generate-service-name';
import {validateGcpRegion} from '../shared/validate-gcp-region';
import {validateImageRemotionVersion} from '../shared/validate-image-remotion-version';
import {validateProjectID} from '../shared/validate-project-id';
import {checkIfServiceExists} from './check-if-service-exists';
import {constructServiceTemplate} from './helpers/construct-service-deploy-request';
import {getCloudRunClient} from './helpers/get-cloud-run-client';
type InternalDeployServiceInput = {
performImageVersionValidation: boolean;
memoryLimit: string;
cpuLimit: string;
timeoutSeconds: number;
minInstances: number;
maxInstances: number;
projectID: string;
region: string;
logLevel: LogLevel;
indent: boolean;
};
export type DeployServiceInput = {
performImageVersionValidation?: boolean;
memoryLimit?: string;
cpuLimit?: string;
timeoutSeconds?: number;
minInstances?: number;
maxInstances?: number;
logLevel?: LogLevel;
projectID: string;
region: string;
};
export type DeployServiceOutput = {
fullName: string | null | undefined;
shortName: string | null | undefined;
uri: string;
alreadyExists: boolean;
};
const deployServiceRaw = async ({
performImageVersionValidation = true,
memoryLimit,
cpuLimit,
timeoutSeconds,
minInstances,
maxInstances,
projectID,
region,
logLevel,
}: InternalDeployServiceInput): Promise<DeployServiceOutput> => {
validateGcpRegion(region);
validateProjectID(projectID);
if (performImageVersionValidation) {
validateImageRemotionVersion();
}
const parent = `projects/${projectID}/locations/${region}`;
const cloudRunClient = getCloudRunClient();
const existingService = await checkIfServiceExists({
memoryLimit,
cpuLimit,
timeoutSeconds,
projectID,
region,
logLevel,
});
const serviceName = generateServiceName({
memoryLimit,
cpuLimit,
timeoutSeconds,
});
if (existingService) {
return {
fullName: `projects/${projectID}/locations/${region}/services/${serviceName}`,
shortName: serviceName,
uri: existingService.uri as string,
alreadyExists: true,
};
}
const request = {
parent,
service: {
// service structure: https://googleapis.dev/nodejs/run/latest/google.cloud.run.v2.IService.html
template: constructServiceTemplate({
memoryLimit,
cpuLimit,
timeoutSeconds,
minInstances,
maxInstances,
}),
},
serviceId: serviceName,
};
// Run request
const [operation] = await cloudRunClient.createService(request);
const [response] = await operation.promise();
return {
fullName: response.name,
shortName: serviceName,
uri: response.uri as string,
alreadyExists: false,
};
};
export const internalDeployService =
NoReactAPIs.wrapWithErrorHandling(deployServiceRaw);
/**
* @description Creates a Cloud Run service in your project that will be able to render a video in GCP.
* @link https://remotion.dev/docs/cloudrun/deployservice
* @param params.performImageVersionValidation Validate that an image exists in the public Artifact Registry that matches the Remotion Version. Default true
* @param params.memoryLimit Memory limit of Cloud Run service to deploy.
* @param params.cpuLimit CPU limit of Cloud Run service to deploy.
* @param params.timeoutSeconds After how many seconds the Cloud Run service should be killed if it does not end itself.
* @param params.projectID GCP Project ID to deploy the Cloud Run service to.
* @param params.region GCP region to deploy the Cloud Run service to.
* @returns {Promise<DeployServiceOutput>} See documentation for detailed structure
*/
export const deployService = ({
performImageVersionValidation = true,
memoryLimit,
cpuLimit,
timeoutSeconds,
minInstances,
maxInstances,
projectID,
region,
logLevel,
}: DeployServiceInput): Promise<DeployServiceOutput> => {
return internalDeployService({
performImageVersionValidation,
memoryLimit: memoryLimit ?? '2Gi',
cpuLimit: cpuLimit ?? '1.0',
timeoutSeconds: timeoutSeconds ?? DEFAULT_TIMEOUT,
minInstances: minInstances ?? DEFAULT_MIN_INSTANCES,
maxInstances: maxInstances ?? DEFAULT_MAX_INSTANCES,
projectID,
region,
logLevel: logLevel ?? 'info',
indent: false,
});
};