Skip to content

Commit

Permalink
More methods added to the driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont authored Feb 14, 2021
1 parent 6de2a67 commit 0719b6b
Showing 1 changed file with 131 additions and 21 deletions.
152 changes: 131 additions & 21 deletions driver/vcd.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -13,38 +17,37 @@ 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
VAppID string
}

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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0719b6b

Please sign in to comment.