2017-07-10 20:52:48 -04:00
|
|
|
package vsphere_template
|
2017-07-09 14:56:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/vmware/govmomi/find"
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
func (s *stepCreateFolder) Run(state multistep.StateBag) multistep.StepAction {
|
2017-07-09 14:56:39 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-07-18 23:10:05 -04:00
|
|
|
finder := state.Get("finder").(*find.Finder)
|
2017-07-19 00:44:18 -04:00
|
|
|
dc := state.Get("datacenter").(string)
|
2017-07-09 14:56:39 -04:00
|
|
|
|
|
|
|
if s.Folder != "" {
|
|
|
|
ui.Say("Creating or checking destination folders...")
|
|
|
|
|
|
|
|
path := s.Folder
|
2017-07-18 23:10:05 -04:00
|
|
|
base := filepath.Join("/", dc, "vm")
|
2017-07-09 14:56:39 -04:00
|
|
|
var folders []string
|
2017-07-10 04:13:54 -04:00
|
|
|
var root *object.Folder
|
|
|
|
var err error
|
2017-07-18 23:10:05 -04:00
|
|
|
// 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
|
2017-07-19 00:44:18 -04:00
|
|
|
// The iteration ends when we find an existing path or if we don't find any we'll use
|
|
|
|
// the base path
|
2017-07-09 14:56:39 -04:00
|
|
|
for {
|
2017-07-18 23:10:05 -04:00
|
|
|
root, err = finder.Folder(context.Background(), filepath.ToSlash(filepath.Join(base, path)))
|
2017-07-09 14:56:39 -04:00
|
|
|
if err != nil {
|
2017-07-25 23:14:59 -04:00
|
|
|
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]
|
2017-07-10 20:52:48 -04:00
|
|
|
}
|
2017-07-09 14:56:39 -04:00
|
|
|
} else {
|
2017-07-25 23:14:59 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2017-07-09 14:56:39 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(folders) - 1; i >= 0; i-- {
|
|
|
|
ui.Message(fmt.Sprintf("Creating folder: %v", folders[i]))
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:10:05 -04:00
|
|
|
func (s *stepCreateFolder) Cleanup(multistep.StateBag) {}
|