Skip to content

Commit 269c4e0

Browse files
committed
Generate a new SSH key pair if one was not alredy configured (#1).
1 parent 36f9aea commit 269c4e0

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

driver.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,19 @@ func (driver *Driver) PreCreateCheck() error {
263263

264264
// Create a new Docker Machine instance on CloudControl.
265265
func (driver *Driver) Create() error {
266-
log.Infof("Importing SSH key...")
267-
268-
err := driver.importSSHKey()
269-
if err != nil {
270-
return err
266+
var err error
267+
if driver.SSHKey != "" {
268+
log.Infof("Importing SSH key '%s'...", driver.SSHKey)
269+
err = driver.importSSHKey()
270+
if err != nil {
271+
return err
272+
}
273+
} else {
274+
log.Infof("Generating new SSH key...")
275+
err = driver.generateSSHKey()
276+
if err != nil {
277+
return err
278+
}
271279
}
272280

273281
log.Infof("Creating server '%s'...", driver.MachineName)

ssh_bootstrap.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ func (driver *Driver) installSSHKey() error {
106106
return nil
107107
}
108108

109+
// Generate an SSH key pair, and save it into the machine store folder.
110+
func (driver *Driver) generateSSHKey() error {
111+
if driver.SSHKeyPath != "" {
112+
return errors.New("SSH key path already configured")
113+
}
114+
115+
driver.SSHKeyPath = driver.ResolveStorePath("id_rsa")
116+
err := ssh.GenerateSSHKey(driver.SSHKeyPath)
117+
if err != nil {
118+
log.Errorf("Failed to generate SSH key pair: %s", err.Error())
119+
120+
return err
121+
}
122+
123+
return nil
124+
}
125+
109126
// Import the configured SSH key files into the machine store folder.
110127
func (driver *Driver) importSSHKey() error {
111128
if driver.SSHKey == "" {
@@ -117,14 +134,14 @@ func (driver *Driver) importSSHKey() error {
117134
)
118135
err := copySSHKey(driver.SSHKey, driver.SSHKeyPath)
119136
if err != nil {
120-
log.Infof("Couldn't copy SSH private key : %s", err.Error())
137+
log.Infof("Couldn't copy SSH private key: %s", err.Error())
121138

122139
return err
123140
}
124141

125142
err = copySSHKey(driver.SSHKey+".pub", driver.SSHKeyPath+".pub")
126143
if err != nil {
127-
log.Infof("Couldn't copy SSH public key : %s", err.Error())
144+
log.Infof("Couldn't copy SSH public key: %s", err.Error())
128145

129146
return err
130147
}

0 commit comments

Comments
 (0)