|
| 1 | +import type { MissingContext } from '@aws-cdk/cloud-assembly-schema'; |
| 2 | +import * as cxapi from '@aws-cdk/cx-api'; |
| 3 | +import { SdkProvider } from 'aws-cdk/lib/api/aws-auth'; |
| 4 | +import * as contextproviders from 'aws-cdk/lib/context-providers'; |
| 5 | +import { debug } from 'aws-cdk/lib/logging'; |
| 6 | +import { Context, PROJECT_CONTEXT } from 'aws-cdk/lib/settings'; |
| 7 | +import { ICloudAssemblySource } from './types'; |
| 8 | +import { ToolkitError } from '../errors'; |
| 9 | + |
| 10 | +export interface CloudExecutableProps { |
| 11 | + /** |
| 12 | + * AWS object (used by contextprovider) |
| 13 | + */ |
| 14 | + readonly sdkProvider: SdkProvider; |
| 15 | + |
| 16 | + /** |
| 17 | + * Application context |
| 18 | + */ |
| 19 | + readonly context: Context; |
| 20 | + |
| 21 | + /** |
| 22 | + * The file used to store application context in (relative to cwd). |
| 23 | + * |
| 24 | + * @default "cdk.context.json" |
| 25 | + */ |
| 26 | + readonly contextFile?: string; |
| 27 | + |
| 28 | + /** |
| 29 | + * Enable context lookups. |
| 30 | + * |
| 31 | + * Producing a `cxapi.CloudAssembly` will fail if this is disabled and context lookups need to be performed. |
| 32 | + * |
| 33 | + * @default true |
| 34 | + */ |
| 35 | + readonly lookups?: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Represent the Cloud Executable and the synthesis we can do on it |
| 40 | + */ |
| 41 | +export class ContextAwareCloudAssembly implements ICloudAssemblySource { |
| 42 | + private canLookup: boolean; |
| 43 | + private context: Context; |
| 44 | + private contextFile: string; |
| 45 | + |
| 46 | + constructor(private readonly source: ICloudAssemblySource, private readonly props: CloudExecutableProps) { |
| 47 | + this.canLookup = props.lookups ?? true; |
| 48 | + this.context = props.context; |
| 49 | + this.contextFile = props.contextFile ?? PROJECT_CONTEXT; // @todo new feature not needed right now |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Produce a Cloud Assembly, i.e. a set of stacks |
| 54 | + */ |
| 55 | + public async produce(): Promise<cxapi.CloudAssembly> { |
| 56 | + // We may need to run the cloud executable multiple times in order to satisfy all missing context |
| 57 | + // (When the executable runs, it will tell us about context it wants to use |
| 58 | + // but it missing. We'll then look up the context and run the executable again, and |
| 59 | + // again, until it doesn't complain anymore or we've stopped making progress). |
| 60 | + let previouslyMissingKeys: Set<string> | undefined; |
| 61 | + while (true) { |
| 62 | + const assembly = await this.source.produce(); |
| 63 | + |
| 64 | + if (assembly.manifest.missing && assembly.manifest.missing.length > 0) { |
| 65 | + const missingKeys = missingContextKeys(assembly.manifest.missing); |
| 66 | + |
| 67 | + if (!this.canLookup) { |
| 68 | + throw new ToolkitError( |
| 69 | + 'Context lookups have been disabled. ' |
| 70 | + + 'Make sure all necessary context is already in \'cdk.context.json\' by running \'cdk synth\' on a machine with sufficient AWS credentials and committing the result. ' |
| 71 | + + `Missing context keys: '${Array.from(missingKeys).join(', ')}'`); |
| 72 | + } |
| 73 | + |
| 74 | + let tryLookup = true; |
| 75 | + if (previouslyMissingKeys && equalSets(missingKeys, previouslyMissingKeys)) { |
| 76 | + debug('Not making progress trying to resolve environmental context. Giving up.'); |
| 77 | + tryLookup = false; |
| 78 | + } |
| 79 | + |
| 80 | + previouslyMissingKeys = missingKeys; |
| 81 | + |
| 82 | + if (tryLookup) { |
| 83 | + debug('Some context information is missing. Fetching...'); |
| 84 | + |
| 85 | + await contextproviders.provideContextValues( |
| 86 | + assembly.manifest.missing, |
| 87 | + this.context, |
| 88 | + this.props.sdkProvider, |
| 89 | + ); |
| 90 | + |
| 91 | + // Cache the new context to disk |
| 92 | + await this.context.save(this.contextFile); |
| 93 | + |
| 94 | + // Execute again |
| 95 | + continue; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return assembly; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Return all keys of missing context items |
| 107 | + */ |
| 108 | +function missingContextKeys(missing?: MissingContext[]): Set<string> { |
| 109 | + return new Set((missing || []).map(m => m.key)); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Are two sets equal to each other |
| 114 | + */ |
| 115 | +function equalSets<A>(a: Set<A>, b: Set<A>) { |
| 116 | + if (a.size !== b.size) { return false; } |
| 117 | + for (const x of a) { |
| 118 | + if (!b.has(x)) { return false; } |
| 119 | + } |
| 120 | + return true; |
| 121 | +} |
0 commit comments