2017-07-10 20:52:48 -04:00
|
|
|
package vsphere_template
|
2017-07-09 14:56:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-07-31 00:26:58 -04:00
|
|
|
"path"
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-07-09 14:56:39 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-07-27 16:55:47 -04:00
|
|
|
"github.com/vmware/govmomi"
|
2017-07-10 04:13:54 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
2017-07-09 14:56:39 -04:00
|
|
|
)
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
type stepCreateFolder struct {
|
2017-07-09 14:56:39 -04:00
|
|
|
Folder string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateFolder) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-07-09 14:56:39 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-07-27 16:55:47 -04:00
|
|
|
cli := state.Get("client").(*govmomi.Client)
|
|
|
|
dcPath := state.Get("dcPath").(string)
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
ui.Message("Creating or checking destination folders...")
|
|
|
|
|
|
|
|
base := path.Join(dcPath, "vm")
|
|
|
|
fullPath := path.Join(base, s.Folder)
|
|
|
|
si := object.NewSearchIndex(cli.Client)
|
|
|
|
|
|
|
|
var folders []string
|
|
|
|
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 otherwise it throws error
|
|
|
|
for {
|
|
|
|
ref, err = si.FindByInventoryPath(context.Background(), fullPath)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if ref == nil {
|
|
|
|
dir, folder := path.Split(fullPath)
|
|
|
|
fullPath = path.Clean(dir)
|
|
|
|
|
|
|
|
if fullPath == dcPath {
|
|
|
|
err = fmt.Errorf("vSphere base path %s not found", base)
|
2017-07-27 16:55:47 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
|
|
|
|
folders = append(folders, folder)
|
|
|
|
} else {
|
|
|
|
break
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
|
2017-08-14 22:00:19 -04:00
|
|
|
if root, ok := ref.(*object.Folder); ok {
|
2017-07-09 14:56:39 -04:00
|
|
|
for i := len(folders) - 1; i >= 0; i-- {
|
|
|
|
ui.Message(fmt.Sprintf("Creating folder: %v", folders[i]))
|
2017-08-14 22:00:19 -04:00
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
root, err = root.CreateFolder(context.Background(), folders[i])
|
2017-07-10 04:13:54 -04:00
|
|
|
if err != nil {
|
2017-07-09 14:56:39 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
|
|
|
|
fullPath = path.Join(fullPath, folders[i])
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
root.SetInventoryPath(fullPath)
|
|
|
|
state.Put("folder", root)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("folder not found: '%v'", ref)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
2017-08-14 22:00:19 -04:00
|
|
|
|
2017-07-09 14:56:39 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
func (s *stepCreateFolder) Cleanup(multistep.StateBag) {}
|