-
Notifications
You must be signed in to change notification settings - Fork 816
/
push-resources.ts
189 lines (166 loc) · 7.18 KB
/
push-resources.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
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
import { getProjectConfig } from './get-project-config';
import { showResourceTable } from './resource-status';
import { onCategoryOutputsChange } from './on-category-outputs-change';
import { initializeEnv } from '../../initialize-env';
import { getProviderPlugins } from './get-provider-plugins';
import { getEnvInfo } from './get-env-info';
import { EnvironmentDoesNotExistError, exitOnNextTick, stateManager, $TSAny, $TSContext } from 'amplify-cli-core';
export async function pushResources(
context: $TSContext,
category?: string,
resourceName?: string,
filteredResources?: { category: string; resourceName: string }[],
rebuild: boolean = false,
) {
if (context.parameters.options['iterative-rollback']) {
// validate --iterative-rollback with --force
if (context.parameters.options.force) {
throw new Error(
"'--iterative-rollback' and '--force' cannot be used together. Consider runnning 'amplify push --force' to iteratively rollback and redeploy.",
);
}
context.exeInfo.iterativeRollback = true;
}
if (context.parameters.options.env) {
const envName: string = context.parameters.options.env;
const allEnvs = context.amplify.getAllEnvs();
if (allEnvs.findIndex(env => env === envName) !== -1) {
context.exeInfo = {};
context.exeInfo.forcePush = false;
context.exeInfo.projectConfig = stateManager.getProjectConfig(undefined, {
throwIfNotExist: false,
});
context.exeInfo.localEnvInfo = getEnvInfo();
if (context.exeInfo.localEnvInfo.envName !== envName) {
context.exeInfo.localEnvInfo.envName = envName;
stateManager.setLocalEnvInfo(context.exeInfo.localEnvInfo.projectPath, context.exeInfo.localEnvInfo);
}
await initializeEnv(context);
} else {
const errMessage = "Environment doesn't exist. Please use 'amplify init' to create a new environment";
context.print.error(errMessage);
await context.usageData.emitError(new EnvironmentDoesNotExistError(errMessage));
exitOnNextTick(1);
}
}
let hasChanges = false;
if (!rebuild) {
// status table does not have a way to show resource in "rebuild" state so skipping it to avoid confusion
hasChanges = await showResourceTable(category, resourceName, filteredResources);
}
// no changes detected
if (!hasChanges && !context.exeInfo.forcePush && !rebuild) {
context.print.info('\nNo changes detected');
return context;
}
// rebuild has an upstream confirmation prompt so no need to prompt again here
let continueToPush = (context.exeInfo && context.exeInfo.inputParams && context.exeInfo.inputParams.yes) || rebuild;
if (!continueToPush) {
if (context.exeInfo.iterativeRollback) {
context.print.info('The CLI will rollback the last known iterative deployment.');
}
continueToPush = await context.amplify.confirmPrompt('Are you sure you want to continue?');
}
let retryPush;
if (continueToPush) {
do {
retryPush = false;
try {
// Get current-cloud-backend's amplify-meta
const currentAmplifyMeta = stateManager.getCurrentMeta();
await providersPush(context, rebuild, category, resourceName, filteredResources);
await onCategoryOutputsChange(context, currentAmplifyMeta);
} catch (err) {
if (await isValidGraphQLAuthError(err.message)) {
retryPush = await handleValidGraphQLAuthError(context, err.message);
}
if (!retryPush) {
throw err;
}
}
} while (retryPush);
} else {
// there's currently no other mechanism to stop the execution of the postPush workflow in this case, so exiting here
exitOnNextTick(1);
}
return continueToPush;
}
async function isValidGraphQLAuthError(message: string) {
if (
message === `@auth directive with 'iam' provider found, but the project has no IAM authentication provider configured.` ||
message ===
`@auth directive with 'userPools' provider found, but the project has no Cognito User Pools authentication provider configured.` ||
message === `@auth directive with 'oidc' provider found, but the project has no OPENID_CONNECT authentication provider configured.` ||
message === `@auth directive with 'apiKey' provider found, but the project has no API Key authentication provider configured.` ||
message === `@auth directive with 'function' provider found, but the project has no Lambda authentication provider configured.`
) {
return true;
}
}
async function handleValidGraphQLAuthError(context: $TSContext, message: string) {
if (message === `@auth directive with 'iam' provider found, but the project has no IAM authentication provider configured.`) {
await addGraphQLAuthRequirement(context, 'AWS_IAM');
return true;
} else if (!context?.parameters?.options?.yes) {
if (
message ===
`@auth directive with 'userPools' provider found, but the project has no Cognito User Pools authentication provider configured.`
) {
await addGraphQLAuthRequirement(context, 'AMAZON_COGNITO_USER_POOLS');
return true;
} else if (
message === `@auth directive with 'oidc' provider found, but the project has no OPENID_CONNECT authentication provider configured.`
) {
await addGraphQLAuthRequirement(context, 'OPENID_CONNECT');
return true;
} else if (
message === `@auth directive with 'apiKey' provider found, but the project has no API Key authentication provider configured.`
) {
await addGraphQLAuthRequirement(context, 'API_KEY');
return true;
} else if (
message === `@auth directive with 'function' provider found, but the project has no Lambda authentication provider configured.`
) {
await addGraphQLAuthRequirement(context, 'AWS_LAMBDA');
return true;
}
}
return false;
}
async function addGraphQLAuthRequirement(context, authType) {
return await context.amplify.invokePluginMethod(context, 'api', undefined, 'addGraphQLAuthorizationMode', [
context,
{
authType: authType,
printLeadText: true,
authSettings: undefined,
},
]);
}
async function providersPush(
context: $TSContext,
rebuild: boolean = false,
category?: string,
resourceName?: string,
filteredResources?: { category: string; resourceName: string }[],
) {
const { providers } = getProjectConfig();
const providerPlugins = getProviderPlugins(context);
const providerPromises: (() => Promise<$TSAny>)[] = [];
for (const provider of providers) {
const providerModule = require(providerPlugins[provider]);
const resourceDefiniton = await context.amplify.getResourceStatus(category, resourceName, provider, filteredResources);
providerPromises.push(providerModule.pushResources(context, resourceDefiniton, rebuild));
}
await Promise.all(providerPromises);
}
export async function storeCurrentCloudBackend(context: $TSContext) {
const { providers } = getProjectConfig();
const providerPlugins = getProviderPlugins(context);
const providerPromises: (() => Promise<$TSAny>)[] = [];
for (const provider of providers) {
const providerModule = require(providerPlugins[provider]);
providerPromises.push(providerModule.storeCurrentCloudBackend(context));
}
await Promise.all(providerPromises);
}