From 270fecce0b5137be8aa61e7da523913bfe912d55 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 26 Feb 2016 00:33:53 -0600 Subject: [PATCH] Got Droplet spinup correctly returning the IP address of the Droplet. --- dospin.go | 5 +++-- dropletmanager.go | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/dospin.go b/dospin.go index a38ed96..aab92c0 100644 --- a/dospin.go +++ b/dospin.go @@ -27,11 +27,12 @@ func main() { client := godo.NewClient(oauthClient) dm := NewDropletManager(client, settings) - _, err = dm.SpinupMachine("minecraft") + ip, err := dm.SpinupMachine("minecraft") if err != nil { - log.Println(err) + log.Println("Error:", err) return } + log.Println("IP: " + ip) //_, err = client.Droplets.Delete(droplet.ID) //if err != nil { // log.Println(err) diff --git a/dropletmanager.go b/dropletmanager.go index a74b47a..df2a1a0 100644 --- a/dropletmanager.go +++ b/dropletmanager.go @@ -3,8 +3,11 @@ package main import ( "errors" "github.com/digitalocean/godo" + "time" ) +const DROPLET_NS = "dospin-" + type DropletManager struct { client *godo.Client settings Settings @@ -30,25 +33,36 @@ func (me *DropletManager) SpinupMachine(name string) (string, error) { } vd := me.settings.VirtualDroplets[name] createRequest := &godo.DropletCreateRequest{ - Name: name, - Region: vd.Region, - Size: vd.Size, + Name: DROPLET_NS + name, + Region: vd.Region, + Size: vd.Size, + PrivateNetworking: true, Image: godo.DropletCreateImage{ ID: image.ID, }, } - droplet, _, err := me.client.Droplets.Create(createRequest) + _, _, err = me.client.Droplets.Create(createRequest) if err != nil { 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) { - name = "dospin:" + name + name = DROPLET_NS + name page := 0 perPage := 200 var droplet godo.Droplet @@ -74,16 +88,18 @@ func (me *DropletManager) getDroplet(name string) (godo.Droplet, error) { 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) { - name = "dospin:" + name + name = DROPLET_NS + name page := 0 perPage := 200 var image godo.Image + var err error for { page++ + // get list of images opt := &godo.ListOptions{ Page: page, @@ -93,16 +109,19 @@ func (me *DropletManager) getSnapshot(name string) (godo.Image, error) { if err != nil { break } + // find image for _, a := range images { if a.Name == name { return a, nil } } + // check next page? if len(images) < perPage { + err = errors.New("Could not find image: " + name) break } } - return image, errors.New("Could not find image") + return image, err }