From 10a959e9f3254576587c78a5d96cca835c81a389 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 27 Jan 2017 18:32:51 -0600 Subject: [PATCH] Switch key list over to using key names --- dospin.json | 5 +++- droplethandler.go | 63 +++++++++++++++++++++++++++++++++-------------- servermanager.go | 1 - settings.go | 2 +- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/dospin.json b/dospin.json index a77117f..db052e4 100644 --- a/dospin.json +++ b/dospin.json @@ -7,7 +7,10 @@ "InitialSize": "4gb", "Size": "4gb", "Region": "nyc1", - "SshKeys": [513314, 1667247], + "SshKeys": [ + "Key1", + "gtalent2@gmail.com" + ], "UsePersistentImage": false, "ImageSlug": "ubuntu-16-04-x64", "Volumes": ["volume-nyc1-01"], diff --git a/droplethandler.go b/droplethandler.go index 7536660..9876eb9 100644 --- a/droplethandler.go +++ b/droplethandler.go @@ -28,22 +28,6 @@ func (t *tokenSource) Token() (*oauth2.Token, error) { return token, nil } -func sshKeys(ids []int) []godo.DropletCreateSSHKey { - var out []godo.DropletCreateSSHKey - for _, id := range ids { - out = append(out, godo.DropletCreateSSHKey{ID: id}) - } - return out -} - -func volumes(names []string) []godo.DropletCreateVolume { - var out []godo.DropletCreateVolume - for _, name := range names { - out = append(out, godo.DropletCreateVolume{Name: name}) - } - return out -} - type DropletHandler struct { client *godo.Client settings Settings @@ -103,8 +87,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) { Region: vd.Region, Size: size, PrivateNetworking: true, - SSHKeys: sshKeys(vd.SshKeys), - Volumes: volumes(vd.Volumes), + SSHKeys: me.sshKeys(vd.SshKeys), + Volumes: me.volumes(vd.Volumes), UserData: vd.UserData, Image: image, } @@ -321,3 +305,46 @@ func (me *DropletHandler) actionWait(actionId int) bool { time.Sleep(1000 * time.Millisecond) } } + +func (me *DropletHandler) sshKeys(keyNames []string) []godo.DropletCreateSSHKey { + // build key map + page := 0 + perPage := 200 + keyMap := make(map[string]string) + for { + page++ + opt := &godo.ListOptions{ + Page: page, + PerPage: perPage, + } + keys, _, err := me.client.Keys.List(opt) + if err != nil { + break + } + + for _, v := range keys { + keyMap[v.Name] = v.Fingerprint + } + + // check next page? + if len(keys) < perPage { + break + } + } + + // build output key list + var out []godo.DropletCreateSSHKey + for _, kn := range keyNames { + fp := keyMap[kn] + out = append(out, godo.DropletCreateSSHKey{Fingerprint: fp}) + } + return out +} + +func (me *DropletHandler) volumes(names []string) []godo.DropletCreateVolume { + var out []godo.DropletCreateVolume + for _, name := range names { + out = append(out, godo.DropletCreateVolume{Name: name}) + } + return out +} diff --git a/servermanager.go b/servermanager.go index 4a1119d..0ab80f5 100644 --- a/servermanager.go +++ b/servermanager.go @@ -74,7 +74,6 @@ func (me *ServerManager) Serve() { running = me.serveAction(action) case <-ticker.C: if time.Since(me.lastKeepAliveTime) > activityTimeout { - log.Println("ServerManager: Activity timeout for", me.name, " - killing server") running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN}) } } diff --git a/settings.go b/settings.go index cb59acb..02b080e 100644 --- a/settings.go +++ b/settings.go @@ -26,7 +26,7 @@ type Server struct { UsePersistentImage bool ImageSlug string UserData string - SshKeys []int + SshKeys []string Volumes []string }