Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
06b62a1
Adding onPoll option to operation-poller (#3046)
joehan Jan 19, 2021
6258dd5
Typescriptify functionsDeployHelper (#3059)
joehan Jan 20, 2021
ec7d079
Typescriptifying gcp.cloudfunctions (#3060)
joehan Jan 20, 2021
f583ef6
Typescriptifying functionsConfig (#3063)
joehan Jan 21, 2021
08b9d56
Typescriptifying deploymentTool (#3061)
joehan Jan 21, 2021
b4944a4
Refactoring prepare stage of functions deploy (#3067)
joehan Jan 21, 2021
3be0dca
refactoring release step of functions deploy to use typescript
joehan Jan 21, 2021
e0e703e
Adding logic to build regional deployments
joehan Jan 24, 2021
046c7d7
Implementing createDeploymentPlan
joehan Jan 26, 2021
b876523
First round of PR feedback, removing most usages of lodash
joehan Jan 28, 2021
9e0e6e9
moving function prompts into their own file
joehan Jan 28, 2021
2a3b547
seperating out a bunch of code from functionsDeployHelper
joehan Jan 28, 2021
51f2395
Resolves merge conflicts
joehan Jan 28, 2021
30cc0e9
refactoring release step of functions deploy to use typescript (#3071)
joehan Feb 1, 2021
6916000
Implements core logic of running function deploys
joehan Feb 1, 2021
3c8d4a0
Typescriptifying prepareFunctionsUpload (#3064)
joehan Feb 1, 2021
11956fa
Implementing createDeploymentPlan (#3081)
joehan Feb 1, 2021
85d0afe
adding timing and logs for deployments
joehan Feb 2, 2021
00b1989
cleaning up unused code
joehan Feb 2, 2021
397d7c4
Fixing some things that were broken while merging
joehan Feb 3, 2021
21f4906
Fixing up the order of wait and close to ensure that queue promsies a…
joehan Feb 4, 2021
3b3edbd
Format and clean up typos
joehan Feb 4, 2021
e428bcb
refactoring error handling to be cleaner
joehan Feb 5, 2021
4c8e2fb
cleaning up extera newlines
joehan Feb 8, 2021
7f48130
first round of pr fixes
joehan Feb 9, 2021
39a7e86
Readding some changes that I accidenttally wiped out during a merge
joehan Feb 9, 2021
1366955
Switching name to id where appropriate
joehan Feb 9, 2021
7513229
fixing another bug caused by functionName vs Id
joehan Feb 9, 2021
8d3d82d
Merge pull request #3107 from firebase/jh-execute-deployment-plans
joehan Feb 9, 2021
6d2260e
Refactor functions-delete (#3110)
joehan Feb 9, 2021
42e6c15
Cleaning up error reporting
joehan Feb 10, 2021
e4ce126
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 10, 2021
12a48ea
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 11, 2021
7cfe9d9
Implement validation for changing trigger types, and fixes from bug b…
joehan Feb 12, 2021
5eb08bd
Merge branch 'master' into jh-functions-refactor
joehan Feb 12, 2021
5ca6bbf
Merge branch 'master' into jh-functions-refactor
joehan Feb 16, 2021
344b674
fixes package.json
joehan Feb 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring prepare stage of functions deploy (#3067)
* refactoring prepare setp of functions deploy to use typescript, check for failure policies, and parse function triggers

* refactoring prepare setp of functions deploy to use typescript, check for failure policies, and parse function triggers

* commenting out functionNamesAreValid

* formats
  • Loading branch information
joehan authored Jan 21, 2021
commit b4944a4425670a94a8b3d764dc32f372ee226325
25 changes: 5 additions & 20 deletions src/deploy/functions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ import { setGracefulCleanup } from "tmp";

import { functionsUploadRegion } from "../../api";
import * as gcp from "../../gcp";
import { logBullet, logSuccess, logWarning } from "../../utils";
import * as prepareFunctionsUpload from "../../prepareFunctionsUpload";
import { logSuccess, logWarning } from "../../utils";
import { checkHttpIam } from "./checkIam";

const GCP_REGION = functionsUploadRegion;

setGracefulCleanup();

async function uploadSource(
context: { projectId: string; uploadUrl?: string },
source: any // eslint-disable-line @typescript-eslint/no-explicit-any
): Promise<void> {
async function uploadSource(context: any): Promise<void> {
const uploadUrl = await gcp.cloudfunctions.generateUploadUrl(context.projectId, GCP_REGION);
context.uploadUrl = uploadUrl;
const apiUploadUrl = uploadUrl.replace("https://storage.googleapis.com", "");
await gcp.storage.upload(source, apiUploadUrl);
await gcp.storage.upload(context.functionsSource, apiUploadUrl);
}

/**
Expand All @@ -30,26 +26,15 @@ async function uploadSource(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function deploy(context: any, options: any, payload: any): Promise<void> {
if (options.config.get("functions")) {
logBullet(
clc.cyan.bold("functions:") +
" preparing " +
clc.bold(options.config.get("functions.source")) +
" directory for uploading..."
);

const source = await prepareFunctionsUpload(context, options);
context.existingFunctions = await gcp.cloudfunctions.listAllFunctions(context.projectId);
payload.functions = {
triggers: options.config.get("functions.triggers"),
};

await checkHttpIam(context, options, payload);

if (!source) {
if (!context.functionsSource) {
return;
}
try {
await uploadSource(context, source);
await uploadSource(context);
logSuccess(
clc.green.bold("functions:") +
" " +
Expand Down
4 changes: 2 additions & 2 deletions src/deploy/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as prepare from "./prepare";
import * as release from "./release";
export { prepare } from "./prepare";
export { deploy } from "./deploy";
export { prepare, release };
export { release };
49 changes: 0 additions & 49 deletions src/deploy/functions/prepare.js

This file was deleted.

65 changes: 65 additions & 0 deletions src/deploy/functions/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as _ from "lodash";
import * as clc from "cli-color";

import * as ensureApiEnabled from "../../ensureApiEnabled";
import * as functionsConfig from "../../functionsConfig";
import * as getProjectId from "../../getProjectId";
import { logBullet } from "../../utils";
import { getRuntimeChoice } from "../../parseRuntimeAndValidateSDK";
import { getFunctionsInfo, promptForFailurePolicies } from "../../functionsDeployHelper";
import * as prepareFunctionsUpload from "../../prepareFunctionsUpload";
import * as validate from "./validate";
import { checkRuntimeDependencies } from "./checkRuntimeDependencies";

export async function prepare(context: any, options: any, payload: any): Promise<void> {
if (!options.config.has("functions")) {
return;
}

const sourceDirName = options.config.get("functions.source");
const sourceDir = options.config.path(sourceDirName);
const projectDir = options.config.projectDir;
const projectId = getProjectId(options);

// Check what runtime to use, first in firebase.json, then in 'engines' field.
const runtimeFromConfig = options.config.get("functions.runtime");
context.runtimeChoice = getRuntimeChoice(sourceDir, runtimeFromConfig);

// Check that all necessary APIs are enabled.
const checkAPIsEnabled = await Promise.all([
ensureApiEnabled.ensure(options.project, "cloudfunctions.googleapis.com", "functions"),
ensureApiEnabled.check(projectId, "runtimeconfig.googleapis.com", "runtimeconfig", true),
checkRuntimeDependencies(projectId, context.runtimeChoice),
]);
_.set(context, "runtimeConfigEnabled", checkAPIsEnabled[1]);

// Get the Firebase Config.
const firebaseConfig = await functionsConfig.getFirebaseConfig(options);
_.set(context, "firebaseConfig", firebaseConfig);

// Prepare the functions directory for upload, and set context.triggers.
logBullet(
clc.cyan.bold("functions:") +
" preparing " +
clc.bold(options.config.get("functions.source")) +
" directory for uploading..."
);
const source = await prepareFunctionsUpload(context, options);
_.set(context, "functionsSource", source);

// Get a list of CloudFunctionTriggers, with duplicates for each region.
payload.functions = {};
payload.functions.triggers = getFunctionsInfo(
options.config.get("functions.triggers"),
projectId
);

// Validate the function code that is being deployed.
validate.functionsDirectoryExists(options, sourceDirName);
// validate.functionNamesAreValid(payload.functionNames);
// TODO: This doesn't do anything meaningful right now because payload.functions is not defined
validate.packageJsonIsValid(sourceDirName, sourceDir, projectDir, !!runtimeFromConfig);

// Display a warning and prompt if any functions have failurePolicies.
await promptForFailurePolicies(options, payload.functions.triggers);
}
44 changes: 2 additions & 42 deletions src/deploy/functions/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = function (context, options, payload) {
failedDeployments = [];

var appEngineLocation = getAppEngineLocation(context.firebaseConfig);
var functionsInfo = helper.getFunctionsInfo(payload.functions.triggers, projectId);
var functionsInfo = payload.functions.triggers;
functionsInfo = functionsInfo.map((fn) => {
if (
fn.eventTrigger &&
Expand All @@ -154,49 +154,9 @@ module.exports = function (context, options, payload) {
var deleteReleaseNames;
var existingScheduledFunctions;

// Collect all the functions that have a retry policy
var failurePolicyFunctions = functionsInfo.filter((fn) => {
return !!fn.failurePolicy;
});

let proceedPrompt = Promise.resolve(true);
if (failurePolicyFunctions.length) {
var failurePolicyFunctionLabels = failurePolicyFunctions.map((fn) => {
return helper.getFunctionLabel(_.get(fn, "name"));
});
var retryMessage =
"The following functions will be retried in case of failure: " +
clc.bold(failurePolicyFunctionLabels.join(", ")) +
". " +
"Retried executions are billed as any other execution, and functions are retried repeatedly until they either successfully execute or the maximum retry period has elapsed, which can be up to 7 days. " +
"For safety, you might want to ensure that your functions are idempotent; see https://firebase.google.com/docs/functions/retries to learn more.";

utils.logLabeledWarning("functions", retryMessage);

if (options.nonInteractive && !options.force) {
throw new FirebaseError("Pass the --force option to deploy functions with a failure policy", {
exit: 1,
});
} else if (!options.nonInteractive) {
proceedPrompt = promptOnce({
type: "confirm",
name: "confirm",
default: false,
message: "Would you like to proceed with deployment?",
});
}
}

delete payload.functions;

return proceedPrompt
.then((proceed) => {
if (!proceed) {
throw new FirebaseError("Deployment canceled.", { exit: 1 });
}

return Promise.resolve(context.existingFunctions);
})
return Promise.resolve(context.existingFunctions)
.then(function (existingFunctions) {
var pluckName = function (functionObject) {
return _.get(functionObject, "name"); // e.g.'projects/proj1/locations/us-central1/functions/func'
Expand Down
47 changes: 47 additions & 0 deletions src/functionsDeployHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as track from "./track";
import * as utils from "./utils";
import * as cloudfunctions from "./gcp/cloudfunctions";
import * as pollOperations from "./pollOperations";
import { promptOnce } from "./prompt";

// TODO: Get rid of this when switching to use operation-poller.
export interface Operation {
Expand Down Expand Up @@ -267,3 +268,49 @@ export function pollDeploys(
throw new FirebaseError("Failed to get status of functions deployments.");
}
}

/**
* Checks if a deployment will create any functions with a failure policy.
* If there are any, prompts the user to acknowledge the retry behavior.
* @param options
* @param functions A list of all functions in the deployment
*/
export async function promptForFailurePolicies(
options: any,
functions: CloudFunctionTrigger[]
): Promise<void> {
// Collect all the functions that have a retry policy
const failurePolicyFunctions = functions.filter((fn: CloudFunctionTrigger) => {
return !!fn.failurePolicy;
});

if (failurePolicyFunctions.length) {
const failurePolicyFunctionLabels = failurePolicyFunctions.map((fn: CloudFunctionTrigger) => {
return getFunctionLabel(_.get(fn, "name"));
});
const retryMessage =
"The following functions will be retried in case of failure: " +
clc.bold(failurePolicyFunctionLabels.join(", ")) +
". " +
"Retried executions are billed as any other execution, and functions are retried repeatedly until they either successfully execute or the maximum retry period has elapsed, which can be up to 7 days. " +
"For safety, you might want to ensure that your functions are idempotent; see https://firebase.google.com/docs/functions/retries to learn more.";

utils.logLabeledWarning("functions", retryMessage);

if (options.nonInteractive && !options.force) {
throw new FirebaseError("Pass the --force option to deploy functions with a failure policy", {
exit: 1,
});
} else if (!options.nonInteractive) {
const proceed = await promptOnce({
type: "confirm",
name: "confirm",
default: false,
message: "Would you like to proceed with deployment?",
});
if (!proceed) {
throw new FirebaseError("Deployment canceled.", { exit: 1 });
}
}
}
}
Loading