generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 54
/
step_create_image.go
91 lines (77 loc) · 2.68 KB
/
step_create_image.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package googlecompute
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/packer-plugin-googlecompute/lib/common"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"google.golang.org/api/compute/v1"
)
// StepCreateImage represents a Packer build step that creates GCE machine
// images.
type StepCreateImage int
// Run executes the Packer build step that creates a GCE machine image.
//
// The image is created from the persistent disk used by the instance. The
// instance must be deleted and the disk retained before doing this step.
func (s *StepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(common.Driver)
ui := state.Get("ui").(packersdk.Ui)
if config.SkipCreateImage {
ui.Say("Skipping image creation...")
return multistep.ActionContinue
}
if config.PackerForce && config.imageAlreadyExists {
ui.Say("Deleting previous image...")
errCh := driver.DeleteImage(config.ImageProjectId, config.ImageName)
err := <-errCh
if err != nil {
err := fmt.Errorf("Error deleting image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
ui.Say("Creating image...")
sourceDiskURI := fmt.Sprintf("/compute/v1/projects/%s/zones/%s/disks/%s", config.ProjectId, config.Zone, config.imageSourceDisk)
imageFeatures := make([]*compute.GuestOsFeature, 0, len(config.ImageGuestOsFeatures))
for _, v := range config.ImageGuestOsFeatures {
imageFeatures = append(imageFeatures, &compute.GuestOsFeature{
Type: v,
})
}
imagePayload := &compute.Image{
Description: config.ImageDescription,
Name: config.ImageName,
Family: config.ImageFamily,
Labels: config.ImageLabels,
Licenses: config.ImageLicenses,
GuestOsFeatures: imageFeatures,
ImageEncryptionKey: config.ImageEncryptionKey.ComputeType(),
SourceDisk: sourceDiskURI,
SourceType: "RAW",
StorageLocations: config.ImageStorageLocations,
}
imageCh, errCh := driver.CreateImage(config.ImageProjectId, imagePayload)
var err error
select {
case err = <-errCh:
case <-time.After(config.StateTimeout):
err = errors.New("time out while waiting for image to register")
}
if err != nil {
err := fmt.Errorf("Error waiting for image: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("image", <-imageCh)
return multistep.ActionContinue
}
// Cleanup.
func (s *StepCreateImage) Cleanup(state multistep.StateBag) {}