Skip to content

Commit d370d93

Browse files
Implemented all actions
- shutdown/stop, poweroff/kill, poweron/start and reboot/restart now work properly - fixed server location flag not being generated with the proper json key - fixed dangling keys upon unsuccessful server deletion - refractored action-waiting
1 parent 9115478 commit d370d93

File tree

2 files changed

+108
-54
lines changed

2 files changed

+108
-54
lines changed

driver/driver.go

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -193,32 +193,15 @@ func (d *Driver) Create() error {
193193
srv, act, err := d.getClient().CreateServer(d.GetMachineName(), d.Type, d.Image, d.Location, d.KeyID)
194194

195195
if err != nil {
196+
d.destroyDanglingKey()
196197
return err
197198
}
198199

199-
log.Debugf(" -> Creating action %d, server %s[%d]", act.Id, srv.Name, srv.Id)
200+
log.Infof(" -> Creating server %s[%d] in %s[%d]", srv.Name, srv.Id, act.Command, act.Id)
200201

201-
for {
202-
act, err = d.getClient().GetAction(act.Id)
203-
204-
if err != nil {
205-
return err
206-
}
207-
208-
if act.Status == "success" {
209-
log.Debugf(" -> Finished create action %d", act.Id)
210-
break
211-
} else if act.Status == "running" {
212-
log.Debugf(" -> Create action[%d]: %d %%", act.Id, act.Progress)
213-
} else if act.Status == "error" {
214-
if act.Error != nil {
215-
return fmt.Errorf("create action %d %s: %s", act.Id, act.Error.Code, act.Error.Message)
216-
} else {
217-
return fmt.Errorf("create action %d: failed for unknown reason", act.Id)
218-
}
219-
}
220-
221-
time.Sleep(1 * time.Second)
202+
if err = d.waitForAction(act); err != nil {
203+
d.destroyDanglingKey()
204+
return err
222205
}
223206

224207
d.ServerID = srv.Id
@@ -228,6 +211,7 @@ func (d *Driver) Create() error {
228211
srvstate, err := d.GetState()
229212

230213
if err != nil {
214+
d.destroyDanglingKey()
231215
return err
232216
}
233217

@@ -244,6 +228,12 @@ func (d *Driver) Create() error {
244228
return nil
245229
}
246230

231+
func (d *Driver) destroyDanglingKey() {
232+
if !d.IsExistingKey && d.KeyID != 0 {
233+
d.getClient().DeleteSSHKey(d.KeyID)
234+
}
235+
}
236+
247237
func (d *Driver) GetSSHHostname() (string, error) {
248238
return d.GetIP()
249239
}
@@ -282,44 +272,23 @@ func (d *Driver) GetState() (state.State, error) {
282272
return state.None, nil
283273
}
284274

285-
func (d *Driver) Kill() error {
286-
panic("implement me")
287-
}
288-
289275
func (d *Driver) Remove() error {
290-
act, err := d.getClient().DeleteServer(d.ServerID)
291-
292-
if err != nil {
293-
return err
294-
}
295-
296-
log.Infof(" -> Destroying server %d, action %d...", d.ServerID, act.Id)
297-
298-
for {
299-
act, err = d.getClient().GetAction(act.Id)
276+
if d.ServerID != 0 {
277+
act, err := d.getClient().DeleteServer(d.ServerID)
300278

301279
if err != nil {
302280
return err
303281
}
304282

305-
if act.Status == "success" {
306-
log.Infof(" -> Finished destroy action %d", act.Id)
307-
break
308-
} else if act.Status == "running" {
309-
log.Infof(" -> Destroy action[%d]: %d %%", act.Id, act.Progress)
310-
} else if act.Status == "error" {
311-
if act.Error != nil {
312-
return fmt.Errorf("destroy action %d %s: %s", act.Id, act.Error.Code, act.Error.Message)
313-
} else {
314-
return fmt.Errorf("destroy action %d: failed for unknown reason", act.Id)
315-
}
316-
}
283+
log.Infof(" -> Destroying server %d in %s[%d]...", d.ServerID, act.Command, act.Id)
317284

318-
time.Sleep(1 * time.Second)
285+
if err = d.waitForAction(act); err != nil {
286+
return err
287+
}
319288
}
320289

321290
if !d.IsExistingKey {
322-
log.Infof(" -> Destroying SSHkey %d...", d.KeyID)
291+
log.Infof(" -> Destroying SSHKey %d...", d.KeyID)
323292
if err := d.getClient().DeleteSSHKey(d.KeyID); err != nil {
324293
return err
325294
}
@@ -329,15 +298,47 @@ func (d *Driver) Remove() error {
329298
}
330299

331300
func (d *Driver) Restart() error {
332-
panic("implement me")
301+
act, err := d.getClient().RebootServer(d.ServerID)
302+
if err != nil {
303+
return err
304+
}
305+
306+
log.Infof(" -> Rebooting server %d in %s[%d]...", d.ServerID, act.Command, act.Id)
307+
308+
return d.waitForAction(act)
333309
}
334310

335311
func (d *Driver) Start() error {
336-
panic("implement me")
312+
act, err := d.getClient().PowerOnServer(d.ServerID)
313+
if err != nil {
314+
return err
315+
}
316+
317+
log.Infof(" -> Starting server %d in %s[%d]...", d.ServerID, act.Command, act.Id)
318+
319+
return d.waitForAction(act)
337320
}
338321

339322
func (d *Driver) Stop() error {
340-
panic("implement me")
323+
act, err := d.getClient().ShutdownServer(d.ServerID)
324+
if err != nil {
325+
return err
326+
}
327+
328+
log.Infof(" -> Shutting down server %d in %s[%d]...", d.ServerID, act.Command, act.Id)
329+
330+
return d.waitForAction(act)
331+
}
332+
333+
func (d *Driver) Kill() error {
334+
act, err := d.getClient().PowerOffServer(d.ServerID)
335+
if err != nil {
336+
return err
337+
}
338+
339+
log.Infof(" -> Powering off server %d in %s[%d]...", d.ServerID, act.Command, act.Id)
340+
341+
return d.waitForAction(act)
341342
}
342343

343344
func (d *Driver) getClient() *hetzner.Client {
@@ -359,3 +360,30 @@ func (d *Driver) copySSHKeyPair(src string) error {
359360

360361
return nil
361362
}
363+
364+
func (d *Driver) waitForAction(a *hetzner.Action) error {
365+
for {
366+
act, err := d.getClient().GetAction(a.Id)
367+
368+
if err != nil {
369+
return err
370+
}
371+
372+
if act.Status == "success" {
373+
log.Debugf(" -> finished %s[%d]", act.Command, act.Id)
374+
break
375+
} else if act.Status == "running" {
376+
log.Debugf(" -> %s[%d]: %d %%", act.Command, act.Id, act.Progress)
377+
} else if act.Status == "error" {
378+
if act.Error != nil {
379+
return fmt.Errorf("%s[%d] %s: %s", act.Command, act.Id, act.Error.Code, act.Error.Message)
380+
} else {
381+
return fmt.Errorf("%s[%d]: failed for unknown reason", act.Command, act.Id)
382+
}
383+
}
384+
385+
time.Sleep(1 * time.Second)
386+
}
387+
388+
return nil
389+
}

driver/hetzner/client.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ type createServerRequest struct {
174174
Name string `json:"name"`
175175
Type string `json:"server_type"`
176176
Image string `json:"image"`
177-
Location string `json:"type,omitempty"`
177+
Location string `json:"location,omitempty"`
178178
SSHKeyIDs []int `json:"ssh_keys,omitempty"`
179179
}
180180

@@ -241,3 +241,29 @@ func (c *Client) DeleteServer(id int) (*Action, error) {
241241

242242
return extractAction(resp.Body())
243243
}
244+
245+
func (c *Client) serverAction(id int, action string) (*Action, error) {
246+
resp, err := c.client.R().Post(fmt.Sprintf("servers/%d/actions/%s", id, action))
247+
248+
if err != nil {
249+
return nil, extractPrettyError(resp.Body(), err)
250+
}
251+
252+
return extractAction(resp.Body())
253+
}
254+
255+
func (c *Client) PowerOnServer(id int) (*Action, error) {
256+
return c.serverAction(id, "poweron")
257+
}
258+
259+
func (c *Client) RebootServer(id int) (*Action, error) {
260+
return c.serverAction(id, "reboot")
261+
}
262+
263+
func (c *Client) ShutdownServer(id int) (*Action, error) {
264+
return c.serverAction(id, "shutdown")
265+
}
266+
267+
func (c *Client) PowerOffServer(id int) (*Action, error) {
268+
return c.serverAction(id, "poweroff")
269+
}

0 commit comments

Comments
 (0)