2017-08-23 15:40:57 -04:00
|
|
|
package driver
|
2017-07-01 23:46:38 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
"github.com/vmware/govmomi/find"
|
2017-07-01 23:46:38 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
2017-08-15 19:49:44 -04:00
|
|
|
"github.com/vmware/govmomi/session"
|
2020-08-31 04:34:41 -04:00
|
|
|
"github.com/vmware/govmomi/vapi/library"
|
2020-07-10 05:01:10 -04:00
|
|
|
"github.com/vmware/govmomi/vapi/rest"
|
2017-08-15 19:49:44 -04:00
|
|
|
"github.com/vmware/govmomi/vim25"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/soap"
|
2020-08-31 04:34:41 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2017-07-01 23:46:38 -04:00
|
|
|
)
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
type Driver interface {
|
|
|
|
NewVM(ref *types.ManagedObjectReference) VirtualMachine
|
|
|
|
FindVM(name string) (VirtualMachine, error)
|
|
|
|
FindCluster(name string) (*Cluster, error)
|
2020-11-19 14:54:31 -05:00
|
|
|
PreCleanVM(ui packersdk.Ui, vmPath string, force bool) error
|
2020-08-31 04:34:41 -04:00
|
|
|
CreateVM(config *CreateConfig) (VirtualMachine, error)
|
|
|
|
|
|
|
|
NewDatastore(ref *types.ManagedObjectReference) Datastore
|
|
|
|
FindDatastore(name string, host string) (Datastore, error)
|
|
|
|
GetDatastoreName(id string) (string, error)
|
|
|
|
GetDatastoreFilePath(datastoreID, dir, filename string) (string, error)
|
|
|
|
|
|
|
|
NewFolder(ref *types.ManagedObjectReference) *Folder
|
|
|
|
FindFolder(name string) (*Folder, error)
|
|
|
|
NewHost(ref *types.ManagedObjectReference) *Host
|
|
|
|
FindHost(name string) (*Host, error)
|
|
|
|
NewNetwork(ref *types.ManagedObjectReference) *Network
|
|
|
|
FindNetwork(name string) (*Network, error)
|
|
|
|
FindNetworks(name string) ([]*Network, error)
|
|
|
|
NewResourcePool(ref *types.ManagedObjectReference) *ResourcePool
|
|
|
|
FindResourcePool(cluster string, host string, name string) (*ResourcePool, error)
|
|
|
|
|
|
|
|
FindContentLibraryByName(name string) (*Library, error)
|
|
|
|
FindContentLibraryItem(libraryId string, name string) (*library.Item, error)
|
|
|
|
FindContentLibraryFileDatastorePath(isoPath string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type VCenterDriver struct {
|
2020-07-14 05:45:19 -04:00
|
|
|
// context that controls the authenticated sessions used to run the VM commands
|
2017-07-01 23:46:38 -04:00
|
|
|
ctx context.Context
|
|
|
|
client *govmomi.Client
|
2020-08-24 10:54:30 -04:00
|
|
|
vimClient *vim25.Client
|
2020-08-20 11:55:25 -04:00
|
|
|
restClient *RestClient
|
2017-07-01 23:46:38 -04:00
|
|
|
finder *find.Finder
|
2017-07-02 16:29:50 -04:00
|
|
|
datacenter *object.Datacenter
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
|
2017-08-23 15:40:57 -04:00
|
|
|
type ConnectConfig struct {
|
|
|
|
VCenterServer string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
InsecureConnection bool
|
|
|
|
Datacenter string
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func NewDriver(config *ConnectConfig) (Driver, error) {
|
2020-07-14 05:45:19 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
vcenterUrl, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
|
2017-07-01 23:46:38 -04:00
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
2017-08-15 19:49:44 -04:00
|
|
|
credentials := url.UserPassword(config.Username, config.Password)
|
2019-07-08 10:48:37 -04:00
|
|
|
vcenterUrl.User = credentials
|
2017-08-15 19:49:44 -04:00
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
soapClient := soap.NewClient(vcenterUrl, config.InsecureConnection)
|
2017-08-15 19:49:44 -04:00
|
|
|
vimClient, err := vim25.NewClient(ctx, soapClient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vimClient.RoundTripper = session.KeepAlive(vimClient.RoundTripper, 10*time.Minute)
|
|
|
|
client := &govmomi.Client{
|
|
|
|
Client: vimClient,
|
|
|
|
SessionManager: session.NewManager(vimClient),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SessionManager.Login(ctx, credentials)
|
2017-07-01 23:46:38 -04:00
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
finder := find.NewFinder(client.Client, false)
|
|
|
|
datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter)
|
|
|
|
if err != nil {
|
2017-07-02 16:29:50 -04:00
|
|
|
return nil, err
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
|
|
|
finder.SetDatacenter(datacenter)
|
|
|
|
|
2020-08-31 10:26:48 -04:00
|
|
|
d := &VCenterDriver{
|
2020-08-24 10:54:30 -04:00
|
|
|
ctx: ctx,
|
|
|
|
client: client,
|
|
|
|
vimClient: vimClient,
|
2020-08-20 11:55:25 -04:00
|
|
|
restClient: &RestClient{
|
|
|
|
client: rest.NewClient(vimClient),
|
|
|
|
credentials: credentials,
|
|
|
|
},
|
2017-07-01 23:46:38 -04:00
|
|
|
datacenter: datacenter,
|
|
|
|
finder: finder,
|
|
|
|
}
|
2020-08-31 04:34:41 -04:00
|
|
|
return d, nil
|
2017-07-01 23:46:38 -04:00
|
|
|
}
|
2020-08-20 11:55:25 -04:00
|
|
|
|
|
|
|
// The rest.Client requires vCenter.
|
|
|
|
// RestClient is to modularize the rest.Client session and use it only when is necessary.
|
|
|
|
// This will allow users without vCenter to use the other features that doesn't use the rest.Client.
|
|
|
|
// To use the client login/logout must be done to create an authenticated session.
|
|
|
|
type RestClient struct {
|
|
|
|
client *rest.Client
|
|
|
|
credentials *url.Userinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RestClient) Login(ctx context.Context) error {
|
|
|
|
return r.client.Login(ctx, r.credentials)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RestClient) Logout(ctx context.Context) error {
|
|
|
|
return r.client.Logout(ctx)
|
|
|
|
}
|