Added start of ServerManager.
This commit is contained in:
parent
18aa0471e9
commit
b31b18674c
@ -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
|
||||
}
|
||||
|
@ -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
96
servermanager.go
Normal 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
|
||||
}
|
@ -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 {
|
||||
|
10
settings.go
10
settings.go
@ -14,10 +14,16 @@ import (
|
||||
|
||||
type Settings struct {
|
||||
ApiToken string
|
||||
VirtualDroplets map[string]VirtualDroplet
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user