2013-12-25 17:52:40 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-26 10:34:27 -05:00
|
|
|
"os"
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
2013-12-26 17:31:23 -05:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2013-12-27 10:37:39 -05:00
|
|
|
vmwcommon.DriverConfig `mapstructure:",squash"`
|
2013-12-26 17:31:23 -05:00
|
|
|
vmwcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vmwcommon.SSHConfig `mapstructure:",squash"`
|
2014-05-09 20:24:19 -04:00
|
|
|
vmwcommon.ToolsConfig `mapstructure:",squash"`
|
2013-12-26 17:31:23 -05:00
|
|
|
vmwcommon.VMXConfig `mapstructure:",squash"`
|
2013-12-25 17:52:40 -05:00
|
|
|
|
2014-05-12 22:02:30 -04:00
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
2014-05-06 20:20:26 -04:00
|
|
|
FloppyFiles []string `mapstructure:"floppy_files"`
|
2014-05-09 20:24:19 -04:00
|
|
|
RemoteType string `mapstructure:"remote_type"`
|
2014-05-06 20:20:26 -04:00
|
|
|
SkipCompaction bool `mapstructure:"skip_compaction"`
|
|
|
|
SourcePath string `mapstructure:"source_path"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
2013-12-26 10:34:27 -05:00
|
|
|
|
2013-12-25 17:52:40 -05: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
|
|
|
|
|
|
|
|
// Defaults
|
2013-12-26 10:34:27 -05:00
|
|
|
if c.VMName == "" {
|
|
|
|
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", c.PackerBuildName)
|
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Prepare the errors
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
2013-12-27 10:37:39 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(c.tpl)...)
|
2013-12-25 18:01:57 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(c.tpl, &c.PackerConfig)...)
|
2013-12-26 17:26:09 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(c.tpl)...)
|
2013-12-26 17:31:23 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(c.tpl)...)
|
2013-12-25 17:52:40 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(c.tpl)...)
|
2014-05-09 20:24:19 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(c.tpl)...)
|
2013-12-26 17:14:19 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(c.tpl)...)
|
2013-12-25 17:52:40 -05:00
|
|
|
|
2013-12-26 10:34:27 -05:00
|
|
|
templates := map[string]*string{
|
2014-05-09 20:24:19 -04:00
|
|
|
"remote_type": &c.RemoteType,
|
2013-12-26 10:34:27 -05:00
|
|
|
"source_path": &c.SourcePath,
|
|
|
|
"vm_name": &c.VMName,
|
|
|
|
}
|
2013-12-25 17:52:40 -05: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-12 21:24:03 -04:00
|
|
|
for i, file := range c.FloppyFiles {
|
|
|
|
var err error
|
|
|
|
c.FloppyFiles[i], err = c.tpl.Process(file, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Error processing floppy_files[%d]: %s",
|
|
|
|
i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 22:02:30 -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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:34:27 -05: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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
// Warnings
|
|
|
|
var warnings []string
|
2013-12-26 17:31:23 -05:00
|
|
|
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.")
|
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, warnings, nil
|
|
|
|
}
|