From b9411712eb29e8092849aada71450cb9d9c7f853 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 25 Feb 2019 15:03:11 +0100 Subject: [PATCH] builder/openstack/step_create_image.go: pass context for cancellation fix #7325 --- builder/openstack/step_create_image.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/builder/openstack/step_create_image.go b/builder/openstack/step_create_image.go index 608a4a818..c50d3eb3e 100644 --- a/builder/openstack/step_create_image.go +++ b/builder/openstack/step_create_image.go @@ -19,7 +19,7 @@ type stepCreateImage struct { UseBlockStorageVolume bool } -func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { +func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) server := state.Get("server").(*servers.Server) ui := state.Get("ui").(packer.Ui) @@ -84,7 +84,7 @@ func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multi // Wait for the image to become ready ui.Say(fmt.Sprintf("Waiting for image %s (image id: %s) to become ready...", config.ImageName, imageId)) - if err := WaitForImage(imageClient, imageId); err != nil { + if err := WaitForImage(ctx, imageClient, imageId); err != nil { err := fmt.Errorf("Error waiting for image: %s", err) state.Put("error", err) ui.Error(err.Error()) @@ -99,11 +99,14 @@ func (s *stepCreateImage) Cleanup(multistep.StateBag) { } // WaitForImage waits for the given Image ID to become ready. -func WaitForImage(client *gophercloud.ServiceClient, imageId string) error { +func WaitForImage(ctx context.Context, client *gophercloud.ServiceClient, imageId string) error { maxNumErrors := 10 numErrors := 0 for { + if err := ctx.Err(); err != nil { + return err + } image, err := images.Get(client, imageId).Extract() if err != nil { errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode)