2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is the common builder ID to all of these artifacts.
|
2015-06-24 14:52:42 -04:00
|
|
|
const BuilderId = "MSOpenTech.hyperv"
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
// Artifact is the result of running the hyperv builder, namely a set
|
|
|
|
// of files associated with the resulting machine.
|
|
|
|
type artifact struct {
|
|
|
|
dir string
|
|
|
|
f []string
|
2020-01-30 05:27:58 -05:00
|
|
|
|
|
|
|
// StateData should store data such as GeneratedData
|
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewArtifact returns a hyperv artifact containing the files
|
|
|
|
// in the given directory.
|
2020-11-19 15:17:11 -05:00
|
|
|
func NewArtifact(dir string, generatedData map[string]interface{}) (packersdk.Artifact, error) {
|
2015-06-21 07:36:07 -04: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
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:32:21 -05:00
|
|
|
return nil
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.Walk(dir, visit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &artifact{
|
2020-01-30 05:27:58 -05:00
|
|
|
dir: dir,
|
|
|
|
f: files,
|
|
|
|
StateData: generatedData,
|
2015-06-21 07:36:07 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifact) BuilderId() string {
|
|
|
|
return BuilderId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *artifact) Files() []string {
|
|
|
|
return a.f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifact) Id() string {
|
|
|
|
return "VM"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *artifact) String() string {
|
|
|
|
return fmt.Sprintf("VM files in directory: %s", a.dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *artifact) State(name string) interface{} {
|
2020-01-30 05:27:58 -05:00
|
|
|
return a.StateData[name]
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *artifact) Destroy() error {
|
|
|
|
return os.RemoveAll(a.dir)
|
|
|
|
}
|