Merge pull request #3330 from bamarni/issue-3268

[Generic] Copy the public key to machine directory (fixes #3268)
This commit is contained in:
Nathan LeClaire 2016-04-22 10:58:53 -07:00
commit 008283733e
2 changed files with 23 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"path"
"strconv" "strconv"
"time" "time"
@ -87,13 +88,6 @@ func (d *Driver) GetSSHUsername() string {
} }
func (d *Driver) GetSSHKeyPath() string { func (d *Driver) GetSSHKeyPath() string {
if d.SSHKey == "" {
return ""
}
if d.SSHKeyPath == "" {
d.SSHKeyPath = d.ResolveStorePath("id_rsa")
}
return d.SSHKeyPath return d.SSHKeyPath
} }
@ -114,8 +108,10 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
func (d *Driver) PreCreateCheck() error { func (d *Driver) PreCreateCheck() error {
if d.SSHKey != "" { if d.SSHKey != "" {
if _, err := os.Stat(d.SSHKey); os.IsNotExist(err) { if _, err := os.Stat(d.SSHKey); os.IsNotExist(err) {
return fmt.Errorf("Ssh key does not exist: %q", d.SSHKey) return fmt.Errorf("SSH key does not exist: %q", d.SSHKey)
} }
// TODO: validate the key is a valid key
} }
return nil return nil
@ -126,13 +122,14 @@ func (d *Driver) Create() error {
log.Info("No SSH key specified. Assuming an existing key at the default location.") log.Info("No SSH key specified. Assuming an existing key at the default location.")
} else { } else {
log.Info("Importing SSH key...") log.Info("Importing SSH key...")
// TODO: validate the key is a valid key
if err := mcnutils.CopyFile(d.SSHKey, d.GetSSHKeyPath()); err != nil { d.SSHKeyPath = d.ResolveStorePath(path.Base(d.SSHKey))
return fmt.Errorf("unable to copy ssh key: %s", err) if err := copySSHKey(d.SSHKey, d.SSHKeyPath); err != nil {
return err
} }
if err := os.Chmod(d.GetSSHKeyPath(), 0600); err != nil { if err := copySSHKey(d.SSHKey+".pub", d.SSHKeyPath+".pub"); err != nil {
return fmt.Errorf("unable to set permissions on the ssh key: %s", err) log.Infof("Couldn't copy SSH public key : %s", err)
} }
} }
@ -185,3 +182,15 @@ func (d *Driver) Kill() error {
func (d *Driver) Remove() error { func (d *Driver) Remove() error {
return nil return nil
} }
func copySSHKey(src, dst string) error {
if err := mcnutils.CopyFile(src, dst); err != nil {
return fmt.Errorf("unable to copy ssh key: %s", err)
}
if err := os.Chmod(dst, 0600); err != nil {
return fmt.Errorf("unable to set permissions on the ssh key: %s", err)
}
return nil
}

View File

@ -63,11 +63,7 @@ func CopyFile(src, dst string) error {
return err return err
} }
if err := os.Chmod(dst, fi.Mode()); err != nil { return os.Chmod(dst, fi.Mode())
return err
}
return nil
} }
func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error { func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error {