packer-cn/config.go

114 lines
3.2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"strconv"
"time"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
communicator.Config `mapstructure:",squash"`
2017-07-01 10:18:54 -04:00
VCenterHost string `mapstructure:"vcenter_host"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
// Location
Template string `mapstructure:"template"`
VMName string `mapstructure:"vm_name"`
2017-07-01 10:38:50 -04:00
FolderName string `mapstructure:"folder"`
2017-07-01 10:45:13 -04:00
Datacenter string `mapstructure:"datacenter"`
Host string `mapstructure:"host"`
ResourcePool string `mapstructure:"resource_pool"`
Datastore string `mapstructure:"datastore"`
// Settings
2017-06-27 03:32:59 -04:00
LinkedClone bool `mapstructure:"linked_clone"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
RawShutdownTimeout string `mapstructure:"shutdown_timeout"`
2017-07-01 10:18:54 -04:00
ShutdownTimeout time.Duration
2017-06-27 03:32:59 -04:00
// Customization
2017-07-01 11:03:52 -04:00
CPUs string `mapstructure:"CPUs"`
ShutdownCommand string `mapstructure:"shutdown_command"`
Ram string `mapstructure:"RAM"`
2017-06-27 03:32:59 -04:00
CreateSnapshot bool `mapstructure:"create_snapshot"`
ctx interpolate.Context
}
func NewConfig(raws ...interface{}) (*Config, []string, error) {
c := new(Config)
err := config.Decode(c, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &c.ctx,
}, raws...)
if err != nil {
return nil, nil, err
}
// Accumulate any errors
errs := new(packer.MultiError)
2017-06-12 14:08:25 -04:00
var warnings []string
// Prepare config(s)
errs = packer.MultiErrorAppend(errs, c.Config.Prepare(&c.ctx)...)
// Check the required params
2017-07-01 10:18:54 -04:00
if c.VCenterHost == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vCenter host required"))
}
if c.Username == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Username required"))
}
if c.Password == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Password required"))
}
if c.Template == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Template VM name required"))
}
if c.VMName == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Target VM name required"))
}
if c.Host == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Target host required"))
}
// Verify numeric parameters if present
2017-07-01 11:03:52 -04:00
if c.CPUs != "" {
if _, err = strconv.Atoi(c.CPUs); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number of CPU sockets"))
}
}
if c.Ram != "" {
if _, err = strconv.Atoi(c.Ram); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number for Ram"))
}
}
if c.RawShutdownTimeout == "" {
c.RawShutdownTimeout = "5m"
}
c.ShutdownTimeout, err = time.ParseDuration(c.RawShutdownTimeout)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err))
}
2017-06-12 14:08:25 -04:00
//if c.Datastore == "" {
// warnings = append(warnings, "Datastore is not specified, will try to find the default one")
//}
if len(errs.Errors) > 0 {
return nil, warnings, errs
}
return c, warnings, nil
}