Skip to content

Commit

Permalink
feat(server): Optional graceful shutdown before deleting servers (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
LionC committed Sep 20, 2023
1 parent daa36c7 commit 0c477a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 45 additions & 1 deletion internal/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func Resource() *schema.Resource {
Optional: true,
Default: false,
},
"shutdown_before_deletion": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -849,6 +854,45 @@ func resourceServerDelete(ctx context.Context, d *schema.ResourceData, m interfa
d.SetId("")
return nil
}

var warnings diag.Diagnostics

if d.Get("shutdown_before_deletion").(bool) {
// Try shutting down the server
shutdownResult, _, err := client.Server.Shutdown(ctx, &hcloud.Server{ID: serverID})
if err != nil {
return hcclient.ErrorToDiag(err)
}

if err = hcclient.WaitForAction(ctx, &client.Action, shutdownResult); err != nil {
return hcclient.ErrorToDiag(err)
}

// Give the server some time to shut down
err = control.Retry(control.DefaultRetries, func() error {
server, _, err := client.Server.GetByID(ctx, serverID)

// If it is not possible to get the server status, it is probably futile to retry
if err != nil {
return control.AbortRetry(err)
}

if server.Status != hcloud.ServerStatusOff {
return fmt.Errorf("Server has not shut down yet")
}

// Server has shut down successfully
return nil
})
if err != nil {
// If shutting down does not work, add a warning and move on with deletion
warnings = append(warnings, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Server id %d did not finish shutting down gracefully in time, deleting it anyways.", serverID),
})
}
}

result, _, err := client.Server.DeleteWithResult(ctx, &hcloud.Server{ID: serverID})
if err != nil {
return hcclient.ErrorToDiag(err)
Expand All @@ -859,7 +903,7 @@ func resourceServerDelete(ctx context.Context, d *schema.ResourceData, m interfa
return hcclient.ErrorToDiag(err)
}

return nil
return warnings
}

func resourceServerIsNotFound(err error, d *schema.ResourceData) bool {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/server.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ The following arguments are supported:
- `delete_protection` - (Optional, bool) Enable or disable delete protection (Needs to be the same as `rebuild_protection`).
- `rebuild_protection` - (Optional, bool) Enable or disable rebuild protection (Needs to be the same as `delete_protection`).
- `allow_deprecated_images` - (Optional, bool) Enable the use of deprecated images (default: false). **Note** Deprecated images will be removed after three months. Using them is then no longer possible.
- `shutdown_before_deletion` - (bool) Whether to try shutting the server down gracefully before deleting it.

`network` support the following fields:
- `network_id` - (Required, int) ID of the network
Expand Down Expand Up @@ -218,6 +219,7 @@ The following attributes are exported:
- `placement_group_id` - (Optional, string) Placement Group ID the server is assigned to.
- `delete_protection` - (bool) Whether delete protection is enabled.
- `rebuild_protection` - (bool) Whether rebuild protection is enabled.
- `shutdown_before_deletion` - (bool) Whether the server will try to shut down gracefully before being deleted.

a single entry in `network` support the following fields:
- `network_id` - (Required, int) ID of the network
Expand Down

0 comments on commit 0c477a3

Please sign in to comment.