From c8c9bbb22ad0458709c26f06a3830ef1542b966c Mon Sep 17 00:00:00 2001 From: Hariharan Jayaraman Date: Mon, 14 May 2018 20:06:23 -0700 Subject: [PATCH] Async delete Resource Group --- builder/azure/arm/builder.go | 1 + builder/azure/arm/config.go | 3 +++ builder/azure/arm/step_create_resource_group.go | 12 +++++++++--- builder/azure/arm/step_delete_resource_group.go | 8 +++++++- builder/azure/common/constants/stateBag.go | 1 + website/source/docs/builders/azure.html.md | 4 ++++ 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 5603f0e52..e59f0f84b 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -344,6 +344,7 @@ func (b *Builder) configureStateBag(stateBag multistep.StateBag) { stateBag.Put(constants.ArmIsManagedImage, b.config.isManagedImage()) stateBag.Put(constants.ArmManagedImageResourceGroupName, b.config.ManagedImageResourceGroupName) stateBag.Put(constants.ArmManagedImageName, b.config.ManagedImageName) + stateBag.Put(constants.ArmAsyncRGDelete, b.config.AsyncRGDelete) } // Parameters that are only known at runtime after querying Azure. diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index de3a0784e..ded561a58 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -151,6 +151,9 @@ type Config struct { Comm communicator.Config `mapstructure:",squash"` ctx *interpolate.Context + + //Cleanup + AsyncRGDelete bool `mapstructure:"async_resourcegroup_delete"` } type keyVaultCertificate struct { diff --git a/builder/azure/arm/step_create_resource_group.go b/builder/azure/arm/step_create_resource_group.go index 9262f868a..8036adb36 100644 --- a/builder/azure/arm/step_create_resource_group.go +++ b/builder/azure/arm/step_create_resource_group.go @@ -118,14 +118,20 @@ func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) { ctx := context.TODO() f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName) if err == nil { - err = f.WaitForCompletion(ctx, s.client.GroupsClient.Client) + if state.Get(constants.ArmAsyncRGDelete).(bool) { + s.say(fmt.Sprintf("\n Not waiting for Resource Group delete as requested by user. Resource Group Name is %s", resourceGroupName)) + } else { + err = f.WaitForCompletion(ctx, s.client.GroupsClient.Client) + } } if err != nil { ui.Error(fmt.Sprintf("Error deleting resource group. Please delete it manually.\n\n"+ "Name: %s\n"+ "Error: %s", resourceGroupName, err)) + return + } + if !state.Get(constants.ArmAsyncRGDelete).(bool) { + ui.Say("Resource group has been deleted.") } - - ui.Say("Resource group has been deleted.") } } diff --git a/builder/azure/arm/step_delete_resource_group.go b/builder/azure/arm/step_delete_resource_group.go index 714f43c1b..e6445ce1d 100644 --- a/builder/azure/arm/step_delete_resource_group.go +++ b/builder/azure/arm/step_delete_resource_group.go @@ -53,7 +53,13 @@ func (s *StepDeleteResourceGroup) deleteResourceGroup(ctx context.Context, state s.say("\nThe resource group was created by Packer, deleting ...") f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName) if err == nil { - f.WaitForCompletion(ctx, s.client.GroupsClient.Client) + if state.Get(constants.ArmAsyncRGDelete).(bool) { + // No need to wait for the complition for delete if request is Accepted + s.say(fmt.Sprintf("\nResource Group is being deleted, not waiting for deletion due to config. Resource Group Name '%s'", resourceGroupName)) + } else { + f.WaitForCompletion(ctx, s.client.GroupsClient.Client) + } + } if err != nil { diff --git a/builder/azure/common/constants/stateBag.go b/builder/azure/common/constants/stateBag.go index 3576f6820..a90c47e16 100644 --- a/builder/azure/common/constants/stateBag.go +++ b/builder/azure/common/constants/stateBag.go @@ -35,4 +35,5 @@ const ( ArmManagedImageResourceGroupName string = "arm.ManagedImageResourceGroupName" ArmManagedImageLocation string = "arm.ManagedImageLocation" ArmManagedImageName string = "arm.ManagedImageName" + ArmAsyncRGDelete string = "arm.AsyncRGDelete" ) diff --git a/website/source/docs/builders/azure.html.md b/website/source/docs/builders/azure.html.md index 9019e035e..23f104269 100644 --- a/website/source/docs/builders/azure.html.md +++ b/website/source/docs/builders/azure.html.md @@ -221,6 +221,10 @@ Providing `temp_resource_group_name` or `location` in combination with `build_re CLI example `azure vm sizes -l westus` +- `async_resourcegroup_delete` (boolean) If you want packer to delete the temporary resource group asynchronously. Its a boolean value + and defaults to false. **Important** Setting this true would mean that your builds are faster however this is a very + small chance that the temporary resource group is not deleted by Azure. + ## Basic Example Here is a basic example for Azure.