Moved all godo code to dropletmanager.go. Better filled out logging. Added -conf flag.

This commit is contained in:
Gary Talent 2016-02-26 18:40:47 -06:00
parent 93257d4826
commit a5481c055e
7 changed files with 57 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
dospin.json dospin.json
test_config.json
dospin dospin
main main

View File

@ -8,31 +8,30 @@
package main package main
import ( import (
"github.com/digitalocean/godo" "flag"
"golang.org/x/oauth2"
"log" "log"
) )
type TokenSource struct { type cmdOptions struct {
AccessToken string config string
} }
func (t *TokenSource) Token() (*oauth2.Token, error) { func parseCmdOptions() cmdOptions {
token := &oauth2.Token{ var o cmdOptions
AccessToken: t.AccessToken, flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
} flag.Parse()
return token, nil return o
} }
func main() { func main() {
settings, err := loadSettings("dospin.json") opts := parseCmdOptions()
log.Println("Loading config:", opts.config)
settings, err := loadSettings(opts.config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
tokenSource := &TokenSource{settings.Token}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) dm := NewDropletManager(settings)
client := godo.NewClient(oauthClient)
dm := NewDropletManager(client, settings)
ip, err := dm.SpinupMachine("minecraft") ip, err := dm.SpinupMachine("minecraft")
if err != nil { if err != nil {
@ -45,8 +44,4 @@ func main() {
log.Println("Error:", err) log.Println("Error:", err)
return return
} }
//_, err = client.Droplets.Delete(droplet.ID)
//if err != nil {
// log.Println(err)
//}
} }

View File

@ -1,5 +1,5 @@
{ {
"Token": "<your token here>", "ApiToken": "<your token here>",
"Services": { "Services": {
"Minecraft": { "Minecraft": {
"Port": 25565, "Port": 25565,

View File

@ -10,21 +10,38 @@ package main
import ( import (
"errors" "errors"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"golang.org/x/oauth2"
"log" "log"
"time" "time"
) )
const DROPLET_NS = "dospin-" 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 { type DropletManager struct {
client *godo.Client client *godo.Client
settings Settings settings Settings
} }
func NewDropletManager(client *godo.Client, settings Settings) *DropletManager { func NewDropletManager(settings Settings) *DropletManager {
retval := new(DropletManager) retval := new(DropletManager)
retval.client = client
retval.settings = settings retval.settings = settings
// setup DO client
tokenSource := &tokenSource{settings.ApiToken}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
retval.client = godo.NewClient(oauthClient)
return retval return retval
} }
@ -53,21 +70,33 @@ func (me *DropletManager) SpinupMachine(name string) (string, error) {
log.Println("Spinup: Creating " + name) log.Println("Spinup: Creating " + name)
droplet, _, err := me.client.Droplets.Create(createRequest) droplet, _, err := me.client.Droplets.Create(createRequest)
if err != nil {
log.Println(err)
if droplet == nil {
return "", err
}
}
// wait until machine is ready // wait until machine is ready
for { for {
d, _, err := me.client.Droplets.Get(droplet.ID) d, _, err := me.client.Droplets.Get(droplet.ID)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return "", err
} else if d.Status == "active" { } else if d.Status == "active" {
break break
} }
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }
if err != nil {
return "", err
}
log.Println("Spinup: Created " + name) 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 // get the private IP and return it
droplet, _, err = me.client.Droplets.Get(droplet.ID) droplet, _, err = me.client.Droplets.Get(droplet.ID)
if err != nil { if err != nil {
@ -97,27 +126,27 @@ func (me *DropletManager) SpindownMachine(name string) error {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
_, _, err = me.client.DropletActions.Shutdown(droplet.ID) _, _, err = me.client.DropletActions.Shutdown(droplet.ID)
if err != nil { 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) log.Println("Spindown: Powered down " + name)
} }
// snapshot existing droplet // 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) action, _, err := me.client.DropletActions.Snapshot(droplet.ID, DROPLET_NS+name)
if err != nil || !me.actionWait(action.ID) { if err != nil || !me.actionWait(action.ID) {
return err return err
} }
log.Println("Spindown: Snapshoted " + name) log.Println("Spindown: Creating imaged " + name)
// delete droplet // delete droplet
log.Println("Spindown: Deleting " + name) log.Println("Spindown: Deleting droplet " + name)
_, err = me.client.Droplets.Delete(droplet.ID) _, err = me.client.Droplets.Delete(droplet.ID)
if err != nil { if err != nil {
return err return err
} }
log.Println("Spindown: Deleted " + name) log.Println("Spindown: Deleted droplet " + name)
return err return err
} }

View File

@ -1,2 +1,2 @@
#! /usr/bin/env sh #! /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 .

View File

@ -1,2 +1,2 @@
#! /usr/bin/env sh #! /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 .

View File

@ -13,7 +13,7 @@ import (
) )
type Settings struct { type Settings struct {
Token string ApiToken string
VirtualDroplets map[string]VirtualDroplet VirtualDroplets map[string]VirtualDroplet
} }