From 9664d68889c62efd03190c6ea09884a20a58f03d Mon Sep 17 00:00:00 2001 From: Evan Hazlett Date: Wed, 14 Jan 2015 20:39:11 -0500 Subject: [PATCH] rebase and updated fusion to work with standard docker b2d 1.4.1 Signed-off-by: Evan Hazlett --- commands.go | 36 +++++++++++++++++++--- drivers/virtualbox/virtualbox.go | 12 ++++---- drivers/vmwarefusion/fusion.go | 52 +++++++++++++++++++++----------- host.go | 24 +++++++-------- 4 files changed, 83 insertions(+), 41 deletions(-) diff --git a/commands.go b/commands.go index 60a7578b2c..1d8f7eadc0 100644 --- a/commands.go +++ b/commands.go @@ -75,6 +75,11 @@ var Commands = []cli.Command{ Usage: "Create a machine", Action: cmdCreate, }, + { + Name: "config", + Usage: "Print the connection config for machine", + Action: cmdConfig, + }, { Name: "inspect", Usage: "Inspect information about a machine", @@ -207,14 +212,35 @@ func cmdCreate(c *cli.Context) { } log.Infof("%q has been created and is now the active machine", name) - // TODO @ehazlett - this will change but at least show how to connect for now - log.Info("To connect, pass these args to Docker: ") - storeDir := c.GlobalString("storage-path") + // TODO @ehazlett: this will likely change but at least show how to connect for now + log.Infof("To connect, use docker $(machine config %s)", name) +} + +func cmdConfig(c *cli.Context) { + + name := c.Args().First() + if name == "" { + cli.ShowCommandHelp(c, "config") + log.Fatal("You must specify a machine name") + } + + store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key")) + + host, err := store.Load(name) + if err != nil { + log.Fatalf("Error loading machine config: %s", err) + } + + storeDir := store.Path caCert := filepath.Join(storeDir, name, "ca.pem") clientCert := filepath.Join(storeDir, name, "client.pem") clientKey := filepath.Join(storeDir, name, "client-key.pem") - log.Infof("--auth=cert --auth-ca=%s --auth-cert=%s --auth-key=%s -H $(machine url)", - caCert, clientCert, clientKey) + machineUrl, err := host.GetURL() + if err != nil { + log.Fatalf("Error getting machine url: %s", err) + } + fmt.Printf("--tls --tlscacert=%s --tlscert=%s --tlskey=%s -H %s", + caCert, clientCert, clientKey, machineUrl) } func cmdInspect(c *cli.Context) { diff --git a/drivers/virtualbox/virtualbox.go b/drivers/virtualbox/virtualbox.go index ae3afb72ce..a57f120b65 100644 --- a/drivers/virtualbox/virtualbox.go +++ b/drivers/virtualbox/virtualbox.go @@ -138,15 +138,15 @@ func (d *Driver) Create() error { } } else { // HACK: Docker 1.4.1 boot2docker image with client/daemon auth - isoURL = "https://ejhazlett.s3.amazonaws.com/public/boot2docker/machine-b2d-docker-1.4.1-identity.iso" + //isoURL = "https://ejhazlett.s3.amazonaws.com/public/boot2docker/machine-b2d-docker-1.4.1-identity.iso" // todo: check latest release URL, download if it's new // until then always use "latest" - // isoURL, err = getLatestReleaseURL() - // if err != nil { - // return err - // } + isoURL, err = getLatestReleaseURL() + if err != nil { + return err + } // todo: use real constant for .docker rootPath := filepath.Join(drivers.GetHomeDir(), ".docker") @@ -510,7 +510,7 @@ func (d *Driver) StartDocker() error { func (d *Driver) StopDocker() error { log.Debug("Stopping Docker...") - cmd, err := d.GetSSHCommand("sudo /etc/init.d/docker stop ; exit 0") + cmd, err := d.GetSSHCommand("if [ -e /var/run/docker.pid ]; then kill `cat /var/run/docker.pid`; rm /var/run/docker.pid; fi") if err != nil { return err } diff --git a/drivers/vmwarefusion/fusion.go b/drivers/vmwarefusion/fusion.go index 2f0aa3fad2..7165ace40c 100644 --- a/drivers/vmwarefusion/fusion.go +++ b/drivers/vmwarefusion/fusion.go @@ -23,6 +23,7 @@ import ( "github.com/docker/machine/drivers" "github.com/docker/machine/ssh" "github.com/docker/machine/state" + cssh "golang.org/x/crypto/ssh" ) const ( @@ -133,7 +134,8 @@ func (d *Driver) Create() error { isoURL = d.Boot2DockerURL } else { // HACK: Docker 1.3 boot2docker image with identity auth and vmtoolsd - isoURL = "https://github.com/cloudnativeapps/boot2docker/releases/download/1.3.1_vmw-identity/boot2docker.iso" + //isoURL = "https://github.com/cloudnativeapps/boot2docker/releases/download/1.3.1_vmw-identity/boot2docker.iso" + isoURL = "https://github.com/boot2docker/boot2docker/releases/download/v1.4.1/boot2docker.iso" } log.Infof("Downloading boot2docker...") if err := downloadISO(d.storePath, "boot2docker.iso", isoURL); err != nil { @@ -199,18 +201,33 @@ func (d *Driver) Create() error { return fmt.Errorf("Machine didn't return an IP after 120 seconds, aborting") } - // we got an IP, let's copy ssh keys over - // Create the dir - vmrun("-gu", B2D_USER, "-gp", B2D_PASS, "createDirectoryInGuest", d.vmxPath(), "/home/docker/.ssh") - - // Copy SSH keys - vmrun("-gu", B2D_USER, "-gp", B2D_PASS, "CopyFileFromHostToGuest", d.vmxPath(), d.publicSSHKeyPath(), "/home/docker/.ssh/authorized_keys") - vmrun("-gu", B2D_USER, "-gp", B2D_PASS, "CopyFileFromHostToGuest", d.vmxPath(), d.publicSSHKeyPath(), "/home/docker/.ssh/authorized_keys2") - - if err := drivers.AddPublicKeyToAuthorizedHosts(d, "/root/.docker/authorized-keys.d"); err != nil { + key, err := ioutil.ReadFile(d.publicSSHKeyPath()) + if err != nil { return err } + // so, vmrun above will not work without vmtools in b2d. since getting stuff into TCL + // is much more painful, we simply use the b2d password to get the initial public key + // onto the machine. from then on we use the pub key. meh. + sshConfig := &cssh.ClientConfig{ + User: B2D_USER, + Auth: []cssh.AuthMethod{ + cssh.Password(B2D_PASS), + }, + } + sshClient, err := cssh.Dial("tcp", fmt.Sprintf("%s:22", ip), sshConfig) + if err != nil { + return err + } + session, err := sshClient.NewSession() + if err != nil { + return err + } + if err := session.Run(fmt.Sprintf("mkdir /home/docker/.ssh && echo \"%s\" > /home/docker/.ssh/authorized_keys", string(key))); err != nil { + return err + } + session.Close() + 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", @@ -225,13 +242,13 @@ func (d *Driver) Create() error { return err } - cmd, err = d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5") - if err != nil { - return err - } - if err := cmd.Run(); err != nil { - return err - } + //cmd, err = d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5") + //if err != nil { + // return err + //} + //if err := cmd.Run(); err != nil { + // return err + //} //cmd, err := d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5") //if err != nil { // return err @@ -315,7 +332,6 @@ func (d *Driver) Upgrade() error { } func (d *Driver) GetSSHCommand(args ...string) (*exec.Cmd, error) { - ip, err := d.GetIP() if err != nil { return nil, err diff --git a/host.go b/host.go index 12768a98cb..551cb1044c 100644 --- a/host.go +++ b/host.go @@ -217,27 +217,27 @@ func (h *Host) ConfigureAuth() error { daemonCfg string ) + // TODO @ehazlett: template? + defaultDaemonOpts := fmt.Sprintf(`--tlsverify \ +--tlscacert=%s \ +--tlskey=%s \ +--tlscert=%s`, machineCaCertPath, machineServerKeyPath, machineServerCertPath) + switch d.DriverName() { case "virtualbox", "vmwarefusion", "vmwarevsphere": - daemonOpts = "--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376" + daemonOpts = "-H tcp://0.0.0.0:2376" daemonOptsCfg = filepath.Join(d.GetDockerConfigDir(), "profile") + opts := fmt.Sprintf("%s %s", defaultDaemonOpts, daemonOpts) daemonCfg = fmt.Sprintf(`EXTRA_ARGS='%s' CACERT=%s SERVERCERT=%s SERVERKEY=%s -DOCKER_TLS=auto`, daemonOpts, machineCaCertPath, machineServerCertPath, machineServerKeyPath) +DOCKER_TLS=no`, opts, machineCaCertPath, machineServerCertPath, machineServerKeyPath) default: - // TODO @ehazlett - use a template here - daemonOpts = fmt.Sprintf(`--tlsverify \ ---tlsverify \ ---tlscacert=%s \ ---tlskey=%s \ ---tlscert=%s \ ---host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376`, machineCaCertPath, - machineServerKeyPath, machineServerCertPath) - + daemonOpts = "--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376" daemonOptsCfg = "/etc/default/docker" - daemonCfg = fmt.Sprintf("export DOCKER_OPTS='%s'", daemonOpts) + opts := fmt.Sprintf("%s %s", defaultDaemonOpts, daemonOpts) + daemonCfg = fmt.Sprintf("export DOCKER_OPTS='%s'", opts) } cmd, err = d.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee -a %s", daemonCfg, daemonOptsCfg)) if err != nil {