Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/debug documentation update errors #266

Merged
merged 7 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 47 additions & 46 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/api/terragrunt/TerragruntCliFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export class TerragruntCliFacade {
});
}

async moduleGroups(cwd: string): Promise<Record<string, string[]>> {
const cmds = ["terragrunt", "output-module-groups"];

const result = await this.quietRunner.run(cmds, {
cwd,
});

return JSON.parse(result.stdout);
}

async collectOutput(cwd: string, outputName: string) {
const cmds = [
"terragrunt",
Expand All @@ -112,4 +122,19 @@ export class TerragruntCliFacade {
cwd,
});
}

async collectOutputs(cwd: string, outputName: string) {
const cmds = [
"terragrunt",
"run-all",
"output",
"-raw",
outputName,
"--terragrunt-non-interactive", // disable terragrunt's own prompts, e.g. at the start of a terragrunt run-all run
];

return await this.quietRunner.run(cmds, {
cwd,
});
}
}
6 changes: 4 additions & 2 deletions src/cli/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ export class Logger {
console.error(colors.red(message));
}

public tip(msg: string) {
printTip(msg);
public tip(msg: string | ((fmt: FormatUtils) => string)) {
const message = typeof msg === "string" ? msg : msg(this.fmtUtils);

printTip(message);
}

public tipCommand(msg: string, command: string) {
Expand Down
150 changes: 98 additions & 52 deletions src/docs/PlatformDocumentationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { PlatformConfig } from "../model/PlatformConfig.ts";
import { DocumentationRepository } from "./DocumentationRepository.ts";
import { MarkdownUtils } from "../model/MarkdownUtils.ts";
import { ComplianceControlRepository } from "../compliance/ComplianceControlRepository.ts";
import {
RunAllPlatformModuleOutputCollector,
RunIndividualPlatformModuleOutputCollector,
} from "./PlatformModuleOutputCollector.ts";

export class PlatformDocumentationGenerator {
constructor(
Expand Down Expand Up @@ -67,30 +71,89 @@ export class PlatformDocumentationGenerator {
dependencies: PlatformDependencies,
docsRepo: DocumentationRepository,
) {
const platformPath = this.foundation.resolvePlatformPath(
dependencies.platform,
);
const platformProgress = new ProgressReporter(
"generate documentation",
this.repo.relativePath(
this.foundation.resolvePlatformPath(dependencies.platform),
),
this.repo.relativePath(platformPath),
this.logger,
);

const tasks = dependencies.modules.map(
async (x) =>
await this.generatePlatformModuleDocumentation(
x,
docsRepo,
dependencies.platform,
),
);
const platformModuleDocumentation = await this
.buildPlatformModuleOutputCollector(dependencies.platform);

await Promise.all(tasks);
// as a fallback process modules serially, unfortunately this is the only "safe" way to collect output
// see https://github.com/meshcloud/collie-cli/issues/265
for (const dep of dependencies.modules) {
const documentationMd = await platformModuleDocumentation.getOutput(dep);

await this.generatePlatformModuleDocumentation(
dep,
documentationMd,
docsRepo,
dependencies.platform,
);
}

platformProgress.done();
}

private async buildPlatformModuleOutputCollector(platform: PlatformConfig) {
const platformHclPath = this.foundation.resolvePlatformPath(
platform,
"platform.hcl",
);

const platformHcl = await Deno.readTextFile(platformHclPath);

const fastModeIdentifier =
"--- BEGIN COLLIE PLATFORM MODULE OUTPUT: ${path_relative_to_include()} ---";

this.logger.verbose(
(fmt) =>
`detecting if fast output collection is supported in ${
fmt.kitPath(
platformHclPath,
)
} by looking for a before_hook emitting "${fastModeIdentifier}"`,
);
const enableFastMode = platformHcl.includes(fastModeIdentifier);
this.logger.verbose(
(_) => "fast output collection is supported: " + enableFastMode,
);

if (enableFastMode) {
const platformPath = this.foundation.resolvePlatformPath(platform);
const collector = new RunAllPlatformModuleOutputCollector(
this.terragrunt,
this.logger,
);

await collector.initialize(platformPath);

return collector;
} else {
this.logger.tip(
(f) =>
`Enable fast output collection for collie in ${
f.kitPath(
platformHclPath,
)
}`,
);

return new RunIndividualPlatformModuleOutputCollector(
this.repo,
this.terragrunt,
this.logger,
);
}
}

private async generatePlatformModuleDocumentation(
dep: KitModuleDependency,
documentationMd: string,
docsRepo: DocumentationRepository,
platform: PlatformConfig,
) {
Expand All @@ -99,51 +162,34 @@ export class PlatformDocumentationGenerator {
dep.kitModuleId,
);

const result = await this.terragrunt.collectOutput(
this.repo.resolvePath(path.dirname(dep.sourcePath)),
"documentation_md",
);
await fs.ensureDir(path.dirname(destPath)); // todo: should we do nesting in the docs output or "flatten" module prefixes?

if (!result.status.success) {
this.logger.warn(
(fmt) =>
`Failed to collect output "documentation_md" from platform module${
fmt.kitPath(
dep.sourcePath,
)
}`,
);
this.logger.warn(result.stderr);
} else {
await fs.ensureDir(path.dirname(destPath)); // todo: should we do nesting in the docs output or "flatten" module prefixes?
const mdSections = [documentationMd];

const mdSections = [result.stdout];

const complianceStatementsBlock = this.generateComplianceStatementSection(
dep,
docsRepo,
destPath,
);
mdSections.push(complianceStatementsBlock);
const complianceStatementsBlock = this.generateComplianceStatementSection(
dep,
docsRepo,
destPath,
);
mdSections.push(complianceStatementsBlock);

const kitModuleSection = this.generateKitModuleSection(
dep,
docsRepo,
destPath,
);
mdSections.push(kitModuleSection);
const kitModuleSection = this.generateKitModuleSection(
dep,
docsRepo,
destPath,
);
mdSections.push(kitModuleSection);

await Deno.writeTextFile(destPath, mdSections.join("\n\n"));
await Deno.writeTextFile(destPath, mdSections.join("\n\n"));

this.logger.verbose(
(fmt) =>
`Wrote output "documentation_md" from platform module ${
fmt.kitPath(
dep.sourcePath,
)
} to ${fmt.kitPath(destPath)}`,
);
}
this.logger.verbose(
(fmt) =>
`Wrote output "documentation_md" from platform module ${
fmt.kitPath(
dep.sourcePath,
)
} to ${fmt.kitPath(destPath)}`,
);
}

private generateKitModuleSection(
Expand Down
21 changes: 21 additions & 0 deletions src/docs/PlatformModuleOutputCollector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assertEquals } from "std/testing/assert";
import { RunAllPlatformModuleOutputCollector } from "./PlatformModuleOutputCollector.ts";

const stdout = `
--- BEGIN COLLIE PLATFORM MODULE OUTPUT: logging ---
foo
123
--- BEGIN COLLIE PLATFORM MODULE OUTPUT: billing ---
bar
xyz`;

Deno.test("can split", () => {
const result = RunAllPlatformModuleOutputCollector.parseTerragrunt(stdout);

const expected = [
{ module: "logging", output: "foo\n123" },
{ module: "billing", output: "bar\nxyz" },
];

assertEquals(result, expected);
});
Loading