diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index d7626a2f0..83028dee3 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -221,7 +221,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack NewStepSnapshotDataDisks(azureClient, ui, &b.config), NewStepCaptureImage(azureClient, ui), NewStepPublishToSharedImageGallery(azureClient, ui, &b.config), - NewStepDeleteResourceGroup(azureClient, ui), NewStepDeleteOSDisk(azureClient, ui), NewStepDeleteAdditionalDisks(azureClient, ui), } @@ -264,7 +263,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack NewStepSnapshotDataDisks(azureClient, ui, &b.config), NewStepCaptureImage(azureClient, ui), NewStepPublishToSharedImageGallery(azureClient, ui, &b.config), - NewStepDeleteResourceGroup(azureClient, ui), NewStepDeleteOSDisk(azureClient, ui), NewStepDeleteAdditionalDisks(azureClient, ui), ) diff --git a/builder/azure/arm/step_create_resource_group.go b/builder/azure/arm/step_create_resource_group.go index 7308886b0..b5be70452 100644 --- a/builder/azure/arm/step_create_resource_group.go +++ b/builder/azure/arm/step_create_resource_group.go @@ -111,27 +111,30 @@ func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) { if state.Get(constants.ArmIsExistingResourceGroup).(bool) { ui.Say("\nThe resource group was not created by Packer, not deleting ...") return - } else { - ui.Say("\nCleanup requested, deleting resource group ...") + } - var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) - ctx := context.TODO() - f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName) - if err == nil { - if state.Get(constants.ArmAsyncResourceGroupDelete).(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.WaitForCompletionRef(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.ArmAsyncResourceGroupDelete).(bool) { - ui.Say("Resource group has been deleted.") + ctx := context.TODO() + resourceGroupName := state.Get(constants.ArmResourceGroupName).(string) + if exists, err := s.exists(ctx, resourceGroupName); !exists || err != nil { + return + } + + ui.Say("\nCleanup requested, deleting resource group ...") + f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName) + if err == nil { + if state.Get(constants.ArmAsyncResourceGroupDelete).(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.WaitForCompletionRef(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.ArmAsyncResourceGroupDelete).(bool) { + 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 deleted file mode 100644 index ed83c56a4..000000000 --- a/builder/azure/arm/step_delete_resource_group.go +++ /dev/null @@ -1,153 +0,0 @@ -package arm - -import ( - "context" - "fmt" - "time" - - "github.com/hashicorp/packer/builder/azure/common/constants" - "github.com/hashicorp/packer/common/retry" - "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" -) - -const ( - maxResourcesToDelete = 50 -) - -type StepDeleteResourceGroup struct { - client *AzureClient - delete func(ctx context.Context, state multistep.StateBag, resourceGroupName string) error - say func(message string) - error func(e error) -} - -func NewStepDeleteResourceGroup(client *AzureClient, ui packer.Ui) *StepDeleteResourceGroup { - var step = &StepDeleteResourceGroup{ - client: client, - say: func(message string) { ui.Say(message) }, - error: func(e error) { ui.Error(e.Error()) }, - } - - step.delete = step.deleteResourceGroup - return step -} - -func (s *StepDeleteResourceGroup) deleteResourceGroup(ctx context.Context, state multistep.StateBag, resourceGroupName string) error { - var err error - if state.Get(constants.ArmIsExistingResourceGroup).(bool) { - s.say("\nThe resource group was not created by Packer, only deleting individual resources ...") - var deploymentName = state.Get(constants.ArmDeploymentName).(string) - err = s.deleteDeploymentResources(ctx, deploymentName, resourceGroupName) - if err != nil { - return err - } - - if keyVaultDeploymentName, ok := state.GetOk(constants.ArmKeyVaultDeploymentName); ok { - // Only delete if custom keyvault was not provided. - if exists := state.Get(constants.ArmIsExistingKeyVault).(bool); !exists { - s.say("\n Deleting the keyvault deployment because it was created by Packer...") - err = s.deleteDeploymentResources(ctx, keyVaultDeploymentName.(string), resourceGroupName) - if err != nil { - return err - } - } - } - - return nil - } else { - s.say("\nThe resource group was created by Packer, deleting ...") - f, err := s.client.GroupsClient.Delete(ctx, resourceGroupName) - if err == nil { - if state.Get(constants.ArmAsyncResourceGroupDelete).(bool) { - // No need to wait for the completion 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.WaitForCompletionRef(ctx, s.client.GroupsClient.Client) - } - - } - - if err != nil { - s.say(s.client.LastError.Error()) - } - return err - } -} - -func (s *StepDeleteResourceGroup) deleteDeploymentResources(ctx context.Context, deploymentName, resourceGroupName string) error { - maxResources := int32(maxResourcesToDelete) - - deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(ctx, resourceGroupName, deploymentName, &maxResources) - if err != nil { - s.reportIfError(err, resourceGroupName) - return err - } - - for deploymentOperations.NotDone() { - deploymentOperation := deploymentOperations.Value() - // Sometimes an empty operation is added to the list by Azure - if deploymentOperation.Properties.TargetResource == nil { - deploymentOperations.Next() - continue - } - - resourceName := *deploymentOperation.Properties.TargetResource.ResourceName - resourceType := *deploymentOperation.Properties.TargetResource.ResourceType - - s.say(fmt.Sprintf(" -> %s : '%s'", - resourceType, - resourceName)) - - retry.Config{ - Tries: 10, - RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 600 * time.Second, Multiplier: 2}).Linear, - }.Run(ctx, func(ctx context.Context) error { - err := deleteResource(ctx, s.client, - resourceType, - resourceName, - resourceGroupName) - if err != nil { - s.reportIfError(err, resourceName) - } - return nil - }) - - if err = deploymentOperations.Next(); err != nil { - return err - } - } - - return nil -} - -func (s *StepDeleteResourceGroup) reportIfError(err error, resourceName string) { - if err != nil { - s.say(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+ - "Name: %s\n"+ - "Error: %s", resourceName, err.Error())) - s.error(err) - } -} - -func (s *StepDeleteResourceGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - s.say("Deleting resource group ...") - - var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) - s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) - - err := s.delete(ctx, state, resourceGroupName) - if err != nil { - state.Put(constants.Error, err) - s.error(err) - - return multistep.ActionHalt - } - - state.Put(constants.ArmIsResourceGroupCreated, false) - - return multistep.ActionContinue -} - -func (*StepDeleteResourceGroup) Cleanup(multistep.StateBag) { -} diff --git a/builder/azure/arm/step_delete_resource_group_test.go b/builder/azure/arm/step_delete_resource_group_test.go deleted file mode 100644 index e67fa15d5..000000000 --- a/builder/azure/arm/step_delete_resource_group_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package arm - -import ( - "context" - "fmt" - "testing" - - "github.com/hashicorp/packer/builder/azure/common/constants" - "github.com/hashicorp/packer/helper/multistep" -) - -func TestStepDeleteResourceGroupShouldFailIfDeleteFails(t *testing.T) { - var testSubject = &StepDeleteResourceGroup{ - delete: func(context.Context, multistep.StateBag, string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, - say: func(message string) {}, - error: func(e error) {}, - } - - stateBag := DeleteTestStateBagStepDeleteResourceGroup() - - var result = testSubject.Run(context.Background(), stateBag) - if result != multistep.ActionHalt { - t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk(constants.Error); ok == false { - t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) - } -} - -func TestStepDeleteResourceGroupShouldPassIfDeletePasses(t *testing.T) { - var testSubject = &StepDeleteResourceGroup{ - delete: func(context.Context, multistep.StateBag, string) error { return nil }, - say: func(message string) {}, - error: func(e error) {}, - } - - stateBag := DeleteTestStateBagStepDeleteResourceGroup() - - var result = testSubject.Run(context.Background(), stateBag) - if result != multistep.ActionContinue { - t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk(constants.Error); ok == true { - t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) - } -} - -func TestStepDeleteResourceGroupShouldDeleteStateBagArmResourceGroupCreated(t *testing.T) { - var testSubject = &StepDeleteResourceGroup{ - delete: func(context.Context, multistep.StateBag, string) error { - return nil - }, - say: func(message string) {}, - error: func(e error) {}, - } - - stateBag := DeleteTestStateBagStepDeleteResourceGroup() - testSubject.Run(context.Background(), stateBag) - - value, ok := stateBag.GetOk(constants.ArmIsResourceGroupCreated) - if !ok { - t.Fatal("Expected the resource bag value arm.IsResourceGroupCreated to exist") - } - - if value.(bool) { - t.Fatalf("Expected arm.IsResourceGroupCreated to be false, but got %q", value) - } -} - -func DeleteTestStateBagStepDeleteResourceGroup() multistep.StateBag { - stateBag := new(multistep.BasicStateBag) - stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") - stateBag.Put(constants.ArmIsResourceGroupCreated, "Unit Test: IsResourceGroupCreated") - - return stateBag -} diff --git a/builder/azure/arm/step_deploy_template.go b/builder/azure/arm/step_deploy_template.go index e23effbe5..daa12275c 100644 --- a/builder/azure/arm/step_deploy_template.go +++ b/builder/azure/arm/step_deploy_template.go @@ -42,6 +42,92 @@ func NewStepDeployTemplate(client *AzureClient, ui packer.Ui, config *Config, de return step } +func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + s.say("Deploying deployment template ...") + + var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) + s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) + s.say(fmt.Sprintf(" -> DeploymentName : '%s'", s.name)) + + return processStepResult( + s.deploy(ctx, resourceGroupName, s.name), + s.error, state) +} + +func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { + defer s.deleteTemplate(context.Background(), state) + + //Only clean up if this was an existing resource group and the resource group + //is marked as created + existingResourceGroup := state.Get(constants.ArmIsExistingResourceGroup).(bool) + resourceGroupCreated := state.Get(constants.ArmIsResourceGroupCreated).(bool) + if !existingResourceGroup || !resourceGroupCreated { + return + } + + ui := state.Get("ui").(packer.Ui) + ui.Say("\nThe resource group was not created by Packer, deleting individual resources ...") + + resourceGroupName := state.Get(constants.ArmResourceGroupName).(string) + computeName := state.Get(constants.ArmComputeName).(string) + deploymentName := s.name + imageType, imageName, err := s.disk(context.TODO(), resourceGroupName, computeName) + if err != nil { + ui.Error("Could not retrieve OS Image details") + } + + ui.Say(" -> Deployment Resources within: " + deploymentName) + if deploymentName != "" { + maxResources := int32(50) + deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(context.TODO(), resourceGroupName, deploymentName, &maxResources) + if err != nil { + ui.Error(fmt.Sprintf("Error deleting resources. Please delete them manually.\n\n"+ + "Name: %s\n"+ + "Error: %s", resourceGroupName, err)) + } + + for deploymentOperations.NotDone() { + deploymentOperation := deploymentOperations.Value() + // Sometimes an empty operation is added to the list by Azure + if deploymentOperation.Properties.TargetResource == nil { + deploymentOperations.Next() + continue + } + + ui.Say(fmt.Sprintf(" -> %s : '%s'", + *deploymentOperation.Properties.TargetResource.ResourceType, + *deploymentOperation.Properties.TargetResource.ResourceName)) + + err = s.delete(context.TODO(), s.client, + *deploymentOperation.Properties.TargetResource.ResourceType, + *deploymentOperation.Properties.TargetResource.ResourceName, + resourceGroupName) + if err != nil { + ui.Error(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+ + "Name: %s\n"+ + "Error: %s", *deploymentOperation.Properties.TargetResource.ResourceName, err)) + } + + if err = deploymentOperations.Next(); err != nil { + ui.Error(fmt.Sprintf("Error deleting resources. Please delete them manually.\n\n"+ + "Name: %s\n"+ + "Error: %s", resourceGroupName, err)) + break + } + } + + // The disk is not defined as an operation in the template so has to be + // deleted separately + ui.Say(fmt.Sprintf(" -> %s : '%s'", imageType, imageName)) + err = s.deleteDisk(context.TODO(), imageType, imageName, resourceGroupName) + if err != nil { + ui.Error(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+ + "Name: %s\n"+ + "Error: %s", imageName, err)) + } + } +} + func (s *StepDeployTemplate) deployTemplate(ctx context.Context, resourceGroupName string, deploymentName string) error { deployment, err := s.factory(s.config) if err != nil { @@ -62,12 +148,13 @@ func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, state multistep var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) var deploymentName = s.name ui := state.Get("ui").(packer.Ui) - ui.Say(fmt.Sprintf("Removing the created Deployment object: '%s'", deploymentName)) + ui.Say(fmt.Sprintf("Removing the created Deployment object: '%s'", deploymentName)) f, err := s.client.DeploymentsClient.Delete(ctx, resourceGroupName, deploymentName) if err == nil { err = f.WaitForCompletionRef(ctx, s.client.DeploymentsClient.Client) } + if err != nil { s.say(s.client.LastError.Error()) } @@ -75,19 +162,6 @@ func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, state multistep return err } -func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - s.say("Deploying deployment template ...") - - var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) - - s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) - s.say(fmt.Sprintf(" -> DeploymentName : '%s'", s.name)) - - return processStepResult( - s.deploy(ctx, resourceGroupName, s.name), - s.error, state) -} - func (s *StepDeployTemplate) getImageDetails(ctx context.Context, resourceGroupName string, computeName string) (string, string, error) { //We can't depend on constants.ArmOSDiskVhd being set var imageName string @@ -174,72 +248,3 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageType string, err = blob.Delete(nil) return err } - -func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { - defer s.deleteTemplate(context.Background(), state) - - //Only clean up if this was an existing resource group and the resource group - //is marked as created - var existingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool) - var resourceGroupCreated = state.Get(constants.ArmIsResourceGroupCreated).(bool) - if !existingResourceGroup || !resourceGroupCreated { - return - } - ui := state.Get("ui").(packer.Ui) - ui.Say("\nThe resource group was not created by Packer, deleting individual resources ...") - - var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) - var computeName = state.Get(constants.ArmComputeName).(string) - var deploymentName = s.name - imageType, imageName, err := s.disk(context.TODO(), resourceGroupName, computeName) - if err != nil { - ui.Error("Could not retrieve OS Image details") - } - - ui.Say(" -> Deployment Resources within: " + deploymentName) - if deploymentName != "" { - maxResources := int32(50) - deploymentOperations, err := s.client.DeploymentOperationsClient.ListComplete(context.TODO(), resourceGroupName, deploymentName, &maxResources) - if err != nil { - ui.Error(fmt.Sprintf("Error deleting resources. Please delete them manually.\n\n"+ - "Name: %s\n"+ - "Error: %s", resourceGroupName, err)) - } - for deploymentOperations.NotDone() { - deploymentOperation := deploymentOperations.Value() - // Sometimes an empty operation is added to the list by Azure - if deploymentOperation.Properties.TargetResource == nil { - deploymentOperations.Next() - continue - } - ui.Say(fmt.Sprintf(" -> %s : '%s'", - *deploymentOperation.Properties.TargetResource.ResourceType, - *deploymentOperation.Properties.TargetResource.ResourceName)) - err = s.delete(context.TODO(), s.client, - *deploymentOperation.Properties.TargetResource.ResourceType, - *deploymentOperation.Properties.TargetResource.ResourceName, - resourceGroupName) - if err != nil { - ui.Error(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+ - "Name: %s\n"+ - "Error: %s", *deploymentOperation.Properties.TargetResource.ResourceName, err)) - } - if err = deploymentOperations.Next(); err != nil { - ui.Error(fmt.Sprintf("Error deleting resources. Please delete them manually.\n\n"+ - "Name: %s\n"+ - "Error: %s", resourceGroupName, err)) - break - } - } - - // The disk is not defined as an operation in the template so has to be - // deleted separately - ui.Say(fmt.Sprintf(" -> %s : '%s'", imageType, imageName)) - err = s.deleteDisk(context.TODO(), imageType, imageName, resourceGroupName) - if err != nil { - ui.Error(fmt.Sprintf("Error deleting resource. Please delete manually.\n\n"+ - "Name: %s\n"+ - "Error: %s", imageName, err)) - } - } -}