Create folder using the invotory path
This commit is contained in:
parent
fb52c42b3d
commit
08c6139da4
|
@ -27,7 +27,7 @@ func (s *stepChooseDatacenter) Run(state multistep.StateBag) multistep.StepActio
|
|||
}
|
||||
|
||||
finder.SetDatacenter(dc)
|
||||
state.Put("datacenter", dc.Name())
|
||||
state.Put("dcPath", dc.InventoryPath)
|
||||
state.Put("finder", finder)
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/mitchellh/multistep"
|
||||
"github.com/vmware/govmomi/find"
|
||||
"github.com/vmware/govmomi"
|
||||
"github.com/vmware/govmomi/object"
|
||||
)
|
||||
|
||||
|
@ -18,39 +18,37 @@ type stepCreateFolder struct {
|
|||
|
||||
func (s *stepCreateFolder) Run(state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
finder := state.Get("finder").(*find.Finder)
|
||||
dc := state.Get("datacenter").(string)
|
||||
cli := state.Get("client").(*govmomi.Client)
|
||||
dcPath := state.Get("dcPath").(string)
|
||||
|
||||
if s.Folder != "" {
|
||||
ui.Say("Creating or checking destination folders...")
|
||||
|
||||
path := s.Folder
|
||||
base := filepath.Join("/", dc, "vm")
|
||||
base := filepath.Join(dcPath, "vm")
|
||||
path := filepath.ToSlash(filepath.Join(base, s.Folder))
|
||||
si := object.NewSearchIndex(cli.Client)
|
||||
|
||||
var folders []string
|
||||
var root *object.Folder
|
||||
var err error
|
||||
var ref object.Reference
|
||||
|
||||
// We iterate over the path starting with full path
|
||||
// If we don't find it, we save the folder name and continue with the previous path
|
||||
// The iteration ends when we find an existing path or if we don't find any we'll use
|
||||
// the base path
|
||||
// The iteration ends when we find an existing path otherwise it throws error
|
||||
for {
|
||||
root, err = finder.Folder(context.Background(), filepath.ToSlash(filepath.Join(base, path)))
|
||||
ref, err = si.FindByInventoryPath(context.Background(), path)
|
||||
if err != nil {
|
||||
if _, ok := err.(*find.NotFoundError); ok {
|
||||
_, folder := filepath.Split(path)
|
||||
folders = append(folders, folder)
|
||||
if i := strings.LastIndex(path, "/"); i == 0 {
|
||||
root, err = finder.Folder(context.Background(), filepath.ToSlash(base))
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
break
|
||||
} else {
|
||||
path = path[:i]
|
||||
}
|
||||
} else {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
if ref == nil {
|
||||
_, folder := filepath.Split(path)
|
||||
folders = append(folders, folder)
|
||||
path = path[:strings.LastIndex(path, "/")]
|
||||
|
||||
if path == dcPath {
|
||||
err = fmt.Errorf("vSphere base path %s not found", filepath.ToSlash(base))
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
|
@ -60,6 +58,7 @@ func (s *stepCreateFolder) Run(state multistep.StateBag) multistep.StepAction {
|
|||
}
|
||||
}
|
||||
|
||||
root := ref.(*object.Folder)
|
||||
for i := len(folders) - 1; i >= 0; i-- {
|
||||
ui.Message(fmt.Sprintf("Creating folder: %v", folders[i]))
|
||||
root, err = root.CreateFolder(context.Background(), folders[i])
|
||||
|
|
|
@ -18,13 +18,13 @@ type stepMoveTemplate struct {
|
|||
func (s *stepMoveTemplate) Run(state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
finder := state.Get("finder").(*find.Finder)
|
||||
dc := state.Get("datacenter").(string)
|
||||
dcPath := state.Get("dcPath").(string)
|
||||
vm := state.Get("vm").(*object.VirtualMachine)
|
||||
|
||||
if s.Folder != "" {
|
||||
ui.Say("Moving template...")
|
||||
|
||||
folder, err := finder.Folder(context.Background(), filepath.ToSlash(filepath.Join("/", dc, "vm", s.Folder)))
|
||||
folder, err := finder.Folder(context.Background(), filepath.ToSlash(filepath.Join(dcPath, "vm", s.Folder)))
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
|
|
Loading…
Reference in New Issue