Update deletion logic for OS disks
* Add an ResourceNotFound error check * Add logic to break temporary disk lease before deleting it
This commit is contained in:
parent
ba6d11d518
commit
d91839b93e
|
@ -221,6 +221,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
NewStepSnapshotDataDisks(azureClient, ui, &b.config),
|
NewStepSnapshotDataDisks(azureClient, ui, &b.config),
|
||||||
NewStepCaptureImage(azureClient, ui),
|
NewStepCaptureImage(azureClient, ui),
|
||||||
NewStepPublishToSharedImageGallery(azureClient, ui, &b.config),
|
NewStepPublishToSharedImageGallery(azureClient, ui, &b.config),
|
||||||
|
NewStepDeleteAdditionalDisks(azureClient, ui),
|
||||||
}
|
}
|
||||||
} else if b.config.OSType == constants.Target_Windows {
|
} else if b.config.OSType == constants.Target_Windows {
|
||||||
steps = []multistep.Step{
|
steps = []multistep.Step{
|
||||||
|
@ -261,6 +262,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
NewStepSnapshotDataDisks(azureClient, ui, &b.config),
|
NewStepSnapshotDataDisks(azureClient, ui, &b.config),
|
||||||
NewStepCaptureImage(azureClient, ui),
|
NewStepCaptureImage(azureClient, ui),
|
||||||
NewStepPublishToSharedImageGallery(azureClient, ui, &b.config),
|
NewStepPublishToSharedImageGallery(azureClient, ui, &b.config),
|
||||||
|
NewStepDeleteAdditionalDisks(azureClient, ui),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("Builder does not support the os_type '%s'", b.config.OSType)
|
return nil, fmt.Errorf("Builder does not support the os_type '%s'", b.config.OSType)
|
||||||
|
|
|
@ -68,12 +68,16 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) {
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
ui.Say("\nThe resource group was not created by Packer, deleting individual resources ...")
|
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
|
deploymentName := s.name
|
||||||
|
resourceGroupName := state.Get(constants.ArmResourceGroupName).(string)
|
||||||
|
|
||||||
|
// Get image disk details before deleting the image; otherwise we won't be able to
|
||||||
|
// delete the disk as the image request will return a 404
|
||||||
|
computeName := state.Get(constants.ArmComputeName).(string)
|
||||||
imageType, imageName, err := s.disk(context.TODO(), resourceGroupName, computeName)
|
imageType, imageName, err := s.disk(context.TODO(), resourceGroupName, computeName)
|
||||||
if err != nil {
|
|
||||||
ui.Error("Could not retrieve OS Image details")
|
if err != nil && !strings.Contains(err.Error(), "ResourceNotFound") {
|
||||||
|
ui.Error(fmt.Sprintf("Could not retrieve OS Image details: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say(" -> Deployment Resources within: " + deploymentName)
|
ui.Say(" -> Deployment Resources within: " + deploymentName)
|
||||||
|
@ -91,7 +95,7 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) {
|
||||||
// Sometimes an empty operation is added to the list by Azure
|
// Sometimes an empty operation is added to the list by Azure
|
||||||
if deploymentOperation.Properties.TargetResource == nil {
|
if deploymentOperation.Properties.TargetResource == nil {
|
||||||
if err := deploymentOperations.Next(); err != nil {
|
if err := deploymentOperations.Next(); err != nil {
|
||||||
ui.Error(fmt.Sprintf("Error deleting resources. Please delete them manually.\n\n"+
|
ui.Error(fmt.Sprintf("Error moving to to next deployment operation ...\n\n"+
|
||||||
"Name: %s\n"+
|
"Name: %s\n"+
|
||||||
"Error: %s", resourceGroupName, err))
|
"Error: %s", resourceGroupName, err))
|
||||||
break
|
break
|
||||||
|
@ -121,8 +125,11 @@ func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The disk is not defined as an operation in the template so has to be
|
// The disk is not defined as an operation in the template so it has to be deleted separately
|
||||||
// deleted separately
|
if imageType == "" && imageName == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf(" -> %s : '%s'", imageType, imageName))
|
ui.Say(fmt.Sprintf(" -> %s : '%s'", imageType, imageName))
|
||||||
err = s.deleteDisk(context.TODO(), imageType, imageName, resourceGroupName)
|
err = s.deleteDisk(context.TODO(), imageType, imageName, resourceGroupName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -237,6 +244,7 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageType string,
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// VHD image
|
// VHD image
|
||||||
u, err := url.Parse(imageName)
|
u, err := url.Parse(imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -250,6 +258,12 @@ func (s *StepDeployTemplate) deleteImage(ctx context.Context, imageType string,
|
||||||
var blobName = strings.Join(xs[2:], "/")
|
var blobName = strings.Join(xs[2:], "/")
|
||||||
|
|
||||||
blob := s.client.BlobStorageClient.GetContainerReference(storageAccountName).GetBlobReference(blobName)
|
blob := s.client.BlobStorageClient.GetContainerReference(storageAccountName).GetBlobReference(blobName)
|
||||||
|
if _, err := blob.BreakLease(nil); err != nil {
|
||||||
|
s.say(s.client.LastError.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = blob.Delete(nil)
|
err = blob.Delete(nil)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue