Validate virtualbox_version_file and guest_additions_mode when communicator is none

This commit is contained in:
Vadym Haidamaka 2019-03-16 18:15:27 +02:00
parent 8853fc1946
commit 0263503c45
5 changed files with 46 additions and 10 deletions

View File

@ -1,9 +0,0 @@
package common
// These are the different valid mode values for "guest_additions_mode" which
// determine how guest additions are delivered to the guest.
const (
GuestAdditionsModeDisable string = "disable"
GuestAdditionsModeAttach = "attach"
GuestAdditionsModeUpload = "upload"
)

View File

@ -0,0 +1,31 @@
package common
import (
"fmt"
"github.com/hashicorp/packer/template/interpolate"
)
// These are the different valid mode values for "guest_additions_mode" which
// determine how guest additions are delivered to the guest.
const (
GuestAdditionsModeDisable string = "disable"
GuestAdditionsModeAttach = "attach"
GuestAdditionsModeUpload = "upload"
)
type GuestAdditionsConfig struct {
Communicator string `mapstructure:"communicator"`
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
}
func (c *GuestAdditionsConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if c.Communicator == "none" && c.GuestAdditionsMode != "disable" {
errs = append(errs, fmt.Errorf("guest_additions_mode has to be "+
"'disable' when communicator = 'none'."))
}
return errs
}

View File

@ -1,18 +1,28 @@
package common
import (
"fmt"
"github.com/hashicorp/packer/template/interpolate"
)
type VBoxVersionConfig struct {
Communicator string `mapstructure:"communicator"`
VBoxVersionFile *string `mapstructure:"virtualbox_version_file"`
}
func (c *VBoxVersionConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if c.VBoxVersionFile == nil {
default_file := ".vbox_version"
c.VBoxVersionFile = &default_file
}
return nil
if c.Communicator == "none" && *c.VBoxVersionFile != "" {
errs = append(errs, fmt.Errorf("virtualbox_version_file has to be an "+
"empty string when communicator = 'none'."))
}
return errs
}

View File

@ -40,6 +40,7 @@ type Config struct {
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
vboxcommon.VBoxBundleConfig `mapstructure:",squash"`
vboxcommon.GuestAdditionsConfig `mapstructure:",squash"`
DiskSize uint `mapstructure:"disk_size"`
GuestAdditionsMode string `mapstructure:"guest_additions_mode"`
@ -101,6 +102,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.GuestAdditionsConfig.Prepare(&b.config.ctx)...)
if b.config.DiskSize == 0 {
b.config.DiskSize = 40000

View File

@ -28,6 +28,7 @@ type Config struct {
vboxcommon.VBoxManageConfig `mapstructure:",squash"`
vboxcommon.VBoxManagePostConfig `mapstructure:",squash"`
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
vboxcommon.GuestAdditionsConfig `mapstructure:",squash"`
Checksum string `mapstructure:"checksum"`
ChecksumType string `mapstructure:"checksum_type"`
@ -97,6 +98,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.GuestAdditionsConfig.Prepare(&c.ctx)...)
c.ChecksumType = strings.ToLower(c.ChecksumType)
c.Checksum = strings.ToLower(c.Checksum)