Compare commits
5 Commits
d57884e2d7
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
2b06810bd7 | |||
5dd596d755 | |||
0a2bc38196 | |||
42e65ebf52 | |||
10a959e9f3 |
13
dospin.go
13
dospin.go
@@ -10,6 +10,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -19,6 +20,7 @@ const (
|
||||
|
||||
type cmdOptions struct {
|
||||
config string
|
||||
logFile string
|
||||
cmd string
|
||||
}
|
||||
|
||||
@@ -26,6 +28,7 @@ func parseCmdOptions() cmdOptions {
|
||||
var o cmdOptions
|
||||
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
|
||||
flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
|
||||
flag.StringVar(&o.logFile, "logFile", "stdout", "Path to the dospin log file")
|
||||
flag.Parse()
|
||||
return o
|
||||
}
|
||||
@@ -45,6 +48,16 @@ func spindownAll(opts cmdOptions) {
|
||||
}
|
||||
|
||||
func runServer(opts cmdOptions) {
|
||||
if opts.logFile != "stdout" {
|
||||
logFile, err := os.OpenFile(opts.logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
||||
if err == nil {
|
||||
defer logFile.Close()
|
||||
log.SetOutput(logFile)
|
||||
} else {
|
||||
log.Print("Could not open log file: ", err)
|
||||
|
||||
}
|
||||
}
|
||||
log.Println("Loading config:", opts.config)
|
||||
settings, err := loadSettings(opts.config)
|
||||
if err != nil {
|
||||
|
@@ -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"],
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -35,7 +35,7 @@ type ServerManager struct {
|
||||
name string
|
||||
ports []int
|
||||
in chan serverManagerEvent
|
||||
done chan interface{}
|
||||
done chan int
|
||||
connStatus chan ConnStatus
|
||||
lastKeepAliveTime time.Time
|
||||
server ServerHandler
|
||||
@@ -47,7 +47,8 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
|
||||
sm.name = name
|
||||
sm.ports = settings.Servers[name].Ports
|
||||
sm.in = make(chan serverManagerEvent)
|
||||
sm.done = make(chan interface{})
|
||||
sm.done = make(chan int)
|
||||
sm.connStatus = make(chan ConnStatus)
|
||||
sm.server = server
|
||||
sm.lastKeepAliveTime = time.Now()
|
||||
|
||||
@@ -74,7 +75,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})
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func (me *ServerManager) Serve() {
|
||||
ticker.Stop()
|
||||
|
||||
// notify done
|
||||
me.done <- 42
|
||||
me.done <- 0
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -26,7 +26,7 @@ type Server struct {
|
||||
UsePersistentImage bool
|
||||
ImageSlug string
|
||||
UserData string
|
||||
SshKeys []int
|
||||
SshKeys []string
|
||||
Volumes []string
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user