Got Droplet spinup correctly returning the IP address of the Droplet.
This commit is contained in:
parent
294af41473
commit
270fecce0b
@ -27,11 +27,12 @@ func main() {
|
|||||||
client := godo.NewClient(oauthClient)
|
client := godo.NewClient(oauthClient)
|
||||||
dm := NewDropletManager(client, settings)
|
dm := NewDropletManager(client, settings)
|
||||||
|
|
||||||
_, err = dm.SpinupMachine("minecraft")
|
ip, err := dm.SpinupMachine("minecraft")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println("Error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Println("IP: " + ip)
|
||||||
//_, err = client.Droplets.Delete(droplet.ID)
|
//_, err = client.Droplets.Delete(droplet.ID)
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// log.Println(err)
|
// log.Println(err)
|
||||||
|
@ -3,8 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/digitalocean/godo"
|
"github.com/digitalocean/godo"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DROPLET_NS = "dospin-"
|
||||||
|
|
||||||
type DropletManager struct {
|
type DropletManager struct {
|
||||||
client *godo.Client
|
client *godo.Client
|
||||||
settings Settings
|
settings Settings
|
||||||
@ -30,25 +33,36 @@ func (me *DropletManager) SpinupMachine(name string) (string, error) {
|
|||||||
}
|
}
|
||||||
vd := me.settings.VirtualDroplets[name]
|
vd := me.settings.VirtualDroplets[name]
|
||||||
createRequest := &godo.DropletCreateRequest{
|
createRequest := &godo.DropletCreateRequest{
|
||||||
Name: name,
|
Name: DROPLET_NS + name,
|
||||||
Region: vd.Region,
|
Region: vd.Region,
|
||||||
Size: vd.Size,
|
Size: vd.Size,
|
||||||
|
PrivateNetworking: true,
|
||||||
Image: godo.DropletCreateImage{
|
Image: godo.DropletCreateImage{
|
||||||
ID: image.ID,
|
ID: image.ID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
droplet, _, err := me.client.Droplets.Create(createRequest)
|
_, _, err = me.client.Droplets.Create(createRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return droplet.PrivateIPv4()
|
// get the private IP and return it
|
||||||
|
ip := ""
|
||||||
|
for {
|
||||||
|
droplet, err = me.getDroplet(name)
|
||||||
|
ip, err = droplet.PrivateIPv4()
|
||||||
|
if ip != "" || (err != nil && err.Error() != "no networks have been defined") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
return ip, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) {
|
func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) {
|
||||||
name = "dospin:" + name
|
name = DROPLET_NS + name
|
||||||
page := 0
|
page := 0
|
||||||
perPage := 200
|
perPage := 200
|
||||||
var droplet godo.Droplet
|
var droplet godo.Droplet
|
||||||
@ -74,16 +88,18 @@ func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return droplet, errors.New("Could not find droplet")
|
return droplet, errors.New("Could not find droplet: " + name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *DropletManager) getSnapshot(name string) (godo.Image, error) {
|
func (me *DropletManager) getSnapshot(name string) (godo.Image, error) {
|
||||||
name = "dospin:" + name
|
name = DROPLET_NS + name
|
||||||
page := 0
|
page := 0
|
||||||
perPage := 200
|
perPage := 200
|
||||||
var image godo.Image
|
var image godo.Image
|
||||||
|
var err error
|
||||||
for {
|
for {
|
||||||
page++
|
page++
|
||||||
|
|
||||||
// get list of images
|
// get list of images
|
||||||
opt := &godo.ListOptions{
|
opt := &godo.ListOptions{
|
||||||
Page: page,
|
Page: page,
|
||||||
@ -93,16 +109,19 @@ func (me *DropletManager) getSnapshot(name string) (godo.Image, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// find image
|
// find image
|
||||||
for _, a := range images {
|
for _, a := range images {
|
||||||
if a.Name == name {
|
if a.Name == name {
|
||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check next page?
|
// check next page?
|
||||||
if len(images) < perPage {
|
if len(images) < perPage {
|
||||||
|
err = errors.New("Could not find image: " + name)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return image, errors.New("Could not find image")
|
return image, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user