forked from guardian/grid
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-config.js
executable file
·92 lines (73 loc) · 2.68 KB
/
generate-config.js
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
const os = require('os');
require('json5/lib/register');
const homeDir = os.homedir();
const defaultConfig = require('./config.json5');
const dotenvConfig = require('dotenv').config({path: path.join(__dirname, '../../.env')}).parsed;
const ServiceConfig = require('./service-config');
const LOCALSTACK_ENDPOINT = 'http://localhost:4566';
AWS.config.update({
credentials: new AWS.SharedIniFileCredentials({
profile: dotenvConfig.AWS_PROFILE
}),
region: dotenvConfig.AWS_DEFAULT_REGION,
endpoint: new AWS.Endpoint(LOCALSTACK_ENDPOINT)
});
const cloudformation = new AWS.CloudFormation();
function getCloudformationStackResources(stackName) {
return cloudformation.describeStackResources({StackName: stackName}).promise();
}
async function doesStackExist(stackName) {
const stacks = await cloudformation.listStacks({}).promise()
const exists = stacks.StackSummaries.find(stack => stack.StackName === stackName);
return !!exists;
}
function writeToDisk({path, content}) {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, 'UTF8', (err) => {
if (err) {
reject(err);
} else {
resolve(content);
}
})
});
}
(async () => {
const coreStackCfnData = await getCloudformationStackResources(dotenvConfig.CORE_STACK_NAME);
const coreStackProps = coreStackCfnData.StackResources.reduce((acc, item) => {
return Object.assign({}, acc, {
[item.LogicalResourceId]: item.PhysicalResourceId
});
}, {});
const coreConfig = {...defaultConfig, ...dotenvConfig, coreStackProps}
const coreServiceConfigs = ServiceConfig.getCoreConfigs(coreConfig);
await Promise.all(
Object.keys(coreServiceConfigs).map(filename => writeToDisk({
path: `${homeDir}/.grid/${filename}.conf`,
content: coreServiceConfigs[filename]
})
));
const authStackExists = await doesStackExist(dotenvConfig.AUTH_STACK_NAME)
const localAuthFilePath = `${homeDir}/.grid/common.conf`;
if (authStackExists) {
const authStackCfnData = await getCloudformationStackResources(dotenvConfig.AUTH_STACK_NAME);
const authStackProps = authStackCfnData.StackResources.reduce((acc, item) => {
return Object.assign({}, acc, {
[item.LogicalResourceId]: item.PhysicalResourceId
});
}, {});
const localAuthConfig = {...defaultConfig, ...dotenvConfig, authStackProps};
await writeToDisk({
path: localAuthFilePath,
content: ServiceConfig.getUseLocalAuthConfig(localAuthConfig)
})
} else {
if(fs.existsSync(localAuthFilePath)) {
fs.unlinkSync(localAuthFilePath)
}
}
})();