github.com/timsutton/packer@v1.3.2/builder/parallels/pvm/config.go (about)

     1  package pvm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	parallelscommon "github.com/hashicorp/packer/builder/parallels/common"
     8  	"github.com/hashicorp/packer/common"
     9  	"github.com/hashicorp/packer/common/bootcommand"
    10  	"github.com/hashicorp/packer/helper/config"
    11  	"github.com/hashicorp/packer/packer"
    12  	"github.com/hashicorp/packer/template/interpolate"
    13  )
    14  
    15  // Config is the configuration structure for the builder.
    16  type Config struct {
    17  	common.PackerConfig                 `mapstructure:",squash"`
    18  	common.FloppyConfig                 `mapstructure:",squash"`
    19  	parallelscommon.OutputConfig        `mapstructure:",squash"`
    20  	parallelscommon.PrlctlConfig        `mapstructure:",squash"`
    21  	parallelscommon.PrlctlPostConfig    `mapstructure:",squash"`
    22  	parallelscommon.PrlctlVersionConfig `mapstructure:",squash"`
    23  	parallelscommon.SSHConfig           `mapstructure:",squash"`
    24  	parallelscommon.ShutdownConfig      `mapstructure:",squash"`
    25  	bootcommand.BootConfig              `mapstructure:",squash"`
    26  	parallelscommon.ToolsConfig         `mapstructure:",squash"`
    27  
    28  	SourcePath     string `mapstructure:"source_path"`
    29  	SkipCompaction bool   `mapstructure:"skip_compaction"`
    30  	VMName         string `mapstructure:"vm_name"`
    31  	ReassignMAC    bool   `mapstructure:"reassign_mac"`
    32  
    33  	ctx interpolate.Context
    34  }
    35  
    36  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    37  	c := new(Config)
    38  	err := config.Decode(c, &config.DecodeOpts{
    39  		Interpolate:        true,
    40  		InterpolateContext: &c.ctx,
    41  		InterpolateFilter: &interpolate.RenderFilter{
    42  			Exclude: []string{
    43  				"boot_command",
    44  				"prlctl",
    45  				"prlctl_post",
    46  				"parallels_tools_guest_path",
    47  			},
    48  		},
    49  	}, raws...)
    50  	if err != nil {
    51  		return nil, nil, err
    52  	}
    53  
    54  	if c.VMName == "" {
    55  		c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
    56  	}
    57  
    58  	// Prepare the errors
    59  	var errs *packer.MultiError
    60  	errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
    61  	errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
    62  	errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(&c.ctx)...)
    63  	errs = packer.MultiErrorAppend(errs, c.PrlctlPostConfig.Prepare(&c.ctx)...)
    64  	errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(&c.ctx)...)
    65  	errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
    66  	errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
    67  	errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
    68  	errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...)
    69  
    70  	if c.SourcePath == "" {
    71  		errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
    72  	} else {
    73  		if _, err := os.Stat(c.SourcePath); err != nil {
    74  			errs = packer.MultiErrorAppend(errs,
    75  				fmt.Errorf("source_path is invalid: %s", err))
    76  		}
    77  	}
    78  
    79  	// Warnings
    80  	var warnings []string
    81  	if c.ShutdownCommand == "" {
    82  		warnings = append(warnings,
    83  			"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
    84  				"will forcibly halt the virtual machine, which may result in data loss.")
    85  	}
    86  
    87  	// Check for any errors.
    88  	if errs != nil && len(errs.Errors) > 0 {
    89  		return nil, warnings, errs
    90  	}
    91  
    92  	return c, warnings, nil
    93  }