Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions cmd/mapt/cmd/azure/hosts/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,27 @@ func getCreateLinux(ostype data.OSType, defaultOSVersion string) *cobra.Command
}

func getDestroyLinux() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: params.DestroyCmdName,
Short: params.DestroyCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return azureLinux.Destroy(&maptContext.ContextArgs{
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
ForceDestroy: viper.IsSet(params.ForceDestroy),
KeepState: viper.IsSet(params.KeepState),
})
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(params.ForceDestroy, false, params.ForceDestroyDesc)
flagSet.Bool(params.KeepState, false, params.KeepStateDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
19 changes: 13 additions & 6 deletions cmd/mapt/cmd/azure/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,27 @@ func getCreateRHEL() *cobra.Command {
}

func getDestroyRHEL() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: params.DestroyCmdName,
Short: params.DestroyCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return azureRHEL.Destroy(&maptContext.ContextArgs{
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
ForceDestroy: viper.IsSet(params.ForceDestroy),
KeepState: viper.IsSet(params.KeepState),
})
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(params.ForceDestroy, false, params.ForceDestroyDesc)
flagSet.Bool(params.KeepState, false, params.KeepStateDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
19 changes: 13 additions & 6 deletions cmd/mapt/cmd/azure/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,30 @@ func getCreateWindowsDesktop() *cobra.Command {
}

func getDestroyWindowsDesktop() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: params.DestroyCmdName,
Short: params.DestroyCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := azureWindows.Destroy(&maptContext.ContextArgs{
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
ForceDestroy: viper.IsSet(params.ForceDestroy),
KeepState: viper.IsSet(params.KeepState),
}); err != nil {
logging.Error(err)
}
return nil
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(params.ForceDestroy, false, params.ForceDestroyDesc)
flagSet.Bool(params.KeepState, false, params.KeepStateDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
19 changes: 13 additions & 6 deletions cmd/mapt/cmd/azure/services/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,27 @@ func getCreateAKS() *cobra.Command {
}

func getDestroyAKS() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: params.DestroyCmdName,
Short: params.DestroyCmdName,
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return azureAKS.Destroy(&maptContext.ContextArgs{
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
Context: cmd.Context(),
ProjectName: viper.GetString(params.ProjectName),
BackedURL: viper.GetString(params.BackedURL),
Debug: viper.IsSet(params.Debug),
DebugLevel: viper.GetUint(params.DebugLevel),
ForceDestroy: viper.IsSet(params.ForceDestroy),
KeepState: viper.IsSet(params.KeepState),
})
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(params.ForceDestroy, false, params.ForceDestroyDesc)
flagSet.Bool(params.KeepState, false, params.KeepStateDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
2 changes: 2 additions & 0 deletions cmd/mapt/cmd/azure/services/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ func destroyKind() *cobra.Command {
DebugLevel: viper.GetUint(params.DebugLevel),
Serverless: viper.IsSet(params.Serverless),
ForceDestroy: viper.IsSet(params.ForceDestroy),
KeepState: viper.IsSet(params.KeepState),
})
},
}
flagSet := pflag.NewFlagSet(params.DestroyCmdName, pflag.ExitOnError)
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
flagSet.Bool(params.ForceDestroy, false, params.ForceDestroyDesc)
flagSet.Bool(params.KeepState, false, params.KeepStateDesc)
c.PersistentFlags().AddFlagSet(flagSet)
return c
}
2 changes: 1 addition & 1 deletion cmd/mapt/cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const (
ForceDestroy string = "force-destroy"
ForceDestroyDesc string = "if force-destroy is set the command will destroy even if there is a lock."
KeepState string = "keep-state"
KeepStateDesc string = "keep Pulumi state files in S3 backend after successful destroy (by default, state files are removed)"
KeepStateDesc string = "keep Pulumi state files in backend storage after successful destroy (by default, state files are removed)"

// Kind
KindCmd = "kind"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.12.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.7.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v3
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v3 v3.0.1/go.mod h1:8h8yhzh9o+0HeSIhUxYny+rEQajScrfIpNktvgYG3Q8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0 h1:wxQx2Bt4xzPIKvW59WQf1tJNx/ZZKPfN+EhPX3Z6CYY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0/go.mod h1:TpiwjwnW/khS0LKs4vW5UmmT9OWcxaveS8U7+tlknzo=
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4 h1:jWQK1GI+LeGGUKBADtcH2rRqPxYB1Ljwms5gFA2LqrM=
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4/go.mod h1:8mwH4klAm9DUgR2EEHyEEAQlRDvLPyg5fQry3y+cDew=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
github.com/AzureAD/microsoft-authentication-library-for-go v1.7.1 h1:edShSHV3DV90+kt+CMaEXEzR9QF7wFrPJxVGz2blMIU=
Expand Down
8 changes: 6 additions & 2 deletions pkg/provider/azure/action/aks/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ func Create(mCtxArgs *mc.ContextArgs, args *AKSArgs) (err error) {

func Destroy(mCtxArgs *mc.ContextArgs) error {
logging.Debug("Destroy AKS")
// Create mapt Context
mCtx, err := mc.Init(mCtxArgs, azure.Provider())
if err != nil {
return err
}
return azure.Destroy(mCtx, stackAKS)
if err := azure.DestroyStack(mCtx, azure.DestroyStackRequest{
Stackname: stackAKS,
}); err != nil {
return err
}
return azure.CleanupState(mCtx)
}

// Main function to deploy all requried resources to azure
Expand Down
9 changes: 6 additions & 3 deletions pkg/provider/azure/action/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ func Create(mCtxArgs *mc.ContextArgs, args *utilKind.KindArgs) (*utilKind.KindRe
}

func Destroy(mCtxArgs *mc.ContextArgs) (err error) {
// Create mapt Context
mCtx, err := mc.Init(mCtxArgs, azure.Provider())
if err != nil {
return err
}
// destroy
return azure.Destroy(mCtx, stackAzureKind)
if err := azure.DestroyStack(mCtx, azure.DestroyStackRequest{
Stackname: stackAzureKind,
}); err != nil {
return err
}
return azure.CleanupState(mCtx)
}

// Main function to deploy all requried resources to azure
Expand Down
9 changes: 6 additions & 3 deletions pkg/provider/azure/action/linux/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ func Create(mCtxArgs *mc.ContextArgs, args *LinuxArgs) (err error) {
}

func Destroy(mCtxArgs *mc.ContextArgs) error {
// Create mapt Context
mCtx, err := mc.Init(mCtxArgs, azure.Provider())
if err != nil {
return err
}
// destroy
return azure.Destroy(mCtx, stackAzureLinux)
if err := azure.DestroyStack(mCtx, azure.DestroyStackRequest{
Stackname: stackAzureLinux,
}); err != nil {
return err
}
return azure.CleanupState(mCtx)
}

// Main function to deploy all requried resources to azure
Expand Down
9 changes: 6 additions & 3 deletions pkg/provider/azure/action/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ func Create(mCtxArgs *mc.ContextArgs, args *WindowsArgs) (err error) {
}

func Destroy(mCtxArgs *mc.ContextArgs) error {
// Create mapt Context
mCtx, err := mc.Init(mCtxArgs, azure.Provider())
if err != nil {
return err
}
// destroy
return azure.Destroy(mCtx, stackCreateWindowsDesktop)
if err := azure.DestroyStack(mCtx, azure.DestroyStackRequest{
Stackname: stackCreateWindowsDesktop,
}); err != nil {
return err
}
return azure.CleanupState(mCtx)
}

// Main function to deploy all requried resources to azure
Expand Down
73 changes: 73 additions & 0 deletions pkg/provider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package azure

import (
"context"
"fmt"
"net/url"
"os"
"slices"
"strings"

"github.com/redhat-developer/mapt/pkg/manager"
mc "github.com/redhat-developer/mapt/pkg/manager/context"
"github.com/redhat-developer/mapt/pkg/manager/credentials"
"github.com/redhat-developer/mapt/pkg/provider/azure/services/storage/blob"
"github.com/redhat-developer/mapt/pkg/util/logging"
)

Expand Down Expand Up @@ -113,6 +116,12 @@ var (
}
)

const pulumiLocksPath = ".pulumi/locks"

type DestroyStackRequest struct {
Stackname string
}

func Destroy(mCtx *mc.Context, stackName string) error {
stack := manager.Stack{
StackName: mCtx.StackNameByProject(stackName),
Expand All @@ -122,6 +131,70 @@ func Destroy(mCtx *mc.Context, stackName string) error {
return manager.DestroyStack(mCtx, stack)
}

func DestroyStack(mCtx *mc.Context, s DestroyStackRequest) error {
logging.Debug("Running destroy operation")
if len(s.Stackname) == 0 {
return fmt.Errorf("stackname is required")
}
if mCtx.IsForceDestroy() {
container, path, parseErr := parseAzblobBackedURL(mCtx)
if parseErr != nil {
logging.Error(parseErr)
} else {
prefix := pulumiLocksPath + "/"
if path != "" {
prefix = path + "/" + prefix
}
if err := blob.Delete(mCtx.Context(), container, prefix); err != nil {
logging.Error(err)
}
}
}
return manager.DestroyStack(
mCtx,
manager.Stack{
StackName: mCtx.StackNameByProject(s.Stackname),
ProjectName: mCtx.ProjectName(),
BackedURL: mCtx.BackedURL(),
ProviderCredentials: DefaultCredentials})
}

func parseAzblobBackedURL(mCtx *mc.Context) (container string, path string, err error) {
if !strings.HasPrefix(mCtx.BackedURL(), "azblob://") {
return "", "", fmt.Errorf("invalid azblob URI: must start with azblob://")
}
u, err := url.Parse(mCtx.BackedURL())
if err != nil {
return "", "", fmt.Errorf("failed to parse azblob URI: %w", err)
}
return u.Host, strings.TrimPrefix(u.Path, "/"), nil
}

func CleanupState(mCtx *mc.Context) error {
if mCtx.IsKeepState() {
return nil
}

container, path, parseErr := parseAzblobBackedURL(mCtx)
if parseErr != nil {
logging.Warnf("Failed to parse azblob backend URL, skipping state cleanup: %v", parseErr)
return nil
}

prefix := ".pulumi/"
if path != "" {
prefix = path + "/"
}
logging.Infof("Cleaning up Pulumi state from azblob://%s/%s", container, path)
if deleteErr := blob.Delete(mCtx.Context(), container, prefix); deleteErr != nil {
logging.Warnf("Failed to cleanup Azure blob state: %v", deleteErr)
} else {
logging.Info("Successfully cleaned up Pulumi state from Azure Blob Storage")
}

return nil
}

func locationSupportsResourceGroup(location string) bool {
return slices.Contains(locationsSupportingResourceGroup, location)
}
Expand Down
Loading