cleanup image and snapshot if target image is still not available after timeout

This commit is contained in:
bozhi.ch 2019-06-13 11:17:39 +08:00
parent a907adb4ab
commit 6a5db1e948
2 changed files with 15 additions and 8 deletions

View File

@ -161,6 +161,7 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR
timeoutPoint = time.Now().Add(args.RetryTimeout) timeoutPoint = time.Now().Add(args.RetryTimeout)
} }
var lastResponse responses.AcsResponse
var lastError error var lastError error
for i := 0; ; i++ { for i := 0; ; i++ {
@ -173,6 +174,7 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR
} }
response, err := args.RequestFunc() response, err := args.RequestFunc()
lastResponse = response
lastError = err lastError = err
evalResult := args.EvalFunc(response, err) evalResult := args.EvalFunc(response, err)
@ -180,17 +182,21 @@ func (c *ClientWrapper) WaitForExpected(args *WaitForExpectArgs) (responses.AcsR
return response, nil return response, nil
} }
if evalResult.stopRetry { if evalResult.stopRetry {
return nil, err return response, err
} }
time.Sleep(args.RetryInterval) time.Sleep(args.RetryInterval)
} }
if args.RetryTimeout > 0 { if lastError == nil {
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) lastError = fmt.Errorf("<no error>")
} }
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) { func (c *ClientWrapper) WaitForInstanceStatus(regionId string, instanceId string, expectedStatus string) (responses.AcsResponse, error) {

View File

@ -52,17 +52,18 @@ func (s *stepCreateAlicloudImage) Run(ctx context.Context, state multistep.State
imageId := createImageResponse.(*ecs.CreateImageResponse).ImageId imageId := createImageResponse.(*ecs.CreateImageResponse).ImageId
imagesResponse, err := client.WaitForImageStatus(config.AlicloudRegion, imageId, ImageStatusAvailable, time.Duration(s.WaitSnapshotReadyTimeout)*time.Second) 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 images := imagesResponse.(*ecs.DescribeImagesResponse).Images.Image
if len(images) == 0 { if len(images) == 0 {
return halt(state, err, "Unable to find created image") return halt(state, err, "Unable to find created image")
} }
s.image = &images[0] s.image = &images[0]
if err != nil {
return halt(state, err, "Timeout waiting for image to be created")
}
var snapshotIds []string var snapshotIds []string
for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping { for _, device := range images[0].DiskDeviceMappings.DiskDeviceMapping {
snapshotIds = append(snapshotIds, device.SnapshotId) snapshotIds = append(snapshotIds, device.SnapshotId)