Cluster support (#61)
This commit is contained in:
parent
3b58d3d63d
commit
cb782c223a
@ -5,30 +5,21 @@ import (
|
|||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
||||||
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CloneConfig struct {
|
type CloneConfig struct {
|
||||||
Template string `mapstructure:"template"`
|
Template string `mapstructure:"template"`
|
||||||
VMName string `mapstructure:"vm_name"`
|
common.VMConfig `mapstructure:",squash"`
|
||||||
Folder string `mapstructure:"folder"`
|
|
||||||
Host string `mapstructure:"host"`
|
|
||||||
ResourcePool string `mapstructure:"resource_pool"`
|
|
||||||
Datastore string `mapstructure:"datastore"`
|
|
||||||
LinkedClone bool `mapstructure:"linked_clone"`
|
LinkedClone bool `mapstructure:"linked_clone"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CloneConfig) Prepare() []error {
|
func (c *CloneConfig) Prepare() []error {
|
||||||
var errs []error
|
errs := c.VMConfig.Prepare()
|
||||||
|
|
||||||
if c.Template == "" {
|
if c.Template == "" {
|
||||||
errs = append(errs, fmt.Errorf("Template name is required"))
|
errs = append(errs, fmt.Errorf("Template name is required"))
|
||||||
}
|
}
|
||||||
if c.VMName == "" {
|
|
||||||
errs = append(errs, fmt.Errorf("Target VM name is required"))
|
|
||||||
}
|
|
||||||
if c.Host == "" {
|
|
||||||
errs = append(errs, fmt.Errorf("vSphere host is required"))
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
@ -52,6 +43,7 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
|
|||||||
vm, err := template.Clone(&driver.CloneConfig{
|
vm, err := template.Clone(&driver.CloneConfig{
|
||||||
Name: s.config.VMName,
|
Name: s.config.VMName,
|
||||||
Folder: s.config.Folder,
|
Folder: s.config.Folder,
|
||||||
|
Cluster: s.config.Cluster,
|
||||||
Host: s.config.Host,
|
Host: s.config.Host,
|
||||||
ResourcePool: s.config.ResourcePool,
|
ResourcePool: s.config.ResourcePool,
|
||||||
Datastore: s.config.Datastore,
|
Datastore: s.config.Datastore,
|
||||||
|
25
common/vm_config.go
Normal file
25
common/vm_config.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type VMConfig struct {
|
||||||
|
VMName string `mapstructure:"vm_name"`
|
||||||
|
Folder string `mapstructure:"folder"`
|
||||||
|
Cluster string `mapstructure:"cluster"`
|
||||||
|
Host string `mapstructure:"host"`
|
||||||
|
ResourcePool string `mapstructure:"resource_pool"`
|
||||||
|
Datastore string `mapstructure:"datastore"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *VMConfig) Prepare() []error {
|
||||||
|
var errs []error
|
||||||
|
|
||||||
|
if c.VMName == "" {
|
||||||
|
errs = append(errs, fmt.Errorf("Target VM name is required"))
|
||||||
|
}
|
||||||
|
if c.Cluster == "" && c.Host == "" {
|
||||||
|
errs = append(errs, fmt.Errorf("vSphere host or cluster is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs
|
||||||
|
}
|
@ -4,7 +4,6 @@ import (
|
|||||||
"github.com/vmware/govmomi/object"
|
"github.com/vmware/govmomi/object"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
"github.com/vmware/govmomi/vim25/mo"
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Host struct {
|
type Host struct {
|
||||||
@ -20,7 +19,7 @@ func (d *Driver) NewHost(ref *types.ManagedObjectReference) *Host {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) FindHost(name string) (*Host, error) {
|
func (d *Driver) FindHost(name string) (*Host, error) {
|
||||||
h, err := d.finder.HostSystem(d.ctx, fmt.Sprintf("/%v/host/%v", d.datacenter.Name(), name))
|
h, err := d.finder.HostSystem(d.ctx, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,15 @@ func (d *Driver) NewResourcePool(ref *types.ManagedObjectReference) *ResourcePoo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) FindResourcePool(host string, name string) (*ResourcePool, error) {
|
func (d *Driver) FindResourcePool(cluster string, host string, name string) (*ResourcePool, error) {
|
||||||
p, err := d.finder.ResourcePool(d.ctx, fmt.Sprintf("/%v/host/%v/Resources/%v", d.datacenter.Name(), host, name))
|
var res string
|
||||||
|
if cluster != "" {
|
||||||
|
res = cluster
|
||||||
|
} else {
|
||||||
|
res = host
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := d.finder.ResourcePool(d.ctx, fmt.Sprintf("%v/Resources/%v", res, name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ func TestResourcePoolAcc(t *testing.T) {
|
|||||||
initDriverAcceptanceTest(t)
|
initDriverAcceptanceTest(t)
|
||||||
|
|
||||||
d := newTestDriver(t)
|
d := newTestDriver(t)
|
||||||
p, err := d.FindResourcePool("esxi-1.vsphere65.test", "pool1/pool2")
|
p, err := d.FindResourcePool("","esxi-1.vsphere65.test", "pool1/pool2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Cannot find the default resource pool '%v': %v", "pool1/pool2", err)
|
t.Fatalf("Cannot find the default resource pool '%v': %v", "pool1/pool2", err)
|
||||||
}
|
}
|
||||||
|
18
driver/vm.go
18
driver/vm.go
@ -18,6 +18,7 @@ type VirtualMachine struct {
|
|||||||
type CloneConfig struct {
|
type CloneConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Folder string
|
Folder string
|
||||||
|
Cluster string
|
||||||
Host string
|
Host string
|
||||||
ResourcePool string
|
ResourcePool string
|
||||||
Datastore string
|
Datastore string
|
||||||
@ -44,6 +45,7 @@ type CreateConfig struct {
|
|||||||
Annotation string
|
Annotation string
|
||||||
Name string
|
Name string
|
||||||
Folder string
|
Folder string
|
||||||
|
Cluster string
|
||||||
Host string
|
Host string
|
||||||
ResourcePool string
|
ResourcePool string
|
||||||
Datastore string
|
Datastore string
|
||||||
@ -79,14 +81,18 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resourcePool, err := d.FindResourcePool(config.Host, config.ResourcePool)
|
resourcePool, err := d.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
host, err := d.FindHost(config.Host)
|
var host *object.HostSystem
|
||||||
if err != nil {
|
if config.Host != "" {
|
||||||
return nil, err
|
h, err := d.FindHost(config.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
host = h.host
|
||||||
}
|
}
|
||||||
|
|
||||||
datastore, err := d.FindDatastore(config.Datastore)
|
datastore, err := d.FindDatastore(config.Datastore)
|
||||||
@ -126,7 +132,7 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
|
|||||||
VmPathName: fmt.Sprintf("[%s]", datastore.Name()),
|
VmPathName: fmt.Sprintf("[%s]", datastore.Name()),
|
||||||
}
|
}
|
||||||
|
|
||||||
task, err := folder.folder.CreateVM(d.ctx, createSpec, resourcePool.pool, host.host)
|
task, err := folder.folder.CreateVM(d.ctx, createSpec, resourcePool.pool, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -172,7 +178,7 @@ func (template *VirtualMachine) Clone(config *CloneConfig) (*VirtualMachine, err
|
|||||||
|
|
||||||
var relocateSpec types.VirtualMachineRelocateSpec
|
var relocateSpec types.VirtualMachineRelocateSpec
|
||||||
|
|
||||||
pool, err := template.driver.FindResourcePool(config.Host, config.ResourcePool)
|
pool, err := template.driver.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -9,16 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CreateConfig struct {
|
type CreateConfig struct {
|
||||||
|
common.VMConfig `mapstructure:",squash"`
|
||||||
common.HardwareConfig `mapstructure:",squash"`
|
common.HardwareConfig `mapstructure:",squash"`
|
||||||
|
|
||||||
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
|
DiskThinProvisioned bool `mapstructure:"disk_thin_provisioned"`
|
||||||
DiskControllerType string `mapstructure:"disk_controller_type"`
|
DiskControllerType string `mapstructure:"disk_controller_type"`
|
||||||
|
|
||||||
VMName string `mapstructure:"vm_name"`
|
|
||||||
Folder string `mapstructure:"folder"`
|
|
||||||
Host string `mapstructure:"host"`
|
|
||||||
ResourcePool string `mapstructure:"resource_pool"`
|
|
||||||
Datastore string `mapstructure:"datastore"`
|
|
||||||
GuestOSType string `mapstructure:"guest_os_type"`
|
GuestOSType string `mapstructure:"guest_os_type"`
|
||||||
Network string `mapstructure:"network"`
|
Network string `mapstructure:"network"`
|
||||||
NetworkCard string `mapstructure:"network_card"`
|
NetworkCard string `mapstructure:"network_card"`
|
||||||
@ -32,16 +28,9 @@ func (c *CreateConfig) Prepare() []error {
|
|||||||
tmp := *c
|
tmp := *c
|
||||||
|
|
||||||
// do recursive calls
|
// do recursive calls
|
||||||
|
errs = append(errs, tmp.VMConfig.Prepare()...)
|
||||||
errs = append(errs, tmp.HardwareConfig.Prepare()...)
|
errs = append(errs, tmp.HardwareConfig.Prepare()...)
|
||||||
|
|
||||||
// check for errors
|
|
||||||
if tmp.VMName == "" {
|
|
||||||
errs = append(errs, fmt.Errorf("Target VM name is required"))
|
|
||||||
}
|
|
||||||
if tmp.Host == "" {
|
|
||||||
errs = append(errs, fmt.Errorf("vSphere host is required"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
@ -74,6 +63,7 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
|||||||
DiskControllerType: s.Config.DiskControllerType,
|
DiskControllerType: s.Config.DiskControllerType,
|
||||||
Name: s.Config.VMName,
|
Name: s.Config.VMName,
|
||||||
Folder: s.Config.Folder,
|
Folder: s.Config.Folder,
|
||||||
|
Cluster: s.Config.Cluster,
|
||||||
Host: s.Config.Host,
|
Host: s.Config.Host,
|
||||||
ResourcePool: s.Config.ResourcePool,
|
ResourcePool: s.Config.ResourcePool,
|
||||||
Datastore: s.Config.Datastore,
|
Datastore: s.Config.Datastore,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user