Skip to content

Commit 451d82f

Browse files
Minor fixes
- added check for non-existent SSH key to allow machine removal after unsuccessful creation without force - switched to defer for dangling key handling
1 parent 7a4caac commit 451d82f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

driver/driver.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Driver struct {
3030
KeyID int
3131
IsExistingKey bool
3232
originalKey string
33+
danglingKey bool
3334
ServerID int
3435
}
3536

@@ -50,6 +51,7 @@ func NewDriver() *Driver {
5051
Image: defaultImage,
5152
Type: defaultType,
5253
IsExistingKey: false,
54+
danglingKey: false,
5355
BaseDriver: &drivers.BaseDriver{
5456
SSHUser: drivers.DefaultSSHUser,
5557
SSHPort: drivers.DefaultSSHPort,
@@ -186,21 +188,21 @@ func (d *Driver) Create() error {
186188
}
187189

188190
d.KeyID = key.Id
191+
d.danglingKey = true
192+
defer d.destroyDanglingKey()
189193
}
190194

191195
log.Infof("Creating Hetzner server...")
192196

193197
srv, act, err := d.getClient().CreateServer(d.GetMachineName(), d.Type, d.Image, d.Location, d.KeyID)
194198

195199
if err != nil {
196-
d.destroyDanglingKey()
197200
return err
198201
}
199202

200203
log.Infof(" -> Creating server %s[%d] in %s[%d]", srv.Name, srv.Id, act.Command, act.Id)
201204

202205
if err = d.waitForAction(act); err != nil {
203-
d.destroyDanglingKey()
204206
return err
205207
}
206208

@@ -211,7 +213,6 @@ func (d *Driver) Create() error {
211213
srvstate, err := d.GetState()
212214

213215
if err != nil {
214-
d.destroyDanglingKey()
215216
return err
216217
}
217218

@@ -225,12 +226,15 @@ func (d *Driver) Create() error {
225226
log.Debugf(" -> Server %s[%d] ready", srv.Name, srv.Id)
226227
d.IPAddress = srv.PublicNet.IPv4.IP
227228

229+
d.danglingKey = false
230+
228231
return nil
229232
}
230233

231234
func (d *Driver) destroyDanglingKey() {
232-
if !d.IsExistingKey && d.KeyID != 0 {
235+
if d.danglingKey && !d.IsExistingKey && d.KeyID != 0 {
233236
d.getClient().DeleteSSHKey(d.KeyID)
237+
d.KeyID = 0
234238
}
235239
}
236240

@@ -287,7 +291,7 @@ func (d *Driver) Remove() error {
287291
}
288292
}
289293

290-
if !d.IsExistingKey {
294+
if !d.IsExistingKey && d.KeyID != 0 {
291295
log.Infof(" -> Destroying SSHKey %d...", d.KeyID)
292296
if err := d.getClient().DeleteSSHKey(d.KeyID); err != nil {
293297
return err

0 commit comments

Comments
 (0)