Commit 8a8ac430 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: upload the ISO to the ESXi machine

parent 859a6f06
...@@ -395,6 +395,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -395,6 +395,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyFiles, Files: b.config.FloppyFiles,
}, },
&stepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
},
&stepCreateDisk{}, &stepCreateDisk{},
&stepCreateVMX{}, &stepCreateVMX{},
&stepHTTPServer{}, &stepHTTPServer{},
......
...@@ -67,6 +67,25 @@ func (d *ESX5Driver) Unregister(vmxPathLocal string) error { ...@@ -67,6 +67,25 @@ func (d *ESX5Driver) Unregister(vmxPathLocal string) error {
return d.sh("vim-cmd", "vmsvc/unregister", d.datastorePath(vmxPathLocal)) return d.sh("vim-cmd", "vmsvc/unregister", d.datastorePath(vmxPathLocal))
} }
func (d *ESX5Driver) UploadISO(localPath string) (string, error) {
cacheRoot, _ := filepath.Abs(".")
targetFile, err := filepath.Rel(cacheRoot, localPath)
if err != nil {
return "", err
}
if err := d.MkdirAll(filepath.Dir(targetFile)); err != nil {
return "", err
}
finalPath := d.datastorePath(targetFile)
if err := d.upload(finalPath, localPath); err != nil {
return "", err
}
return finalPath, nil
}
func (d *ESX5Driver) ToolsIsoPath(string) string { func (d *ESX5Driver) ToolsIsoPath(string) string {
return "" return ""
} }
...@@ -180,33 +199,6 @@ func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) { ...@@ -180,33 +199,6 @@ func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) {
return address, nil return address, nil
} }
/*
func (d *ESX5Driver) Download(*common.DownloadConfig, multistep.StateBag) (string, error, bool) {
config := state.Get("config").(*config)
cacheRoot, _ := filepath.Abs(".")
targetFile, err := filepath.Rel(cacheRoot, dconfig.TargetPath)
if err != nil {
return "", err, false
}
if err := d.MkdirAll(filepath.Dir(targetFile)); err != nil {
return "", err, false
}
path := d.datastorePath(targetFile)
if d.verifyChecksum(config.ISOChecksumType, config.ISOChecksum, path) {
log.Println("Initial checksum matched, no download needed.")
return path, nil, true
}
// TODO(dougm) progress and handle interrupt
err = d.sh("wget", dconfig.Url, "-O", path)
return path, err, true
}
*/
func (d *ESX5Driver) DirExists(path string) (bool, error) { func (d *ESX5Driver) DirExists(path string) (bool, error) {
err := d.sh("test", "-e", d.datastorePath(path)) err := d.sh("test", "-e", d.datastorePath(path))
return err == nil, err return err == nil, err
......
package vmware
import (
"testing"
)
func TestESX5Driver_implDriver(t *testing.T) {
var _ Driver = new(ESX5Driver)
}
func TestESX5Driver_implRemoteDriver(t *testing.T) {
var _ RemoteDriver = new(ESX5Driver)
}
package vmware
import (
"os"
)
// OutputDir is an interface type that abstracts the creation and handling
// of the output directory for VMware-based products. The abstraction is made
// so that the output directory can be properly made on remote (ESXi) based
// VMware products as well as local.
type OutputDir interface {
DirExists(string) (bool, error)
MkdirAll(string) error
RemoveAll(string) error
}
// localOutputDir is an OutputDir implementation where the directory
// is on the local machine.
type localOutputDir struct{}
func (localOutputDir) DirExists(path string) (bool, error) {
_, err := os.Stat(path)
return err == nil, err
}
func (localOutputDir) MkdirAll(path string) error {
return os.MkdirAll(path, 0755)
}
func (localOutputDir) RemoveAll(path string) error {
return os.RemoveAll(path)
}
package vmware
type RemoteDriver interface {
Driver
// UploadISO uploads a local ISO to the remote side and returns the
// new path that should be used in the VMX along with an error if it
// exists.
UploadISO(string) (string, error)
// Adds a VM to inventory specified by the path to the VMX given.
Register(string) error
// Removes a VM from inventory specified by the path to the VMX given.
Unregister(string) error
}
package vmware
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
// stepRemoteUpload uploads some thing from the state bag to a remote driver
// (if it can) and stores that new remote path into the state bag.
type stepRemoteUpload struct {
Key string
Message string
}
func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
remote, ok := driver.(RemoteDriver)
if !ok {
return multistep.ActionContinue
}
ui.Say(s.Message)
path := state.Get(s.Key).(string)
log.Printf("Remote uploading: %s", path)
newPath, err := remote.UploadISO(path)
if err != nil {
err := fmt.Errorf("Error uploading file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put(s.Key, newPath)
return multistep.ActionContinue
}
func (s *stepRemoteUpload) Cleanup(state multistep.StateBag) {
}
...@@ -22,13 +22,6 @@ type stepRun struct { ...@@ -22,13 +22,6 @@ type stepRun struct {
vmxPath string vmxPath string
} }
type Inventory interface {
// Adds a VM to inventory specified by the path to the VMX given.
Register(string) error
// Removes a VM from inventory specified by the path to the VMX given.
Unregister(string) error
}
func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*config) config := state.Get("config").(*config)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
...@@ -49,8 +42,8 @@ func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { ...@@ -49,8 +42,8 @@ func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
"%s:%d", vncIp, vncPort)) "%s:%d", vncIp, vncPort))
} }
if inv, ok := driver.(Inventory); ok { if remoteDriver, ok := driver.(RemoteDriver); ok {
if err := inv.Register(vmxPath); err != nil { if err := remoteDriver.Register(vmxPath); err != nil {
err := fmt.Errorf("Error registering VM: %s", err) err := fmt.Errorf("Error registering VM: %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
...@@ -98,9 +91,9 @@ func (s *stepRun) Cleanup(state multistep.StateBag) { ...@@ -98,9 +91,9 @@ func (s *stepRun) Cleanup(state multistep.StateBag) {
} }
} }
if inv, ok := driver.(Inventory); ok { if remoteDriver, ok := driver.(RemoteDriver); ok {
ui.Say("Unregistering virtual machine...") ui.Say("Unregistering virtual machine...")
if err := inv.Unregister(s.vmxPath); err != nil { if err := remoteDriver.Unregister(s.vmxPath); err != nil {
ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) ui.Error(fmt.Sprintf("Error unregistering VM: %s", err))
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment