github.com/rothwerx/packer@v0.9.0/builder/parallels/pvm/config.go (about)

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