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/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
@ -31,7 +32,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
if errs != nil {
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
@ -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("hook", hook)
state.Put("ui", ui)
generatedData := &builder.GeneratedData{State: state}
// Build the steps
steps := []multistep.Step{
@ -61,6 +75,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCreateInstance{
Debug: b.config.PackerDebug,
SerialLogFile: b.config.SerialLogFile,
GeneratedData: generatedData,
},
&stepInstanceInfo{},
&communicator.StepConnect{
@ -73,7 +88,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Comm: &b.config.Communicator,
},
&StepTeardownInstance{},
&stepCreateImage{},
&stepCreateImage{
GeneratedData: generatedData,
},
}
// Run the steps

View File

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

View File

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

View File

@ -6,6 +6,7 @@ import (
"fmt"
"log"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -13,9 +14,11 @@ import (
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)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(*Config)
@ -52,7 +55,7 @@ func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multis
image, ok := resp.(*compute.Image)
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)
@ -62,6 +65,14 @@ func (stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multis
log.Printf("Image Storage size: %d", image.StorageSize)
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
}

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"github.com/c2h5oh/datasize"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
@ -21,6 +22,8 @@ const StandardImagesFolderID = "standard-images"
type StepCreateInstance struct {
Debug bool
SerialLogFile string
GeneratedData *builder.GeneratedData
}
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))
}
// 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
}