-
Notifications
You must be signed in to change notification settings - Fork 267
/
cleanupcloudstack.go
61 lines (51 loc) 路 1.63 KB
/
cleanupcloudstack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmd
import (
"context"
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/aws/eks-anywhere/internal/test/cleanup"
"github.com/aws/eks-anywhere/pkg/logger"
)
var cleanUpCloudstackCmd = &cobra.Command{
Use: "cloudstack",
Short: "Clean up e2e vms on cloudstack",
Long: "Clean up vms created for e2e testing on cloudstack",
SilenceUsage: true,
PreRun: preRunCleanUpCloudstackSetup,
RunE: func(cmd *cobra.Command, args []string) error {
err := cleanUpCloudstackTestResources(cmd.Context())
if err != nil {
logger.Fatal(err, "Failed to cleanup e2e vms on cloudstack")
}
return nil
},
}
func preRunCleanUpCloudstackSetup(cmd *cobra.Command, args []string) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
err := viper.BindPFlag(flag.Name, flag)
if err != nil {
log.Fatalf("Error initializing flags: %v", err)
}
})
}
var requiredCloudstackCleanUpFlags = []string{clusterNameFlagName}
func init() {
cleanUpInstancesCmd.AddCommand(cleanUpCloudstackCmd)
cleanUpCloudstackCmd.Flags().StringP(clusterNameFlagName, "n", "", "Cluster name for associated vms")
for _, flag := range requiredCloudstackCleanUpFlags {
if err := cleanUpCloudstackCmd.MarkFlagRequired(flag); err != nil {
log.Fatalf("Error marking flag %s as required: %v", flag, err)
}
}
}
func cleanUpCloudstackTestResources(ctx context.Context) error {
clusterName := viper.GetString(clusterNameFlagName)
err := cleanup.CleanUpCloudstackTestResources(ctx, clusterName, false)
if err != nil {
return fmt.Errorf("running cleanup for cloudstack vms: %v", err)
}
return nil
}