forked from codemonger-io/mumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-domain-name.ts
97 lines (92 loc) · 3 KB
/
setup-domain-name.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
import {
CloudFormationClient,
DescribeStacksCommand,
} from '@aws-sdk/client-cloudformation';
import { SSMClient, PutParameterCommand } from '@aws-sdk/client-ssm';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import domainNameConfig from '../configs/domain-name-config';
import {
DEPLOYMENT_STAGES,
DeploymentStage,
isDeploymentStage,
} from '../lib/deployment-stage';
yargs(hideBin(process.argv))
.command(
'$0 <stage> [domain]',
'set domain name parameter',
yargs => {
return yargs
.positional('stage', {
describe: 'deployment stage to be configured',
type: 'string',
choices: DEPLOYMENT_STAGES,
})
.positional('domain', {
describe: 'optional domain name to be assigned. if omitted, taken from the CloudFormation output (development), or taken from `configs/domain-name-config.ts` (production)',
type: 'string',
});
},
async ({ stage, domain }) => {
if (!isDeploymentStage(stage)) {
throw new RangeError('invalid deployment stage: ' + stage);
}
console.log('configuring stage:', stage);
if (stage === 'production') {
domain = domain ?? domainNameConfig.domainName;
}
await setupDomainName(stage, domain);
console.log('done');
},
)
.argv;
async function setupDomainName(
stage: DeploymentStage,
explicitDomainName?: string,
) {
const outputs = await getCloudFormationOutputs(stage);
const parameterPath = outputs['DomainNameParameterPath'];
if (parameterPath == null) {
throw new Error('no domain name parameter path is outputted');
}
console.log('domain name parameter path:', parameterPath);
const domainName =
explicitDomainName || outputs['MumbleApiDistributionDomainName'];
if (domainName == null) {
throw new Error('no distribution domain name is outputted');
}
console.log('distribution domain name:', domainName);
await putDomainNameParameter(parameterPath, domainName);
}
// obtains CloudFormation outputs as a key-value mapping object.
async function getCloudFormationOutputs(
stage: DeploymentStage,
): Promise<{ [key: string]: string }> {
const client = new CloudFormationClient({});
const results = await client.send(new DescribeStacksCommand({
StackName: `mumble-${stage}`,
}));
const stacks = results.Stacks ?? [];
const stack = stacks[0] ?? {};
const outputs = stack.Outputs ?? [];
const outputsMap: { [key: string]: string } = {};
for (const { OutputKey: key, OutputValue: value } of outputs) {
if (key != null && value != null) {
outputsMap[key] = value;
}
}
return outputsMap;
}
async function putDomainNameParameter(
parameterPath: string,
domainName: string,
) {
const client = new SSMClient({});
const results = await client.send(new PutParameterCommand({
Name: parameterPath,
Description: 'Domain name of the Mumble endpoints',
Value: domainName,
Type: 'String', // it should not be a secret, right?
Overwrite: true,
}));
}