0
0
forked from toolshed/abra

feat: remove old app configs

See toolshed/abra#577
This commit is contained in:
2025-08-17 15:13:36 +02:00
committed by decentral1se
parent fac372dc73
commit f5a843bd90
3 changed files with 83 additions and 2 deletions

View File

@ -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")
}

38
pkg/client/configs.go Normal file
View File

@ -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
}

View File

@ -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"