diff --git a/builder/yandex/builder.go b/builder/yandex/builder.go index 126eafd61..fab9d0861 100644 --- a/builder/yandex/builder.go +++ b/builder/yandex/builder.go @@ -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 diff --git a/builder/yandex/driver_yc.go b/builder/yandex/driver_yc.go index 66d5cd598..40d6923ae 100644 --- a/builder/yandex/driver_yc.go +++ b/builder/yandex/driver_yc.go @@ -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), diff --git a/builder/yandex/image.go b/builder/yandex/image.go index c3d80b7ca..db423cc51 100644 --- a/builder/yandex/image.go +++ b/builder/yandex/image.go @@ -7,6 +7,7 @@ type Image struct { Licenses []string MinDiskSizeGb int Name string + Description string Family string SizeGb int } diff --git a/builder/yandex/step_create_image.go b/builder/yandex/step_create_image.go index d16a1aa5e..12d617354 100644 --- a/builder/yandex/step_create_image.go +++ b/builder/yandex/step_create_image.go @@ -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 } diff --git a/builder/yandex/step_create_instance.go b/builder/yandex/step_create_instance.go index 377d4f0d2..e89ba5269 100644 --- a/builder/yandex/step_create_instance.go +++ b/builder/yandex/step_create_instance.go @@ -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 }