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: accept onDestroy command to run before resource destruction #228

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions API.md

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

34 changes: 32 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ export interface LocalExecConfig {
* @default true
*/
readonly copyBeforeRun?: boolean;

/**
* Command to run at destroy-time.
*/
readonly onDestroy?: string;
}

export { NullProvider as Provider } from "@cdktf/provider-null/lib/provider";

interface LocalExecProvisioner {
"local-exec": {
working_dir: string;
command: string;
when?: string;
};
}

export class LocalExec extends Resource {
public cwd: string;
public command: string;
public onDestroy?: string;

constructor(scope: Construct, id: string, config: LocalExecConfig) {
super(scope, id, config);
Expand All @@ -63,8 +77,9 @@ export class LocalExec extends Resource {

this.cwd = workingDir;
this.command = config.command;
this.onDestroy = config.onDestroy;

this.addOverride("provisioner", [
const overrides: LocalExecProvisioner[] = [
{
"local-exec": {
working_dir: workingDir,
Expand All @@ -74,6 +89,21 @@ export class LocalExec extends Resource {
}),
},
},
]);
];

if (typeof this.onDestroy === "string") {
overrides.push({
"local-exec": {
when: "destroy",
working_dir: workingDir,
command: Lazy.stringValue({
// TODO: wrap command to capture stdout and stderr
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved
produce: () => this.onDestroy,
}),
},
});
}

this.addOverride("provisioner", overrides);
}
}
31 changes: 27 additions & 4 deletions test/local-exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const apply = (addConstructs: (scope: Construct) => void) => {
cwd: outdir,
});

return outdir;
const destroy = () => {
execSync("terraform destroy -auto-approve", {
cwd: outdir,
});
};

return { outdir, destroy };
};

const workingDirectoryForAsset = (manifestPath: string, name: string) => {
Expand Down Expand Up @@ -61,7 +67,7 @@ describe("LocalExec", () => {

test("runs command inside copy of working directory", () => {
// Create a file in the stack directory
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
new LocalExec(stack, "myresource", {
command: `cp ${__filename} test.txt`,
cwd: __dirname,
Expand Down Expand Up @@ -112,7 +118,7 @@ describe("LocalExec", () => {

test("runs command can use token value inside a command", () => {
const passwordLength = 4;
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
new provider.RandomProvider(stack, "random");

const waiter = new LocalExec(stack, "timer", {
Expand Down Expand Up @@ -144,7 +150,7 @@ describe("LocalExec", () => {

test("command can be overwritten", () => {
// Create a file in the stack directory
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
const exec = new LocalExec(stack, "myresource", {
command: "ls -la",
cwd: __dirname,
Expand All @@ -158,4 +164,21 @@ describe("LocalExec", () => {
expect(fs.existsSync(path.resolve(workingDir, "test.txt"))).toBe(true);
expect(fs.existsSync(path.resolve(__dirname, "test.txt"))).toBe(false);
});

test("can specify a command to run at destroy-time", () => {
// Create a file in the working directory
const { destroy } = apply((stack) => {
new LocalExec(stack, "test", {
command: "cp origin.txt test.txt",
cwd: testdir,
onDestroy: "rm test.txt",
});
});

expect(fs.existsSync(path.resolve(testdir, "test.txt"))).toBe(true);

destroy();

expect(fs.existsSync(path.resolve(testdir, "test.txt"))).toBe(false);
});
});