add machine name to VM name and hostname

This work sets the machine name in the Cloud API or Hypervisor. As well
as setting the hostname inside the VM.

I've added the machine name to the NewDriver func to allow for
identification by Cloud APIs and for the driver package.

Each driver will attempt to set the hostname for the VM, except for
Azure, which sets it based on the DNS name.

Signed-off-by: Simon Thulbourn <simon+github@thulbourn.com>
This commit is contained in:
Simon Thulbourn
2015-01-05 16:45:08 +00:00
parent 8a0d468a49
commit 861b16dba9
18 changed files with 237 additions and 145 deletions

View File

@@ -56,7 +56,7 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
}
c := ComputeUtil{
zone: driver.Zone,
instanceName: driver.InstanceName,
instanceName: driver.MachineName,
userName: driver.UserName,
project: driver.Project,
service: service,
@@ -207,6 +207,21 @@ func (c *ComputeUtil) createInstance(d *Driver) error {
return err
}
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 c.updateDocker(d)
}

View File

@@ -16,7 +16,7 @@ import (
// Driver is a struct compatible with the docker.hosts.drivers.Driver interface.
type Driver struct {
InstanceName string
MachineName string
Zone string
MachineType string
storePath string
@@ -28,11 +28,10 @@ type Driver struct {
// CreateFlags are the command line flags used to create a driver.
type CreateFlags struct {
InstanceName *string
Zone *string
MachineType *string
UserName *string
Project *string
Zone *string
MachineType *string
UserName *string
Project *string
}
func init() {
@@ -64,12 +63,6 @@ func GetCreateFlags() []cli.Flag {
Value: "docker-user",
EnvVar: "GOOGLE_USERNAME",
},
cli.StringFlag{
Name: "google-instance-name",
Usage: "GCE Instance Name",
Value: "docker-machine",
EnvVar: "GOOGLE_INSTANCE_NAME",
},
cli.StringFlag{
Name: "google-project",
Usage: "GCE Project",
@@ -79,8 +72,10 @@ func GetCreateFlags() []cli.Flag {
}
// NewDriver creates a Driver with the specified storePath.
func NewDriver(storePath string) (drivers.Driver, error) {
return &Driver{storePath: storePath,
func NewDriver(machineName string, storePath string) (drivers.Driver, error) {
return &Driver{
MachineName: machineName,
storePath: storePath,
sshKeyPath: path.Join(storePath, "id_rsa"),
publicSSHKeyPath: path.Join(storePath, "id_rsa.pub"),
}, nil
@@ -93,7 +88,6 @@ func (driver *Driver) DriverName() string {
// SetConfigFromFlags initializes the driver based on the command line flags.
func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
driver.InstanceName = flags.String("google-instance-name")
driver.Zone = flags.String("google-zone")
driver.MachineType = flags.String("google-machine-type")
driver.UserName = flags.String("google-username")
@@ -118,7 +112,7 @@ func (driver *Driver) Create() error {
// Check if the instance already exists. There will be an error if the instance
// doesn't exist, so just check instance for nil.
if instance, _ := c.instance(); instance != nil {
return fmt.Errorf("Instance %v already exists.", driver.InstanceName)
return fmt.Errorf("Instance %v already exists.", driver.MachineName)
}
log.Infof("Generating SSH Key")

View File

@@ -46,9 +46,11 @@ func init() {
// go test can't take args from stdin, so the path to an existing token must be passed as a flag.
os.Link(*tokenPath, path.Join(tmpDir, "gce_token"))
log.Fatal("hai")
driver = &Driver{
storePath: tmpDir,
InstanceName: "test-instance",
MachineName: "test-instance",
Zone: "us-central1-a",
MachineType: "n1-standard-1",
UserName: os.Getenv("USER"),
@@ -108,6 +110,7 @@ type operation struct {
DiskExpected bool
InstanceExpected bool
State state.State
Arguments []interface{}
}
func TestBasicOperations(t *testing.T) {