Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 21 additions & 4 deletions builder/azure/arm/step_delete_additional_disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,30 @@ func (s *StepDeleteAdditionalDisk) Run(ctx context.Context, state multistep.Stat
var isExistingResourceGroup = state.Get(constants.ArmIsExistingResourceGroup).(bool)
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)

if dataDisks == nil {
s.say(fmt.Sprintf(" -> No Additional Disks specified"))
if isManagedDisk && !isExistingResourceGroup {
s.say(fmt.Sprintf(" -> Additional Disk : skipping, will "))
Comment thread
alexeldeib marked this conversation as resolved.
Outdated
return multistep.ActionContinue
}

if isManagedDisk && !isExistingResourceGroup {
s.say(fmt.Sprintf(" -> Additional Disk : skipping, managed disk was used..."))
if isManagedDisk {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think part of this confusion around where we can delete this disk is that I moved the call to NewStepDeleteAddtionalDisk into the StepDeployTemplate#Cleanup but in reality it should be its own thing.

While testing this today with a Windows OS build, non-managed, I was getting failures due to NewStepDeleteAddtionalDisk being called twice. After further investigation I found that for Windows builds NewStepDeployTemplate happens twice: one for the key vault deployment, and one for the virtual machine deployment. This issue only occurs for VHD builds.

Your solution for deleting the OS disk works, but still ends in a failed build because of the double call. I know what I'm pointing out is a different issue than the one you are fixing, but I believe they are related and if we move NewStepDeleteAddtionalDisk out of StepDeployTemplate#Cleanup then we can use your logic for deleting the OS disk or even the original code where we capture the os type and uri to be used for deletion in the cleanup function.

https://github.com/hashicorp/packer/pull/9559/files#diff-75e07109af7dc9db6f9e087b685e273810d56c6c48f04c153c2300e2d17c094eR134-R144

I moved NewStepDeleteAddtionalDisk into the Deployment cleanup step because it was previously being called after the Publish to SIG step, and if that failed the deletion would not happen. So my thought was to ensure the cleanup always happens where they are created - in StepDeployTemplate.

Here's a gist of the template I am using.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice! That takes care of the ordering issue between the cleanup steps very cleanly. I'll incorporate your patch and give it a try on my end 👍

s.say("Deleting the temporary managed OS disk ...")
var vhdUri interface{}
var ok bool
if vhdUri, ok = state.GetOk(constants.ArmOSDiskVhd); ok == false {
msg := "Failed to find VHD URI in state bag!"
s.say(msg)
return processStepResult(errors.New(msg), s.error, state)
}
vhdUriString := vhdUri.(string)

if err := s.deleteManaged(ctx, resourceGroupName, vhdUriString); err != nil {
s.say("Failed to delete the temporary managed OS Disk!")
return processStepResult(err, s.error, state)
}
}

if dataDisks == nil {
s.say(fmt.Sprintf(" -> No Additional Disks specified"))
return multistep.ActionContinue
}

Expand Down
17 changes: 14 additions & 3 deletions builder/azure/arm/step_delete_additional_disks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,26 @@ func TestStepDeleteAdditionalDiskShouldFailIfManagedDiskInExistingResourceGroupF
}
}

func TestStepDeleteAdditionalDiskShouldFailIfManagedDiskInExistingResourceGroupIsDeleted(t *testing.T) {
func TestStepDeleteAdditionalDiskShouldPassIfManagedOsDiskInExistingResourceGroupIsDeleted(t *testing.T) {
var count int
deleteManaged := func(context.Context, string, string) error {
count++
return nil
}

var testSubject = &StepDeleteAdditionalDisk{
delete: func(string, string) error { return nil },
say: func(message string) {},
error: func(e error) {},
deleteManaged: func(context.Context, string, string) error { return nil },
deleteManaged: deleteManaged,
}

stateBag := new(multistep.BasicStateBag)
stateBag.Put(constants.ArmAdditionalDiskVhds, []string{"subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk"})
stateBag.Put(constants.ArmAdditionalDiskVhds, []string{"subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/datadisk"})
stateBag.Put(constants.ArmIsManagedImage, true)
stateBag.Put(constants.ArmIsExistingResourceGroup, true)
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")

var result = testSubject.Run(context.Background(), stateBag)
if result != multistep.ActionContinue {
Expand All @@ -214,6 +221,10 @@ func TestStepDeleteAdditionalDiskShouldFailIfManagedDiskInExistingResourceGroupI
if _, ok := stateBag.GetOk(constants.Error); ok == true {
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
}

if count != 2 {
t.Fatalf("Expected to delete OS and data disk in existing resource group, but only deleted %d disks.", count)
}
}

func DeleteTestStateBagStepDeleteAdditionalDisk(osDiskVhds []string) multistep.StateBag {
Expand Down