From 9fabad6f1dd8f2718467085a841c89f1612179ca Mon Sep 17 00:00:00 2001 From: Roman Mingazeev Date: Mon, 7 Dec 2020 18:27:22 +0300 Subject: [PATCH] rebase && some fixes --- builder/yandex/common_config.go | 8 ++++++ builder/yandex/config.go | 8 ------ builder/yandex/config.hcl2spec.go | 4 +-- .../yandex-export/post-processor.go | 25 ++++++++++++++----- .../yandex-export/post-processor.hcl2spec.go | 2 ++ .../yandex/CommonConfig-not-required.mdx | 3 +++ .../builder/yandex/Config-not-required.mdx | 3 --- 7 files changed, 34 insertions(+), 19 deletions(-) diff --git a/builder/yandex/common_config.go b/builder/yandex/common_config.go index bec224a3a..8ff19c69d 100644 --- a/builder/yandex/common_config.go +++ b/builder/yandex/common_config.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "regexp" + "time" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate" @@ -25,6 +26,9 @@ type CommonConfig struct { // File path to save serial port output of the launched instance. SerialLogFile string `mapstructure:"serial_log_file" required:"false"` + // The time to wait for instance state changes. + // Defaults to `5m`. + StateTimeout time.Duration `mapstructure:"state_timeout" required:"false"` InstanceConfig `mapstructure:",squash"` DiskConfig `mapstructure:",squash"` @@ -41,6 +45,10 @@ func (c *CommonConfig) Prepare(errs *packersdk.MultiError) *packersdk.MultiError } } + if c.StateTimeout == 0 { + c.StateTimeout = 5 * time.Minute + } + errs = c.CloudConfig.Prepare(errs) errs = c.InstanceConfig.Prepare(errs) errs = c.DiskConfig.Prepare(errs) diff --git a/builder/yandex/config.go b/builder/yandex/config.go index 2b11a4ccd..fb54bbcab 100644 --- a/builder/yandex/config.go +++ b/builder/yandex/config.go @@ -6,7 +6,6 @@ package yandex import ( "errors" "fmt" - "time" "github.com/hashicorp/packer/packer-plugin-sdk/common" "github.com/hashicorp/packer/packer-plugin-sdk/communicator" @@ -42,9 +41,6 @@ type Config struct { TargetImageFolderID string `mapstructure:"target_image_folder_id" required:"false"` ctx interpolate.Context - // The time to wait for instance state changes. - // Defaults to `5m`. - StateTimeout time.Duration `mapstructure:"state_timeout" required:"false"` } func (c *Config) Prepare(raws ...interface{}) ([]string, error) { @@ -99,10 +95,6 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { c.TargetImageFolderID = c.FolderID } - if c.StateTimeout == 0 { - c.StateTimeout = 5 * time.Minute - } - // Check for any errors. if errs != nil && len(errs.Errors) > 0 { return nil, errs diff --git a/builder/yandex/config.hcl2spec.go b/builder/yandex/config.hcl2spec.go index 24e070c5c..3e0ec7661 100644 --- a/builder/yandex/config.hcl2spec.go +++ b/builder/yandex/config.hcl2spec.go @@ -71,6 +71,7 @@ type FlatConfig struct { Token *string `mapstructure:"token" required:"true" cty:"token" hcl:"token"` MaxRetries *int `mapstructure:"max_retries" cty:"max_retries" hcl:"max_retries"` SerialLogFile *string `mapstructure:"serial_log_file" required:"false" cty:"serial_log_file" hcl:"serial_log_file"` + StateTimeout *string `mapstructure:"state_timeout" required:"false" cty:"state_timeout" hcl:"state_timeout"` InstanceCores *int `mapstructure:"instance_cores" required:"false" cty:"instance_cores" hcl:"instance_cores"` InstanceGpus *int `mapstructure:"instance_gpus" required:"false" cty:"instance_gpus" hcl:"instance_gpus"` InstanceMemory *int `mapstructure:"instance_mem_gb" required:"false" cty:"instance_mem_gb" hcl:"instance_mem_gb"` @@ -102,7 +103,6 @@ type FlatConfig struct { SourceImageID *string `mapstructure:"source_image_id" required:"false" cty:"source_image_id" hcl:"source_image_id"` SourceImageName *string `mapstructure:"source_image_name" cty:"source_image_name" hcl:"source_image_name"` TargetImageFolderID *string `mapstructure:"target_image_folder_id" required:"false" cty:"target_image_folder_id" hcl:"target_image_folder_id"` - StateTimeout *string `mapstructure:"state_timeout" required:"false" cty:"state_timeout" hcl:"state_timeout"` } // FlatMapstructure returns a new FlatConfig. @@ -179,6 +179,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "token": &hcldec.AttrSpec{Name: "token", Type: cty.String, Required: false}, "max_retries": &hcldec.AttrSpec{Name: "max_retries", Type: cty.Number, Required: false}, "serial_log_file": &hcldec.AttrSpec{Name: "serial_log_file", Type: cty.String, Required: false}, + "state_timeout": &hcldec.AttrSpec{Name: "state_timeout", Type: cty.String, Required: false}, "instance_cores": &hcldec.AttrSpec{Name: "instance_cores", Type: cty.Number, Required: false}, "instance_gpus": &hcldec.AttrSpec{Name: "instance_gpus", Type: cty.Number, Required: false}, "instance_mem_gb": &hcldec.AttrSpec{Name: "instance_mem_gb", Type: cty.Number, Required: false}, @@ -210,7 +211,6 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "source_image_id": &hcldec.AttrSpec{Name: "source_image_id", Type: cty.String, Required: false}, "source_image_name": &hcldec.AttrSpec{Name: "source_image_name", Type: cty.String, Required: false}, "target_image_folder_id": &hcldec.AttrSpec{Name: "target_image_folder_id", Type: cty.String, Required: false}, - "state_timeout": &hcldec.AttrSpec{Name: "state_timeout", Type: cty.String, Required: false}, } return s } diff --git a/post-processor/yandex-export/post-processor.go b/post-processor/yandex-export/post-processor.go index 070ba50f7..352158f86 100644 --- a/post-processor/yandex-export/post-processor.go +++ b/post-processor/yandex-export/post-processor.go @@ -9,11 +9,11 @@ import ( "fmt" "log" "strings" - "time" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/yandex" "github.com/hashicorp/packer/packer-plugin-sdk/common" + "github.com/hashicorp/packer/packer-plugin-sdk/communicator" "github.com/hashicorp/packer/packer-plugin-sdk/multistep" "github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" @@ -105,7 +105,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { // through the internal network - we need access // to the global Internet: either through ipv4 or ipv6 // TODO: delete this when access appears - if p.config.UseIPv4Nat && p.config.UseIPv6 == false { + if p.config.UseIPv4Nat == false && p.config.UseIPv6 == false { p.config.UseIPv4Nat = true } p.config.Preemptible = true //? safety @@ -198,9 +198,18 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa "zone": p.config.Zone, }, ) - yandexConfig.InstanceConfig.InstanceName = exporterName - yandexConfig.DiskName = exporterName + if yandexConfig.InstanceConfig.InstanceName == "" { + yandexConfig.InstanceConfig.InstanceName = exporterName + } + if yandexConfig.DiskName == "" { + yandexConfig.DiskName = exporterName + } + errs := yandexConfig.Communicator.Prepare(interpolate.NewContext()) + if len(errs) > 0 { + err := &packersdk.MultiError{Errors: errs} + return nil, false, false, err + } ui.Say(fmt.Sprintf("Validating service_account_id: '%s'...", yandexConfig.ServiceAccountID)) if err := validateServiceAccount(ctx, driver.SDK(), yandexConfig.ServiceAccountID); err != nil { return nil, false, false, err @@ -250,10 +259,15 @@ func ycSaneDefaults(c *Config, md map[string]string) yandex.Config { yandexConfig := yandex.Config{ CommonConfig: c.CommonConfig, AccessConfig: c.AccessConfig, + Communicator: communicator.Config{ + Type: "ssh", + SSH: communicator.SSH{ + SSHUsername: "ubuntu", + }, + }, } if c.SSHPrivateKeyFile != "" { yandexConfig.Communicator.SSH.SSHPrivateKeyFile = c.SSHPrivateKeyFile - yandexConfig.Communicator.SSH.SSHUsername = "ubuntu" } if yandexConfig.Metadata == nil { yandexConfig.Metadata = md @@ -265,7 +279,6 @@ func ycSaneDefaults(c *Config, md map[string]string) yandex.Config { yandexConfig.SourceImageFamily = "ubuntu-1604-lts" yandexConfig.SourceImageFolderID = yandex.StandardImagesFolderID - yandexConfig.StateTimeout = 3 * time.Minute yandexConfig.ServiceAccountID = c.ServiceAccountID diff --git a/post-processor/yandex-export/post-processor.hcl2spec.go b/post-processor/yandex-export/post-processor.hcl2spec.go index d25c1dead..6d575bd05 100644 --- a/post-processor/yandex-export/post-processor.hcl2spec.go +++ b/post-processor/yandex-export/post-processor.hcl2spec.go @@ -22,6 +22,7 @@ type FlatConfig struct { Token *string `mapstructure:"token" required:"true" cty:"token" hcl:"token"` MaxRetries *int `mapstructure:"max_retries" cty:"max_retries" hcl:"max_retries"` SerialLogFile *string `mapstructure:"serial_log_file" required:"false" cty:"serial_log_file" hcl:"serial_log_file"` + StateTimeout *string `mapstructure:"state_timeout" required:"false" cty:"state_timeout" hcl:"state_timeout"` InstanceCores *int `mapstructure:"instance_cores" required:"false" cty:"instance_cores" hcl:"instance_cores"` InstanceGpus *int `mapstructure:"instance_gpus" required:"false" cty:"instance_gpus" hcl:"instance_gpus"` InstanceMemory *int `mapstructure:"instance_mem_gb" required:"false" cty:"instance_mem_gb" hcl:"instance_mem_gb"` @@ -71,6 +72,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "token": &hcldec.AttrSpec{Name: "token", Type: cty.String, Required: false}, "max_retries": &hcldec.AttrSpec{Name: "max_retries", Type: cty.Number, Required: false}, "serial_log_file": &hcldec.AttrSpec{Name: "serial_log_file", Type: cty.String, Required: false}, + "state_timeout": &hcldec.AttrSpec{Name: "state_timeout", Type: cty.String, Required: false}, "instance_cores": &hcldec.AttrSpec{Name: "instance_cores", Type: cty.Number, Required: false}, "instance_gpus": &hcldec.AttrSpec{Name: "instance_gpus", Type: cty.Number, Required: false}, "instance_mem_gb": &hcldec.AttrSpec{Name: "instance_mem_gb", Type: cty.Number, Required: false}, diff --git a/website/pages/partials/builder/yandex/CommonConfig-not-required.mdx b/website/pages/partials/builder/yandex/CommonConfig-not-required.mdx index c0e0ef7d8..65b117798 100644 --- a/website/pages/partials/builder/yandex/CommonConfig-not-required.mdx +++ b/website/pages/partials/builder/yandex/CommonConfig-not-required.mdx @@ -1,3 +1,6 @@ - `serial_log_file` (string) - File path to save serial port output of the launched instance. + +- `state_timeout` (duration string | ex: "1h5m2s") - The time to wait for instance state changes. + Defaults to `5m`. diff --git a/website/pages/partials/builder/yandex/Config-not-required.mdx b/website/pages/partials/builder/yandex/Config-not-required.mdx index 759906a3a..0a5bbcbed 100644 --- a/website/pages/partials/builder/yandex/Config-not-required.mdx +++ b/website/pages/partials/builder/yandex/Config-not-required.mdx @@ -11,6 +11,3 @@ - `target_image_folder_id` (string) - The ID of the folder to save built image in. This defaults to value of 'folder_id'. - -- `state_timeout` (duration string | ex: "1h5m2s") - The time to wait for instance state changes. - Defaults to `5m`.