adding artifact testing and using builder id

This commit is contained in:
bugbuilder 2017-09-23 15:43:57 -03:00
parent f1773a57f8
commit 75a4ca7351
5 changed files with 40 additions and 16 deletions

View File

@ -125,7 +125,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
&stepCreateFolder{
Folder: p.config.Folder,
},
NewStepMarkAsTemplate(artifact.Id(), source),
NewStepMarkAsTemplate(artifact, source),
}
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
runner.Run(state)

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/post-processor/vsphere"
"github.com/mitchellh/multistep"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/object"
@ -18,18 +19,16 @@ type stepMarkAsTemplate struct {
RemoteFolder string
}
func NewStepMarkAsTemplate(vmname, source string) *stepMarkAsTemplate {
func NewStepMarkAsTemplate(artifact packer.Artifact, source string) *stepMarkAsTemplate {
remoteFolder := "Discovered virtual machine"
vmname := artifact.Id()
if strings.Contains(vmname, "::") {
local := strings.Split(vmname, "::")
datastore := local[0]
remoteFolder = local[1]
vmname = local[2]
if artifact.BuilderId() == vsphere.BuilderId {
id := strings.Split(artifact.Id(), "::")
datastore := id[0]
remoteFolder = id[1]
vmname = id[2]
source = path.Join("/vmfs/volumes/", datastore, vmname, vmname+".vmx")
}
return &stepMarkAsTemplate{

View File

@ -13,6 +13,15 @@ type Artifact struct {
vmname string
}
func NewArtifact(datastore, vmfolder, vmname string, files []string) *Artifact {
return &Artifact{
files: files,
datastore: datastore,
vmfolder: vmfolder,
vmname: vmname,
}
}
func (*Artifact) BuilderId() string {
return BuilderId
}

View File

@ -0,0 +1,21 @@
package vsphere
import (
"github.com/hashicorp/packer/packer"
"testing"
)
func TestArtifact_ImplementsArtifact(t *testing.T) {
var raw interface{}
raw = &Artifact{}
if _, ok := raw.(packer.Artifact); !ok {
t.Fatalf("Artifact should be a Artifact")
}
}
func TestArtifact_Id(t *testing.T) {
artifact := NewArtifact("datastore", "vmfolder", "vmname", nil)
if artifact.Id() != "datastore::vmfolder::vmname" {
t.Fatalf("must return datastore, vmfolder and vmname splitted by :: as Id")
}
}

View File

@ -142,12 +142,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, fmt.Errorf("Failed: %s\n", err)
}
artifact = &Artifact{
datastore: p.config.Datastore,
files: artifact.Files(),
vmfolder: p.config.VMFolder,
vmname: p.config.VMName,
}
artifact = NewArtifact(p.config.Datastore, p.config.VMFolder, p.config.VMName, artifact.Files())
return artifact, true, nil
}