Added start of ServerManager.

This commit is contained in:
Gary Talent 2016-02-27 16:10:44 -06:00
parent 18aa0471e9
commit b31b18674c
5 changed files with 113 additions and 11 deletions

View File

@ -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
}

View File

@ -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

96
servermanager.go Normal file
View File

@ -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
}

View File

@ -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 {

View File

@ -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
}