From b31b18674c9d15360b0bb0bd6451d7c7d6af239e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 27 Feb 2016 16:10:44 -0600 Subject: [PATCH] Added start of ServerManager. --- dospin.go | 4 +- droplethandler.go | 6 +-- servermanager.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++ servicemanager.go | 6 +-- settings.go | 12 ++++-- 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 servermanager.go diff --git a/dospin.go b/dospin.go index 242e527..6882df1 100644 --- a/dospin.go +++ b/dospin.go @@ -33,14 +33,14 @@ func main() { dh := NewDropletHandler(settings) - ip, err := dh.SpinupMachine("minecraft") + ip, err := dh.Spinup("minecraft") if err != nil { log.Println("Error:", err) return } log.Println("IP: " + ip) - if err := dh.SpindownMachine("minecraft"); err != nil { + if err := dh.Spindown("minecraft"); err != nil { log.Println("Error:", err) return } diff --git a/droplethandler.go b/droplethandler.go index ff55881..c203d7e 100644 --- a/droplethandler.go +++ b/droplethandler.go @@ -48,7 +48,7 @@ func NewDropletHandler(settings Settings) *DropletHandler { /* Gets the Droplet if it already exists, instantiates it if it does not. */ -func (me *DropletHandler) SpinupMachine(name string) (string, error) { +func (me *DropletHandler) Spinup(name string) (string, error) { if droplet, err := me.getDroplet(name); err == nil { return droplet.PrivateIPv4() } else { @@ -57,7 +57,7 @@ func (me *DropletHandler) SpinupMachine(name string) (string, error) { if err != nil { return "", err } - vd := me.settings.VirtualDroplets[name] + vd := me.settings.VirtualServers[name] createRequest := &godo.DropletCreateRequest{ Name: DROPLET_NS + name, Region: vd.Region, @@ -106,7 +106,7 @@ func (me *DropletHandler) SpinupMachine(name string) (string, error) { } } -func (me *DropletHandler) SpindownMachine(name string) error { +func (me *DropletHandler) Spindown(name string) error { droplet, err := me.getDroplet(name) if err != nil { return err diff --git a/servermanager.go b/servermanager.go new file mode 100644 index 0000000..4fe8747 --- /dev/null +++ b/servermanager.go @@ -0,0 +1,96 @@ +package main + +import "log" + +const ( + SERVERMANAGER_SPINUP = iota + SERVERMANAGER_SPINDOWN + SERVERMANAGER_STOP +) + +type ServerManager struct { + name string + ports []int + in chan int + out chan int + usageScore int // spin down server when this reaches 0 + server ServerHandler +} + +func NewServerManager(name string, server ServerHandler, settings Settings) *ServerManager { + sm := new(ServerManager) + + sm.in = make(chan int) + sm.out = make(chan int) + sm.usageScore = 5 + sm.server = server + + // find the ports associated with this server in settings + for _, s := range settings.Services { + if s.LogicalServer == name { + sm.ports = append(sm.ports, s.Port) + } + } + + return sm +} + +/* + Serves channel requests. +*/ +func (me *ServerManager) Serve() { + for running := true; running; { + select { + case action := <-me.in: + running = me.serveAction(action) + } + } +} + +/* + Sends the serve loop a spinup message. +*/ +func (me *ServerManager) Spinup() { + me.in <- SERVERMANAGER_SPINUP +} + +/* + Sends the serve loop a spindown message. +*/ +func (me *ServerManager) Spindown() { + me.in <- SERVERMANAGER_SPINDOWN +} + +/* + Sends the serve loop a quit message. +*/ +func (me *ServerManager) Stop() { + me.in <- SERVERMANAGER_STOP +} + +func (me *ServerManager) addPortForwards(ip string) { +} + +func (me *ServerManager) rmPortForwards() { +} + +func (me *ServerManager) serveAction(action int) bool { + running := true + switch action { + case SERVERMANAGER_SPINUP: + ip, err := me.server.Spinup(me.name) + me.addPortForwards(ip) + if err != nil { + log.Println("ServerManager: Could not spin up "+me.name+":", err) + } + case SERVERMANAGER_SPINDOWN: + err := me.server.Spindown(me.name) + me.rmPortForwards() + if err != nil { + log.Println("ServerManager: Could not spin down "+me.name+":", err) + } + case SERVERMANAGER_STOP: + running = false + } + return running +} diff --git a/servicemanager.go b/servicemanager.go index 3c01b0f..ef8a5b7 100644 --- a/servicemanager.go +++ b/servicemanager.go @@ -15,8 +15,8 @@ import ( type ServerHandler interface { // Takes snapshot name, and returns the IP to connect to. - SpinupMachine(name string) (string, error) - SpindownMachine(name string) error + Spinup(name string) (string, error) + Spindown(name string) error } type service struct { @@ -64,7 +64,7 @@ func (me *ServiceManager) setupService(serviceName, machineName string, port int // connection accepted // spinup machine - ip, err := me.machineManager.SpinupMachine(machineName) + ip, err := me.machineManager.Spinup(machineName) // setup port forwarding if err == nil { diff --git a/settings.go b/settings.go index d6ead76..1daa18b 100644 --- a/settings.go +++ b/settings.go @@ -13,11 +13,17 @@ import ( ) type Settings struct { - ApiToken string - VirtualDroplets map[string]VirtualDroplet + ApiToken string + Services map[string]Service + VirtualServers map[string]VirtualServer } -type VirtualDroplet struct { +type Service struct { + Port int + LogicalServer string +} + +type VirtualServer struct { Size string Region string }