This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.ts
243 lines (206 loc) · 7.19 KB
/
generate.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright 2019 Balena Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import { fs } from 'mz';
import * as path from 'path';
import * as flags from '../lib/flags';
import * as frameGenerator from '../lib/controllers/frame/frame-generator';
import * as templateGenerator from '../lib/controllers/template-generator/template-generator';
import * as frameTemplate from '../lib/controllers/frame-template';
import { Command } from '@oclif/command';
import { ConfigManifest } from '../lib/controllers/config-manifest/config-manifest';
import { createConfigStore } from '../lib/controllers/config-store/config-store';
import { ConfigurationManager } from '../lib/controllers/configuration-manager/configuration-manager';
import {
loadEnvironment,
EnvironmentContext,
} from '../lib/controllers/environment/environment';
import * as frameAdapter from '../lib/controllers/frame/adapter/filesystem';
import * as templateAdapter from '../lib/controllers/frame-template/adapter/filesystem';
import * as templateRenderer from '../lib/controllers/frame-template/renderer/mustache';
import * as generatorRenderer from '../lib/controllers/template-generator/renderer/mustache';
import { loadKeyframe } from '../lib/controllers/keyframe/index';
import { ConfigStoreError } from '../lib/error-types';
import { Frame } from '../lib/controllers/frame/frame';
import { ConfigStoreAccess } from '../lib/controllers/config-store';
/**
* Generate Command class
* The init command is used for generating deployment artifacts, using an environment configuration.
* If an environment configuration doesn't exist, or is out of sync, it's generated/synced.
*/
export const generateFrame = async (
environmentPath: string,
target: string,
keyframePath?: string,
): Promise<Frame> => {
// get our directory context correct
const getEnvironmentDirectory = async (p: string) => {
if ((await fs.stat(p)).isDirectory()) {
return p;
}
return path.dirname(p);
};
const environmentDir = await getEnvironmentDirectory(environmentPath);
const productDir = path.join(environmentDir, 'product');
const resolvedKeyframePath =
keyframePath || path.join(productDir, 'keyframe.yml');
// find the manifests to use...
const manifestFiles = await Bluebird.filter(
[
'config-manifest.yml', // product-specific manifest
`deploy/${target}/config-manifest.yml`, // target-specific manifest
],
async p => await fs.exists(path.join(productDir, p)),
);
// create a merged manifest...
const configManifest = await ConfigManifest.create(productDir, manifestFiles);
// create a context for the environment...
const context = await loadEnvironment(environmentDir);
// create an Environment ConfigStore instance
const configStore = await getConfigStore(context);
// sync the environment configmap
const configManager = await ConfigurationManager.create({
mode: 'quiet',
configManifest,
configStore,
});
// ensure the config map is synced
await configManager.sync();
// create the frame
const frameGeneratorDir = path.normalize(
path.join(productDir, `deploy/${target}/generators`),
);
// create the frame templates
const frameTemplateDir = path.normalize(
path.join(productDir, `deploy/${target}/templates`),
);
// read the keyframe
const keyframe = await loadKeyframe(resolvedKeyframePath);
// read the generators
const frameGenerators = await frameTemplate.fromDirectory(frameGeneratorDir);
// generate the frame templates...
const generatedFrameTemplates = await templateGenerator.generate(
frameGenerators,
generatorRenderer.Renderer,
keyframe,
);
// export the templates to the filesystem...
await templateAdapter
.filesystemExportAdapter(frameTemplateDir)
.export(generatedFrameTemplates);
// read the templates
const frameTemplates = await frameTemplate.fromDirectory(frameTemplateDir);
return await frameGenerator.generate(
frameTemplates,
templateRenderer.Renderer,
configStore,
);
};
const getConfigStore = async (ctx: EnvironmentContext) => {
const configStoreDefinition = ctx.environment['config-store'];
if (configStoreDefinition.envfile !== undefined) {
return await createConfigStore({
envFile: {
path: path.normalize(
path.join(ctx.directory, configStoreDefinition.envfile.path),
),
},
});
}
if (configStoreDefinition.kubernetes !== undefined) {
const defaults: ConfigStoreAccess = {
kubernetes: {
kubeconfig: '',
namespace: '',
},
};
const fromEnv = {
kubernetes: {
kubeconfig: process.env.KATAPULT_KUBE_CONFIG,
namespace: process.env.KATAPULT_KUBE_NAMESPACE,
},
};
const config = configStoreDefinition.kubernetes;
const fromConfig =
config !== null
? {
kubernetes: {
kubeconfig: config.kubeconfig,
namespace: config.namespace,
},
}
: {};
const kubernetesAccess = _.merge(defaults, fromConfig, fromEnv, {
kubernetes: { bastion: undefined },
});
if (
Object.keys(process.env).filter(k =>
k.startsWith('KATAPULT_KUBE_BASTION_'),
).length > 0
) {
kubernetesAccess.kubernetes.bastion = _.merge(
kubernetesAccess.kubernetes.bastion,
{
apiHost: process.env.KATAPULT_KUBE_BASTION_API_HOST,
apiPort: process.env.KATAPULT_KUBE_BASTION_API_PORT,
host: process.env.KATAPULT_KUBE_BASTION_HOST,
port: process.env.KATAPULT_KUBE_BASTION_PORT,
username: process.env.KATAPULT_KUBE_BASTION_USER,
key: process.env.KATAPULT_KUBE_BASTION_KEY,
keyPassphrase: process.env.KUBE_BASTION_KEY_PASSPHRASE,
},
);
}
if (config != null && config.bastion != null) {
kubernetesAccess.kubernetes.bastion = _.merge(
kubernetesAccess.kubernetes.bastion,
{
apiHost: config.bastion.apiHost,
apiPort: config.bastion.apiPort,
host: config.bastion.host,
port: config.bastion.port,
username: config.bastion.user,
key: config.bastion.key,
keyPassphrase: config.bastion.passphrase,
},
);
}
return await createConfigStore(kubernetesAccess as ConfigStoreAccess);
}
throw new ConfigStoreError(
`Unable to create a config store for the chosen provider '${Object.keys(
configStoreDefinition,
).join(',')}'`,
);
};
export default class Generate extends Command {
static description = 'Generate a Frame from an Environment';
static flags = {
environmentPath: flags.environmentPath,
outputPath: flags.outputPath,
target: flags.target,
keyframe: flags.keyframePath,
};
async run() {
// Parse command flags
const { flags } = this.parse(Generate);
const frame = await generateFrame(
flags.environmentPath,
flags.target,
flags.keyframe,
);
const outputTo = path.normalize(path.join(flags.outputPath, flags.target));
await frameAdapter.filesystemDeployAdapter(outputTo).deploy(frame);
}
}