2013-12-25 13:27:53 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-25 18:07:07 -04:00
|
|
|
"strconv"
|
2013-12-25 13:27:53 -05:00
|
|
|
|
2018-10-25 18:07:07 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-25 13:27:53 -05:00
|
|
|
)
|
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
const (
|
2018-10-25 18:07:07 -04:00
|
|
|
// BuilderId for the local artifacts
|
|
|
|
BuilderId = "mitchellh.vmware"
|
|
|
|
BuilderIdESX = "mitchellh.vmware-esx"
|
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
ArtifactConfFormat = "artifact.conf.format"
|
|
|
|
ArtifactConfKeepRegistered = "artifact.conf.keep_registered"
|
|
|
|
ArtifactConfSkipExport = "artifact.conf.skip_export"
|
|
|
|
)
|
|
|
|
|
2013-12-25 13:27:53 -05:00
|
|
|
// Artifact is the result of running the VMware builder, namely a set
|
|
|
|
// of files associated with the resulting machine.
|
2017-02-03 08:56:13 -05:00
|
|
|
type artifact struct {
|
|
|
|
builderId string
|
|
|
|
id string
|
2018-10-25 18:07:07 -04:00
|
|
|
dir OutputDir
|
2017-02-03 08:56:13 -05:00
|
|
|
f []string
|
|
|
|
config map[string]string
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
func (a *artifact) BuilderId() string {
|
2018-10-25 17:41:01 -04:00
|
|
|
return a.builderId
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
func (a *artifact) Files() []string {
|
2013-12-25 13:27:53 -05:00
|
|
|
return a.f
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
func (a *artifact) Id() string {
|
2017-07-27 20:59:11 -04:00
|
|
|
return a.id
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
func (a *artifact) String() string {
|
2013-12-25 13:27:53 -05:00
|
|
|
return fmt.Sprintf("VM files in directory: %s", a.dir)
|
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
func (a *artifact) State(name string) interface{} {
|
2018-10-25 17:17:35 -04:00
|
|
|
return a.config[name]
|
2014-01-19 13:32:44 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
func (a *artifact) Destroy() error {
|
2018-10-25 18:07:07 -04:00
|
|
|
return a.dir.RemoveAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewArtifact(remoteType string, format string, exportOutputPath string, vmName string, skipExport bool, keepRegistered bool, state multistep.StateBag) (packer.Artifact, error) {
|
|
|
|
var files []string
|
|
|
|
var dir OutputDir
|
|
|
|
var err error
|
2018-11-02 14:10:51 -04:00
|
|
|
if remoteType != "" && !skipExport {
|
2018-10-25 18:07:07 -04:00
|
|
|
dir = new(LocalOutputDir)
|
|
|
|
dir.SetOutputDir(exportOutputPath)
|
|
|
|
files, err = dir.ListFiles()
|
|
|
|
} else {
|
|
|
|
files, err = state.Get("dir").(OutputDir).ListFiles()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the proper builder ID
|
|
|
|
builderId := BuilderId
|
|
|
|
if remoteType != "" {
|
|
|
|
builderId = BuilderIdESX
|
|
|
|
}
|
|
|
|
|
|
|
|
config := make(map[string]string)
|
|
|
|
config[ArtifactConfKeepRegistered] = strconv.FormatBool(keepRegistered)
|
|
|
|
config[ArtifactConfFormat] = format
|
|
|
|
config[ArtifactConfSkipExport] = strconv.FormatBool(skipExport)
|
|
|
|
|
|
|
|
return &artifact{
|
|
|
|
builderId: builderId,
|
|
|
|
id: vmName,
|
|
|
|
dir: dir,
|
|
|
|
f: files,
|
|
|
|
config: config,
|
|
|
|
}, nil
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|