2013-12-25 13:27:53 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-10-25 17:17:35 -04:00
|
|
|
"path/filepath"
|
2013-12-25 13:27:53 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-25 13:27:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuilderId for the local artifacts
|
|
|
|
const BuilderId = "mitchellh.vmware"
|
2017-02-03 08:56:13 -05:00
|
|
|
const BuilderIdESX = "mitchellh.vmware-esx"
|
2013-12-25 13:27:53 -05:00
|
|
|
|
2018-10-25 17:17:35 -04:00
|
|
|
const (
|
|
|
|
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
|
|
|
|
dir string
|
|
|
|
f []string
|
|
|
|
config map[string]string
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
// NewLocalArtifact returns a VMware artifact containing the files
|
|
|
|
// in the given directory.
|
2013-12-25 13:27:53 -05:00
|
|
|
// NewLocalArtifact returns a VMware artifact containing the files
|
|
|
|
// in the given directory.
|
2017-07-27 20:59:11 -04:00
|
|
|
func NewLocalArtifact(id string, dir string) (packer.Artifact, error) {
|
2013-12-25 13:27:53 -05:00
|
|
|
files := make([]string, 0, 5)
|
|
|
|
visit := func(path string, info os.FileInfo, err error) error {
|
2017-01-26 19:32:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-25 13:27:53 -05:00
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
2017-01-26 19:32:21 -05:00
|
|
|
return nil
|
2013-12-25 13:27:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.Walk(dir, visit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
return &artifact{
|
|
|
|
builderId: id,
|
|
|
|
dir: dir,
|
|
|
|
f: files,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:41:01 -04:00
|
|
|
func NewArtifact(vmname string, dir OutputDir, files []string, config map[string]string, esxi bool) (packer.Artifact, error) {
|
2017-02-03 08:56:13 -05:00
|
|
|
builderID := BuilderId
|
|
|
|
if esxi {
|
|
|
|
builderID = BuilderIdESX
|
|
|
|
}
|
|
|
|
|
|
|
|
return &artifact{
|
|
|
|
builderId: builderID,
|
2018-10-25 17:41:01 -04:00
|
|
|
id: vmname,
|
2017-02-03 08:56:13 -05:00
|
|
|
dir: dir.String(),
|
|
|
|
f: files,
|
2013-12-25 13:27:53 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-12-25 13:27:53 -05:00
|
|
|
return os.RemoveAll(a.dir)
|
|
|
|
}
|