builder/vmware: register the VMX to the proper path on esx
This commit is contained in:
parent
4fd6b3222f
commit
5f380a614f
|
@ -19,6 +19,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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 {
|
type ESX5Driver struct {
|
||||||
Host string
|
Host string
|
||||||
Port uint
|
Port uint
|
||||||
|
@ -26,7 +28,8 @@ type ESX5Driver struct {
|
||||||
Password string
|
Password string
|
||||||
Datastore string
|
Datastore string
|
||||||
|
|
||||||
comm packer.Communicator
|
comm packer.Communicator
|
||||||
|
outputDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) CompactDisk(diskPathLocal string) error {
|
func (d *ESX5Driver) CompactDisk(diskPathLocal string) error {
|
||||||
|
@ -56,7 +59,7 @@ func (d *ESX5Driver) Stop(vmxPathLocal string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) Register(vmxPathLocal string) error {
|
func (d *ESX5Driver) Register(vmxPathLocal string) error {
|
||||||
vmxPath := d.datastorePath(vmxPathLocal)
|
vmxPath := filepath.Join(d.outputDir, filepath.Base(vmxPathLocal))
|
||||||
if err := d.upload(vmxPath, vmxPathLocal); err != nil {
|
if err := d.upload(vmxPath, vmxPathLocal); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -64,7 +67,8 @@ func (d *ESX5Driver) Register(vmxPathLocal string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) Unregister(vmxPathLocal string) error {
|
func (d *ESX5Driver) Unregister(vmxPathLocal string) error {
|
||||||
return d.sh("vim-cmd", "vmsvc/unregister", d.datastorePath(vmxPathLocal))
|
vmxPath := filepath.Join(d.outputDir, filepath.Base(vmxPathLocal))
|
||||||
|
return d.sh("vim-cmd", "vmsvc/unregister", vmxPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) UploadISO(localPath string) (string, error) {
|
func (d *ESX5Driver) UploadISO(localPath string) (string, error) {
|
||||||
|
@ -74,7 +78,7 @@ func (d *ESX5Driver) UploadISO(localPath string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := d.MkdirAll(filepath.Dir(targetFile)); err != nil {
|
if err := d.mkdir(filepath.Dir(targetFile)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,17 +203,21 @@ func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) {
|
||||||
return address, nil
|
return address, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) DirExists(path string) (bool, error) {
|
func (d *ESX5Driver) DirExists() (bool, error) {
|
||||||
err := d.sh("test", "-e", d.datastorePath(path))
|
err := d.sh("test", "-e", d.outputDir)
|
||||||
return err == nil, nil
|
return err == nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) MkdirAll(path string) error {
|
func (d *ESX5Driver) MkdirAll() error {
|
||||||
return d.sh("mkdir", "-p", d.datastorePath(path))
|
return d.mkdir(d.outputDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) RemoveAll(path string) error {
|
func (d *ESX5Driver) RemoveAll() error {
|
||||||
return d.sh("rm", "-rf", d.datastorePath(path))
|
return d.sh("rm", "-rf", d.outputDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *ESX5Driver) SetOutputDir(path string) {
|
||||||
|
d.outputDir = d.datastorePath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) datastorePath(path string) string {
|
func (d *ESX5Driver) datastorePath(path string) string {
|
||||||
|
@ -279,6 +287,10 @@ func (d *ESX5Driver) checkGuestIPHackEnabled() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *ESX5Driver) mkdir(path string) error {
|
||||||
|
return d.sh("mkdir", "-p", path)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *ESX5Driver) upload(dst, src string) error {
|
func (d *ESX5Driver) upload(dst, src string) error {
|
||||||
f, err := os.Open(src)
|
f, err := os.Open(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -9,24 +9,31 @@ import (
|
||||||
// so that the output directory can be properly made on remote (ESXi) based
|
// so that the output directory can be properly made on remote (ESXi) based
|
||||||
// VMware products as well as local.
|
// VMware products as well as local.
|
||||||
type OutputDir interface {
|
type OutputDir interface {
|
||||||
DirExists(string) (bool, error)
|
DirExists() (bool, error)
|
||||||
MkdirAll(string) error
|
MkdirAll() error
|
||||||
RemoveAll(string) error
|
RemoveAll() error
|
||||||
|
SetOutputDir(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// localOutputDir is an OutputDir implementation where the directory
|
// localOutputDir is an OutputDir implementation where the directory
|
||||||
// is on the local machine.
|
// is on the local machine.
|
||||||
type localOutputDir struct{}
|
type localOutputDir struct {
|
||||||
|
dir string
|
||||||
|
}
|
||||||
|
|
||||||
func (localOutputDir) DirExists(path string) (bool, error) {
|
func (d *localOutputDir) DirExists() (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(d.dir)
|
||||||
return err == nil, err
|
return err == nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (localOutputDir) MkdirAll(path string) error {
|
func (d *localOutputDir) MkdirAll() error {
|
||||||
return os.MkdirAll(path, 0755)
|
return os.MkdirAll(d.dir, 0755)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (localOutputDir) RemoveAll(path string) error {
|
func (d *localOutputDir) RemoveAll() error {
|
||||||
return os.RemoveAll(path)
|
return os.RemoveAll(d.dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *localOutputDir) SetOutputDir(path string) {
|
||||||
|
d.dir = path
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,18 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepPrepareOutputDir struct{}
|
type stepPrepareOutputDir struct {
|
||||||
|
dir OutputDir
|
||||||
|
}
|
||||||
|
|
||||||
func (s *stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
config := state.Get("config").(*config)
|
config := state.Get("config").(*config)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
dir := s.outputDir(state)
|
dir := s.outputDir(state)
|
||||||
exists, err := dir.DirExists(config.OutputDir)
|
dir.SetOutputDir(config.OutputDir)
|
||||||
|
|
||||||
|
exists, err := dir.DirExists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
|
@ -22,14 +26,16 @@ func (s *stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepActio
|
||||||
|
|
||||||
if exists && config.PackerForce {
|
if exists && config.PackerForce {
|
||||||
ui.Say("Deleting previous output directory...")
|
ui.Say("Deleting previous output directory...")
|
||||||
dir.RemoveAll(config.OutputDir)
|
dir.RemoveAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dir.MkdirAll(config.OutputDir); err != nil {
|
if err := dir.MkdirAll(); err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.dir = dir
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,19 +44,19 @@ func (s *stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
|
||||||
_, halted := state.GetOk(multistep.StateHalted)
|
_, halted := state.GetOk(multistep.StateHalted)
|
||||||
|
|
||||||
if cancelled || halted {
|
if cancelled || halted {
|
||||||
config := state.Get("config").(*config)
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
dir := s.outputDir(state)
|
if s.dir != nil {
|
||||||
ui.Say("Deleting output directory...")
|
ui.Say("Deleting output directory...")
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
err := dir.RemoveAll(config.OutputDir)
|
err := s.dir.RemoveAll()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Error removing output dir: %s", err)
|
log.Printf("Error removing output dir: %s", err)
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue