Skip to content
Snippets Groups Projects
Commit c95027bc authored by Erick Hitter's avatar Erick Hitter
Browse files

Parse and log stale droplets

parent 58a1bbae
No related branches found
No related tags found
1 merge request!1Initial release
...@@ -3,5 +3,6 @@ ...@@ -3,5 +3,6 @@
"debug": false, "debug": false,
"api-key": "", "api-key": "",
"threshold": 3600, "threshold": 3600,
"schedule": "* */10 * * * *" "schedule": "* */10 * * * *",
"delete-stale": true
} }
...@@ -10,18 +10,21 @@ import ( ...@@ -10,18 +10,21 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"syscall" "syscall"
"time"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"github.com/dustin/go-humanize"
"github.com/robfig/cron" "github.com/robfig/cron"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
type config struct { type config struct {
DebugDest string `json:"debug-dest"` DebugDest string `json:"debug-dest"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
APIKey string `json:"api-key"` APIKey string `json:"api-key"`
Threshold int `json:"threshold"` Threshold int `json:"threshold"`
Schedule string `json:"schedule"` Schedule string `json:"schedule"`
DeleteStale bool `json:"delete-stale"`
} }
type tokenSource struct { type tokenSource struct {
...@@ -37,8 +40,9 @@ var ( ...@@ -37,8 +40,9 @@ var (
apiKey string apiKey string
threshold int threshold int
schedule string schedule string
deleteStale bool
client *godo.Client client *godo.Client
) )
...@@ -69,6 +73,7 @@ func init() { ...@@ -69,6 +73,7 @@ func init() {
threshold = cfg.Threshold threshold = cfg.Threshold
schedule = cfg.Schedule schedule = cfg.Schedule
deleteStale = cfg.DeleteStale
setUpLogger() setUpLogger()
} }
...@@ -79,8 +84,10 @@ func main() { ...@@ -79,8 +84,10 @@ func main() {
sig := make(chan os.Signal, 1) sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
if debug { if deleteStale {
logger.Println("Test") logger.Println("Stale droplets WILL BE DELETED automatically")
} else {
logger.Println("Stale droplets will be logged, but not deleted")
} }
authenticate() authenticate()
...@@ -119,7 +126,9 @@ func checkAPI() { ...@@ -119,7 +126,9 @@ func checkAPI() {
ctx := context.TODO() ctx := context.TODO()
droplets, err := listDroplets(ctx, client) droplets, err := listDroplets(ctx, client)
if err != nil { if err != nil {
logger.Fatal("Failed to retrieve droplet list") logger.Println("Warning! Failed to retrieve droplet list.")
logger.Print(err)
return
} }
for _, droplet := range droplets { for _, droplet := range droplets {
...@@ -159,6 +168,26 @@ func listDroplets(ctx context.Context, client *godo.Client) ([]godo.Droplet, err ...@@ -159,6 +168,26 @@ func listDroplets(ctx context.Context, client *godo.Client) ([]godo.Droplet, err
func checkDropletAge(droplet godo.Droplet) { func checkDropletAge(droplet godo.Droplet) {
logger.Print(droplet.ID) logger.Print(droplet.ID)
logger.Print(droplet.Created) logger.Print(droplet.Created)
thr := time.Now().Add(time.Duration(threshold))
created, err := time.Parse(time.RFC3339, droplet.Created)
if err != nil {
logger.Printf("Could not parse created-timestamp for droplet ID %d", droplet.ID)
return
}
if thr.After(created) {
logger.Printf("Stale droplet => ID: %d; name: \"%s\"; created: %s, %s (%d)", droplet.ID, droplet.Name, humanize.Time(created), droplet.Created, created.Unix())
if deleteStale {
deleteDroplet(droplet)
}
}
}
func deleteDroplet(droplet godo.Droplet) bool {
logger.Printf("Deleting droplet %d", droplet.ID)
return false
} }
func setUpLogger() { func setUpLogger() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment