packer-cn/builder/vmware/vmx/config.go

93 lines
3.0 KiB
Go
Raw Normal View History

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"
2015-05-27 17:21:15 -04:00
"github.com/mitchellh/packer/helper/config"
2013-12-25 17:52:40 -05:00
"github.com/mitchellh/packer/packer"
2015-05-27 17:21:15 -04:00
"github.com/mitchellh/packer/template/interpolate"
2013-12-25 17:52:40 -05:00
)
// Config is the configuration structure for the builder.
type Config struct {
2013-12-26 17:31:23 -05:00
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
2016-07-26 15:42:04 -04:00
common.FloppyConfig `mapstructure:",squash"`
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"`
vmwcommon.ToolsConfig `mapstructure:",squash"`
2013-12-26 17:31:23 -05:00
vmwcommon.VMXConfig `mapstructure:",squash"`
2013-12-25 17:52:40 -05:00
BootCommand []string `mapstructure:"boot_command"`
RemoteType string `mapstructure:"remote_type"`
SkipCompaction bool `mapstructure:"skip_compaction"`
SourcePath string `mapstructure:"source_path"`
VMName string `mapstructure:"vm_name"`
2013-12-26 10:34:27 -05:00
2015-05-27 17:21:15 -04:00
ctx interpolate.Context
2013-12-25 17:52:40 -05:00
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
2015-05-27 17:21:15 -04:00
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
2015-05-27 17:21:15 -04:00
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
"tools_upload_path",
},
},
}, raws...)
2013-12-25 17:52:40 -05:00
if err != nil {
return nil, nil, err
}
// 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
2015-05-27 17:21:15 -04:00
var errs *packer.MultiError
errs = packer.MultiErrorAppend(errs, c.DriverConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
2015-05-27 17:21:15 -04:00
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...)
2016-07-26 15:42:04 -04:00
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
2013-12-26 10:34:27 -05:00
if c.SourcePath == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required"))
2013-12-26 10:34:27 -05:00
} 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
}