google: updated to new driver interface

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-03-09 23:18:18 -04:00
parent d2869cbd38
commit ec91959979
No known key found for this signature in database
GPG Key ID: A519480096146526
3 changed files with 98 additions and 155 deletions

View File

@ -18,7 +18,7 @@ import (
_ "github.com/docker/machine/drivers/amazonec2" _ "github.com/docker/machine/drivers/amazonec2"
_ "github.com/docker/machine/drivers/azure" _ "github.com/docker/machine/drivers/azure"
_ "github.com/docker/machine/drivers/digitalocean" _ "github.com/docker/machine/drivers/digitalocean"
//_ "github.com/docker/machine/drivers/google" _ "github.com/docker/machine/drivers/google"
//_ "github.com/docker/machine/drivers/hyperv" //_ "github.com/docker/machine/drivers/hyperv"
_ "github.com/docker/machine/drivers/none" _ "github.com/docker/machine/drivers/none"
//_ "github.com/docker/machine/drivers/openstack" //_ "github.com/docker/machine/drivers/openstack"

View File

@ -29,7 +29,6 @@ type ComputeUtil struct {
const ( const (
apiURL = "https://www.googleapis.com/compute/v1/projects/" apiURL = "https://www.googleapis.com/compute/v1/projects/"
//imageName = "https://www.googleapis.com/compute/v1/projects/google-containers/global/images/container-vm-v20150129"
imageName = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150128" imageName = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20150128"
firewallRule = "docker-machines" firewallRule = "docker-machines"
port = "2376" port = "2376"
@ -48,7 +47,7 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
authTokenPath: driver.AuthTokenPath, authTokenPath: driver.AuthTokenPath,
zone: driver.Zone, zone: driver.Zone,
instanceName: driver.MachineName, instanceName: driver.MachineName,
userName: driver.UserName, userName: driver.SSHUser,
project: driver.Project, project: driver.Project,
service: service, service: service,
zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone, zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone,
@ -203,7 +202,7 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
c.waitForSSH(ip) c.waitForSSH(ip)
// Update the SSH Key // Update the SSH Key
sshKey, err := ioutil.ReadFile(d.publicSSHKeyPath) sshKey, err := ioutil.ReadFile(d.GetSSHKeyPath() + ".pub")
if err != nil { if err != nil {
return err return err
} }
@ -226,39 +225,6 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
return err return err
} }
log.Info("Configuring Machine...")
log.Debugf("Setting hostname: %s", d.MachineName)
cmd, err := d.GetSSHCommand(fmt.Sprintf(
"echo \"127.0.0.1 %s\" | sudo tee -a /etc/hosts && sudo hostname %s && echo \"%s\" | sudo tee /etc/hostname",
d.MachineName,
d.MachineName,
d.MachineName,
))
if err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func (c *ComputeUtil) updateDocker(d *Driver) error {
log.Debugf("Upgrading Docker")
cmd, err := d.GetSSHCommand("sudo apt-get update && sudo apt-get install --upgrade lxc-docker")
if err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
return nil return nil
} }

View File

@ -2,16 +2,14 @@ package google
import ( import (
"fmt" "fmt"
"path/filepath"
"github.com/docker/machine/state"
"os/exec"
"path"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/machine/drivers" "github.com/docker/machine/drivers"
"github.com/docker/machine/provider"
"github.com/docker/machine/ssh" "github.com/docker/machine/ssh"
"github.com/docker/machine/state"
) )
const ( const (
@ -21,18 +19,17 @@ const (
// Driver is a struct compatible with the docker.hosts.drivers.Driver interface. // Driver is a struct compatible with the docker.hosts.drivers.Driver interface.
type Driver struct { type Driver struct {
MachineName string MachineName string
SSHUser string
SSHPort int
Zone string Zone string
MachineType string MachineType string
Scopes string Scopes string
DiskSize int DiskSize int
AuthTokenPath string AuthTokenPath string
storePath string storePath string
UserName string
Project string Project string
CaCertPath string CaCertPath string
PrivateKeyPath string PrivateKeyPath string
sshKeyPath string
publicSSHKeyPath string
SwarmMaster bool SwarmMaster bool
SwarmHost string SwarmHost string
SwarmDiscovery string SwarmDiscovery string
@ -42,7 +39,6 @@ type Driver struct {
type CreateFlags struct { type CreateFlags struct {
Zone *string Zone *string
MachineType *string MachineType *string
UserName *string
Project *string Project *string
Scopes *string Scopes *string
DiskSize *int DiskSize *int
@ -109,36 +105,67 @@ func NewDriver(machineName string, storePath string, caCert string, privateKey s
storePath: storePath, storePath: storePath,
CaCertPath: caCert, CaCertPath: caCert,
PrivateKeyPath: privateKey, PrivateKeyPath: privateKey,
sshKeyPath: path.Join(storePath, "id_rsa"),
publicSSHKeyPath: path.Join(storePath, "id_rsa.pub"),
}, nil }, nil
} }
func (d *Driver) AuthorizePort(ports []*drivers.Port) error {
return nil
}
func (d *Driver) DeauthorizePort(ports []*drivers.Port) error {
return nil
}
func (d *Driver) GetMachineName() string {
return d.MachineName
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
func (d *Driver) GetSSHKeyPath() string {
return filepath.Join(d.storePath, "id_rsa")
}
func (d *Driver) GetSSHPort() (int, error) {
return d.SSHPort, nil
}
func (d *Driver) GetSSHUsername() string {
return d.SSHUser
}
func (d *Driver) GetProviderType() provider.ProviderType {
return provider.Remote
}
// DriverName returns the name of the driver. // DriverName returns the name of the driver.
func (driver *Driver) DriverName() string { func (d *Driver) DriverName() string {
return "google" return "google"
} }
// SetConfigFromFlags initializes the driver based on the command line flags. // SetConfigFromFlags initializes the driver based on the command line flags.
func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
driver.Zone = flags.String("google-zone") d.Zone = flags.String("google-zone")
driver.MachineType = flags.String("google-machine-type") d.MachineType = flags.String("google-machine-type")
driver.DiskSize = flags.Int("google-disk-size") d.DiskSize = flags.Int("google-disk-size")
driver.UserName = flags.String("google-username") d.AuthTokenPath = flags.String("google-auth-token")
driver.AuthTokenPath = flags.String("google-auth-token") d.Project = flags.String("google-project")
driver.Project = flags.String("google-project") d.Scopes = flags.String("google-scopes")
driver.Scopes = flags.String("google-scopes") d.SwarmMaster = flags.Bool("swarm-master")
driver.SwarmMaster = flags.Bool("swarm-master") d.SwarmHost = flags.String("swarm-host")
driver.SwarmHost = flags.String("swarm-host") d.SwarmDiscovery = flags.String("swarm-discovery")
driver.SwarmDiscovery = flags.String("swarm-discovery") if d.Project == "" {
if driver.Project == "" {
return fmt.Errorf("Please specify the Google Cloud Project name using the option --google-project.") return fmt.Errorf("Please specify the Google Cloud Project name using the option --google-project.")
} }
d.SSHUser = flags.String("google-username")
d.SSHPort = 22
return nil return nil
} }
func (driver *Driver) initApis() (*ComputeUtil, error) { func (d *Driver) initApis() (*ComputeUtil, error) {
return newComputeUtil(driver) return newComputeUtil(d)
} }
func (d *Driver) PreCreateCheck() error { func (d *Driver) PreCreateCheck() error {
@ -146,8 +173,8 @@ func (d *Driver) PreCreateCheck() error {
} }
// Create creates a GCE VM instance acting as a docker host. // Create creates a GCE VM instance acting as a docker host.
func (driver *Driver) Create() error { func (d *Driver) Create() error {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
@ -155,20 +182,20 @@ func (driver *Driver) Create() error {
// Check if the instance already exists. There will be an error if the instance // Check if the instance already exists. There will be an error if the instance
// doesn't exist, so just check instance for nil. // doesn't exist, so just check instance for nil.
if instance, _ := c.instance(); instance != nil { if instance, _ := c.instance(); instance != nil {
return fmt.Errorf("Instance %v already exists.", driver.MachineName) return fmt.Errorf("Instance %v already exists.", d.MachineName)
} }
log.Infof("Generating SSH Key") log.Infof("Generating SSH Key")
if err := ssh.GenerateSSHKey(driver.sshKeyPath); err != nil { if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err return err
} }
return c.createInstance(driver) return c.createInstance(d)
} }
// GetURL returns the URL of the remote docker daemon. // GetURL returns the URL of the remote docker daemon.
func (driver *Driver) GetURL() (string, error) { func (d *Driver) GetURL() (string, error) {
ip, err := driver.GetIP() ip, err := d.GetIP()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -177,8 +204,8 @@ func (driver *Driver) GetURL() (string, error) {
} }
// GetIP returns the IP address of the GCE instance. // GetIP returns the IP address of the GCE instance.
func (driver *Driver) GetIP() (string, error) { func (d *Driver) GetIP() (string, error) {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -186,8 +213,8 @@ func (driver *Driver) GetIP() (string, error) {
} }
// GetState returns a docker.hosts.state.State value representing the current state of the host. // GetState returns a docker.hosts.state.State value representing the current state of the host.
func (driver *Driver) GetState() (state.State, error) { func (d *Driver) GetState() (state.State, error) {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return state.None, err return state.None, err
} }
@ -215,17 +242,17 @@ func (driver *Driver) GetState() (state.State, error) {
} }
// Start creates a GCE instance and attaches it to the existing disk. // Start creates a GCE instance and attaches it to the existing disk.
func (driver *Driver) Start() error { func (d *Driver) Start() error {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
return c.createInstance(driver) return c.createInstance(d)
} }
// Stop deletes the GCE instance, but keeps the disk. // Stop deletes the GCE instance, but keeps the disk.
func (driver *Driver) Stop() error { func (d *Driver) Stop() error {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
@ -233,12 +260,12 @@ func (driver *Driver) Stop() error {
} }
// Remove deletes the GCE instance and the disk. // Remove deletes the GCE instance and the disk.
func (driver *Driver) Remove() error { func (d *Driver) Remove() error {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
s, err := driver.GetState() s, err := d.GetState()
if err != nil { if err != nil {
return err return err
} }
@ -251,8 +278,8 @@ func (driver *Driver) Remove() error {
} }
// Restart deletes and recreates the GCE instance, keeping the disk. // Restart deletes and recreates the GCE instance, keeping the disk.
func (driver *Driver) Restart() error { func (d *Driver) Restart() error {
c, err := newComputeUtil(driver) c, err := newComputeUtil(d)
if err != nil { if err != nil {
return err return err
} }
@ -260,60 +287,10 @@ func (driver *Driver) Restart() error {
return err return err
} }
return c.createInstance(driver) return c.createInstance(d)
} }
// Kill deletes the GCE instance, but keeps the disk. // Kill deletes the GCE instance, but keeps the disk.
func (driver *Driver) Kill() error { func (d *Driver) Kill() error {
return driver.Stop() return d.Stop()
}
func (d *Driver) StartDocker() error {
log.Debug("Starting Docker...")
cmd, err := d.GetSSHCommand("sudo service docker start")
if err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func (d *Driver) StopDocker() error {
log.Debug("Stopping Docker...")
cmd, err := d.GetSSHCommand("sudo service docker stop")
if err != nil {
return err
}
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func (d *Driver) GetDockerConfigDir() string {
return dockerConfigDir
}
// GetSSHCommand returns a command that will run over SSH on the GCE instance.
func (driver *Driver) GetSSHCommand(args ...string) (*exec.Cmd, error) {
ip, err := driver.GetIP()
if err != nil {
return nil, err
}
return ssh.GetSSHCommand(ip, 22, driver.UserName, driver.sshKeyPath, args...), nil
}
// Upgrade upgrades the docker daemon on the host to the latest version.
func (driver *Driver) Upgrade() error {
c, err := newComputeUtil(driver)
if err != nil {
return err
}
return c.updateDocker(driver)
} }