File: step-attach-disk.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (100 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package yandexexport

import (
	"context"
	"fmt"

	"github.com/hashicorp/packer/builder/yandex"
	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	"github.com/hashicorp/packer/packer-plugin-sdk/packer"
	"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
)

type StepAttachDisk struct {
	yandex.CommonConfig
	ImageID string
}

func (c *StepAttachDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(yandex.Driver)
	ui := state.Get("ui").(packer.Ui)
	instanceID := state.Get("instance_id").(string)

	ui.Say("Create secondary disk from image for export...")

	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),
		Size:        imageDesc.GetMinDiskSize(),
		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 {
		return yandex.StepHaltWithError(state, fmt.Errorf("could not get Disk ID from create operation metadata"))
	}
	state.Put("secondary_disk_id", md.GetDiskId())

	if err := op.Wait(ctx); err != nil {
		return yandex.StepHaltWithError(state, err)
	}

	ui.Say("Attach secondary disk to instance...")

	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) {
	ui := state.Get("ui").(packer.Ui)
	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())
		}
	}
}