From 0719b6b1406dd4dd29648f3ce3bc5503ebbad399 Mon Sep 17 00:00:00 2001 From: Juan Font Date: Sun, 14 Feb 2021 00:24:18 +0000 Subject: [PATCH] More methods added to the driver. --- driver/vcd.go | 152 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 21 deletions(-) diff --git a/driver/vcd.go b/driver/vcd.go index 75d0b8e..31d680b 100644 --- a/driver/vcd.go +++ b/driver/vcd.go @@ -1,9 +1,13 @@ package vcd import ( + "fmt" + "net" "net/url" + "strconv" "github.com/docker/machine/libmachine/drivers" + "github.com/docker/machine/libmachine/mcnflag" "github.com/docker/machine/libmachine/state" "github.com/vmware/go-vcloud-director/v2/govcd" ) @@ -13,16 +17,15 @@ type Driver struct { VcdURL *url.URL VcdOrg string VcdVdc string // virtual datacenter - VcdAllowInsecure bool + VcdInsecure bool VcdUser string VcdPassword string + VcdOrgVDCNetwork string - ComputeID string - OrgVDCNet string EdgeGateway string PublicIP string Catalog string - CatalogItem string + Template string DockerPort int CPUCount int MemorySize int @@ -30,21 +33,21 @@ type Driver struct { } const ( - defaultCatalog = "Public Catalog" - defaultCatalogItem = "Ubuntu Server 12.04 LTS (amd64 20150127)" - defaultCpus = 1 - defaultMemory = 2048 - defaultSSHPort = 22 - defaultDockerPort = 2376 + defaultCatalog = "Public" + defaultTemplate = "Ubuntu_Server_20.04" + defaultCpus = 1 + defaultMemory = 2048 + defaultSSHPort = 22 + defaultDockerPort = 2376 ) func NewDriver(hostName, storePath string) drivers.Driver { return &Driver{ - Catalog: defaultCatalog, - CatalogItem: defaultCatalogItem, - CPUCount: defaultCpus, - MemorySize: defaultMemory, - DockerPort: defaultDockerPort, + Catalog: defaultCatalog, + Template: defaultTemplate, + CPUCount: defaultCpus, + MemorySize: defaultMemory, + DockerPort: defaultDockerPort, BaseDriver: &drivers.BaseDriver{ SSHPort: defaultSSHPort, MachineName: hostName, @@ -63,9 +66,116 @@ func (d *Driver) DriverName() string { return "vcd" } +// GetCreateFlags returns the mcnflag.Flag slice representing the flags +// that can be set, their descriptions and defaults. +func (d *Driver) GetCreateFlags() []mcnflag.Flag { + return []mcnflag.Flag{ + mcnflag.StringFlag{ + EnvVar: "VCD_URL", + Name: "vcd-url", + Usage: "vCloud Director URL", + }, + mcnflag.StringFlag{ + EnvVar: "VCD_ORG", + Name: "vcd-org", + Usage: "vCloud Director Org", + }, + mcnflag.StringFlag{ + EnvVar: "VCD_VDC", + Name: "vcd-vdc", + Usage: "vCloud Director Virtual Datacenter", + }, + mcnflag.BoolFlag{ + EnvVar: "VCD_INSECURE", + Name: "vcd-insecure", + Usage: "vCloud Director Insecure Connection", + }, + mcnflag.StringFlag{ + EnvVar: "VCD_USERNAME", + Name: "vcd-username", + Usage: "vCloud Director username", + }, + mcnflag.StringFlag{ + EnvVar: "VCD_PASSWORD", + Name: "vcd-password", + Usage: "vCloud Director password", + }, + mcnflag.StringFlag{ + EnvVar: "VDC_ORGVDCNETWORK", + Name: "vdc-orgvdcnetwork", + Usage: "vCloud Director OrgVDC network", + }, + mcnflag.StringFlag{ + EnvVar: "VCD_CATALOG", + Name: "vcd-catalog", + Usage: "vCloud Director catalog", + Value: defaultCatalog, + }, + mcnflag.StringFlag{ + EnvVar: "VCD_TEMPLATE", + Name: "vcd-template", + Usage: "vCloud Director vApp template", + Value: defaultTemplate, + }, + } +} + +// GetIP returns an IP or hostname that this host is available at +// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net +func (d *Driver) GetIP() (string, error) { + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) + err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) + if err != nil { + return "", err + } + org, err := client.GetOrgByName(d.VcdOrg) + if err != nil { + return "", err + } + vdc, err := org.GetVDCByName(d.VcdVdc, false) + if err != nil { + return "", err + } + vapp, err := vdc.GetVAppById(d.VAppID, true) + if err != nil { + return "", err + } + + // We assume that the vApp has only one VM with only one NIC + for _, vm := range vapp.VApp.Children.VM { + if vm.NetworkConnectionSection != nil { + networks := vm.NetworkConnectionSection.NetworkConnection + for _, n := range networks { + if n.ExternalIPAddress != "" { + return n.ExternalIPAddress, nil + } + } + } + } + return "", fmt.Errorf("could not get public IP") +} + +// GetSSHHostname returns the IP of the server +func (d *Driver) GetSSHHostname() (string, error) { + return d.GetIP() +} + +// GetURL returns a Docker compatible host URL for connecting to this host +// e.g. tcp://1.2.3.4:2376 +func (d *Driver) GetURL() (string, error) { + if err := drivers.MustBeRunning(d); err != nil { + return "", err + } + ip, err := d.GetIP() + if err != nil { + return "", err + } + return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, strconv.Itoa(d.DockerPort))), nil +} + // GetState returns the state that the host is in (running, stopped, etc) func (d *Driver) GetState() (state.State, error) { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return state.Error, err @@ -103,7 +213,7 @@ func (d *Driver) GetState() (state.State, error) { // Kill stops a host forcefully func (d *Driver) Kill() error { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return err @@ -142,7 +252,7 @@ func (d *Driver) PreCreateCheck() error { // Remove a host func (d *Driver) Remove() error { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return err @@ -183,7 +293,7 @@ func (d *Driver) Remove() error { // Restart a host. This may just call Stop(); Start() if the provider does not // have any special restart behaviour. func (d *Driver) Restart() error { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return err @@ -222,7 +332,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error { // Start a host func (d *Driver) Start() error { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return err @@ -255,7 +365,7 @@ func (d *Driver) Start() error { // Stop a host gracefully func (d *Driver) Stop() error { - client := govcd.NewVCDClient(*d.VcdURL, d.VcdAllowInsecure) + client := govcd.NewVCDClient(*d.VcdURL, d.VcdInsecure) err := client.Authenticate(d.VcdUser, d.VcdPassword, d.VcdOrg) if err != nil { return err