2020-12-10 06:52:22 -05:00
|
|
|
package yandexexport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2021-01-15 04:51:44 -05:00
|
|
|
"github.com/c2h5oh/datasize"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2021-01-13 06:14:06 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-12-10 06:52:22 -05:00
|
|
|
"github.com/hashicorp/packer/builder/yandex"
|
|
|
|
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepAttachDisk struct {
|
|
|
|
yandex.CommonConfig
|
2021-01-15 04:51:44 -05:00
|
|
|
ImageID string
|
|
|
|
ExtraSize int
|
2020-12-10 06:52:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StepAttachDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(yandex.Driver)
|
2021-01-13 06:14:06 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-12-10 06:52:22 -05:00
|
|
|
instanceID := state.Get("instance_id").(string)
|
|
|
|
|
2020-12-10 07:17:09 -05:00
|
|
|
ui.Say("Create secondary disk from image for export...")
|
2020-12-10 06:52:22 -05:00
|
|
|
|
|
|
|
imageDesc, err := driver.SDK().Compute().Image().Get(ctx, &compute.GetImageRequest{
|
|
|
|
ImageId: c.ImageID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := driver.SDK().WrapOperation(driver.SDK().Compute().Disk().Create(ctx, &compute.CreateDiskRequest{
|
|
|
|
Source: &compute.CreateDiskRequest_ImageId{
|
|
|
|
ImageId: c.ImageID,
|
|
|
|
},
|
|
|
|
Name: fmt.Sprintf("export-%s-disk", instanceID),
|
2021-01-15 04:51:44 -05:00
|
|
|
Size: int64(datasize.ByteSize(c.ExtraSize)*datasize.GB) + imageDesc.GetMinDiskSize(),
|
2020-12-10 06:52:22 -05:00
|
|
|
ZoneId: c.Zone,
|
|
|
|
FolderId: c.FolderID,
|
|
|
|
TypeId: c.DiskType,
|
|
|
|
Description: "Temporary disk for exporting",
|
|
|
|
}))
|
|
|
|
if op == nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
protoMD, err := op.Metadata()
|
|
|
|
if err != nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
md, ok := protoMD.(*compute.CreateDiskMetadata)
|
|
|
|
if !ok {
|
2020-12-10 07:17:21 -05:00
|
|
|
return yandex.StepHaltWithError(state, fmt.Errorf("could not get Disk ID from create operation metadata"))
|
2020-12-10 06:52:22 -05:00
|
|
|
}
|
|
|
|
state.Put("secondary_disk_id", md.GetDiskId())
|
|
|
|
|
|
|
|
if err := op.Wait(ctx); err != nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:17:27 -05:00
|
|
|
ui.Say("Attach secondary disk to instance...")
|
2020-12-10 06:52:22 -05:00
|
|
|
|
|
|
|
op, err = driver.SDK().WrapOperation(driver.SDK().Compute().Instance().AttachDisk(ctx, &compute.AttachInstanceDiskRequest{
|
|
|
|
InstanceId: instanceID,
|
|
|
|
AttachedDiskSpec: &compute.AttachedDiskSpec{
|
|
|
|
AutoDelete: true,
|
|
|
|
DeviceName: "doexport",
|
|
|
|
Disk: &compute.AttachedDiskSpec_DiskId{
|
|
|
|
DiskId: md.GetDiskId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
ui.Message("Wait attached disk...")
|
|
|
|
if err := op.Wait(ctx); err != nil {
|
|
|
|
return yandex.StepHaltWithError(state, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Remove("secondary_disk_id")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAttachDisk) Cleanup(state multistep.StateBag) {
|
2021-01-13 06:14:06 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-12-10 06:52:22 -05:00
|
|
|
driver := state.Get("driver").(yandex.Driver)
|
|
|
|
if diskID, ok := state.GetOk("secondary_disk_id"); ok {
|
|
|
|
ui.Say("Remove the secondary disk...")
|
|
|
|
op, err := driver.SDK().WrapOperation(driver.SDK().Compute().Disk().Delete(context.Background(), &compute.DeleteDiskRequest{
|
|
|
|
DiskId: diskID.(string),
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := op.Wait(context.Background()); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|