github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/ops/aws/canary/lib/config.ts (about) 1 import * as cdk from "aws-cdk-lib"; 2 3 // These values map to the keys in our cdk.json and make sure 4 // no values that aren't supported are passed in with the 5 // -c config=env flag 6 const supportedEnvironments = ["prod", "staging"] as const; 7 8 type SupportedEnvironments = typeof supportedEnvironments[number]; 9 10 // This maps to the values you specified in your cdk.json file 11 // if you add any values to your cdk.json file, also add them here! 12 export type CanaryConfig = { 13 readonly env: SupportedEnvironments; 14 readonly envTitle: string; 15 readonly bacalhauEnvironment: string; 16 readonly account: string; 17 readonly region: string; 18 readonly createOperators: boolean; 19 readonly dashboardPublicUrl: string; 20 }; 21 22 export type PipelineConfig = { 23 readonly env: SupportedEnvironments; 24 readonly suffix: string; 25 readonly account: string; 26 readonly region: string; 27 readonly bacalhauSourceConnection: SourceConnectionProps 28 }; 29 30 export type SourceConnectionProps = { 31 readonly owner: string; 32 readonly repo: string; 33 readonly branch: string; 34 readonly connectionArn: string; 35 } 36 37 // This function is used by your CDK app and pulls your config values 38 // from the context 39 export const getCanaryConfig = (app: cdk.App, forceEnv?: any): CanaryConfig => { 40 const node = app.node.tryGetContext("canary"); 41 if (!node) { 42 throw new Error( 43 "`canary` is missing in cdk context" 44 ); 45 } 46 47 const env = forceEnv || app.node.tryGetContext("config"); 48 if (!env) { 49 throw new Error( 50 "Context variable defining the environment must be passed to cdk: `cdk -c config=XXX`" 51 ); 52 } 53 if (!supportedEnvironments.includes(env)) { 54 throw new Error( 55 `${env} is not in supported environments: ${supportedEnvironments.join( 56 ", " 57 )}` 58 ); 59 } 60 // this contains the values in the context without being 61 // validated 62 const unparsedEnv = node[env]; 63 const envTitle = env.charAt(0).toUpperCase() + env.slice(1); 64 65 return { 66 env: env, 67 envTitle: envTitle, 68 bacalhauEnvironment: ensureString(unparsedEnv, "bacalhauEnvironment"), 69 account: ensureString(unparsedEnv, "account"), 70 region: ensureString(unparsedEnv, "region"), 71 createOperators: ensureBool(unparsedEnv, "createOperators"), 72 dashboardPublicUrl: ensureString(unparsedEnv, "dashboardPublicUrl"), 73 }; 74 }; 75 76 // This function is used by your CDK app and pulls your config values 77 // from the context 78 export const getPipelineConfig = (app: cdk.App, forceEnv?: any): PipelineConfig => { 79 const node = app.node.tryGetContext("pipeline"); 80 if (!node) { 81 throw new Error( 82 "`pipeline` is missing in cdk context" 83 ); 84 } 85 const env = forceEnv || app.node.tryGetContext("config") || "prod"; 86 87 if (!supportedEnvironments.includes(env)) { 88 throw new Error( 89 `${env} is not in supported environments: ${supportedEnvironments.join( 90 ", " 91 )}` 92 ); 93 } 94 // this contains the values in the context without being 95 // validated 96 const unparsedEnv = node[env]; 97 98 return { 99 env: env, 100 suffix: unparsedEnv["suffix"], // suffix can be blank 101 account: ensureString(unparsedEnv, "account"), 102 region: ensureString(unparsedEnv, "region"), 103 bacalhauSourceConnection: { 104 owner: ensureString(unparsedEnv['bacalhauSourceConnection'], "owner"), 105 repo: ensureString(unparsedEnv['bacalhauSourceConnection'], "repo"), 106 branch: ensureString(unparsedEnv['bacalhauSourceConnection'], "branch"), 107 connectionArn: ensureString(unparsedEnv['bacalhauSourceConnection'], "connectionArn"), 108 } 109 }; 110 }; 111 112 // this function ensures that the value from the config is 113 // the correct type. If you have any types other than 114 // strings be sure to create a new validation function 115 function ensureString(object: { [name: string]: any }, 116 key: keyof CanaryConfig | keyof PipelineConfig | keyof SourceConnectionProps): string { 117 if (!(key in object) || 118 typeof object[key] !== "string" || 119 object[key].trim().length === 0) { 120 throw new Error(key + " does not exist in config: " + JSON.stringify(object)); 121 } 122 return object[key]; 123 } 124 125 function ensureBool(object: { [name: string]: any }, 126 key: keyof CanaryConfig | keyof PipelineConfig | keyof SourceConnectionProps): boolean { 127 if (!(key in object) || typeof object[key] !== "boolean") { 128 throw new Error(key + " does not exist in config: " + JSON.stringify(object)); 129 } 130 return object[key]; 131 }