Switch key list over to using key names

This commit is contained in:
Gary Talent 2017-01-27 18:32:51 -06:00
parent d57884e2d7
commit 10a959e9f3
4 changed files with 50 additions and 21 deletions

View File

@ -7,7 +7,10 @@
"InitialSize": "4gb", "InitialSize": "4gb",
"Size": "4gb", "Size": "4gb",
"Region": "nyc1", "Region": "nyc1",
"SshKeys": [513314, 1667247], "SshKeys": [
"Key1",
"gtalent2@gmail.com"
],
"UsePersistentImage": false, "UsePersistentImage": false,
"ImageSlug": "ubuntu-16-04-x64", "ImageSlug": "ubuntu-16-04-x64",
"Volumes": ["volume-nyc1-01"], "Volumes": ["volume-nyc1-01"],

View File

@ -28,22 +28,6 @@ func (t *tokenSource) Token() (*oauth2.Token, error) {
return token, nil 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 { type DropletHandler struct {
client *godo.Client client *godo.Client
settings Settings settings Settings
@ -103,8 +87,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
Region: vd.Region, Region: vd.Region,
Size: size, Size: size,
PrivateNetworking: true, PrivateNetworking: true,
SSHKeys: sshKeys(vd.SshKeys), SSHKeys: me.sshKeys(vd.SshKeys),
Volumes: volumes(vd.Volumes), Volumes: me.volumes(vd.Volumes),
UserData: vd.UserData, UserData: vd.UserData,
Image: image, Image: image,
} }
@ -321,3 +305,46 @@ func (me *DropletHandler) actionWait(actionId int) bool {
time.Sleep(1000 * time.Millisecond) 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
}

View File

@ -74,7 +74,6 @@ func (me *ServerManager) Serve() {
running = me.serveAction(action) running = me.serveAction(action)
case <-ticker.C: case <-ticker.C:
if time.Since(me.lastKeepAliveTime) > activityTimeout { if time.Since(me.lastKeepAliveTime) > activityTimeout {
log.Println("ServerManager: Activity timeout for", me.name, " - killing server")
running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN}) running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN})
} }
} }

View File

@ -26,7 +26,7 @@ type Server struct {
UsePersistentImage bool UsePersistentImage bool
ImageSlug string ImageSlug string
UserData string UserData string
SshKeys []int SshKeys []string
Volumes []string Volumes []string
} }