cleanup to make it actually build after that monstrous rebase

This commit is contained in:
Megan Marsh 2018-10-25 14:17:35 -07:00
parent 77abb3afd4
commit 290f4a7c4c
12 changed files with 50 additions and 36 deletions

View File

@ -3,6 +3,7 @@ package common
import (
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/packer/packer"
)
@ -11,6 +12,12 @@ import (
const BuilderId = "mitchellh.vmware"
const BuilderIdESX = "mitchellh.vmware-esx"
const (
ArtifactConfFormat = "artifact.conf.format"
ArtifactConfKeepRegistered = "artifact.conf.keep_registered"
ArtifactConfSkipExport = "artifact.conf.skip_export"
)
// Artifact is the result of running the VMware builder, namely a set
// of files associated with the resulting machine.
type artifact struct {
@ -48,7 +55,7 @@ func NewLocalArtifact(id string, dir string) (packer.Artifact, error) {
}, nil
}
func NewArtifact(dir OutputDir, files []string, esxi bool) (packer.Artifact, err) {
func NewArtifact(dir OutputDir, files []string, config map[string]string, esxi bool) (packer.Artifact, error) {
builderID := BuilderId
if esxi {
builderID = BuilderIdESX
@ -69,7 +76,7 @@ func (a *artifact) Files() []string {
return a.f
}
func (*artifact) Id() string {
func (a *artifact) Id() string {
return a.id
}
@ -78,7 +85,7 @@ func (a *artifact) String() string {
}
func (a *artifact) State(name string) interface{} {
return nil
return a.config[name]
}
func (a *artifact) Destroy() error {

View File

@ -84,6 +84,7 @@ type Driver interface {
// system, or an error if the driver couldn't be initialized.
func NewDriver(dconfig *DriverConfig, config *SSHConfig, commConfig *communicator.Config, vmName string) (Driver, error) {
drivers := []Driver{}
if dconfig.RemoteType != "" {
drivers = []Driver{
&ESX5Driver{
@ -91,7 +92,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, commConfig *communicato
Port: dconfig.RemotePort,
Username: dconfig.RemoteUser,
Password: dconfig.RemotePassword,
PrivateKey: dconfig.RemotePrivateKey,
PrivateKeyFile: dconfig.RemotePrivateKey,
Datastore: dconfig.RemoteDatastore,
CacheDatastore: dconfig.RemoteCacheDatastore,
CacheDirectory: dconfig.RemoteCacheDirectory,

View File

@ -16,7 +16,6 @@ import (
"strings"
"time"
commonssh "github.com/hashicorp/packer/common/ssh"
"github.com/hashicorp/packer/communicator/ssh"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
@ -28,7 +27,7 @@ import (
// ESX5 driver talks to an ESXi5 hypervisor remotely over SSH to build
// virtual machines. This driver can only manage one machine at a time.
type ESX5Driver struct {
base vmwcommon.VmwareDriver
base VmwareDriver
Host string
Port uint
@ -184,13 +183,13 @@ func (d *ESX5Driver) IsDestroyed() (bool, error) {
}
func (d *ESX5Driver) UploadISO(localPath string, checksum string, checksumType string) (string, error) {
finalPath := d.cachePath(localPath)
finalPath := d.CachePath(localPath)
if err := d.mkdir(filepath.ToSlash(filepath.Dir(finalPath))); err != nil {
return "", err
}
log.Printf("Verifying checksum of %s", finalPath)
if d.verifyChecksum(checksumType, checksum, finalPath) {
if d.VerifyChecksum(checksumType, checksum, finalPath) {
log.Println("Initial checksum matched, no upload needed.")
return finalPath, nil
}
@ -203,7 +202,7 @@ func (d *ESX5Driver) UploadISO(localPath string, checksum string, checksumType s
}
func (d *ESX5Driver) RemoveCache(localPath string) error {
finalPath := d.cachePath(localPath)
finalPath := d.CachePath(localPath)
log.Printf("Removing remote cache path %s (local %s)", finalPath, localPath)
return d.sh("rm", "-f", strconv.Quote(finalPath))
}
@ -564,7 +563,7 @@ func (d *ESX5Driver) datastorePath(path string) string {
return filepath.ToSlash(filepath.Join("/vmfs/volumes", d.Datastore, dirPath, filepath.Base(path)))
}
func (d *ESX5Driver) cachePath(path string) string {
func (d *ESX5Driver) CachePath(path string) string {
return filepath.ToSlash(filepath.Join("/vmfs/volumes", d.CacheDatastore, d.CacheDirectory, filepath.Base(path)))
}
@ -662,7 +661,7 @@ func (d *ESX5Driver) Download(src, dst string) error {
return d.comm.Download(d.datastorePath(src), file)
}
func (d *ESX5Driver) verifyChecksum(ctype string, hash string, file string) bool {
func (d *ESX5Driver) VerifyChecksum(ctype string, hash string, file string) bool {
if ctype == "none" {
if err := d.sh("stat", strconv.Quote(file)); err != nil {
return false
@ -731,7 +730,7 @@ func (d *ESX5Driver) esxcli(args ...string) (*esxcliReader, error) {
return &esxcliReader{r, header}, nil
}
func (d *ESX5Driver) GetVmwareDriver() vmwcommon.VmwareDriver {
func (d *ESX5Driver) GetVmwareDriver() VmwareDriver {
return d.base
}

View File

@ -3,7 +3,7 @@ package common
import (
"fmt"
"github.com/mitchellh/packer/template/interpolate"
"github.com/hashicorp/packer/template/interpolate"
)
type ExportConfig struct {

View File

@ -4,12 +4,6 @@ import (
"fmt"
)
const (
ArtifactConfFormat = "artifact.conf.format"
ArtifactConfKeepRegistered = "artifact.conf.keep_registered"
ArtifactConfSkipExport = "artifact.conf.skip_export"
)
// Artifact is the result of running the VMware builder, namely a set
// of files associated with the resulting machine.
type RemoteArtifact struct {
@ -28,7 +22,7 @@ func (a *RemoteArtifact) Files() []string {
return a.f
}
func (*RemoteArtifact) Id() string {
func (a *RemoteArtifact) Id() string {
return a.id
}

View File

@ -1,7 +1,6 @@
package common
import (
"bytes"
"context"
"fmt"
"log"
@ -29,7 +28,6 @@ type StepConfigureVMX struct {
func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
log.Printf("Configuring VMX...\n")
var vmxContents []byte
var err error
ui := state.Get("ui").(packer.Ui)

View File

@ -71,7 +71,7 @@ func (s *StepExport) Run(_ context.Context, state multistep.StateBag) multistep.
// Export the VM
if s.OutputDir == "" {
s.OutputDir = c.VMName + "." + s.Format
s.OutputDir = s.VMName + "." + s.Format
}
if s.Format == "ova" {

View File

@ -13,11 +13,13 @@ type StepRegister struct {
registeredPath string
Format string
KeepRegistered bool
SkipExport bool
}
func (s *StepRegister) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
vmxPath := state.Get("vmx_path").(string)
if remoteDriver, ok := driver.(RemoteDriver); ok {
@ -51,7 +53,7 @@ func (s *StepRegister) Cleanup(state multistep.StateBag) {
}
if remoteDriver, ok := driver.(RemoteDriver); ok {
if s.Format == "" || config.SkipExport {
if s.Format == "" || s.SkipExport {
ui.Say("Unregistering virtual machine...")
if err := remoteDriver.Unregister(s.registeredPath); err != nil {
ui.Error(fmt.Sprintf("Error unregistering VM: %s", err))

View File

@ -318,6 +318,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&vmwcommon.StepRegister{
Format: b.config.Format,
KeepRegistered: b.config.KeepRegistered,
SkipExport: b.config.SkipExport,
},
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
@ -405,7 +406,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
return nil, err
}
return vmwcommon.NewArtifact(dir, files, b.config.RemoteType != ""), nil
config := make(map[string]string)
config[vmwcommon.ArtifactConfKeepRegistered] = strconv.FormatBool(b.config.KeepRegistered)
config[vmwcommon.ArtifactConfFormat] = b.config.Format
config[vmwcommon.ArtifactConfSkipExport] = strconv.FormatBool(b.config.SkipExport)
return vmwcommon.NewArtifact(dir, files, config, b.config.RemoteType != "")
}
func (b *Builder) Cancel() {

View File

@ -36,10 +36,10 @@ func (s *stepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult
checksum := config.ISOChecksum
checksumType := config.ISOChecksumType
if esx5, ok := remote.(*ESX5Driver); ok {
remotePath := esx5.cachePath(path)
if esx5, ok := remote.(*vmwcommon.ESX5Driver); ok {
remotePath := esx5.CachePath(path)
if esx5.verifyChecksum(checksumType, checksum, remotePath) {
if esx5.VerifyChecksum(checksumType, checksum, remotePath) {
ui.Say("Remote cache was verified skipping remote upload...")
state.Put(s.Key, remotePath)
return multistep.ActionContinue
@ -68,7 +68,7 @@ func (s *stepRemoteUpload) Cleanup(state multistep.StateBag) {
driver := state.Get("driver").(vmwcommon.Driver)
remote, ok := driver.(RemoteDriver)
remote, ok := driver.(vmwcommon.RemoteDriver)
if !ok {
return
}

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"strconv"
"time"
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
@ -105,6 +106,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&vmwcommon.StepRegister{
Format: b.config.Format,
KeepRegistered: b.config.KeepRegistered,
SkipExport: b.config.SkipExport,
},
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
@ -191,7 +193,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
return nil, err
}
return vmwcommon.NewArtifact(dir, files, b.config.RemoteType != ""), nil
config := make(map[string]string)
config[vmwcommon.ArtifactConfKeepRegistered] = strconv.FormatBool(b.config.KeepRegistered)
config[vmwcommon.ArtifactConfFormat] = b.config.Format
config[vmwcommon.ArtifactConfSkipExport] = strconv.FormatBool(b.config.SkipExport)
return vmwcommon.NewArtifact(dir, files, config, b.config.RemoteType != "")
}
// Cancel.

View File

@ -8,7 +8,7 @@ import (
"strings"
"time"
"github.com/hashicorp/packer/builder/vmware/iso"
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
@ -19,8 +19,8 @@ import (
)
var builtins = map[string]string{
vsphere.BuilderId: "vmware",
iso.BuilderIdESX: "vmware",
vsphere.BuilderId: "vmware",
vmwcommon.BuilderIdESX: "vmware",
}
type Config struct {
@ -96,9 +96,9 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
"Artifact type %s does not fit this requirement", artifact.BuilderId())
}
f := artifact.State(iso.ArtifactConfFormat)
k := artifact.State(iso.ArtifactConfKeepRegistered)
s := artifact.State(iso.ArtifactConfSkipExport)
f := artifact.State(vmwcommon.ArtifactConfFormat)
k := artifact.State(vmwcommon.ArtifactConfKeepRegistered)
s := artifact.State(vmwcommon.ArtifactConfSkipExport)
if f != "" && k != "true" && s == "false" {
return nil, false, errors.New("To use this post-processor with exporting behavior you need set keep_registered as true")