-
Notifications
You must be signed in to change notification settings - Fork 904
/
release.js
528 lines (499 loc) · 19.2 KB
/
release.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
"use strict";
/**
* If you make any changes to this file, run the integration test in scripts/test-functions-deploy.js
*/
var clc = require("cli-color");
var _ = require("lodash");
var { FirebaseError } = require("../../error");
var gcp = require("../../gcp");
var logger = require("../../logger");
var track = require("../../track");
var utils = require("../../utils");
var helper = require("../../functionsDeployHelper");
var runtimeSelector = require("../../runtimeChoiceSelector");
var { getAppEngineLocation } = require("../../functionsConfig");
var { promptOnce } = require("../../prompt");
var { createOrUpdateSchedulesAndTopics } = require("./createOrUpdateSchedulesAndTopics");
var deploymentTool = require("../../deploymentTool");
var timings = {};
var deployments = [];
var failedDeployments = [];
const DEFAULT_PUBLIC_POLICY = {
version: 3,
bindings: [
{
role: "roles/cloudfunctions.invoker",
members: ["allUsers"],
},
],
};
function _startTimer(name, type) {
timings[name] = { type: type, t0: process.hrtime() };
}
function _endTimer(name) {
if (!timings[name]) {
logger.debug("[functions] no timer initialized for", name);
return;
}
// hrtime returns a duration as an array of [seconds, nanos]
var duration = process.hrtime(timings[name].t0);
track(
"Functions Deploy (Duration)",
timings[name].type,
duration[0] * 1000 + Math.round(duration[1] * 1e-6)
);
}
function _fetchTriggerUrls(projectId, ops, sourceUrl) {
if (!_.find(ops, ["trigger.httpsTrigger", {}])) {
// No HTTPS functions being deployed
return Promise.resolve();
}
return gcp.cloudfunctions.listAll(projectId).then(function(functions) {
var httpFunctions = _.chain(functions)
.filter({ sourceUploadUrl: sourceUrl })
.filter("httpsTrigger")
.value();
_.forEach(httpFunctions, function(httpFunc) {
_.chain(ops)
.find({ func: httpFunc.name })
.assign({ triggerUrl: httpFunc.httpsTrigger.url })
.value();
});
return Promise.resolve();
});
}
var printSuccess = function(op) {
_endTimer(op.func);
utils.logSuccess(
clc.bold.green("functions[" + helper.getFunctionLabel(op.func) + "]: ") +
"Successful " +
op.type +
" operation. "
);
if (op.triggerUrl && op.type !== "delete") {
logger.info(
clc.bold("Function URL"),
"(" + helper.getFunctionName(op.func) + "):",
op.triggerUrl
);
}
};
var printFail = function(op) {
_endTimer(op.func);
failedDeployments.push(helper.getFunctionName(op.func));
utils.logWarning(
clc.bold.yellow("functions[" + helper.getFunctionLabel(op.func) + "]: ") + "Deployment error."
);
if (op.error.code === 8) {
logger.debug(op.error.message);
logger.info(
"You have exceeded your deployment quota, please deploy your functions in batches by using the --only flag, " +
"and wait a few minutes before deploying again. Go to " +
clc.underline("https://firebase.google.com/docs/cli/#deploy_specific_functions") +
" to learn more."
);
} else {
logger.info(op.error.message);
}
};
var printTooManyOps = function(projectId) {
utils.logWarning(
clc.bold.yellow("functions:") + " too many functions are being deployed, cannot poll status."
);
logger.info(
"In a few minutes, you can check status at " + utils.consoleUrl(projectId, "/functions/logs")
);
logger.info(
"You can use the --only flag to deploy only a portion of your functions in the future."
);
deployments = []; // prevents analytics tracking of deployments
};
module.exports = function(context, options, payload) {
if (!options.config.has("functions")) {
return Promise.resolve();
}
var projectId = context.projectId;
var sourceUrl = context.uploadUrl;
var appEngineLocation = getAppEngineLocation(context.firebaseConfig);
// Used in CLI releases v3.4.0 to v3.17.6
var legacySourceUrlTwo =
"gs://" + "staging." + context.firebaseConfig.storageBucket + "/firebase-functions-source";
// Used in CLI releases v3.3.0 and prior
var legacySourceUrlOne = "gs://" + projectId + "-gcf/" + projectId;
var functionsInfo = helper.getFunctionsInfo(payload.functions.triggers, projectId);
functionsInfo = functionsInfo.map((fn) => {
if (
fn.eventTrigger &&
fn.schedule &&
fn.eventTrigger.eventType === "google.pubsub.topic.publish"
) {
const [, , , region, , funcName] = fn.name.split("/");
const newResource = `${fn.eventTrigger.resource}/firebase-schedule-${funcName}-${region}`;
fn.eventTrigger.resource = newResource;
}
return fn;
});
var uploadedNames = _.map(functionsInfo, "name");
var functionFilterGroups = helper.getFilterGroups(options);
var deleteReleaseNames;
var existingScheduledFunctions;
delete payload.functions;
return gcp.cloudfunctions
.listAll(projectId)
.then(function(existingFunctions) {
var pluckName = function(functionObject) {
return _.get(functionObject, "name"); // e.g.'projects/proj1/locations/us-central1/functions/func'
};
var existingNames = _.map(existingFunctions, pluckName);
var isScheduled = function(functionObject) {
return _.get(functionObject, "labels.deployment-scheduled") === "true";
};
existingScheduledFunctions = _.chain(existingFunctions)
.filter(isScheduled)
.map(pluckName)
.value();
var releaseNames = helper.getReleaseNames(uploadedNames, existingNames, functionFilterGroups);
// If not using function filters, then `deleteReleaseNames` should be equivalent to existingNames so that intersection is a noop
deleteReleaseNames = functionFilterGroups.length > 0 ? releaseNames : existingNames;
helper.logFilters(existingNames, releaseNames, functionFilterGroups);
// Create functions
_.chain(uploadedNames)
.difference(existingNames)
.intersection(releaseNames)
.forEach(function(name) {
var functionInfo = _.find(functionsInfo, { name: name });
var functionTrigger = helper.getFunctionTrigger(functionInfo);
var functionName = helper.getFunctionName(name);
var region = helper.getRegion(name);
var runtime = context.runtimeChoice || helper.getDefaultRuntime();
utils.logBullet(
clc.bold.cyan("functions: ") +
"creating " +
runtimeSelector.getHumanFriendlyRuntimeName(runtime) +
" function " +
clc.bold(helper.getFunctionLabel(name)) +
"..."
);
logger.debug("Trigger is: ", JSON.stringify(functionTrigger));
var eventType = functionTrigger.eventTrigger
? functionTrigger.eventTrigger.eventType
: "https";
_startTimer(name, "create");
deployments.push({
name: name,
retryFunction: () => {
return gcp.cloudfunctions
.create({
projectId: projectId,
region: region,
eventType: eventType,
functionName: functionName,
entryPoint: functionInfo.entryPoint,
trigger: functionTrigger,
labels: _.assign({}, deploymentTool.labels, functionInfo.labels),
sourceUploadUrl: sourceUrl,
runtime: runtime,
availableMemoryMb: functionInfo.availableMemoryMb,
timeout: functionInfo.timeout,
})
.then((createRes) => {
if (_.has(functionTrigger, "httpsTrigger")) {
logger.debug(`Setting public policy for function ${functionName}`);
return gcp.cloudfunctions
.setIamPolicy({
functionName,
projectId,
region,
policy: DEFAULT_PUBLIC_POLICY,
})
.then(() => {
return createRes;
});
}
return createRes;
});
},
trigger: functionTrigger,
});
})
.value();
// Update functions
_.chain(uploadedNames)
.intersection(existingNames)
.intersection(releaseNames)
.forEach(function(name) {
var functionInfo = _.find(functionsInfo, { name: name });
var functionTrigger = helper.getFunctionTrigger(functionInfo);
var functionName = helper.getFunctionName(name);
var region = helper.getRegion(name);
var eventType = functionTrigger.eventTrigger
? functionTrigger.eventTrigger.eventType
: "https";
var existingFunction = _.find(existingFunctions, {
name: name,
});
var existingEventType = _.get(existingFunction, "eventTrigger.eventType");
var migratingTrigger = false;
if (
eventType.match(/google.storage.object./) &&
existingEventType === "providers/cloud.storage/eventTypes/object.change"
) {
migratingTrigger = true;
} else if (
eventType === "google.pubsub.topic.publish" &&
existingEventType === "providers/cloud.pubsub/eventTypes/topic.publish"
) {
migratingTrigger = true;
}
if (migratingTrigger) {
throw new FirebaseError(
"Function " +
clc.bold(functionName) +
" was deployed using a legacy trigger type and cannot be updated without deleting " +
"the previous function. Follow the instructions on " +
clc.underline(
"https://firebase.google.com/docs/functions/manage-functions#modify-trigger"
) +
" for how to change the trigger without losing events.\n"
);
} else {
var options = {
projectId: projectId,
region: region,
functionName: functionName,
trigger: functionTrigger,
sourceUploadUrl: sourceUrl,
labels: _.assign({}, deploymentTool.labels, functionInfo.labels),
availableMemoryMb: functionInfo.availableMemoryMb,
timeout: functionInfo.timeout,
};
if (context.runtimeChoice) {
options.runtime = context.runtimeChoice;
}
var runtime = options.runtime || _.get(existingFunction, "runtime", "nodejs6"); // legacy functions are Node 6
utils.logBullet(
clc.bold.cyan("functions: ") +
"updating " +
runtimeSelector.getHumanFriendlyRuntimeName(runtime) +
" function " +
clc.bold(helper.getFunctionLabel(name)) +
"..."
);
logger.debug("Trigger is: ", JSON.stringify(functionTrigger));
_startTimer(name, "update");
deployments.push({
name: name,
retryFunction: function() {
return gcp.cloudfunctions.update(options);
},
trigger: functionTrigger,
});
}
})
.value();
// Delete functions
var functionsToDelete = _.chain(existingFunctions)
.filter(function(functionInfo) {
if (typeof functionInfo.labels === "undefined") {
return (
functionInfo.sourceArchiveUrl === legacySourceUrlOne ||
functionInfo.sourceArchiveUrl === legacySourceUrlTwo
);
}
return deploymentTool.check(functionInfo.labels);
}) // only delete functions uploaded via firebase-tools
.map(pluckName)
.difference(uploadedNames)
.intersection(deleteReleaseNames)
.value();
if (functionsToDelete.length === 0) {
return Promise.resolve();
}
var deleteList = _.map(functionsToDelete, function(func) {
return "\t" + helper.getFunctionLabel(func);
}).join("\n");
if (options.nonInteractive && !options.force) {
var deleteCommands = _.map(functionsToDelete, function(func) {
return (
"\tfirebase functions:delete " +
helper.getFunctionName(func) +
" --region " +
helper.getRegion(func)
);
}).join("\n");
throw new FirebaseError(
"The following functions are found in your project but do not exist in your local source code:\n" +
deleteList +
"\n\nAborting because deletion cannot proceed in non-interactive mode. To fix, manually delete the functions by running:\n" +
clc.bold(deleteCommands)
);
} else if (!options.force) {
logger.info(
"\nThe following functions are found in your project but do not exist in your local source code:\n" +
deleteList +
"\n\nIf you are renaming a function or changing its region, it is recommended that you create the new " +
"function first before deleting the old one to prevent event loss. For more info, visit " +
clc.underline(
"https://firebase.google.com/docs/functions/manage-functions#modify" + "\n"
)
);
}
const next = options.force
? Promise.resolve(true)
: promptOnce({
type: "confirm",
name: "confirm",
default: false,
message:
"Would you like to proceed with deletion? Selecting no will continue the rest of the deployments.",
});
return next.then(function(proceed) {
if (!proceed) {
if (deployments.length !== 0) {
utils.logBullet(clc.bold.cyan("functions: ") + "continuing with other deployments.");
}
return;
}
functionsToDelete.forEach(function(name) {
var functionName = helper.getFunctionName(name);
var scheduleName = helper.getScheduleName(name, appEngineLocation);
var topicName = helper.getTopicName(name);
var region = helper.getRegion(name);
utils.logBullet(
clc.bold.cyan("functions: ") +
"deleting function " +
clc.bold(helper.getFunctionLabel(name)) +
"..."
);
_startTimer(name, "delete");
var retryFunction;
var isScheduledFunction = _.includes(existingScheduledFunctions, name);
if (isScheduledFunction) {
retryFunction = function() {
return gcp.cloudscheduler
.deleteJob(scheduleName)
.catch((err) => {
// if err.status is 404, the schedule doesnt exist, so catch the error
// if err.status is 403, the project doesnt have the api enabled and there are no schedules to delete, so catch the error
logger.debug(err);
if (
err.context.response.statusCode != 404 &&
err.context.response.statusCode != 403
) {
throw new FirebaseError(
`Failed to delete schedule for ${functionName} with status ${err.status}`,
err
);
}
})
.then(() => {
return gcp.pubsub.deleteTopic(topicName);
})
.catch((err) => {
// if err.status is 404, the topic doesnt exist, so catch the error
// if err.status is 403, the project doesnt have the api enabled and there are no topics to delete, so catch the error
if (
err.context.response.statusCode != 404 &&
err.context.response.statusCode != 403
) {
throw new FirebaseError(
`Failed to delete topic for ${functionName} with status ${err.status}`,
err
);
}
})
.then(() => {
return gcp.cloudfunctions.delete({
projectId: projectId,
region: region,
functionName: functionName,
});
});
};
} else {
retryFunction = function() {
return gcp.cloudfunctions.delete({
projectId: projectId,
region: region,
functionName: functionName,
});
};
}
deployments.push({
name: name,
retryFunction: retryFunction,
});
});
});
})
.then(function() {
// filter out functions that are excluded via --only and --except flags
var functionsInDeploy = functionsInfo.filter((trigger) => {
return functionFilterGroups.length > 0
? _.includes(deleteReleaseNames, trigger.name)
: true;
});
return createOrUpdateSchedulesAndTopics(
context.projectId,
functionsInDeploy,
existingScheduledFunctions,
appEngineLocation
);
})
.then(function() {
return utils.promiseAllSettled(
_.map(deployments, function(op) {
return op.retryFunction().then(function(res) {
return _.merge(op, res);
});
})
);
})
.then(function(allOps) {
var failedCalls = _.chain(allOps)
.filter({ state: "rejected" })
.map("reason")
.value();
var successfulCalls = _.chain(allOps)
.filter({ state: "fulfilled" })
.map("value")
.value();
failedDeployments = failedCalls.map((error) => _.get(error, "context.function", ""));
return _fetchTriggerUrls(projectId, successfulCalls, sourceUrl)
.then(function() {
return helper.pollDeploys(
successfulCalls,
printSuccess,
printFail,
printTooManyOps,
projectId
);
})
.then(() => {
if (deployments.length > 0) {
track("Functions Deploy (Result)", "failure", failedDeployments.length);
track(
"Functions Deploy (Result)",
"success",
deployments.length - failedDeployments.length
);
}
if (failedDeployments.length > 0) {
logger.info("\n\nFunctions deploy had errors with the following functions:");
const sortedFailedDeployments = failedDeployments.sort();
for (let i = 0; i < sortedFailedDeployments.length; i++) {
logger.info(`\t${sortedFailedDeployments[i]}`);
}
logger.info("\n\nTo try redeploying those functions, run:");
logger.info(
" " +
clc.bold("firebase deploy --only ") +
clc.bold(sortedFailedDeployments.map((name) => `functions:${name}`).join(","))
);
logger.info("\n\nTo continue deploying other features (such as database), run:");
logger.info(" " + clc.bold("firebase deploy --except functions"));
return Promise.reject(new FirebaseError("Functions did not deploy properly."));
}
});
});
};