post-processor-vsphere: user variables for vm_name, username, password

This commit is contained in:
mheidenr 2013-12-02 12:39:52 +01:00
parent 75d5486dfd
commit 129749a2e4
1 changed files with 25 additions and 13 deletions

View File

@ -28,6 +28,8 @@ type Config struct {
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
} }
type PostProcessor struct { type PostProcessor struct {
@ -40,20 +42,15 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return err return err
} }
tpl, err := packer.NewConfigTemplate() p.config.tpl, err = packer.NewConfigTemplate()
if err != nil { if err != nil {
return err return err
} }
tpl.UserVars = p.config.PackerUserVars p.config.tpl.UserVars = p.config.PackerUserVars
// Accumulate any errors // Accumulate any errors
errs := new(packer.MultiError) errs := new(packer.MultiError)
if err := tpl.Validate(p.config.VMName); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error parsing output template: %s", err))
}
if _, err := exec.LookPath("ovftool"); err != nil { if _, err := exec.LookPath("ovftool"); err != nil {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found: %s", err)) errs, fmt.Errorf("ovftool not found: %s", err))
@ -103,19 +100,34 @@ 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")
} }
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)
}
ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx)) 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", p.config.VMName), fmt.Sprintf("--name=%s", vm_name),
fmt.Sprintf("--datastore=%s", p.config.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",
p.config.Username, username,
p.config.Password, password,
p.config.Host, p.config.Host,
p.config.Datacenter, p.config.Datacenter,
p.config.Cluster, p.config.Cluster,