2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateImage struct {
|
|
|
|
imageId string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-12-20 11:09:44 -05:00
|
|
|
client := state.Get("cvm_client").(*cvm.Client)
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
config := state.Get("config").(*Config)
|
2018-12-20 11:09:44 -05:00
|
|
|
instance := state.Get("instance").(*cvm.Instance)
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
Say(state, config.ImageName, "Trying to create a new image")
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
req := cvm.NewCreateImageRequest()
|
|
|
|
req.ImageName = &config.ImageName
|
|
|
|
req.ImageDescription = &config.ImageDescription
|
|
|
|
req.InstanceId = instance.InstanceId
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2019-06-19 05:17:19 -04:00
|
|
|
// TODO: We should allow user to specify which data disk should be
|
|
|
|
// included into created image.
|
|
|
|
var dataDiskIds []*string
|
|
|
|
for _, disk := range instance.DataDisks {
|
|
|
|
dataDiskIds = append(dataDiskIds, disk.DiskId)
|
|
|
|
}
|
|
|
|
if len(dataDiskIds) > 0 {
|
|
|
|
req.DataDiskIds = dataDiskIds
|
|
|
|
}
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
True := "True"
|
|
|
|
False := "False"
|
|
|
|
if config.ForcePoweroff {
|
|
|
|
req.ForcePoweroff = &True
|
|
|
|
} else {
|
|
|
|
req.ForcePoweroff = &False
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Sysprep {
|
|
|
|
req.Sysprep = &True
|
|
|
|
} else {
|
|
|
|
req.Sysprep = &False
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := client.CreateImage(req)
|
|
|
|
return e
|
2019-06-23 11:23:48 -04:00
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to create image")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
Message(state, "Waiting for image ready", "")
|
2018-12-20 11:09:44 -05:00
|
|
|
err = WaitForImageReady(client, config.ImageName, "NORMAL", 3600)
|
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to wait for image ready")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describeReq := cvm.NewDescribeImagesRequest()
|
|
|
|
FILTER_IMAGE_NAME := "image-name"
|
|
|
|
describeReq.Filters = []*cvm.Filter{
|
|
|
|
{
|
|
|
|
Name: &FILTER_IMAGE_NAME,
|
|
|
|
Values: []*string{&config.ImageName},
|
|
|
|
},
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
var describeResp *cvm.DescribeImagesResponse
|
|
|
|
err = Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
describeResp, e = client.DescribeImages(describeReq)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to wait for image ready")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
if *describeResp.Response.TotalCount == 0 {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, fmt.Errorf("No image return"), "Failed to crate image")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
s.imageId = *describeResp.Response.ImageSet[0].ImageId
|
|
|
|
state.Put("image", describeResp.Response.ImageSet[0])
|
2019-10-21 01:21:21 -04:00
|
|
|
Message(state, s.imageId, "Image created")
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
tencentCloudImages := make(map[string]string)
|
|
|
|
tencentCloudImages[config.Region] = s.imageId
|
|
|
|
state.Put("tencentcloudimages", tencentCloudImages)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.imageId == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
ctx := context.TODO()
|
2018-12-20 11:09:44 -05:00
|
|
|
client := state.Get("cvm_client").(*cvm.Client)
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
SayClean(state, "image")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
req := cvm.NewDeleteImagesRequest()
|
|
|
|
req.ImageIds = []*string{&s.imageId}
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := client.DeleteImages(req)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
Error(state, err, fmt.Sprintf("Failed to delete image(%s), please delete it manually", s.imageId))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
}
|