post-processor/vsphere: template process in prepare phase

This commit is contained in:
Mitchell Hashimoto 2013-12-11 14:01:14 -08:00
parent e36e8983f0
commit f5e591b2bb
1 changed files with 35 additions and 56 deletions

View File

@ -16,18 +16,17 @@ var builtins = map[string]string{
type Config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
Insecure bool `mapstructure:"insecure"` Insecure bool `mapstructure:"insecure"`
Cluster string `mapstructure:"cluster"` Cluster string `mapstructure:"cluster"`
Datacenter string `mapstructure:"datacenter"` Datacenter string `mapstructure:"datacenter"`
Datastore string `mapstructure:"datastore"` Datastore string `mapstructure:"datastore"`
Debug bool `mapstructure:"debug"` Host string `mapstructure:"host"`
Host string `mapstructure:"host"` Password string `mapstructure:"password"`
Password string `mapstructure:"password"` ResourcePool string `mapstructure:"resource_pool"`
ResourcePool string `mapstructure:"resource_pool"` Username string `mapstructure:"username"`
Username string `mapstructure:"username"` VMFolder string `mapstructure:"vm_folder"`
VMFolder string `mapstructure:"vm_folder"` VMName string `mapstructure:"vm_name"`
VMName string `mapstructure:"vm_name"` VMNetwork string `mapstructure:"vm_network"`
VMNetwork string `mapstructure:"vm_network"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
@ -56,23 +55,29 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
errs, fmt.Errorf("ovftool not found: %s", err)) errs, fmt.Errorf("ovftool not found: %s", err))
} }
validates := map[string]*string{ templates := map[string]*string{
"cluster": &p.config.Cluster, "cluster": &p.config.Cluster,
"datacenter": &p.config.Datacenter, "datacenter": &p.config.Datacenter,
"datastore": &p.config.Datastore, "datastore": &p.config.Datastore,
"host": &p.config.Host, "host": &p.config.Host,
"vm_network": &p.config.VMNetwork, "vm_network": &p.config.VMNetwork,
"password": &p.config.Password, "password": &p.config.Password,
"resource_pool": &p.config.ResourcePool, "resource_pool": &p.config.ResourcePool,
"username": &p.config.Username, "username": &p.config.Username,
"vm_folder": &p.config.VMFolder, "vm_folder": &p.config.VMFolder,
"vm_name": &p.config.VMName, "vm_name": &p.config.VMName,
} }
for n := range validates { for key, ptr := range templates {
if *validates[n] == "" { if *ptr == "" {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, fmt.Errorf("%s must be set", n)) errs, fmt.Errorf("%s must be set", key))
}
*ptr, err = p.config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", key, err))
} }
} }
@ -100,50 +105,24 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, fmt.Errorf("VMX file not found") return nil, false, fmt.Errorf("VMX file not found")
} }
// Get user variables from template
vm_name, err := p.config.tpl.Process(p.config.VMName, p.config.PackerUserVars)
if err != nil {
return nil, false, fmt.Errorf("Failed: %s", err)
}
username, err := p.config.tpl.Process(p.config.Username, p.config.PackerUserVars)
if err != nil {
return nil, false, fmt.Errorf("Failed: %s", err)
}
password, err := p.config.tpl.Process(p.config.Password, p.config.PackerUserVars)
if err != nil {
return nil, false, fmt.Errorf("Failed: %s", err)
}
datastore, err := p.config.tpl.Process(p.config.Datastore, p.config.PackerUserVars)
if err != nil {
return nil, false, fmt.Errorf("Failed: %s", err)
}
ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
args := []string{ args := []string{
fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure), fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
"--acceptAllEulas", "--acceptAllEulas",
fmt.Sprintf("--name=%s", vm_name), fmt.Sprintf("--name=%s", p.config.VMName),
fmt.Sprintf("--datastore=%s", datastore), fmt.Sprintf("--datastore=%s", p.config.Datastore),
fmt.Sprintf("--network=%s", p.config.VMNetwork), fmt.Sprintf("--network=%s", p.config.VMNetwork),
fmt.Sprintf("--vmFolder=%s", p.config.VMFolder), fmt.Sprintf("--vmFolder=%s", p.config.VMFolder),
fmt.Sprintf("%s", vmx), fmt.Sprintf("%s", vmx),
fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s", fmt.Sprintf("vi://%s:%s@%s/%s/host/%s/Resources/%s",
username, p.config.Username,
password, p.config.Password,
p.config.Host, p.config.Host,
p.config.Datacenter, p.config.Datacenter,
p.config.Cluster, p.config.Cluster,
p.config.ResourcePool), p.config.ResourcePool),
} }
if p.config.Debug { ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
ui.Message(fmt.Sprintf("DEBUG: %s", args))
}
var out bytes.Buffer var out bytes.Buffer
cmd := exec.Command("ovftool", args...) cmd := exec.Command("ovftool", args...)
cmd.Stdout = &out cmd.Stdout = &out