grada-run 0.49.8 → 0.49.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/apply.js +137 -115
- package/src/commands/init.js +1023 -834
- package/src/utils/recovery.js +35 -1
- package/src/utils/terraform-errors.js +2 -2
package/package.json
CHANGED
package/src/commands/apply.js
CHANGED
|
@@ -15,7 +15,7 @@ import { readInitManifest, recordAppliedTarget } from '../utils/backup.js';
|
|
|
15
15
|
import { requireManagedTree, parseStateBucketBackend, upsertAutoTfvar } from '../utils/terraform-metadata.js';
|
|
16
16
|
import { readTerraformComputeTarget, resolveRegion, readRenderedContainerEnvNames } from '../utils/resolvers.js';
|
|
17
17
|
import { requireAwakeEnvironment, resolveSleepTarget, guardSleepingEnv } from '../utils/sleep-state.js';
|
|
18
|
-
import { promptStateBucketRecovery, recreateStateBucket } from '../utils/recovery.js';
|
|
18
|
+
import { promptStateBucketRecovery, recreateStateBucket, offerInFlowRecovery, confirmRecoveryAction } from '../utils/recovery.js';
|
|
19
19
|
import { resolveProjectName, readResourceSuffix } from '../utils/resolvers.js';
|
|
20
20
|
import { ensureLambdaSeedImage } from '../utils/lambda-ecr.js';
|
|
21
21
|
import { checkAwsCredentials, ensureStateBucketExists, isAuthError, printAwsAuthGuidance, resolveClient } from '../utils/aws.js';
|
|
@@ -406,6 +406,18 @@ function preflightSecretEnvCollisions({ targetDir }) {
|
|
|
406
406
|
return { kind: 'collision', keys: collisions };
|
|
407
407
|
}
|
|
408
408
|
|
|
409
|
+
// Picks the live AWSCURRENT version id out of a DescribeSecret
|
|
410
|
+
// response — null when none is staged current (a missing or
|
|
411
|
+
// stageless map means there is nothing safe to adopt).
|
|
412
|
+
function currentLiveVersionId(described) {
|
|
413
|
+
const stages = described?.VersionIdsToStages;
|
|
414
|
+
if (!stages || typeof stages !== 'object') return null;
|
|
415
|
+
for (const [versionId, versionStages] of Object.entries(stages)) {
|
|
416
|
+
if (Array.isArray(versionStages) && versionStages.includes('AWSCURRENT')) return versionId;
|
|
417
|
+
}
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
|
|
409
421
|
export async function applyStack(input = {}) {
|
|
410
422
|
setActiveCommandName('apply');
|
|
411
423
|
try {
|
|
@@ -809,36 +821,31 @@ async function applyStackMain(input = {}) {
|
|
|
809
821
|
const pairs = parseCollisionImports(errString)
|
|
810
822
|
.filter((pair) => !pair.address.startsWith('aws_secretsmanager_secret.'));
|
|
811
823
|
if (pairs.length > 0) {
|
|
812
|
-
const
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
});
|
|
826
|
-
proceed = !isCancel(confirmed) && confirmed === true;
|
|
827
|
-
}
|
|
828
|
-
if (proceed) {
|
|
829
|
-
try {
|
|
824
|
+
const recovery = await offerInFlowRecovery({
|
|
825
|
+
options,
|
|
826
|
+
refusal: {
|
|
827
|
+
message: '\n✖ apply needs approval to import collided resources into state.',
|
|
828
|
+
hint: ' Re-run with --auto-approve to import non-interactively.\n',
|
|
829
|
+
event: 'infrastructure_applied',
|
|
830
|
+
telemetry: { projectName: actualProjectName },
|
|
831
|
+
errorCode: 'COLLISION_IMPORT_NON_INTERACTIVE',
|
|
832
|
+
},
|
|
833
|
+
approve: () => confirmRecoveryAction(
|
|
834
|
+
`Import ${pairs.length} collided resource${pairs.length === 1 ? '' : 's'} into state and retry?`
|
|
835
|
+
),
|
|
836
|
+
act: async () => {
|
|
830
837
|
for (const pair of pairs) {
|
|
831
838
|
await runTerraform(['import', pair.address, pair.id], tfDir, null, `Importing ${pair.address}`);
|
|
832
839
|
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
}
|
|
840
|
+
},
|
|
841
|
+
});
|
|
842
|
+
if (recovery.status === 'refused') return recovery.refusal;
|
|
843
|
+
if (recovery.status === 'recovered') {
|
|
844
|
+
trackEvent('collision_resources_imported', { projectName: actualProjectName, count: pairs.length });
|
|
845
|
+
log.message(color.cyan('Imported collided resources — retrying apply...'));
|
|
846
|
+
return applyStack({ ...options, _skipPreview: true, _collisionImported: true });
|
|
841
847
|
}
|
|
848
|
+
if (recovery.status === 'failed') collisionImportError = recovery.error;
|
|
842
849
|
}
|
|
843
850
|
}
|
|
844
851
|
trackEvent('bucket_recovery_probed', { projectName: actualProjectName, collisions });
|
|
@@ -860,38 +867,32 @@ async function applyStackMain(input = {}) {
|
|
|
860
867
|
// Adopt the shared provider instead of hand-editing: the
|
|
861
868
|
// GitHub provider URL is account-singleton, so the existing
|
|
862
869
|
// provider IS the match — flip the var and retry.
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
});
|
|
877
|
-
adopt = !isCancel(confirmed) && confirmed === true;
|
|
878
|
-
}
|
|
879
|
-
if (adopt) {
|
|
880
|
-
try {
|
|
870
|
+
const recovery = await offerInFlowRecovery({
|
|
871
|
+
options,
|
|
872
|
+
refusal: {
|
|
873
|
+
message: '\n✖ apply needs approval to adopt the existing GitHub OIDC provider.',
|
|
874
|
+
hint: ' Re-run with --auto-approve to adopt non-interactively.\n',
|
|
875
|
+
event: 'infrastructure_applied',
|
|
876
|
+
telemetry: { projectName: actualProjectName },
|
|
877
|
+
errorCode: 'OIDC_ADOPT_NON_INTERACTIVE',
|
|
878
|
+
},
|
|
879
|
+
approve: () => confirmRecoveryAction(
|
|
880
|
+
'Adopt the existing GitHub OIDC provider (writes create_oidc_provider = false) and retry?'
|
|
881
|
+
),
|
|
882
|
+
act: async () => {
|
|
881
883
|
upsertAutoTfvar(tfDir, 'create_oidc_provider', false);
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
trackEvent('oidc_provider_adopted', { projectName: actualProjectName });
|
|
890
|
-
log.message(color.cyan('Adopted the existing OIDC provider — retrying apply...'));
|
|
891
|
-
return applyStack({ ...options, _skipPreview: true, _oidcAdopted: true });
|
|
892
|
-
}
|
|
884
|
+
},
|
|
885
|
+
});
|
|
886
|
+
if (recovery.status === 'refused') return recovery.refusal;
|
|
887
|
+
if (recovery.status === 'recovered') {
|
|
888
|
+
trackEvent('oidc_provider_adopted', { projectName: actualProjectName });
|
|
889
|
+
log.message(color.cyan('Adopted the existing OIDC provider — retrying apply...'));
|
|
890
|
+
return applyStack({ ...options, _skipPreview: true, _oidcAdopted: true });
|
|
893
891
|
}
|
|
894
|
-
//
|
|
892
|
+
// A failed write must not crash inside the error handler:
|
|
893
|
+
// fall through to the manual guidance with the write error
|
|
894
|
+
// appended. Declined runs fall through the same way.
|
|
895
|
+
if (recovery.status === 'failed') adoptUpsertError = recovery.error;
|
|
895
896
|
}
|
|
896
897
|
|
|
897
898
|
if (code === 'OIDC_PROVIDER_CONFLICT') {
|
|
@@ -916,36 +917,32 @@ async function applyStackMain(input = {}) {
|
|
|
916
917
|
// Offer the reconfigure in-flow instead of shelling the
|
|
917
918
|
// user out to raw terraform; declined or failed runs
|
|
918
919
|
// fall through to the runbook below.
|
|
919
|
-
const
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
});
|
|
933
|
-
proceed = !isCancel(confirmed) && confirmed === true;
|
|
934
|
-
}
|
|
935
|
-
if (proceed) {
|
|
936
|
-
try {
|
|
920
|
+
const recovery = await offerInFlowRecovery({
|
|
921
|
+
options,
|
|
922
|
+
refusal: {
|
|
923
|
+
message: '\n✖ apply needs approval to reconfigure the Terraform backend.',
|
|
924
|
+
hint: ' Re-run with --auto-approve to reconfigure non-interactively.\n',
|
|
925
|
+
event: 'infrastructure_applied',
|
|
926
|
+
telemetry: { projectName: actualProjectName },
|
|
927
|
+
errorCode: 'BACKEND_RECONFIGURE_NON_INTERACTIVE',
|
|
928
|
+
},
|
|
929
|
+
approve: () => confirmRecoveryAction(
|
|
930
|
+
'Reconfigure the Terraform backend (init -reconfigure) and retry?'
|
|
931
|
+
),
|
|
932
|
+
act: async () => {
|
|
937
933
|
await runTerraform(['init', '-reconfigure'], tfDir, null, 'Reconfiguring backend');
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
}
|
|
934
|
+
},
|
|
935
|
+
});
|
|
936
|
+
if (recovery.status === 'refused') return recovery.refusal;
|
|
937
|
+
if (recovery.status === 'recovered') {
|
|
938
|
+
trackEvent('backend_reconfigured', { projectName: actualProjectName });
|
|
939
|
+
log.message(color.cyan('Reconfigured the backend — retrying apply...'));
|
|
940
|
+
return applyStack({ ...options, _skipPreview: true, _backendReconfigured: true });
|
|
946
941
|
}
|
|
942
|
+
if (recovery.status === 'failed') reconfigureError = recovery.error;
|
|
947
943
|
}
|
|
948
944
|
let secretRecoveryError = null;
|
|
945
|
+
let secretImported = false;
|
|
949
946
|
if (code === 'SECRET_PENDING_DELETION' && secretName && options._secretRecovered !== true) {
|
|
950
947
|
// Offer the recovery in-flow instead of shelling the user
|
|
951
948
|
// out to pasted AWS CLI + raw terraform; declined, failed,
|
|
@@ -953,28 +950,30 @@ async function applyStackMain(input = {}) {
|
|
|
953
950
|
// runbook below. Restore is non-destructive (auto under
|
|
954
951
|
// --auto-approve); force-delete always needs an explicit
|
|
955
952
|
// interactive choice, never auto.
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
953
|
+
let versionAdopted = null;
|
|
954
|
+
const recovery = await offerInFlowRecovery({
|
|
955
|
+
options,
|
|
956
|
+
refusal: {
|
|
957
|
+
message: '\n✖ apply needs approval to recover the deleted secret vault.',
|
|
958
|
+
hint: ' Re-run with --auto-approve to restore non-interactively.\n',
|
|
959
|
+
event: 'infrastructure_applied',
|
|
960
|
+
telemetry: { projectName: actualProjectName },
|
|
961
|
+
errorCode: 'SECRET_RECOVERY_NON_INTERACTIVE',
|
|
962
|
+
},
|
|
963
|
+
approve: async () => {
|
|
964
|
+
const choice = await select({
|
|
965
|
+
message: `Secret "${secretName}" is scheduled for deletion — recover it and retry?`,
|
|
966
|
+
options: [
|
|
967
|
+
{ value: 'restore', label: 'Restore the vault, import, and retry' },
|
|
968
|
+
{ value: 'force-delete', label: 'Force-delete without recovery and retry' },
|
|
969
|
+
{ value: 'cancel', label: 'Cancel (show manual steps)' },
|
|
970
|
+
],
|
|
971
|
+
});
|
|
972
|
+
if (!isCancel(choice) && (choice === 'restore' || choice === 'force-delete')) return choice;
|
|
973
|
+
return null;
|
|
974
|
+
},
|
|
975
|
+
autoMethod: 'restore',
|
|
976
|
+
act: async (method) => {
|
|
978
977
|
const targetRegion = detectedConfig.region || process.env.AWS_REGION || 'us-east-2';
|
|
979
978
|
const client = resolveClient(options.secretsClient, SecretsManagerClient, { region: targetRegion });
|
|
980
979
|
if (method === 'restore') {
|
|
@@ -986,23 +985,43 @@ async function applyStackMain(input = {}) {
|
|
|
986
985
|
// Argv-spawned (no shell), so the count index
|
|
987
986
|
// needs no quoting — unlike the printed runbook.
|
|
988
987
|
await runTerraform(['import', 'aws_secretsmanager_secret.app_secrets[0]', described.ARN], tfDir, null, `Importing ${secretName}`);
|
|
988
|
+
secretImported = true;
|
|
989
|
+
// Adopt the live current version too: without
|
|
990
|
+
// it the retry recreates the placeholder
|
|
991
|
+
// version over the restored real values
|
|
992
|
+
// (ignore_changes never stops creation).
|
|
993
|
+
const liveVersion = currentLiveVersionId(described);
|
|
994
|
+
versionAdopted = liveVersion !== null;
|
|
995
|
+
if (liveVersion) {
|
|
996
|
+
await runTerraform(['import', 'aws_secretsmanager_secret_version.app_secrets_initial[0]', `${described.ARN}|${liveVersion}`], tfDir, null, `Importing ${secretName} live values`);
|
|
997
|
+
}
|
|
989
998
|
} else {
|
|
990
999
|
await client.send(new DeleteSecretCommand({ SecretId: secretName, ForceDeleteWithoutRecovery: true }));
|
|
991
1000
|
}
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
1001
|
+
},
|
|
1002
|
+
});
|
|
1003
|
+
if (recovery.status === 'refused') return recovery.refusal;
|
|
1004
|
+
if (recovery.status === 'recovered') {
|
|
1005
|
+
const method = recovery.method;
|
|
1006
|
+
trackEvent('secret_deletion_recovered', {
|
|
1007
|
+
projectName: actualProjectName,
|
|
1008
|
+
method,
|
|
1009
|
+
...(method === 'restore' ? { adopted: versionAdopted === true } : {}),
|
|
1010
|
+
});
|
|
1011
|
+
if (method === 'restore') {
|
|
1012
|
+
if (versionAdopted) {
|
|
1013
|
+
log.message(color.cyan('Restored the secret vault and adopted its live values — retrying apply...'));
|
|
999
1014
|
} else {
|
|
1000
|
-
log.message(color.cyan('
|
|
1001
|
-
log.message(color.
|
|
1015
|
+
log.message(color.cyan('Restored the secret vault and imported it — retrying apply...'));
|
|
1016
|
+
log.message(color.yellow(' Live values could not be adopted: run secrets audit after apply and re-push if they look wrong.'));
|
|
1002
1017
|
}
|
|
1003
|
-
|
|
1018
|
+
} else {
|
|
1019
|
+
log.message(color.cyan('Force-deleted the secret vault — retrying apply...'));
|
|
1020
|
+
log.message(color.dim(' Re-push your secrets after apply (secrets push): the vault is recreated empty.'));
|
|
1004
1021
|
}
|
|
1022
|
+
return applyStack({ ...options, _skipPreview: true, _secretRecovered: true });
|
|
1005
1023
|
}
|
|
1024
|
+
if (recovery.status === 'failed') secretRecoveryError = recovery.error;
|
|
1006
1025
|
}
|
|
1007
1026
|
const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId, secretName });
|
|
1008
1027
|
log.error(color.red(`✖ ${runbook.message}`));
|
|
@@ -1013,6 +1032,9 @@ async function applyStackMain(input = {}) {
|
|
|
1013
1032
|
if (secretRecoveryError) {
|
|
1014
1033
|
log.message(color.dim(` (Automatic recovery failed: ${secretRecoveryError.message})`));
|
|
1015
1034
|
}
|
|
1035
|
+
if (secretRecoveryError && secretImported) {
|
|
1036
|
+
log.message(color.yellow(' The vault is already imported — run the version-adopt import from the steps above before re-applying, or the live values will be overwritten with placeholders.'));
|
|
1037
|
+
}
|
|
1016
1038
|
log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
|
|
1017
1039
|
} else {
|
|
1018
1040
|
// Generic fallback for unknown errors
|