-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental support for deploying foundation level modules
these have become a useful pattern to manage things that are consistent across your entire foundation like managing a common terraform state across multiple platforms, a central CI/CD pipeline for your IAC or generating docs (v2 coming soon...)
- Loading branch information
1 parent
f897106
commit 8e43ec5
Showing
6 changed files
with
139 additions
and
2 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
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,109 @@ | ||
import * as path from "std/path"; | ||
|
||
import { | ||
TerragruntArguments, | ||
TerragruntCliFacade, | ||
toVerb, | ||
} from "/api/terragrunt/TerragruntCliFacade.ts"; | ||
import { FoundationRepository } from "/model/FoundationRepository.ts"; | ||
import { Logger } from "../cli/Logger.ts"; | ||
import { ProgressReporter } from "/cli/ProgressReporter.ts"; | ||
import { | ||
CollieRepository, | ||
PLATFORM_MODULE_GLOB, | ||
} from "/model/CollieRepository.ts"; | ||
|
||
export class FoundationDeployer { | ||
constructor( | ||
private readonly repo: CollieRepository, | ||
protected readonly foundation: FoundationRepository, | ||
private readonly terragrunt: TerragruntCliFacade, | ||
private readonly logger: Logger, | ||
) {} | ||
|
||
async deployFoundationModules( | ||
mode: TerragruntArguments, | ||
module: string | undefined, | ||
autoApprove: boolean, | ||
) { | ||
const foundationOrModulePath = this.foundation.resolvePath( | ||
module || "", | ||
); | ||
|
||
const relativePlatformOrModulePath = this.repo.relativePath( | ||
foundationOrModulePath, | ||
); | ||
|
||
const progress = this.buildProgressReporter( | ||
mode, | ||
relativePlatformOrModulePath, | ||
); | ||
|
||
const tgfiles = await this.foundationModuleTerragruntFiles( | ||
relativePlatformOrModulePath, | ||
); | ||
if (tgfiles.length === 0) { | ||
this.logger.warn( | ||
(fmt) => | ||
`detected no foundation modules at ${ | ||
fmt.kitPath(foundationOrModulePath) | ||
}, will skip invoking "terragrunt <cmd>"`, | ||
); | ||
|
||
this.logger.tipCommand( | ||
"Apply a kit module to this foundation to create a foundation module using", | ||
"kit apply", | ||
); | ||
} else if (tgfiles.length === 1) { | ||
// we can't run terragrunt in the platform dir, so we have to infer the platformModule path from | ||
// the discovered terragrunt file | ||
const singleModulePath = path.dirname(tgfiles[0].path); | ||
|
||
this.logger.debug( | ||
(fmt) => | ||
`detected a single foundation module at ${ | ||
fmt.kitPath(singleModulePath) | ||
}, will deploy with "terragrunt <cmd>"`, | ||
); | ||
await this.terragrunt.run(singleModulePath, mode, { autoApprove }); | ||
} else { | ||
this.logger.debug( | ||
(fmt) => | ||
`detected a stack of foundation modules at ${ | ||
fmt.kitPath( | ||
foundationOrModulePath, | ||
) | ||
}, will deploy with "terragrunt run-all <cmd>"`, | ||
); | ||
|
||
await this.terragrunt.runAll(foundationOrModulePath, mode, { | ||
excludeDirs: [PLATFORM_MODULE_GLOB], // if we let terragrunt run a run-all, need to explicitly exclude all platform modules | ||
autoApprove, | ||
}); | ||
} | ||
|
||
progress.done(); | ||
} | ||
|
||
private async foundationModuleTerragruntFiles(relativeModulePath: string) { | ||
const excludes = { | ||
testModules: true, | ||
tenantModules: true, | ||
platformModules: true, | ||
}; | ||
|
||
const files = await this.repo.processFilesGlob( | ||
// todo: exclude platforms/folder | ||
`${relativeModulePath}/**/terragrunt.hcl`, | ||
(file) => file, | ||
excludes, | ||
); | ||
|
||
// a terragrunt stack conists of multiple executable terragrunt files | ||
return files; | ||
} | ||
|
||
private buildProgressReporter(mode: TerragruntArguments, id: string) { | ||
return new ProgressReporter(toVerb(mode), id, this.logger); | ||
} | ||
} |
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
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