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

@ -20,7 +20,6 @@ type Config struct {
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"`
@ -56,7 +55,7 @@ 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,
@ -69,10 +68,16 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
"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