Fill up build variables (aka `generated_data`)

See description at https://packer.io/docs/extending/custom-builders#build-variables
This commit is contained in:
Gennady Lipenkov 2020-07-09 01:46:05 +03:00
parent 4cd1950cd0
commit 378c9746fa
5 changed files with 47 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -31,7 +32,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
if errs != nil { if errs != nil {
return nil, warnings, errs return nil, warnings, errs
} }
return nil, warnings, nil generatedData := []string{
"ImageID",
"ImageName",
"ImageFamily",
"ImageDescription",
"ImageFolderID",
"SourceImageID",
"SourceImageName",
"SourceImageDescription",
"SourceImageFamily",
"SourceImageFolderID",
}
return generatedData, warnings, nil
} }
// Run executes a yandex Packer build and returns a packer.Artifact // Run executes a yandex Packer build and returns a packer.Artifact
@ -51,6 +64,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
state.Put("sdk", driver.SDK()) state.Put("sdk", driver.SDK())
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
generatedData := &builder.GeneratedData{State: state}
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
@ -61,6 +75,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCreateInstance{ &StepCreateInstance{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
SerialLogFile: b.config.SerialLogFile, SerialLogFile: b.config.SerialLogFile,
GeneratedData: generatedData,
}, },
&stepInstanceInfo{}, &stepInstanceInfo{},
&communicator.StepConnect{ &communicator.StepConnect{
@ -73,7 +88,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Comm: &b.config.Communicator, Comm: &b.config.Communicator,
}, },
&StepTeardownInstance{}, &StepTeardownInstance{},
&stepCreateImage{}, &stepCreateImage{
GeneratedData: generatedData,
},
} }
// Run the steps // Run the steps

View File

@ -112,6 +112,7 @@ func (d *driverYC) GetImage(imageID string) (*Image, error) {
Labels: image.Labels, Labels: image.Labels,
Licenses: image.ProductIds, Licenses: image.ProductIds,
Name: image.Name, Name: image.Name,
Description: image.Description,
FolderID: image.FolderId, FolderID: image.FolderId,
MinDiskSizeGb: toGigabytes(image.MinDiskSize), MinDiskSizeGb: toGigabytes(image.MinDiskSize),
SizeGb: toGigabytes(image.StorageSize), SizeGb: toGigabytes(image.StorageSize),
@ -132,6 +133,7 @@ func (d *driverYC) GetImageFromFolder(ctx context.Context, folderID string, fami
Labels: image.Labels, Labels: image.Labels,
Licenses: image.ProductIds, Licenses: image.ProductIds,
Name: image.Name, Name: image.Name,
Description: image.Description,
FolderID: image.FolderId, FolderID: image.FolderId,
Family: image.Family, Family: image.Family,
MinDiskSizeGb: toGigabytes(image.MinDiskSize), MinDiskSizeGb: toGigabytes(image.MinDiskSize),

View File

@ -7,6 +7,7 @@ type Image struct {
Licenses []string Licenses []string
MinDiskSizeGb int MinDiskSizeGb int
Name string Name string
Description string
Family string Family string
SizeGb int SizeGb int
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -13,9 +14,11 @@ import (
ycsdk "github.com/yandex-cloud/go-sdk" ycsdk "github.com/yandex-cloud/go-sdk"
) )
type stepCreateImage struct{} type stepCreateImage struct {
GeneratedData *builder.GeneratedData
}
func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
sdk := state.Get("sdk").(*ycsdk.SDK) sdk := state.Get("sdk").(*ycsdk.SDK)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config) c := state.Get("config").(*Config)
@ -52,7 +55,7 @@ func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multis
image, ok := resp.(*compute.Image) image, ok := resp.(*compute.Image)
if !ok { if !ok {
return stepHaltWithError(state, errors.New("Response doesn't contain Image")) return stepHaltWithError(state, errors.New("API call response doesn't contain Compute Image"))
} }
log.Printf("Image ID: %s", image.Id) log.Printf("Image ID: %s", image.Id)
@ -62,6 +65,14 @@ func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multis
log.Printf("Image Storage size: %d", image.StorageSize) log.Printf("Image Storage size: %d", image.StorageSize)
state.Put("image", image) state.Put("image", image)
// provision generated_data from declared in Builder.Prepare func
// see doc https://www.packer.io/docs/extending/custom-builders#build-variables for details
s.GeneratedData.Put("ImageID", image.Id)
s.GeneratedData.Put("ImageName", image.Name)
s.GeneratedData.Put("ImageFamily", image.Family)
s.GeneratedData.Put("ImageDescription", image.Description)
s.GeneratedData.Put("ImageFolderID", image.FolderId)
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"github.com/c2h5oh/datasize" "github.com/c2h5oh/datasize"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -21,6 +22,8 @@ const StandardImagesFolderID = "standard-images"
type StepCreateInstance struct { type StepCreateInstance struct {
Debug bool Debug bool
SerialLogFile string SerialLogFile string
GeneratedData *builder.GeneratedData
} }
func createNetwork(ctx context.Context, c *Config, d Driver) (*vpc.Network, error) { func createNetwork(ctx context.Context, c *Config, d Driver) (*vpc.Network, error) {
@ -262,6 +265,14 @@ runcmd:
ui.Message(fmt.Sprintf("Disk ID %s. ", instance.BootDisk.DiskId)) ui.Message(fmt.Sprintf("Disk ID %s. ", instance.BootDisk.DiskId))
} }
// provision generated_data from declared in Builder.Prepare func
// see doc https://www.packer.io/docs/extending/custom-builders#build-variables for details
s.GeneratedData.Put("SourceImageID", sourceImage.ID)
s.GeneratedData.Put("SourceImageName", sourceImage.Name)
s.GeneratedData.Put("SourceImageDescription", sourceImage.Description)
s.GeneratedData.Put("SourceImageFamily", sourceImage.Family)
s.GeneratedData.Put("SourceImageFolderID", sourceImage.FolderID)
return multistep.ActionContinue return multistep.ActionContinue
} }