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
test_config.json
dospin
main

View File

@ -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)
//}
}

View File

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

View File

@ -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
}

View File

@ -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 .

View File

@ -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 .

View File

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