Skip to content

Commit

Permalink
fix(cli): run terraform init in serial so no text file is busy in the…
Browse files Browse the repository at this point in the history
… cache

Closes #2741
  • Loading branch information
DanielMSchmidt committed Apr 27, 2023
1 parent 5fce991 commit 45838df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/@cdktf/cli-core/src/lib/cdktf-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ export class CdktfProject {
const stack = this.getStackExecutor(
getSingleStack(stacks, opts?.stackName, "diff")
);
await stack.initalizeTerraform(opts.noColor);

try {
await stack.diff(opts);
Expand Down Expand Up @@ -389,6 +390,7 @@ export class CdktfProject {
!opts.parallelism || opts.parallelism < 0 ? Infinity : opts.parallelism;
const allExecutions = [];

await this.initializeStacksToRunInSerial();
while (this.stacksToRun.filter((stack) => stack.isPending).length > 0) {
const runningStacks = this.stacksToRun.filter((stack) => stack.isRunning);
if (runningStacks.length >= maxParallelRuns) {
Expand Down Expand Up @@ -545,6 +547,7 @@ export class CdktfProject {
this.getStackExecutor(stack, {})
);

await this.initializeStacksToRunInSerial();
const outputs = await Promise.all(
this.stacksToRun.map(async (s) => {
const output = await s.fetchOutputs();
Expand All @@ -559,4 +562,11 @@ export class CdktfProject {
{}
) as NestedTerraformOutputs;
}

// Serially run terraform init to prohibit text file busy errors for the cache files
private async initializeStacksToRunInSerial(): Promise<void> {
for (const stack of this.stacksToRun) {
await stack.initalizeTerraform();
}
}
}
15 changes: 9 additions & 6 deletions packages/@cdktf/cli-core/src/lib/cdktf-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,16 @@ export class CdktfStack {
};
}

private async initalizeTerraform(noColor?: boolean) {
const terraform = await getTerraformClient(
private async terraformClient() {
return await getTerraformClient(
this.options.abortSignal,
this.options.stack,
this.createTerraformLogHandler.bind(this)
);
}

public async initalizeTerraform(noColor?: boolean) {
const terraform = await this.terraformClient();
const needsInit = await this.checkNeedsInit();
if (!needsInit) {
// Skip terraform init as everything's up to date
Expand Down Expand Up @@ -353,7 +356,7 @@ export class CdktfStack {
}) {
await this.run(async () => {
this.updateState({ type: "planning", stackName: this.stack.name });
const terraform = await this.initalizeTerraform(noColor);
const terraform = await this.terraformClient();

await terraform.plan({
destroy: false,
Expand All @@ -377,7 +380,7 @@ export class CdktfStack {
const { refreshOnly, terraformParallelism, noColor, vars, varFiles } = opts;
await this.run(async () => {
this.updateState({ type: "planning", stackName: this.stack.name });
const terraform = await this.initalizeTerraform(noColor);
const terraform = await this.terraformClient();

const { cancelled } = await terraform.deploy(
{
Expand Down Expand Up @@ -463,7 +466,7 @@ export class CdktfStack {
const { terraformParallelism, noColor, vars, varFiles } = opts;
await this.run(async () => {
this.updateState({ type: "planning", stackName: this.stack.name });
const terraform = await this.initalizeTerraform(noColor);
const terraform = await this.terraformClient();
const { cancelled } = await terraform.destroy(
{
autoApprove: this.options.autoApprove,
Expand Down Expand Up @@ -531,7 +534,7 @@ export class CdktfStack {

public async fetchOutputs() {
await this.run(async () => {
const terraform = await this.initalizeTerraform();
const terraform = await this.terraformClient();

const outputs = await terraform.output();
const outputsByConstructId = getConstructIdsForOutputs(
Expand Down

0 comments on commit 45838df

Please sign in to comment.