2014-04-06 13:21:22 -04:00
|
|
|
package pvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-12 08:52:27 -04:00
|
|
|
"os"
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
parallelscommon "github.com/mitchellh/packer/builder/parallels/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
parallelscommon.FloppyConfig `mapstructure:",squash"`
|
|
|
|
parallelscommon.OutputConfig `mapstructure:",squash"`
|
2014-09-02 03:53:21 -04:00
|
|
|
parallelscommon.PrlctlConfig `mapstructure:",squash"`
|
|
|
|
parallelscommon.PrlctlVersionConfig `mapstructure:",squash"`
|
2014-04-06 13:21:22 -04:00
|
|
|
parallelscommon.RunConfig `mapstructure:",squash"`
|
|
|
|
parallelscommon.SSHConfig `mapstructure:",squash"`
|
|
|
|
parallelscommon.ShutdownConfig `mapstructure:",squash"`
|
2014-09-02 03:53:21 -04:00
|
|
|
parallelscommon.ToolsConfig `mapstructure:",squash"`
|
2014-04-06 13:21:22 -04:00
|
|
|
|
2014-09-02 03:53:21 -04:00
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
|
|
|
SourcePath string `mapstructure:"source_path"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
2014-08-12 08:52:27 -04:00
|
|
|
ReassignMac bool `mapstructure:"reassign_mac"`
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
tpl *packer.ConfigTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
|
|
|
md, err := common.DecodeConfig(c, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
c.tpl.UserVars = c.PackerUserVars
|
|
|
|
|
|
|
|
if c.VMName == "" {
|
|
|
|
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the errors
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(c.tpl)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
|
2014-09-02 03:53:21 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.PrlctlConfig.Prepare(c.tpl)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.PrlctlVersionConfig.Prepare(c.tpl)...)
|
2014-04-06 13:21:22 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
|
2014-09-02 03:53:21 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(c.tpl)...)
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
templates := map[string]*string{
|
2014-09-02 03:53:21 -04:00
|
|
|
"source_path": &c.SourcePath,
|
|
|
|
"vm_name": &c.VMName,
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = c.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:39:00 -04:00
|
|
|
for i, command := range c.BootCommand {
|
|
|
|
if err := c.tpl.Validate(command); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Error processing boot_command[%d]: %s", i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
if c.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(c.SourcePath); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("source_path is invalid: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnings
|
|
|
|
var warnings []string
|
|
|
|
if c.ShutdownCommand == "" {
|
|
|
|
warnings = append(warnings,
|
|
|
|
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
|
|
|
"will forcibly halt the virtual machine, which may result in data loss.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, warnings, nil
|
|
|
|
}
|