Skip to content

Commit

Permalink
New Feature: docker#1342 Get an internal IP of cloud hosted machine (…
Browse files Browse the repository at this point in the history
…actual implementation for GCE, placeholders for the rest currently)

Signed-off-by: Andrew Grande <[email protected]>
  • Loading branch information
aperepel committed Jun 12, 2015
1 parent 6d26992 commit 6099f0e
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 30 deletions.
7 changes: 7 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ var Commands = []cli.Command{
Usage: "Get the IP address of a machine",
Description: "Argument(s) are one or more machine names.",
Action: cmdIp,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "private, p",
Usage: "Return an instance's private IP address.",
},
},
},
{
Name: "kill",
Expand Down Expand Up @@ -410,6 +416,7 @@ func machineCommand(actionName string, host *libmachine.Host, errorChan chan<- e
"kill": host.Kill,
"upgrade": host.Upgrade,
"ip": host.PrintIP,
"privateIp": host.PrintPrivateIP,
}

log.Debugf("command=%s machine=%s", actionName, host.Name)
Expand Down
13 changes: 12 additions & 1 deletion commands/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import (
)

func cmdIp(c *cli.Context) {
if err := runActionWithContext("ip", c); err != nil {
if len(c.Args()) == 0 {
cli.ShowCommandHelp(c, "ip")
log.Fatal("You must specify a machine name")
}

ctx := "ip"

if c.Bool("private") {
ctx = "privateIp"
}

if err := runActionWithContext(ctx, c); err != nil {
log.Fatal(err)
}
}
5 changes: 5 additions & 0 deletions drivers/amazonec2/amazonec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package amazonec2
import (
"crypto/md5"
"crypto/rand"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -442,6 +443,10 @@ func (d *Driver) GetIP() (string, error) {
return inst.IpAddress, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
inst, err := d.getInstance()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions drivers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (d *Driver) GetIP() (string, error) {
return d.getHostname(), nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
if err := d.setUserSubscription(); err != nil {
return state.Error, err
Expand Down
5 changes: 5 additions & 0 deletions drivers/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package digitalocean

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -266,6 +267,10 @@ func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
droplet, _, err := d.getClient().Droplets.Get(d.DropletID)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Driver interface {
// e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net
GetIP() (string, error)

// GetPrivateIP returns an internal host IP
GetPrivateIP() (string, error)

// GetMachineName returns the name of the machine
GetMachineName() string

Expand Down
5 changes: 5 additions & 0 deletions drivers/exoscale/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exoscale

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -173,6 +174,10 @@ func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey)
vm, err := client.GetVirtualMachine(d.Id)
Expand Down
5 changes: 5 additions & 0 deletions drivers/fakedriver/fakedriver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fakedriver

import (
"errors"
"github.com/docker/machine/drivers"
"github.com/docker/machine/state"
)
Expand Down Expand Up @@ -37,6 +38,10 @@ func (d *FakeDriver) GetIP() (string, error) {
return "1.2.3.4", nil
}

func (d *Driver) GetPrivateIP() (string, error) {
errors.New("not implemented")
}

func (d *FakeDriver) GetSSHHostname() (string, error) {
return "", nil
}
Expand Down
5 changes: 5 additions & 0 deletions drivers/generic/generic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic

import (
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -164,6 +165,10 @@ func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
addr := fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort)
_, err := net.DialTimeout("tcp", addr, defaultTimeout)
Expand Down
37 changes: 25 additions & 12 deletions drivers/google/compute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import (

// ComputeUtil is used to wrap the raw GCE API code and store common parameters.
type ComputeUtil struct {
zone string
instanceName string
userName string
project string
diskTypeURL string
service *raw.Service
zoneURL string
authTokenPath string
globalURL string
ipAddress string
SwarmMaster bool
SwarmHost string
zone string
instanceName string
userName string
project string
diskTypeURL string
service *raw.Service
zoneURL string
authTokenPath string
globalURL string
ipAddress string
privateIpAddress string
SwarmMaster bool
SwarmHost string
}

const (
Expand Down Expand Up @@ -305,3 +306,15 @@ func (c *ComputeUtil) ip() (string, error) {
}
return c.ipAddress, nil
}

// returns an internal IP address of the instance.
func (c *ComputeUtil) privateIp() (string, error) {
if c.privateIpAddress == "" {
instance, err := c.service.Instances.Get(c.project, c.zone, c.instanceName).Do()
if err != nil {
return "", err
}
c.privateIpAddress = instance.NetworkInterfaces[0].NetworkIP
}
return c.privateIpAddress, nil
}
45 changes: 28 additions & 17 deletions drivers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ import (

// Driver is a struct compatible with the docker.hosts.drivers.Driver interface.
type Driver struct {
IPAddress string
MachineName string
SSHUser string
SSHPort int
Zone string
MachineType string
DiskType string
Scopes string
DiskSize int
AuthTokenPath string
storePath string
Project string
CaCertPath string
PrivateKeyPath string
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
IPAddress string
PrivateIPAddress string
MachineName string
SSHUser string
SSHPort int
Zone string
MachineType string
DiskType string
Scopes string
DiskSize int
AuthTokenPath string
storePath string
Project string
CaCertPath string
PrivateKeyPath string
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
}

func init() {
Expand Down Expand Up @@ -211,6 +212,15 @@ func (d *Driver) GetIP() (string, error) {
return c.ip()
}

// returns the internal IP address of the GCE instance.
func (d *Driver) GetPrivateIP() (string, error) {
c, err := newComputeUtil(d)
if err != nil {
return "", err
}
return c.privateIp()
}

// GetState returns a docker.hosts.state.State value representing the current state of the host.
func (d *Driver) GetState() (state.State, error) {
c, err := newComputeUtil(d)
Expand Down Expand Up @@ -250,6 +260,7 @@ func (d *Driver) Start() error {
return err
}
d.IPAddress, err = d.GetIP()
d.PrivateIPAddress, err = d.GetPrivateIP()
return err
}

Expand Down
5 changes: 5 additions & 0 deletions drivers/none/none.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package none

import (
"errors"
"fmt"
neturl "net/url"

Expand Down Expand Up @@ -58,6 +59,10 @@ func (d *Driver) GetIP() (string, error) {
return d.IPAddress, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetMachineName() string {
return ""
}
Expand Down
5 changes: 5 additions & 0 deletions drivers/openstack/openstack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openstack

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -312,6 +313,10 @@ func (d *Driver) GetIP() (string, error) {
return "", fmt.Errorf("No IP found for the machine")
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
log.WithField("MachineId", d.MachineId).Debug("Get status for OpenStack instance...")
if err := d.initCompute(); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions drivers/softlayer/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package softlayer

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -305,6 +306,11 @@ func (d *Driver) GetIP() (string, error) {
}
}

func (d *Driver) GetPrivateIP() (string, error) {
// TODO there's some private ip code in GetIP(), review for applicability
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
s, err := d.getClient().VirtualGuest().PowerState(d.Id)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions drivers/virtualbox/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ func (d *Driver) GetIP() (string, error) {
return "", fmt.Errorf("No IP address found %s", output)
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
Expand Down
5 changes: 5 additions & 0 deletions drivers/vmwarefusion/fusion_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package vmwarefusion

import (
"archive/tar"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -182,6 +183,10 @@ func (d *Driver) GetIP() (string, error) {
return ip, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
// VMRUN only tells use if the vm is running or not
if stdout, _, _ := vmrun("list"); strings.Contains(stdout, d.vmxPath()) {
Expand Down
5 changes: 5 additions & 0 deletions drivers/vmwarevcloudair/vcloudair.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package vmwarevcloudair

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -243,6 +244,10 @@ func (d *Driver) GetIP() (string, error) {
return d.PublicIP, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", errors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {

p, err := govcloudair.NewClient()
Expand Down
5 changes: 5 additions & 0 deletions drivers/vmwarevsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package vmwarevsphere

import (
"archive/tar"
sysErrors "errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -228,6 +229,10 @@ func (d *Driver) GetIP() (string, error) {
return ip, nil
}

func (d *Driver) GetPrivateIP() (string, error) {
return "", sysErrors.New("not implemented")
}

func (d *Driver) GetState() (state.State, error) {
vcConn := NewVcConn(d)
stdout, err := vcConn.VMInfo()
Expand Down
Loading

0 comments on commit 6099f0e

Please sign in to comment.