2013-12-25 11:27:53 -07:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-25 15:07:07 -07:00
|
|
|
"strconv"
|
2013-12-25 11:27:53 -07:00
|
|
|
|
2018-10-25 15:07:07 -07:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-25 11:27:53 -07:00
|
|
|
)
|
|
|
|
|
2018-10-25 14:17:35 -07:00
|
|
|
const (
|
2018-10-25 15:07:07 -07:00
|
|
|
// BuilderId for the local artifacts
|
|
|
|
BuilderId = "mitchellh.vmware"
|
|
|
|
BuilderIdESX = "mitchellh.vmware-esx"
|
|
|
|
|
2018-10-25 14:17:35 -07:00
|
|
|
ArtifactConfFormat = "artifact.conf.format"
|
|
|
|
ArtifactConfKeepRegistered = "artifact.conf.keep_registered"
|
|
|
|
ArtifactConfSkipExport = "artifact.conf.skip_export"
|
|
|
|
)
|
|
|
|
|
2013-12-25 11:27:53 -07:00
|
|
|
// Artifact is the result of running the VMware builder, namely a set
|
|
|
|
// of files associated with the resulting machine.
|
2017-02-03 15:56:13 +02:00
|
|
|
type artifact struct {
|
|
|
|
builderId string
|
|
|
|
id string
|
2018-10-25 15:07:07 -07:00
|
|
|
dir OutputDir
|
2017-02-03 15:56:13 +02:00
|
|
|
f []string
|
|
|
|
config map[string]string
|
2020-01-30 11:27:58 +01:00
|
|
|
|
|
|
|
// StateData should store data such as GeneratedData
|
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
2013-12-25 11:27:53 -07:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:56:13 +02:00
|
|
|
func (a *artifact) BuilderId() string {
|
2018-10-25 14:41:01 -07:00
|
|
|
return a.builderId
|
2013-12-25 11:27:53 -07:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:56:13 +02:00
|
|
|
func (a *artifact) Files() []string {
|
2013-12-25 11:27:53 -07:00
|
|
|
return a.f
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:17:35 -07:00
|
|
|
func (a *artifact) Id() string {
|
2017-07-28 03:59:11 +03:00
|
|
|
return a.id
|
2013-12-25 11:27:53 -07:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:56:13 +02:00
|
|
|
func (a *artifact) String() string {
|
2013-12-25 11:27:53 -07:00
|
|
|
return fmt.Sprintf("VM files in directory: %s", a.dir)
|
|
|
|
}
|
|
|
|
|
2017-02-03 15:56:13 +02:00
|
|
|
func (a *artifact) State(name string) interface{} {
|
2020-01-30 11:27:58 +01:00
|
|
|
if _, ok := a.StateData[name]; ok {
|
|
|
|
return a.StateData[name]
|
|
|
|
}
|
2018-10-25 14:17:35 -07:00
|
|
|
return a.config[name]
|
2014-01-19 18:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:56:13 +02:00
|
|
|
func (a *artifact) Destroy() error {
|
2018-11-27 16:34:26 -08:00
|
|
|
if a.dir != nil {
|
|
|
|
return a.dir.RemoveAll()
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-25 15:07:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:10:51 -07:00
|
|
|
if remoteType != "" && !skipExport {
|
2018-10-25 15:07:07 -07:00
|
|
|
dir = new(LocalOutputDir)
|
|
|
|
dir.SetOutputDir(exportOutputPath)
|
|
|
|
} else {
|
2018-12-14 12:46:38 +00:00
|
|
|
dir = state.Get("dir").(OutputDir)
|
2018-10-25 15:07:07 -07:00
|
|
|
}
|
2018-12-14 12:46:38 +00:00
|
|
|
files, err = dir.ListFiles()
|
2018-10-25 15:07:07 -07:00
|
|
|
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,
|
2020-01-30 11:27:58 +01:00
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2018-10-25 15:07:07 -07:00
|
|
|
}, nil
|
2013-12-25 11:27:53 -07:00
|
|
|
}
|