dospin/dospin.go

83 lines
1.6 KiB
Go
Raw Normal View History

2016-02-26 17:35:57 -06:00
/*
Copyright 2016 gtalent2@gmail.com
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
2016-02-14 23:38:28 -06:00
package main
import (
"flag"
2016-02-14 23:38:28 -06:00
"log"
)
const (
CMD_SERVE = "serve"
CMD_SPINDOWNALL = "spindownall"
)
type cmdOptions struct {
config string
cmd string
2016-02-14 23:38:28 -06:00
}
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.Parse()
return o
2016-02-14 23:38:28 -06:00
}
func spindownAll(opts cmdOptions) {
// load settings file
settings, err := loadSettings(opts.config)
if err != nil {
log.Fatal(err)
}
// spin down servers
for name, _ := range settings.Servers {
dh := NewDropletHandler(settings)
dh.Spindown(name)
}
}
func runServer(opts cmdOptions) {
log.Println("Loading config:", opts.config)
settings, err := loadSettings(opts.config)
2016-02-14 23:38:28 -06:00
if err != nil {
log.Fatal(err)
}
for name, sv := range settings.Servers {
dh := NewDropletHandler(settings)
sm := NewServerManager(name, dh, settings)
2016-02-14 23:38:28 -06:00
// start the ServerManager
go sm.Serve()
// assign this ServerManager to all appropriate ports
for _, port := range sv.Ports {
log.Println("Setting up port", port)
setupService(sm, port)
}
}
done := make(chan interface{})
<-done
2016-02-14 23:38:28 -06:00
}
func main() {
opts := parseCmdOptions()
switch opts.cmd {
case CMD_SPINDOWNALL:
spindownAll(opts)
case CMD_SERVE:
runServer(opts)
default:
println("Invalid cmd: " + opts.cmd)
}
}