-
Notifications
You must be signed in to change notification settings - Fork 3.3k
clean up azure temporary managed os disk #10713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4388695
clean up temporary managed os disk
alexeldeib ab88422
improve message for skipping disk deletion
alexeldeib 6ef856a
re-arrange osdisk/additional disk cleanup
alexeldeib 3847c86
remove additional disk cleanup
alexeldeib d93d0ac
add some validation on scenarios
alexeldeib a007898
alway clean up resources inside template cleanup
alexeldeib 0e769ca
tidy to master
alexeldeib c385f24
clarify naming and comments
alexeldeib a96d4d4
test: add acceptance testing for azure cleanup
alexeldeib ac10575
Merge branch 'ace/removeOsDisk' of github.com:alexeldeib/packer into …
alexeldeib 74f6e1f
revert template changes
alexeldeib bae16d9
fix err check
alexeldeib 1b2c36a
delete resources in parallel with retry to avoid serial deletion
alexeldeib bc39c32
nit: improve log message for transient deletion errors
alexeldeib 2f72108
fix: typo
alexeldeib a2957d2
remove unused func to make lint happy
alexeldeib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,16 +15,17 @@ import ( | |
| ) | ||
|
|
||
| type StepDeployTemplate struct { | ||
| client *AzureClient | ||
| deploy func(ctx context.Context, resourceGroupName string, deploymentName string) error | ||
| delete func(ctx context.Context, deploymentName, resourceGroupName string) error | ||
| disk func(ctx context.Context, resourceGroupName string, computeName string) (string, string, error) | ||
| deleteDisk func(ctx context.Context, imageType string, imageName string, resourceGroupName string) error | ||
| say func(message string) | ||
| error func(e error) | ||
| config *Config | ||
| factory templateFactoryFunc | ||
| name string | ||
| client *AzureClient | ||
| deploy func(ctx context.Context, resourceGroupName string, deploymentName string) error | ||
| delete func(ctx context.Context, deploymentName, resourceGroupName string) error | ||
| disk func(ctx context.Context, resourceGroupName string, computeName string) (string, string, error) | ||
| deleteDisk func(ctx context.Context, imageType string, imageName string, resourceGroupName string) error | ||
| deleteDeployment func(ctx context.Context, state multistep.StateBag) error | ||
| say func(message string) | ||
| error func(e error) | ||
| config *Config | ||
| factory templateFactoryFunc | ||
| name string | ||
| } | ||
|
|
||
| func NewStepDeployTemplate(client *AzureClient, ui packersdk.Ui, config *Config, deploymentName string, factory templateFactoryFunc) *StepDeployTemplate { | ||
|
|
@@ -41,6 +42,7 @@ func NewStepDeployTemplate(client *AzureClient, ui packersdk.Ui, config *Config, | |
| step.delete = step.deleteDeploymentResources | ||
| step.disk = step.getImageDetails | ||
| step.deleteDisk = step.deleteImage | ||
| step.deleteDeployment = step.deleteDeploymentObject | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice add. The name provides more details for sure. |
||
| return step | ||
| } | ||
|
|
||
|
|
@@ -58,32 +60,45 @@ func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) | |
|
|
||
| func (s *StepDeployTemplate) Cleanup(state multistep.StateBag) { | ||
| defer func() { | ||
| err := s.deleteTemplate(context.Background(), state) | ||
| err := s.deleteDeployment(context.Background(), state) | ||
| if err != nil { | ||
| s.say(s.client.LastError.Error()) | ||
| s.say(err.Error()) | ||
| } | ||
| }() | ||
|
|
||
| // Only clean up if this is an existing resource group that has been verified to exist. | ||
| // ArmIsResourceGroupCreated is set in step_create_resource_group to true, when Packer has verified that the resource group exists. | ||
| // ArmIsExistingResourceGroup is set to true when build_resource_group is set in the Packer configuration. | ||
| existingResourceGroup := state.Get(constants.ArmIsExistingResourceGroup).(bool) | ||
| resourceGroupCreated := state.Get(constants.ArmIsResourceGroupCreated).(bool) | ||
| if !existingResourceGroup || !resourceGroupCreated { | ||
| return | ||
| } | ||
|
|
||
| ui := state.Get("ui").(packersdk.Ui) | ||
| ui.Say("\nThe resource group was not created by Packer, deleting individual resources ...") | ||
| ui.Say("\nDeleting individual resources ...") | ||
|
|
||
| deploymentName := s.name | ||
| resourceGroupName := state.Get(constants.ArmResourceGroupName).(string) | ||
| err := s.deleteDeploymentResources(context.TODO(), deploymentName, resourceGroupName) | ||
| // 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) | ||
|
|
||
| if err != nil && !strings.Contains(err.Error(), "ResourceNotFound") { | ||
| ui.Error(fmt.Sprintf("Could not retrieve OS Image details: %s", err)) | ||
| } | ||
| err = s.delete(context.TODO(), deploymentName, resourceGroupName) | ||
| if err != nil { | ||
| s.reportIfError(err, resourceGroupName) | ||
| } | ||
|
|
||
| NewStepDeleteAdditionalDisks(s.client, ui).Run(context.TODO(), state) | ||
| // The disk was not found on the VM, this is an error. | ||
| if imageType == "" && imageName == "" { | ||
| ui.Error(fmt.Sprintf("Failed to find temporary OS disk on VM. Please delete manually.\n\n"+ | ||
| "VM Name: %s\n"+ | ||
| "Error: %s", computeName, err)) | ||
| return | ||
| } | ||
|
|
||
| ui.Say(fmt.Sprintf(" Deleting -> %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 { | ||
|
|
@@ -106,7 +121,7 @@ func (s *StepDeployTemplate) deployTemplate(ctx context.Context, resourceGroupNa | |
| return err | ||
| } | ||
|
|
||
| func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, state multistep.StateBag) error { | ||
| func (s *StepDeployTemplate) deleteDeploymentObject(ctx context.Context, state multistep.StateBag) error { | ||
| deploymentName := s.name | ||
| resourceGroupName := state.Get(constants.ArmResourceGroupName).(string) | ||
| ui := state.Get("ui").(packersdk.Ui) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.