From ec5edb03547ce32eeb8bf9197c3b492919bd3233 Mon Sep 17 00:00:00 2001 From: Evan Hazlett Date: Wed, 27 May 2015 10:56:43 -0400 Subject: [PATCH] redhat: fix tty provision bug Signed-off-by: Evan Hazlett --- drivers/utils.go | 14 +++++++++--- libmachine/provision/redhat.go | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/drivers/utils.go b/drivers/utils.go index 4b221816d4..5f6b6d620e 100644 --- a/drivers/utils.go +++ b/drivers/utils.go @@ -8,15 +8,15 @@ import ( "github.com/docker/machine/utils" ) -func RunSSHCommandFromDriver(d Driver, command string) (string, error) { +func GetSSHClientFromDriver(d Driver) (ssh.Client, error) { addr, err := d.GetSSHHostname() if err != nil { - return "", err + return nil, err } port, err := d.GetSSHPort() if err != nil { - return "", err + return nil, err } auth := &ssh.Auth{ @@ -24,13 +24,21 @@ func RunSSHCommandFromDriver(d Driver, command string) (string, error) { } client, err := ssh.NewClient(d.GetSSHUsername(), addr, port, auth) + return client, err + +} + +func RunSSHCommandFromDriver(d Driver, command string) (string, error) { + client, err := GetSSHClientFromDriver(d) if err != nil { return "", err } log.Debugf("About to run SSH command:\n%s", command) + output, err := client.Output(command) log.Debugf("SSH cmd err, output: %v: %s", err, output) + return output, err } diff --git a/libmachine/provision/redhat.go b/libmachine/provision/redhat.go index 4dec0d955c..68ef14ef61 100644 --- a/libmachine/provision/redhat.go +++ b/libmachine/provision/redhat.go @@ -11,6 +11,7 @@ import ( "github.com/docker/machine/libmachine/provision/pkgaction" "github.com/docker/machine/libmachine/swarm" "github.com/docker/machine/log" + "github.com/docker/machine/ssh" "github.com/docker/machine/utils" ) @@ -47,6 +48,47 @@ type RedHatProvisioner struct { DockerRPMPath string } +func (provisioner *RedHatProvisioner) SSHCommand(args string) (string, error) { + client, err := drivers.GetSSHClientFromDriver(provisioner.Driver) + if err != nil { + return "", err + } + + // redhat needs "-t" for tty allocation on ssh therefore we check for the + // external client and add as needed + // TODO: does this need to be done for the native ssh client? + if c, ok := client.(ssh.ExternalClient); ok { + log.Debugf("detected external ssh; added tty allocation flag") + c.BaseArgs = append(c.BaseArgs, "-t") + client = c + } + + return client.Output(args) +} + +func (provisioner *RedHatProvisioner) SetHostname(hostname string) error { + // we have to have SetHostname here as well to use the RedHat provisioner + // SSHCommand to add the tty allocation + if _, err := provisioner.SSHCommand(fmt.Sprintf( + "sudo hostname %s && echo %q | sudo tee /etc/hostname", + hostname, + hostname, + )); err != nil { + return err + } + + // ubuntu/debian use 127.0.1.1 for non "localhost" loopback hostnames: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution + if _, err := provisioner.SSHCommand(fmt.Sprintf( + "if grep -xq 127.0.1.1.* /etc/hosts; then sudo sed -i 's/^127.0.1.1.*/127.0.1.1 %s/g' /etc/hosts; else echo '127.0.1.1 %s' | sudo tee -a /etc/hosts; fi", + hostname, + hostname, + )); err != nil { + return err + } + + return nil +} + func (provisioner *RedHatProvisioner) Service(name string, action pkgaction.ServiceAction) error { reloadDaemon := false switch action {