This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try gracefully shutting down before deleting
Signed-off-by: Johan Siebens <[email protected]>
- Loading branch information
Showing
4 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/digitalocean/godo" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
func shutdownDroplet( | ||
dropletId int, | ||
client *godo.Client, | ||
log hclog.Logger) error { | ||
|
||
// Gracefully power off the droplet. | ||
log.Debug("Gracefully shutting down droplet...") | ||
_, _, err := client.DropletActions.PowerOff(context.TODO(), dropletId) | ||
if err != nil { | ||
// If we get an error the first time, actually report it | ||
return fmt.Errorf("error shutting down droplet: %s", err) | ||
} | ||
|
||
err = waitForDropletState("off", dropletId, client, log, 5*time.Minute) | ||
if err != nil { | ||
log.Warn("Timeout while waiting to for droplet to become 'off'") | ||
} | ||
|
||
log.Debug("Deleting Droplet...") | ||
_, err = client.Droplets.Delete(context.TODO(), dropletId) | ||
if err != nil { | ||
return fmt.Errorf("error deleting droplet: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/digitalocean/godo" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
func waitForDropletState( | ||
desiredState string, dropletId int, | ||
client *godo.Client, | ||
log hclog.Logger, | ||
timeout time.Duration) error { | ||
done := make(chan struct{}) | ||
defer close(done) | ||
|
||
result := make(chan error, 1) | ||
go func() { | ||
attempts := 0 | ||
for { | ||
attempts += 1 | ||
|
||
log.Debug(fmt.Sprintf("Checking droplet status... (attempt: %d)", attempts)) | ||
droplet, _, err := client.Droplets.Get(context.TODO(), dropletId) | ||
if err != nil { | ||
result <- err | ||
return | ||
} | ||
|
||
if droplet.Status == desiredState { | ||
result <- nil | ||
return | ||
} | ||
|
||
// Wait 3 seconds in between | ||
time.Sleep(3 * time.Second) | ||
|
||
// Verify we shouldn't exit | ||
select { | ||
case <-done: | ||
// We finished, so just exit the goroutine | ||
return | ||
default: | ||
// Keep going | ||
} | ||
} | ||
}() | ||
|
||
log.Debug(fmt.Sprintf("Waiting for up to %d seconds for droplet to become %s", timeout/time.Second, desiredState)) | ||
select { | ||
case err := <-result: | ||
return err | ||
case <-time.After(timeout): | ||
err := fmt.Errorf("timeout while waiting to for droplet to become '%s'", desiredState) | ||
return err | ||
} | ||
} |