From a5481c055e22b2bdec923459aeb6a693797e6746 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 26 Feb 2016 18:40:47 -0600 Subject: [PATCH] Moved all godo code to dropletmanager.go. Better filled out logging. Added -conf flag. --- .gitignore | 1 + dospin.go | 31 +++++++++++++----------------- dospin.json | 2 +- dropletmanager.go | 49 +++++++++++++++++++++++++++++++++++++---------- list_droplets.sh | 2 +- list_images.sh | 2 +- settings.go | 2 +- 7 files changed, 57 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 35c3e7c..4b6d3e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dospin.json +test_config.json dospin main diff --git a/dospin.go b/dospin.go index a4cdc6f..78c56b5 100644 --- a/dospin.go +++ b/dospin.go @@ -8,31 +8,30 @@ package main import ( - "github.com/digitalocean/godo" - "golang.org/x/oauth2" + "flag" "log" ) -type TokenSource struct { - AccessToken string +type cmdOptions struct { + config string } -func (t *TokenSource) Token() (*oauth2.Token, error) { - token := &oauth2.Token{ - AccessToken: t.AccessToken, - } - return token, nil +func parseCmdOptions() cmdOptions { + var o cmdOptions + flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file") + flag.Parse() + return o } func main() { - settings, err := loadSettings("dospin.json") + opts := parseCmdOptions() + log.Println("Loading config:", opts.config) + settings, err := loadSettings(opts.config) if err != nil { log.Fatal(err) } - tokenSource := &TokenSource{settings.Token} - oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) - client := godo.NewClient(oauthClient) - dm := NewDropletManager(client, settings) + + dm := NewDropletManager(settings) ip, err := dm.SpinupMachine("minecraft") if err != nil { @@ -45,8 +44,4 @@ func main() { log.Println("Error:", err) return } - //_, err = client.Droplets.Delete(droplet.ID) - //if err != nil { - // log.Println(err) - //} } diff --git a/dospin.json b/dospin.json index ad7b953..f38463b 100644 --- a/dospin.json +++ b/dospin.json @@ -1,5 +1,5 @@ { - "Token": "", + "ApiToken": "", "Services": { "Minecraft": { "Port": 25565, diff --git a/dropletmanager.go b/dropletmanager.go index 8a9c9ef..107e7e2 100644 --- a/dropletmanager.go +++ b/dropletmanager.go @@ -10,21 +10,38 @@ package main import ( "errors" "github.com/digitalocean/godo" + "golang.org/x/oauth2" "log" "time" ) const DROPLET_NS = "dospin-" +type tokenSource struct { + AccessToken string +} + +func (t *tokenSource) Token() (*oauth2.Token, error) { + token := &oauth2.Token{ + AccessToken: t.AccessToken, + } + return token, nil +} + type DropletManager struct { client *godo.Client settings Settings } -func NewDropletManager(client *godo.Client, settings Settings) *DropletManager { +func NewDropletManager(settings Settings) *DropletManager { retval := new(DropletManager) - retval.client = client retval.settings = settings + + // setup DO client + tokenSource := &tokenSource{settings.ApiToken} + oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) + retval.client = godo.NewClient(oauthClient) + return retval } @@ -53,21 +70,33 @@ func (me *DropletManager) SpinupMachine(name string) (string, error) { log.Println("Spinup: Creating " + name) droplet, _, err := me.client.Droplets.Create(createRequest) + if err != nil { + log.Println(err) + if droplet == nil { + return "", err + } + } // wait until machine is ready for { d, _, err := me.client.Droplets.Get(droplet.ID) if err != nil { log.Println(err) + return "", err } else if d.Status == "active" { break } time.Sleep(500 * time.Millisecond) } - if err != nil { - return "", err - } log.Println("Spinup: Created " + name) + // delete the image + log.Println("Spinup: Deleting image " + name) + _, err = me.client.Images.Delete(image.ID) + if err != nil { + log.Println("Spinup: Could not delete image: ", err) + } + log.Println("Spinup: Deleted image " + name) + // get the private IP and return it droplet, _, err = me.client.Droplets.Get(droplet.ID) if err != nil { @@ -97,27 +126,27 @@ func (me *DropletManager) SpindownMachine(name string) error { time.Sleep(100 * time.Millisecond) _, _, err = me.client.DropletActions.Shutdown(droplet.ID) if err != nil { - log.Println("Power down of ", name, " failed: ", err) + log.Println("Spindown: Power down of ", name, " failed: ", err) } } log.Println("Spindown: Powered down " + name) } // snapshot existing droplet - log.Println("Spindown: Snapshoting " + name) + log.Println("Spindown: Creating image " + name) action, _, err := me.client.DropletActions.Snapshot(droplet.ID, DROPLET_NS+name) if err != nil || !me.actionWait(action.ID) { return err } - log.Println("Spindown: Snapshoted " + name) + log.Println("Spindown: Creating imaged " + name) // delete droplet - log.Println("Spindown: Deleting " + name) + log.Println("Spindown: Deleting droplet " + name) _, err = me.client.Droplets.Delete(droplet.ID) if err != nil { return err } - log.Println("Spindown: Deleted " + name) + log.Println("Spindown: Deleted droplet " + name) return err } diff --git a/list_droplets.sh b/list_droplets.sh index 77cb83f..a30a25f 100755 --- a/list_droplets.sh +++ b/list_droplets.sh @@ -1,2 +1,2 @@ #! /usr/bin/env sh -curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .Token`" "https://api.digitalocean.com/v2/droplets?page=1&per_page=100&private=true" | jq . +curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/droplets?page=1&per_page=100&private=true" | jq . diff --git a/list_images.sh b/list_images.sh index c6a7cab..d8b9cd1 100755 --- a/list_images.sh +++ b/list_images.sh @@ -1,2 +1,2 @@ #! /usr/bin/env sh -curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .Token`" "https://api.digitalocean.com/v2/images?page=1&per_page=100&private=true" | jq . +curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/images?page=1&per_page=100&private=true" | jq . diff --git a/settings.go b/settings.go index 2847db9..d6ead76 100644 --- a/settings.go +++ b/settings.go @@ -13,7 +13,7 @@ import ( ) type Settings struct { - Token string + ApiToken string VirtualDroplets map[string]VirtualDroplet }