-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable rendering MTPs to disk, from disk
- `bin/renderlocal` can take an `--mtp`, and any number of `--env`s (greater than 0) and render resources, writing them to `--out` - create a `LocalMustacheTemplateController` that resembles the mocked one used in tests - update tests to use `LocalMustacheTemplateController` - depends on proposed changes to `razeedeploy-core`
- Loading branch information
1 parent
1f648bf
commit 9c47c9e
Showing
5 changed files
with
144 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env node | ||
|
||
const nopt = require('nopt'); | ||
const path = require('path'); | ||
|
||
const { FetchEnvs, MockController } = require('@razee/razeedeploy-core'); | ||
const IOUtils = require('../src/IOUtils'); | ||
const LocalMustacheTemplateController = require('../src/LocalMustacheTemplateController'); | ||
|
||
const args = nopt( | ||
{ | ||
mtp: path, | ||
out: path, | ||
env: [path, Array] | ||
}, | ||
{}, process.argv, 2 | ||
); | ||
|
||
if (args.mtp === undefined) { | ||
console.error("exactly one --mtp is required!"); | ||
process.exit(1); | ||
} | ||
|
||
if (args.env === undefined || args.env.length < 1) { | ||
console.error("at least one --env is required!"); | ||
process.exit(1); | ||
} | ||
|
||
async function renderLocal() { | ||
const mtp = await IOUtils.readYamlFile(args.mtp); | ||
const kubeData = await IOUtils.kubeDataFromYamlFiles(...args.env); | ||
|
||
const eventData = { | ||
type: 'ADDED', | ||
object: mtp[0] | ||
} | ||
|
||
const fetchEnvs = new FetchEnvs(new MockController(eventData, kubeData)); | ||
const view = await fetchEnvs.get('spec'); | ||
|
||
const mtpController = new LocalMustacheTemplateController({eventData: eventData, kubeData: kubeData}); | ||
let templates = mtpController.concatTemplates(); | ||
await mtpController.processTemplate(templates, view); | ||
|
||
if (templates.length > 1) { | ||
throw Error("can only handle one template!"); | ||
} | ||
|
||
if (args.out === undefined) { | ||
await IOUtils.printYaml(templates[0]); | ||
} else { | ||
await IOUtils.writeYamlFile(templates[0], args.out) | ||
} | ||
}; | ||
|
||
renderLocal(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const fs = require('fs'); | ||
const yaml = require('js-yaml'); | ||
|
||
async function kubeDataFromYamlFiles(...filePaths) { | ||
let kubeData = {}; | ||
for (const filePath of filePaths) { | ||
let data = await readYamlFile(filePath); | ||
for (const element of data) { | ||
const kind = element.kind; | ||
if (!(kind in kubeData)) { | ||
kubeData[kind] = []; | ||
} | ||
kubeData[kind].push(element); | ||
} | ||
} | ||
return kubeData; | ||
} | ||
|
||
async function objToYaml(asObj) { | ||
return await yaml.dump(asObj); | ||
} | ||
|
||
async function printYaml(asObj) { | ||
process.stdout.write(await objToYaml(asObj)); | ||
} | ||
|
||
async function readFile(filePath) { | ||
return await fs.promises.readFile(filePath); | ||
} | ||
|
||
async function readYamlFile(filePath) { | ||
return await yamlToObj(await readFile(filePath)); | ||
} | ||
|
||
async function writeFile(contents, filePath) { | ||
fs.writeFile(filePath, contents, (err) => { console.error(err); }); | ||
} | ||
|
||
async function writeYamlFile(asObj, filePath) { | ||
await writeFile(await objToYaml(asObj), filePath); | ||
} | ||
|
||
async function yamlToObj(asYaml) { | ||
return await yaml.loadAll(asYaml); | ||
} | ||
|
||
module.exports = { | ||
kubeDataFromYamlFiles, | ||
printYaml, | ||
readYamlFile, | ||
writeYamlFile | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { MockKubeResourceMeta } = require('@razee/razeedeploy-core'); | ||
const MustacheTemplateController = require('../src/MustacheTemplateController'); | ||
|
||
module.exports = class LocalMustacheTemplateController extends MustacheTemplateController { | ||
constructor(params) { | ||
let kubeData = {}; | ||
if (params.kubeData !== undefined) { | ||
kubeData = params.kubeData; | ||
delete params.kubeData; | ||
} | ||
params.kubeResourceMeta = new MockKubeResourceMeta( | ||
'deploy.razee.io/v1alpha2', 'MustacheTemplate', kubeData | ||
); | ||
params.logger = { | ||
info: () => {} | ||
}; | ||
super(params); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters