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

47 lines
1.4 KiB
Go
Raw Normal View History

2019-06-13 03:16:49 -04:00
package uhost
import (
"context"
2019-06-19 09:32:33 -04:00
"fmt"
2020-11-17 19:31:03 -05:00
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/packer"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
2019-06-13 03:16:49 -04:00
)
type stepCheckSourceImageId struct {
SourceUHostImageId string
}
func (s *stepCheckSourceImageId) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
2019-10-12 04:46:21 -04:00
client := state.Get("client").(*ucloudcommon.UCloudClient)
2019-06-13 03:16:49 -04:00
ui.Say("Querying source image id...")
imageSet, err := client.DescribeImageById(s.SourceUHostImageId)
if err != nil {
2019-10-12 04:46:21 -04:00
if ucloudcommon.IsNotFoundError(err) {
return ucloudcommon.Halt(state, err, "")
2019-06-13 03:16:49 -04:00
}
2019-10-12 04:46:21 -04:00
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
2019-06-13 03:16:49 -04:00
}
2019-10-12 04:46:21 -04:00
if imageSet.OsType == ucloudcommon.OsTypeWindows {
return ucloudcommon.Halt(state, err, "The ucloud-uhost builder does not support Windows images yet")
2019-06-13 03:16:49 -04:00
}
_, uOK := state.GetOk("user_data")
_, fOK := state.GetOk("user_data_file")
if uOK || fOK {
if !ucloudcommon.IsStringIn("CloudInit", imageSet.Features) {
return ucloudcommon.Halt(state, err, fmt.Sprintf("The image %s must have %q feature when set the %q or %q, got %#v", imageSet.ImageId, "CloudInit", "user_data", "user_data_file", imageSet.Features))
}
}
2019-06-13 03:16:49 -04:00
state.Put("source_image", imageSet)
return multistep.ActionContinue
}
func (s *stepCheckSourceImageId) Cleanup(multistep.StateBag) {}