rebase and updated fusion to work with standard docker b2d 1.4.1

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-01-14 20:39:11 -05:00
parent fc2e5f278b
commit 9664d68889
4 changed files with 83 additions and 41 deletions

View File

@ -75,6 +75,11 @@ var Commands = []cli.Command{
Usage: "Create a machine", Usage: "Create a machine",
Action: cmdCreate, Action: cmdCreate,
}, },
{
Name: "config",
Usage: "Print the connection config for machine",
Action: cmdConfig,
},
{ {
Name: "inspect", Name: "inspect",
Usage: "Inspect information about a machine", 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) 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 // TODO @ehazlett: this will likely change but at least show how to connect for now
log.Info("To connect, pass these args to Docker: ") log.Infof("To connect, use docker $(machine config %s)", name)
storeDir := c.GlobalString("storage-path") }
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") caCert := filepath.Join(storeDir, name, "ca.pem")
clientCert := filepath.Join(storeDir, name, "client.pem") clientCert := filepath.Join(storeDir, name, "client.pem")
clientKey := filepath.Join(storeDir, name, "client-key.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)", machineUrl, err := host.GetURL()
caCert, clientCert, clientKey) 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) { func cmdInspect(c *cli.Context) {

View File

@ -138,15 +138,15 @@ func (d *Driver) Create() error {
} }
} else { } else {
// HACK: Docker 1.4.1 boot2docker image with client/daemon auth // 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 // todo: check latest release URL, download if it's new
// until then always use "latest" // until then always use "latest"
// isoURL, err = getLatestReleaseURL() isoURL, err = getLatestReleaseURL()
// if err != nil { if err != nil {
// return err return err
// } }
// todo: use real constant for .docker // todo: use real constant for .docker
rootPath := filepath.Join(drivers.GetHomeDir(), ".docker") rootPath := filepath.Join(drivers.GetHomeDir(), ".docker")
@ -510,7 +510,7 @@ func (d *Driver) StartDocker() error {
func (d *Driver) StopDocker() error { func (d *Driver) StopDocker() error {
log.Debug("Stopping Docker...") 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 { if err != nil {
return err return err
} }

View File

@ -23,6 +23,7 @@ import (
"github.com/docker/machine/drivers" "github.com/docker/machine/drivers"
"github.com/docker/machine/ssh" "github.com/docker/machine/ssh"
"github.com/docker/machine/state" "github.com/docker/machine/state"
cssh "golang.org/x/crypto/ssh"
) )
const ( const (
@ -133,7 +134,8 @@ func (d *Driver) Create() error {
isoURL = d.Boot2DockerURL isoURL = d.Boot2DockerURL
} else { } else {
// HACK: Docker 1.3 boot2docker image with identity auth and vmtoolsd // 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...") log.Infof("Downloading boot2docker...")
if err := downloadISO(d.storePath, "boot2docker.iso", isoURL); err != nil { 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") return fmt.Errorf("Machine didn't return an IP after 120 seconds, aborting")
} }
// we got an IP, let's copy ssh keys over key, err := ioutil.ReadFile(d.publicSSHKeyPath())
// Create the dir if err != nil {
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 {
return err 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) log.Debugf("Setting hostname: %s", d.MachineName)
cmd, err := d.GetSSHCommand(fmt.Sprintf( 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", "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 return err
} }
cmd, err = d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5") //cmd, err = d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5")
if err != nil { //if err != nil {
return err // return err
} //}
if err := cmd.Run(); err != nil { //if err := cmd.Run(); err != nil {
return err // return err
} //}
//cmd, err := d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5") //cmd, err := d.GetSSHCommand("sudo /etc/init.d/docker restart; sleep 5")
//if err != nil { //if err != nil {
// return err // return err
@ -315,7 +332,6 @@ func (d *Driver) Upgrade() error {
} }
func (d *Driver) GetSSHCommand(args ...string) (*exec.Cmd, error) { func (d *Driver) GetSSHCommand(args ...string) (*exec.Cmd, error) {
ip, err := d.GetIP() ip, err := d.GetIP()
if err != nil { if err != nil {
return nil, err return nil, err

24
host.go
View File

@ -217,27 +217,27 @@ func (h *Host) ConfigureAuth() error {
daemonCfg string daemonCfg string
) )
// TODO @ehazlett: template?
defaultDaemonOpts := fmt.Sprintf(`--tlsverify \
--tlscacert=%s \
--tlskey=%s \
--tlscert=%s`, machineCaCertPath, machineServerKeyPath, machineServerCertPath)
switch d.DriverName() { switch d.DriverName() {
case "virtualbox", "vmwarefusion", "vmwarevsphere": 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") daemonOptsCfg = filepath.Join(d.GetDockerConfigDir(), "profile")
opts := fmt.Sprintf("%s %s", defaultDaemonOpts, daemonOpts)
daemonCfg = fmt.Sprintf(`EXTRA_ARGS='%s' daemonCfg = fmt.Sprintf(`EXTRA_ARGS='%s'
CACERT=%s CACERT=%s
SERVERCERT=%s SERVERCERT=%s
SERVERKEY=%s SERVERKEY=%s
DOCKER_TLS=auto`, daemonOpts, machineCaCertPath, machineServerCertPath, machineServerKeyPath) DOCKER_TLS=no`, opts, machineCaCertPath, machineServerCertPath, machineServerKeyPath)
default: default:
// TODO @ehazlett - use a template here daemonOpts = "--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376"
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)
daemonOptsCfg = "/etc/default/docker" 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)) cmd, err = d.GetSSHCommand(fmt.Sprintf("echo \"%s\" | sudo tee -a %s", daemonCfg, daemonOptsCfg))
if err != nil { if err != nil {