Adding linked clones (#17)

* Simple linked cloning, just crashing if no snapshots
This commit is contained in:
Elizaveta Tretyakova 2017-06-12 01:45:25 +03:00 committed by GitHub
parent ac139471c8
commit 66705e4a26
2 changed files with 21 additions and 0 deletions

View File

@ -27,6 +27,7 @@ type Config struct {
Host string `mapstructure:"host"`
ResourcePool string `mapstructure:"resource_pool"`
Datastore string `mapstructure:"datastore"`
LinkedClone bool `mapstructure:"linked_clone"`
// Hardware
Cpus string `mapstructure:"cpus"`

View File

@ -10,6 +10,8 @@ import (
"github.com/vmware/govmomi/find"
"fmt"
"net/url"
"github.com/vmware/govmomi/vim25/mo"
"errors"
)
type CloneParameters struct {
@ -20,6 +22,7 @@ type CloneParameters struct {
vmSrc *object.VirtualMachine
ctx context.Context
vmName string
linkedClone bool
}
type StepCloneVM struct{
@ -86,6 +89,7 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
vmSrc: vmSrc,
ctx: ctx,
vmName: s.config.VMName,
linkedClone: s.config.LinkedClone,
})
if err != nil {
state.Put("error", err)
@ -139,11 +143,27 @@ func cloneVM(params *CloneParameters) (vm *object.VirtualMachine, err error) {
datastoreRef := params.datastore.Reference()
relocateSpec.Datastore = &datastoreRef
}
if params.linkedClone == true {
relocateSpec.DiskMoveType = "createNewChildDiskBacking"
}
cloneSpec := types.VirtualMachineCloneSpec{
Location: relocateSpec,
PowerOn: false,
}
if params.linkedClone == true {
var vmImage mo.VirtualMachine
err = params.vmSrc.Properties(params.ctx, params.vmSrc.Reference(), []string{"snapshot"}, &vmImage)
if err != nil {
err = fmt.Errorf("Error reading base VM properties: %s", err)
return
}
if vmImage.Snapshot == nil {
err = errors.New("`linked_clone=true`, but image VM has no snapshots")
return
}
cloneSpec.Snapshot = vmImage.Snapshot.CurrentSnapshot
}
// Cloning itself
task, err := params.vmSrc.Clone(params.ctx, params.folder, params.vmName, cloneSpec)