diff --git a/cli/app/remove.go b/cli/app/remove.go index 9013d771..f65f8638 100644 --- a/cli/app/remove.go +++ b/cli/app/remove.go @@ -9,7 +9,7 @@ import ( "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/client" "coopcloud.tech/abra/pkg/log" - stack "coopcloud.tech/abra/pkg/upstream/stack" + "coopcloud.tech/abra/pkg/upstream/stack" "github.com/AlecAivazis/survey/v2" "github.com/docker/docker/api/types" "github.com/spf13/cobra" @@ -78,6 +78,22 @@ flag.`, log.Fatal(err) } + configs, err := client.GetConfigs(cl, context.Background(), app.Server, fs) + if err != nil { + log.Fatal(err) + } + configNames := client.GetConfigNames(configs) + + if len(configNames) > 0 { + if err := client.RemoveConfigs(cl, context.Background(), configNames, internal.Force); err != nil { + log.Fatalf("removing configs failed: %s", err) + } + + log.Infof("%d config(s) removed successfully", len(configNames)) + } else { + log.Info("no configs to remove") + } + secretList, err := cl.SecretList(context.Background(), types.SecretListOptions{Filters: fs}) if err != nil { log.Fatal(err) @@ -120,7 +136,7 @@ flag.`, log.Fatalf("removing volumes failed: %s", err) } - log.Infof("%d volumes removed successfully", len(volumeNames)) + log.Infof("%d volume(s) removed successfully", len(volumeNames)) } else { log.Info("no volumes to remove") } diff --git a/pkg/client/configs.go b/pkg/client/configs.go new file mode 100644 index 00000000..efe14ab4 --- /dev/null +++ b/pkg/client/configs.go @@ -0,0 +1,38 @@ +package client + +import ( + "context" + "fmt" + + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/swarm" + "github.com/docker/docker/client" +) + +func GetConfigs(cl *client.Client, ctx context.Context, server string, fs filters.Args) ([]swarm.Config, error) { + configList, err := cl.ConfigList(ctx, swarm.ConfigListOptions{Filters: fs}) + if err != nil { + return configList, err + } + + return configList, nil +} + +func GetConfigNames(configs []swarm.Config) []string { + var confNames []string + + for _, conf := range configs { + confNames = append(confNames, conf.Spec.Name) + } + + return confNames +} + +func RemoveConfigs(cl *client.Client, ctx context.Context, configNames []string, force bool) error { + for _, confName := range configNames { + if err := cl.ConfigRemove(context.Background(), confName); err != nil { + return fmt.Errorf("conf %s: %s", confName, err) + } + } + return nil +} diff --git a/tests/integration/app_remove.bats b/tests/integration/app_remove.bats index 0a1d0281..f24e2ef4 100644 --- a/tests/integration/app_remove.bats +++ b/tests/integration/app_remove.bats @@ -124,6 +124,33 @@ teardown(){ assert_output --partial 'removed' } +# bats test_tags=slow +@test "detect no configs to remove" { + _deploy_app + _undeploy_app + + run $ABRA app rm "$TEST_APP_DOMAIN" --no-input + assert_success + assert_output --partial 'no configs to remove' +} + +# bats test_tags=slow +@test "remove old app configs" { + _deploy_app + _undeploy_app + + sanitisedDomainName="${TEST_APP_DOMAIN//./_}" + run docker config create "${sanitisedDomainName}_test_conf_v99" "$ABRA_DIR/recipes/abra-test-recipe/abra.sh" + assert_success + + assert bash -c "docker config ls | grep -q test_conf_v99" + + run $ABRA app rm "$TEST_APP_DOMAIN" --no-input + assert_success + + refute bash -c "docker config ls | grep -q test_conf_v99" +} + @test "remove .env file" { assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env"