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

feat(cli): allow skipping synth #2993

Merged
merged 4 commits into from
Jul 6, 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
55 changes: 42 additions & 13 deletions packages/@cdktf/cli-core/src/lib/cdktf-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,29 @@ export type MultipleStackOptions = {
stackNames?: string[];
};

export type SkipSynthOptions = {
skipSynth?: boolean;
};

export type FetchOutputOptions = SkipSynthOptions & MultipleStackOptions;

export type AutoApproveOptions = {
autoApprove?: boolean;
};

export type DiffOptions = SingleStackOptions & {
refreshOnly?: boolean;
terraformParallelism?: number;
vars?: string[];
varFiles?: string[];
noColor?: boolean;
migrateState?: boolean;
};
export type DiffOptions = SingleStackOptions &
SkipSynthOptions & {
refreshOnly?: boolean;
terraformParallelism?: number;
vars?: string[];
varFiles?: string[];
noColor?: boolean;
migrateState?: boolean;
skipSynth?: boolean;
};

export type MutationOptions = MultipleStackOptions &
SkipSynthOptions &
AutoApproveOptions & {
refreshOnly?: boolean;
ignoreMissingStackDependencies?: boolean;
Expand Down Expand Up @@ -362,8 +371,22 @@ export class CdktfProject {
return stacks;
}

public async readSynthesizedStacks() {
const stacks = await SynthStack.readSynthesizedStacks(this.outDir);

printAnnotations(stacks);

this.onUpdate({
type: "synthesized",
stacks,
});
return stacks;
}

public async diff(opts: DiffOptions = {}) {
const stacks = await this.synth();
const stacks = opts.skipSynth
? await this.readSynthesizedStacks()
: await this.synth();
const stack = this.getStackExecutor(
getSingleStack(stacks, opts?.stackName, "diff")
);
Expand Down Expand Up @@ -459,7 +482,9 @@ export class CdktfProject {
}

public async deploy(opts: MutationOptions = {}) {
const stacks = await this.synth();
const stacks = opts.skipSynth
? await this.readSynthesizedStacks()
: await this.synth();
const stacksToRun = getMultipleStacks(stacks, opts.stackNames, "deploy");
if (!opts.ignoreMissingStackDependencies) {
checkIfAllDependenciesAreIncluded(stacksToRun);
Expand Down Expand Up @@ -515,7 +540,9 @@ export class CdktfProject {
}

public async destroy(opts: MutationOptions = {}) {
const stacks = await this.synth();
const stacks = opts.skipSynth
? await this.readSynthesizedStacks()
: await this.synth();
const stacksToRun = getMultipleStacks(stacks, opts.stackNames, "destroy");

if (!opts.ignoreMissingStackDependencies) {
Expand Down Expand Up @@ -597,8 +624,10 @@ export class CdktfProject {
});
}

public async fetchOutputs(opts: MultipleStackOptions) {
const stacks = await this.synth();
public async fetchOutputs(opts: FetchOutputOptions) {
const stacks = opts.skipSynth
? await this.readSynthesizedStacks()
: await this.synth();

const stacksToRun = getMultipleStacks(
stacks,
Expand Down
56 changes: 36 additions & 20 deletions packages/@cdktf/cli-core/src/lib/synth-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,52 @@ Command output on stdout:
process.exit(1);
}

if (!(await fs.pathExists(path.join(outdir, Manifest.fileName)))) {
const errorMessage = `ERROR: synthesis failed, because app was expected to call 'synth()', but didn't. Thus "${outdir}/${Manifest.fileName}" was not created.`;
// end performance timer
const endTime = performance.now();

let stacks: SynthesizedStack[] = [];
try {
stacks = await SynthStack.readSynthesizedStacks(outdir);
} catch (e: any) {
const errorMessage = `ERROR: synthesis failed, because app was expected to call 'synth()', but didn't. Thus "${outdir}/${Manifest.fileName}" was not created: ${e}`;
if (graceful) {
throw new Error(errorMessage);
}
logger.error(errorMessage);
process.exit(1);
}

// end performance timer
const endTime = performance.now();
await this.synthTelemetry(endTime - startTime, stacks, synthOrigin);

if (stacks.length === 0) {
logger.error("ERROR: No Terraform code synthesized.");
}

const stackNames = stacks.map((s) => s.name);
const orphanedDirectories = existingDirectories.filter(
(e) => !stackNames.includes(path.basename(e))
);

for (const orphanedDirectory of orphanedDirectories) {
fs.rmSync(orphanedDirectory, { recursive: true });
}

return stacks;
}

public static async readSynthesizedStacks(
outdir: string
): Promise<SynthesizedStack[]> {
const manifestPath = path.join(outdir, Manifest.fileName);
if (!(await fs.pathExists(manifestPath))) {
throw new Error(
`Could not find manifest file at ${manifestPath}. In case --skip-synth was passed, please try again without the flag.`
);
}

const stacks: SynthesizedStack[] = [];
const manifest = JSON.parse(
fs.readFileSync(path.join(outdir, Manifest.fileName)).toString()
fs.readFileSync(manifestPath).toString()
) as ManifestJson;

for (const stackName in manifest.stacks) {
Expand All @@ -183,21 +214,6 @@ Command output on stdout:
});
}

await this.synthTelemetry(endTime - startTime, stacks, synthOrigin);

if (stacks.length === 0) {
logger.error("ERROR: No Terraform code synthesized.");
}

const stackNames = stacks.map((s) => s.name);
const orphanedDirectories = existingDirectories.filter(
(e) => !stackNames.includes(path.basename(e))
);

for (const orphanedDirectory of orphanedDirectories) {
fs.rmSync(orphanedDirectory, { recursive: true });
}

return stacks;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ class Command extends BaseCommand {
required: false,
desc: "Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.",
})
.option("skip-synth", {
type: "boolean",
default: false,
required: false,
desc: "Skip synthesis of the application, assume the synthesized Terraform code is already present and up to date",
})
.showHelpOnFail(true);

public async handleCommand(argv: any) {
Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ class Command extends BaseCommand {
required: false,
desc: "Pass this flag after switching state backends to approve a state migration for all targeted stacks",
})
.option("skip-synth", {
type: "boolean",
default: false,
required: false,
desc: "Skip synthesis of the application, assume the synthesized Terraform code is already present and up to date",
})
.showHelpOnFail(true);

public async handleCommand(argv: any) {
Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class Command extends BaseCommand {
required: false,
desc: "Pass this flag after switching state backends to approve a state migration for the targeted stack",
})
.option("skip-synth", {
type: "boolean",
default: false,
required: false,
desc: "Skip synthesis of the application, assume the synthesized Terraform code is already present and up to date",
})
.showHelpOnFail(true);

public async handleCommand(argv: any) {
Expand Down
8 changes: 8 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function deploy(argv: any) {
const varFiles = sanitizeVarFiles(argv.varFile);
const noColor = argv.noColor;
const migrateState = argv.migrateState;
const skipSynth = argv.skipSynth;

let outputsPath: string | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand All @@ -163,6 +164,7 @@ export async function deploy(argv: any) {
varFiles,
noColor,
migrateState,
skipSynth,
})
);
}
Expand All @@ -184,6 +186,7 @@ export async function destroy(argv: any) {
const varFiles = sanitizeVarFiles(argv.varFile);
const noColor = argv.noColor;
const migrateState = argv.migrateState;
const skipSynth = argv.skipSynth;

await renderInk(
React.createElement(Destroy, {
Expand All @@ -198,6 +201,7 @@ export async function destroy(argv: any) {
varFiles,
noColor,
migrateState,
skipSynth,
})
);
}
Expand All @@ -216,6 +220,7 @@ export async function diff(argv: any) {
const varFiles = sanitizeVarFiles(argv.varFile);
const noColor = argv.noColor;
const migrateState = argv.migrateState;
const skipSynth = argv.skipSynth;

await renderInk(
React.createElement(Diff, {
Expand All @@ -228,6 +233,7 @@ export async function diff(argv: any) {
varFiles,
noColor,
migrateState,
skipSynth,
})
);
}
Expand Down Expand Up @@ -437,6 +443,7 @@ export async function output(argv: any) {
const outDir = argv.output;
const stacks = argv.stacks;
const includeSensitiveOutputs = argv.outputsFileIncludeSensitiveOutputs;
const skipSynth = argv.skipSynth;
let outputsPath: string | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
let onOutputsRetrieved: (outputs: NestedTerraformOutputs) => void = () => {};
Expand All @@ -454,6 +461,7 @@ export async function output(argv: any) {
synthCommand: command,
onOutputsRetrieved,
outputsPath,
skipSynth,
})
);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Command extends BaseCommand {
desc: "Whether to include sensitive outputs in the output file",
default: false,
})
.option("skip-synth", {
type: "boolean",
default: false,
required: false,
desc: "Skip synthesis of the application, assume the synthesized Terraform code is already present and up to date",
})
.showHelpOnFail(true);

public async handleCommand(argv: any) {
Expand Down
3 changes: 3 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/ui/deploy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ interface DeployConfig {
varFiles?: string[];
noColor?: boolean;
migrateState?: boolean;
skipSynth?: boolean;
}

export const Deploy = ({
Expand All @@ -83,6 +84,7 @@ export const Deploy = ({
varFiles,
noColor,
migrateState,
skipSynth,
}: DeployConfig): React.ReactElement => {
const [outputs, setOutputs] = useState<NestedTerraformOutputs>();
const { status, logEntries } = useCdktfProject(
Expand All @@ -99,6 +101,7 @@ export const Deploy = ({
varFiles,
noColor,
migrateState,
skipSynth,
});

if (onOutputsRetrieved) {
Expand Down
3 changes: 3 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/ui/destroy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface DestroyConfig {
migrateState?: boolean;
vars?: string[];
varFiles?: string[];
skipSynth?: boolean;
}

export const Destroy = ({
Expand All @@ -40,6 +41,7 @@ export const Destroy = ({
migrateState,
vars,
varFiles,
skipSynth,
}: DestroyConfig): React.ReactElement => {
const { status, logEntries } = useCdktfProject(
{ outDir, synthCommand },
Expand All @@ -54,6 +56,7 @@ export const Destroy = ({
migrateState,
vars,
varFiles,
skipSynth,
})
);

Expand Down
3 changes: 3 additions & 0 deletions packages/cdktf-cli/src/bin/cmds/ui/diff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface DiffConfig {
varFiles?: string[];
noColor?: boolean;
migrateState?: boolean;
skipSynth?: boolean;
}

export const Diff = ({
Expand All @@ -30,6 +31,7 @@ export const Diff = ({
varFiles,
noColor,
migrateState,
skipSynth,
}: DiffConfig): React.ReactElement => {
const { status, logEntries } = useCdktfProject(
{ outDir, synthCommand },
Expand All @@ -42,6 +44,7 @@ export const Diff = ({
varFiles,
noColor,
migrateState,
skipSynth,
})
);

Expand Down
7 changes: 6 additions & 1 deletion packages/cdktf-cli/src/bin/cmds/ui/output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type OutputConfig = {
synthCommand: string;
onOutputsRetrieved: (outputs: NestedTerraformOutputs) => void;
outputsPath?: string;
skipSynth?: boolean;
};

export const Output = ({
Expand All @@ -23,11 +24,15 @@ export const Output = ({
synthCommand,
onOutputsRetrieved,
outputsPath,
skipSynth,
}: OutputConfig): React.ReactElement => {
const { status, logEntries, returnValue } = useCdktfProject(
{ outDir, synthCommand },
async (project) => {
const outputs = await project.fetchOutputs({ stackNames: targetStacks });
const outputs = await project.fetchOutputs({
stackNames: targetStacks,
skipSynth,
});
onOutputsRetrieved(outputs);
return outputs;
}
Expand Down
Loading