packer-cn/builder/ucloud/uhost/step_create_image.go

106 lines
3.2 KiB
Go
Raw Normal View History

2019-06-13 03:16:49 -04:00
package uhost
import (
"context"
"fmt"
2019-10-12 04:46:21 -04:00
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
2019-06-13 03:16:49 -04:00
"github.com/hashicorp/packer/common/retry"
"time"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
"github.com/ucloud/ucloud-sdk-go/ucloud"
)
type stepCreateImage struct {
image *uhost.UHostImageSet
}
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2019-10-12 04:46:21 -04:00
client := state.Get("client").(*ucloudcommon.UCloudClient)
conn := client.UHostConn
2019-06-13 03:16:49 -04:00
instance := state.Get("instance").(*uhost.UHostInstanceSet)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
2019-07-26 05:03:57 -04:00
ui.Say(fmt.Sprintf("Creating image %s...", config.ImageName))
2019-06-13 03:16:49 -04:00
req := conn.NewCreateCustomImageRequest()
req.ImageName = ucloud.String(config.ImageName)
req.ImageDescription = ucloud.String(config.ImageDescription)
req.UHostId = ucloud.String(instance.UHostId)
resp, err := conn.CreateCustomImage(req)
if err != nil {
2019-10-12 04:46:21 -04:00
return ucloudcommon.Halt(state, err, "Error on creating image")
2019-06-13 03:16:49 -04:00
}
2019-07-26 05:03:57 -04:00
ui.Message(fmt.Sprintf("Waiting for the created image %q to become available...", resp.ImageId))
2019-06-13 03:16:49 -04:00
err = retry.Config{
2019-10-18 02:52:20 -04:00
StartTimeout: time.Duration(config.WaitImageReadyTimeout) * time.Second,
2019-06-13 03:16:49 -04:00
ShouldRetry: func(err error) bool {
2019-10-12 04:46:21 -04:00
return ucloudcommon.IsExpectedStateError(err)
2019-06-13 03:16:49 -04:00
},
RetryDelay: (&retry.Backoff{InitialBackoff: 2 * time.Second, MaxBackoff: 12 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error {
inst, err := client.DescribeImageById(resp.ImageId)
if err != nil {
return err
}
2019-10-12 04:46:21 -04:00
if inst == nil || inst.State != ucloudcommon.ImageStateAvailable {
return ucloudcommon.NewExpectedStateError("image", resp.ImageId)
2019-06-13 03:16:49 -04:00
}
return nil
})
if err != nil {
2019-10-12 04:46:21 -04:00
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on waiting for image %q to become available", resp.ImageId))
2019-06-13 03:16:49 -04:00
}
imageSet, err := client.DescribeImageById(resp.ImageId)
if err != nil {
2019-10-12 04:46:21 -04:00
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on reading image when creating %q", resp.ImageId))
2019-06-13 03:16:49 -04:00
}
s.image = imageSet
state.Put("image_id", imageSet.ImageId)
2019-10-12 04:46:21 -04:00
images := []ucloudcommon.ImageInfo{
2019-06-13 03:16:49 -04:00
{
ImageId: imageSet.ImageId,
ProjectId: config.ProjectId,
Region: config.Region,
},
}
2019-10-12 04:46:21 -04:00
state.Put("ucloud_images", ucloudcommon.NewImageInfoSet(images))
2019-06-19 09:32:33 -04:00
ui.Message(fmt.Sprintf("Creating image %q complete", imageSet.ImageId))
2019-06-13 03:16:49 -04:00
return multistep.ActionContinue
}
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
if s.image == nil {
return
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
2019-10-12 04:46:21 -04:00
client := state.Get("client").(*ucloudcommon.UCloudClient)
conn := client.UHostConn
2019-06-13 03:16:49 -04:00
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting image because of cancellation or error...")
req := conn.NewTerminateCustomImageRequest()
req.ImageId = ucloud.String(s.image.ImageId)
_, err := conn.TerminateCustomImage(req)
if err != nil {
ui.Error(fmt.Sprintf("Error on deleting image %q", s.image.ImageId))
}
2019-06-19 09:32:33 -04:00
ui.Message(fmt.Sprintf("Deleting image %q complete", s.image.ImageId))
2019-06-13 03:16:49 -04:00
}