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"
|
2017-06-13 07:11:41 -04:00
|
|
|
"time"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2017-07-01 11:32:16 -04:00
|
|
|
// Connection
|
2017-07-01 11:59:36 -04:00
|
|
|
VCenterServer string `mapstructure:"vcenter_server"`
|
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
InsecureConnection bool `mapstructure:"insecure_connection"`
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-05-31 04:38:50 -04:00
|
|
|
// Location
|
|
|
|
Template string `mapstructure:"template"`
|
2017-07-01 10:38:50 -04:00
|
|
|
FolderName string `mapstructure:"folder"`
|
2017-07-01 11:32:16 -04:00
|
|
|
VMName string `mapstructure:"vm_name"`
|
2017-05-31 04:38:50 -04:00
|
|
|
Host string `mapstructure:"host"`
|
|
|
|
ResourcePool string `mapstructure:"resource_pool"`
|
|
|
|
Datastore string `mapstructure:"datastore"`
|
2017-07-01 11:32:16 -04:00
|
|
|
LinkedClone bool `mapstructure:"linked_clone"`
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-06-27 03:32:59 -04:00
|
|
|
// Customization
|
2017-07-01 18:34:50 -04:00
|
|
|
HardwareConfig `mapstructure:",squash"`
|
2017-07-01 11:32:16 -04:00
|
|
|
|
|
|
|
// Provisioning
|
|
|
|
communicator.Config `mapstructure:",squash"`
|
2017-06-27 03:32:59 -04:00
|
|
|
|
2017-07-01 11:32:16 -04:00
|
|
|
// Post-processing
|
|
|
|
ShutdownCommand string `mapstructure:"shutdown_command"`
|
|
|
|
RawShutdownTimeout string `mapstructure:"shutdown_timeout"`
|
|
|
|
ShutdownTimeout time.Duration
|
|
|
|
CreateSnapshot bool `mapstructure:"create_snapshot"`
|
|
|
|
ConvertToTemplate bool `mapstructure:"convert_to_template"`
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-01 11:32:16 -04:00
|
|
|
ctx interpolate.Context
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(raws ...interface{}) (*Config, []string, error) {
|
|
|
|
c := new(Config)
|
2017-07-01 11:32:16 -04:00
|
|
|
{
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errs := new(packer.MultiError)
|
2017-06-12 14:08:25 -04:00
|
|
|
var warnings []string
|
2017-05-09 10:23:57 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.Config.Prepare(&c.ctx)...)
|
|
|
|
|
2017-07-01 11:54:10 -04:00
|
|
|
if c.VCenterServer == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vCenter hostname is required"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
if c.Username == "" {
|
2017-07-01 11:54:10 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Username is required"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
if c.Password == "" {
|
2017-07-01 11:54:10 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Password is required"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
if c.Template == "" {
|
2017-07-01 11:54:10 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Template name is required"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
if c.VMName == "" {
|
2017-07-01 11:54:10 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Target VM name is required"))
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
2017-05-31 04:38:50 -04:00
|
|
|
if c.Host == "" {
|
2017-07-01 11:54:10 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vSphere host is required"))
|
2017-05-31 04:38:50 -04:00
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-01 11:32:16 -04:00
|
|
|
if c.RawShutdownTimeout != "" {
|
|
|
|
timeout, err := time.ParseDuration(c.RawShutdownTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err))
|
|
|
|
}
|
|
|
|
c.ShutdownTimeout = timeout
|
|
|
|
} else {
|
|
|
|
c.ShutdownTimeout = 5 * time.Minute
|
2017-06-13 07:11:41 -04:00
|
|
|
}
|
|
|
|
|
2017-05-09 10:23:57 -04:00
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
return nil, warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, warnings, nil
|
|
|
|
}
|