2017-05-09 10:23:57 -04:00
|
|
|
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"
|
2017-06-13 07:11:41 -04:00
|
|
|
"time"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-05-31 04:38:50 -04:00
|
|
|
// 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"`
|
2017-05-31 04:38:50 -04:00
|
|
|
Host string `mapstructure:"host"`
|
|
|
|
ResourcePool string `mapstructure:"resource_pool"`
|
|
|
|
Datastore string `mapstructure:"datastore"`
|
2017-06-13 07:11:41 -04:00
|
|
|
|
|
|
|
// Settings
|
2017-06-27 03:32:59 -04:00
|
|
|
LinkedClone bool `mapstructure:"linked_clone"`
|
|
|
|
ConvertToTemplate bool `mapstructure:"convert_to_template"`
|
2017-06-13 07:11:41 -04:00
|
|
|
RawShutdownTimeout string `mapstructure:"shutdown_timeout"`
|
2017-07-01 10:18:54 -04:00
|
|
|
ShutdownTimeout time.Duration
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-06-27 03:32:59 -04:00
|
|
|
// Customization
|
2017-07-01 11:03:52 -04:00
|
|
|
CPUs string `mapstructure:"CPUs"`
|
2017-05-09 10:23:57 -04:00
|
|
|
ShutdownCommand string `mapstructure:"shutdown_command"`
|
|
|
|
Ram string `mapstructure:"RAM"`
|
2017-06-27 03:32:59 -04:00
|
|
|
CreateSnapshot bool `mapstructure:"create_snapshot"`
|
|
|
|
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
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
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
// 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"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
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"))
|
|
|
|
}
|
2017-05-31 04:38:50 -04:00
|
|
|
if c.Host == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Target host required"))
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
// 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"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Ram != "" {
|
|
|
|
if _, err = strconv.Atoi(c.Ram); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid number for Ram"))
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 07:11:41 -04:00
|
|
|
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")
|
|
|
|
//}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return nil, warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, warnings, nil
|
|
|
|
}
|