Skip to content

Commit

Permalink
[task] default yes in destroy task (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
pducolin authored Oct 9, 2024
1 parent 9167ca5 commit dc0306f
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 76 deletions.
33 changes: 27 additions & 6 deletions integration-tests/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"testing"
"unicode"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -80,14 +81,16 @@ func testAzureInvokeVM(t *testing.T, tmpConfigFile string, workingDirectory stri
t.Helper()

stackName := fmt.Sprintf("az-invoke-vm-%s", os.Getenv("CI_PIPELINE_ID"))
stackName = sanitizeStackName(stackName)

t.Log("creating vm")
createCmd := exec.Command("invoke", "az.create-vm", "--no-interactive", "--stack-name", stackName, "--config-path", tmpConfigFile)
createCmd.Dir = workingDirectory
createOutput, err := createCmd.Output()
assert.NoError(t, err, "Error found creating vm: %s", string(createOutput))

t.Log("destroying vm")
destroyCmd := exec.Command("invoke", "az.destroy-vm", "--yes", "--no-clean-known-hosts", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd := exec.Command("invoke", "az.destroy-vm", "--no-clean-known-hosts", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd.Dir = workingDirectory
destroyOutput, err := destroyCmd.Output()
require.NoError(t, err, "Error found destroying stack: %s", string(destroyOutput))
Expand All @@ -97,14 +100,16 @@ func testAwsInvokeVM(t *testing.T, tmpConfigFile string, workingDirectory string
t.Helper()

stackName := fmt.Sprintf("aws-invoke-vm-%s", os.Getenv("CI_PIPELINE_ID"))
stackName = sanitizeStackName(stackName)

t.Log("creating vm")
createCmd := exec.Command("invoke", "aws.create-vm", "--no-interactive", "--stack-name", stackName, "--config-path", tmpConfigFile, "--use-fakeintake")
createCmd.Dir = workingDirectory
createOutput, err := createCmd.Output()
assert.NoError(t, err, "Error found creating vm: %s", string(createOutput))

t.Log("destroying vm")
destroyCmd := exec.Command("invoke", "aws.destroy-vm", "--yes", "--no-clean-known-hosts", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd := exec.Command("invoke", "aws.destroy-vm", "--no-clean-known-hosts", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd.Dir = workingDirectory
destroyOutput, err := destroyCmd.Output()
require.NoError(t, err, "Error found destroying stack: %s", string(destroyOutput))
Expand All @@ -113,6 +118,7 @@ func testAwsInvokeVM(t *testing.T, tmpConfigFile string, workingDirectory string
func testInvokeDockerVM(t *testing.T, tmpConfigFile string, workingDirectory string) {
t.Helper()
stackName := fmt.Sprintf("invoke-docker-vm-%s", os.Getenv("CI_PIPELINE_ID"))
stackName = sanitizeStackName(stackName)
t.Log("creating vm with docker")
var stdOut, stdErr bytes.Buffer

Expand All @@ -127,7 +133,7 @@ func testInvokeDockerVM(t *testing.T, tmpConfigFile string, workingDirectory str
stdErr.Reset()

t.Log("destroying vm with docker")
destroyCmd := exec.Command("invoke", "destroy-docker", "--yes", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd := exec.Command("invoke", "destroy-docker", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd.Dir = workingDirectory
destroyCmd.Stdout = &stdOut
destroyCmd.Stderr = &stdErr
Expand All @@ -142,14 +148,15 @@ func testInvokeKind(t *testing.T, tmpConfigFile string, workingDirectory string)
stackParts = append(stackParts, os.Getenv("CI_PIPELINE_ID"))
}
stackName := strings.Join(stackParts, "-")
stackName = sanitizeStackName(stackName)
t.Log("creating kind cluster")
createCmd := exec.Command("invoke", "create-kind", "--no-interactive", "--stack-name", stackName, "--config-path", tmpConfigFile, "--use-fakeintake", "--use-loadBalancer")
createCmd.Dir = workingDirectory
createOutput, err := createCmd.Output()
assert.NoError(t, err, "Error found creating kind cluster: %s", string(createOutput))

t.Log("destroying kind cluster")
destroyCmd := exec.Command("invoke", "destroy-kind", "--yes", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd := exec.Command("invoke", "destroy-kind", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd.Dir = workingDirectory
destroyOutput, err := destroyCmd.Output()
require.NoError(t, err, "Error found destroying kind cluster: %s", string(destroyOutput))
Expand All @@ -161,15 +168,15 @@ func testInvokeKindOperator(t *testing.T, tmpConfigFile string, workingDirectory
if os.Getenv("CI") == "true" {
stackName = fmt.Sprintf("%s-%s", stackName, os.Getenv("CI_PIPELINE_ID"))
}

stackName = sanitizeStackName(stackName)
t.Log("creating kind cluster with operator")
createCmd := exec.Command("invoke", "aws.create-kind", "--install-agent-with-operator", "true", "--no-interactive", "--stack-name", stackName, "--config-path", tmpConfigFile, "--use-fakeintake", "--use-loadBalancer")
createCmd.Dir = workingDirectory
createOutput, err := createCmd.Output()
assert.NoError(t, err, "Error found creating kind cluster: %s; %s", string(createOutput), err)

t.Log("destroying kind cluster with operator")
destroyCmd := exec.Command("invoke", "destroy-kind", "--yes", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd := exec.Command("invoke", "destroy-kind", "--stack-name", stackName, "--config-path", tmpConfigFile)
destroyCmd.Dir = workingDirectory
destroyOutput, err := destroyCmd.Output()
require.NoError(t, err, "Error found destroying kind cluster: %s", string(destroyOutput))
Expand Down Expand Up @@ -228,3 +235,17 @@ func rootPath() (string, error) {
}
return strings.TrimSpace(string(path)), nil
}

func sanitizeStackName(s string) string {
return strings.Map(
func(r rune) rune {
// valid values are alphanumeric and hyphen
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || unicode.IsDigit(r) || r == '-' {
return r
}
// drop invalid runes
return -1
},
s,
)
}
6 changes: 3 additions & 3 deletions tasks/aks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def create_aks(
)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_aks(ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False):
@task(help={"stack_name": doc.stack_name})
def destroy_aks(ctx: Context, stack_name: Optional[str] = None):
print('This command is deprecated, please use `az.destroy-aks` instead')
print("Running `az.destroy-aks`...")
from tasks.azure.aks import destroy_aks as destroy_aks_azure

destroy_aks_azure(ctx, stack_name=stack_name, yes=yes)
destroy_aks_azure(ctx, stack_name=stack_name)
3 changes: 0 additions & 3 deletions tasks/aws/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ def _show_connection_message(ctx: Context, full_stack_name: str, copy_to_clipboa
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
"yes": doc.yes,
}
)
def destroy_docker(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
yes: Optional[bool] = False,
):
"""
Destroy an environment created by invoke aws.create-docker.
Expand All @@ -106,7 +104,6 @@ def destroy_docker(
scenario_name=scenario_name,
config_path=config_path,
stack=stack_name,
force_yes=yes,
)


Expand Down
6 changes: 3 additions & 3 deletions tasks/aws/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def _show_connection_message(ctx: Context, config_path: Optional[str], full_stac
pyperclip.copy(command)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_ecs(ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False):
@task(help={"stack_name": doc.stack_name})
def destroy_ecs(ctx: Context, stack_name: Optional[str] = None):
"""
Destroy a ECS environment created with invoke aws.create-ecs.
"""
destroy(ctx, scenario_name=scenario_name, stack=stack_name, force_yes=yes)
destroy(ctx, scenario_name=scenario_name, stack=stack_name)
6 changes: 3 additions & 3 deletions tasks/aws/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def _show_connection_message(ctx: Context, full_stack_name: str, config_path: Op
pyperclip.copy(command)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_eks(ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False):
@task(help={"stack_name": doc.stack_name})
def destroy_eks(ctx: Context, stack_name: Optional[str] = None):
"""
Destroy a EKS environment created with invoke aws.create-eks.
"""
destroy(ctx, scenario_name=scenario_name, stack=stack_name, force_yes=yes)
destroy(ctx, scenario_name=scenario_name, stack=stack_name)
9 changes: 2 additions & 7 deletions tasks/aws/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ def create_installer_lab(
print(f"Installer lab created: {full_stack_name}")


@task(
help={
"yes": doc.yes,
}
)
@task
def destroy_installer_lab(
ctx: Context,
yes: Optional[bool] = False,
):
destroy(ctx, scenario_name=scenario_name, stack="installer-lab", force_yes=yes)
destroy(ctx, scenario_name=scenario_name, stack="installer-lab")

print("Installer lab destroyed")
3 changes: 0 additions & 3 deletions tasks/aws/kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ def _show_connection_message(ctx: Context, full_stack_name: str, copy_to_clipboa
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
"yes": doc.yes,
}
)
def destroy_kind(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
yes: Optional[bool] = False,
):
"""
Destroy an environment created by invoke aws.create-kind.
Expand All @@ -111,7 +109,6 @@ def destroy_kind(
scenario_name=scenario_name,
config_path=config_path,
stack=stack_name,
force_yes=yes,
)


Expand Down
3 changes: 0 additions & 3 deletions tasks/aws/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,13 @@ def create_vm(
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
"yes": doc.yes,
"clean_known_hosts": doc.clean_known_hosts,
}
)
def destroy_vm(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
yes: Optional[bool] = False,
clean_known_hosts: Optional[bool] = True,
):
"""
Expand All @@ -138,7 +136,6 @@ def destroy_vm(
scenario_name=scenario_name,
config_path=config_path,
stack=stack_name,
force_yes=yes,
)
if clean_known_hosts:
clean_known_hosts_func(host)
Expand Down
8 changes: 3 additions & 5 deletions tasks/azure/aks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ def create_aks(
_show_connection_message(ctx, full_stack_name, interactive)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_aks(
ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False, config_path: Optional[str] = None
):
@task(help={"stack_name": doc.stack_name})
def destroy_aks(ctx: Context, stack_name: Optional[str] = None, config_path: Optional[str] = None):
"""
Destroy a AKS environment created with invoke az.create-aks.
"""
destroy(ctx, scenario_name=scenario_name, stack=stack_name, force_yes=yes, config_path=config_path)
destroy(ctx, scenario_name=scenario_name, stack=stack_name, config_path=config_path)


def _show_connection_message(ctx: Context, full_stack_name: str, copy_to_clipboard: Optional[bool]):
Expand Down
3 changes: 0 additions & 3 deletions tasks/azure/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ def create_vm(
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
"yes": doc.yes,
"clean_known_hosts": doc.clean_known_hosts,
}
)
def destroy_vm(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
yes: Optional[bool] = True,
clean_known_hosts: Optional[bool] = True,
):
"""
Expand All @@ -132,7 +130,6 @@ def destroy_vm(
scenario_name=scenario_name,
config_path=config_path,
stack=stack_name,
force_yes=yes,
)
if clean_known_hosts:
clean_known_hosts_func(host)
Expand Down
4 changes: 1 addition & 3 deletions tasks/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ def destroy(
scenario_name: str,
config_path: Optional[str] = None,
stack: Optional[str] = None,
force_yes: Optional[bool] = False,
):
"""
Destroy an environment
"""

full_stack_name = get_stack_name(stack, scenario_name)
short_stack_names, full_stack_names = _get_existing_stacks()
force_destroy = "--yes --skip-preview" if force_yes else ""

if len(short_stack_names) == 0:
info("No stack to destroy")
Expand All @@ -52,7 +50,7 @@ def destroy(
for stack_name in short_stack_names:
error(f" {stack_name}")
else:
cmd = f"pulumi destroy --remove -s {full_stack_name} {force_destroy}"
cmd = f"pulumi destroy --remove --yes --skip-preview -s {full_stack_name}"
pty = True
if tool.is_windows():
pty = False
Expand Down
1 change: 0 additions & 1 deletion tasks/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
bottlerocket_node_group: str = "Install a bottlerocket node group (default True)"
windows_node_group: str = "Install a Windows node group (default False)"
fakeintake: str = "Use a dedicated fake Datadog intake (default False)"
yes: str = "Automatically approve and perform the destroy without previewing it"
use_aws_vault: str = "Wrap aws command with aws-vault, default to True"
interactive: str = "Enable interactive mode, if set to False notifications and copy to clipboard are disabled"
config_path: str = "Specify a custom config path to use"
Expand Down
4 changes: 1 addition & 3 deletions tasks/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ def create_docker(
help={
"config_path": doc.config_path,
"stack_name": doc.stack_name,
"yes": doc.yes,
}
)
def destroy_docker(
ctx: Context,
config_path: Optional[str] = None,
stack_name: Optional[str] = None,
yes: Optional[bool] = False,
):
print('This command is deprecated, please use `aws.destroy-docker` instead')
print("Running `aws.destroy-docker`...")
from tasks.aws.docker import destroy_docker as destroy_docker_aws

destroy_docker_aws(ctx, config_path, stack_name, yes)
destroy_docker_aws(ctx, config_path, stack_name)
6 changes: 3 additions & 3 deletions tasks/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def create_ecs(
)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_ecs(ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False):
@task(help={"stack_name": doc.stack_name})
def destroy_ecs(ctx: Context, stack_name: Optional[str] = None):
print('This command is deprecated, please use `aws.create-ecs` instead')
print("Running `aws.create-ecs`...")
from tasks.aws.ecs import destroy_ecs as destroy_ecs_aws

destroy_ecs_aws(ctx, stack_name, yes)
destroy_ecs_aws(ctx, stack_name)
6 changes: 3 additions & 3 deletions tasks/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def create_eks(
)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_eks(ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False):
@task(help={"stack_name": doc.stack_name})
def destroy_eks(ctx: Context, stack_name: Optional[str] = None):
print('This command is deprecated, please use `aws.create-eks` instead')
print("Running `aws.create-eks`...")
from tasks.aws.eks import destroy_eks as destroy_eks_aws

destroy_eks_aws(ctx, stack_name, yes)
destroy_eks_aws(ctx, stack_name)
8 changes: 3 additions & 5 deletions tasks/gcp/gke.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ def create_gke(
_show_connection_message(ctx, full_stack_name, interactive)


@task(help={"stack_name": doc.stack_name, "yes": doc.yes})
def destroy_gke(
ctx: Context, stack_name: Optional[str] = None, yes: Optional[bool] = False, config_path: Optional[str] = None
):
@task(help={"stack_name": doc.stack_name})
def destroy_gke(ctx: Context, stack_name: Optional[str] = None, config_path: Optional[str] = None):
"""
Destroy a GKE environment created with invoke gcp.create-gke.
"""
destroy(ctx, scenario_name=scenario_name, stack=stack_name, force_yes=yes, config_path=config_path)
destroy(ctx, scenario_name=scenario_name, stack=stack_name, config_path=config_path)


def _show_connection_message(ctx: Context, full_stack_name: str, copy_to_clipboard: Optional[bool]):
Expand Down
Loading

0 comments on commit dc0306f

Please sign in to comment.