diff --git a/builder/alicloud/ecs/client.go b/builder/alicloud/ecs/client.go index ac599049f..c18a1585c 100644 --- a/builder/alicloud/ecs/client.go +++ b/builder/alicloud/ecs/client.go @@ -161,6 +161,7 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR timeoutPoint = time.Now().Add(args.RetryTimeout) } + var lastResponse responses.AcsResponse var lastError error for i := 0; ; i++ { @@ -173,6 +174,7 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR } response, err := args.RequestFunc() + lastResponse = response lastError = err evalResult := args.EvalFunc(response, err) @@ -180,17 +182,21 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR return response, nil } if evalResult.stopRetry { - return nil, err + return response, err } time.Sleep(args.RetryInterval) } - if args.RetryTimeout > 0 { - return nil, fmt.Errorf("evaluate failed after %d seconds timeout with %d seconds retry interval: %s", int(args.RetryTimeout.Seconds()), int(args.RetryInterval.Seconds()), lastError) + if lastError == nil { + lastError = fmt.Errorf("") } - return nil, fmt.Errorf("evaluate failed after %d times retry with %d seconds retry interval: %s", args.RetryTimes, int(args.RetryInterval.Seconds()), lastError) + if args.RetryTimeout > 0 { + return lastResponse, fmt.Errorf("evaluate failed after %d seconds timeout with %d seconds retry interval: %s", int(args.RetryTimeout.Seconds()), int(args.RetryInterval.Seconds()), lastError) + } + + return lastResponse, fmt.Errorf("evaluate failed after %d times retry with %d seconds retry interval: %s", args.RetryTimes, int(args.RetryInterval.Seconds()), lastError) } func (c *ClientWrapper) WaitForInstanceStatus(regionId string, instanceId string, expectedStatus string) (responses.AcsResponse, error) { diff --git a/builder/alicloud/ecs/step_create_image.go b/builder/alicloud/ecs/step_create_image.go index 6b460d35b..f69842733 100644 --- a/builder/alicloud/ecs/step_create_image.go +++ b/builder/alicloud/ecs/step_create_image.go @@ -52,17 +52,18 @@ func (s *stepCreateAlicloudImage) Run(ctx context.Context, state multistep.State imageId := createImageResponse.(*ecs.CreateImageResponse).ImageId imagesResponse, err := client.WaitForImageStatus(config.AlicloudRegion, imageId, ImageStatusAvailable, time.Duration(s.WaitSnapshotReadyTimeout)*time.Second) - if err != nil { - return halt(state, err, "Timeout waiting for image to be created") - } + // save image first for cleaning up if timeout images := imagesResponse.(*ecs.DescribeImagesResponse).Images.Image if len(images) == 0 { return halt(state, err, "Unable to find created image") } - s.image = &images[0] + if err != nil { + return halt(state, err, "Timeout waiting for image to be created") + } + var snapshotIds []string for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping { snapshotIds = append(snapshotIds, device.SnapshotId)