2016-02-26 17:35:57 -06:00
|
|
|
/*
|
2017-01-25 17:59:25 -06:00
|
|
|
Copyright 2016-2017 gtalent2@gmail.com
|
2016-02-26 17:35:57 -06:00
|
|
|
|
|
|
|
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 (
|
2016-02-26 18:40:47 -06:00
|
|
|
"flag"
|
2016-02-14 23:38:28 -06:00
|
|
|
"log"
|
2017-01-27 19:48:03 -06:00
|
|
|
"os"
|
2016-02-14 23:38:28 -06:00
|
|
|
)
|
|
|
|
|
2016-02-29 00:39:33 -06:00
|
|
|
const (
|
|
|
|
CMD_SERVE = "serve"
|
|
|
|
CMD_SPINDOWNALL = "spindownall"
|
|
|
|
)
|
|
|
|
|
2016-02-26 18:40:47 -06:00
|
|
|
type cmdOptions struct {
|
2017-01-27 19:48:03 -06:00
|
|
|
config string
|
|
|
|
logFile string
|
|
|
|
cmd string
|
2016-02-14 23:38:28 -06:00
|
|
|
}
|
|
|
|
|
2016-02-26 18:40:47 -06:00
|
|
|
func parseCmdOptions() cmdOptions {
|
|
|
|
var o cmdOptions
|
2016-02-29 00:39:33 -06:00
|
|
|
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
|
2017-01-28 02:36:40 -06:00
|
|
|
flag.StringVar(&o.config, "config", "dospin.yaml", "Path to the dospin config file")
|
2017-01-27 19:48:03 -06:00
|
|
|
flag.StringVar(&o.logFile, "logFile", "stdout", "Path to the dospin log file")
|
2016-02-26 18:40:47 -06:00
|
|
|
flag.Parse()
|
|
|
|
return o
|
2016-02-14 23:38:28 -06:00
|
|
|
}
|
|
|
|
|
2016-02-29 00:39:33 -06:00
|
|
|
func spindownAll(opts cmdOptions) {
|
|
|
|
// load settings file
|
|
|
|
settings, err := loadSettings(opts.config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-02-29 00:06:31 -06:00
|
|
|
|
2016-02-29 00:39:33 -06:00
|
|
|
// spin down servers
|
|
|
|
for name, _ := range settings.Servers {
|
|
|
|
dh := NewDropletHandler(settings)
|
|
|
|
dh.Spindown(name)
|
|
|
|
}
|
2016-02-29 00:06:31 -06:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:00:09 -05:00
|
|
|
func runServer(opts cmdOptions) int {
|
2017-01-27 19:48:03 -06:00
|
|
|
if opts.logFile != "stdout" {
|
|
|
|
logFile, err := os.OpenFile(opts.logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
|
|
|
if err == nil {
|
2017-01-27 19:52:58 -06:00
|
|
|
defer logFile.Close()
|
2017-01-27 19:48:03 -06:00
|
|
|
log.SetOutput(logFile)
|
|
|
|
} else {
|
|
|
|
log.Print("Could not open log file: ", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 18:40:47 -06:00
|
|
|
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)
|
|
|
|
}
|
2016-02-26 18:40:47 -06:00
|
|
|
|
2016-02-29 00:06:31 -06:00
|
|
|
for name, sv := range settings.Servers {
|
|
|
|
dh := NewDropletHandler(settings)
|
|
|
|
sm := NewServerManager(name, dh, settings)
|
2016-02-14 23:38:28 -06:00
|
|
|
|
2016-02-29 00:06:31 -06:00
|
|
|
// start the ServerManager
|
|
|
|
go sm.Serve()
|
2016-02-26 17:32:39 -06:00
|
|
|
|
2016-03-04 00:13:43 -06:00
|
|
|
// assign this ServerManager all appropriate ports
|
2016-02-29 00:06:31 -06:00
|
|
|
for _, port := range sv.Ports {
|
|
|
|
log.Println("Setting up port", port)
|
2016-03-04 00:13:43 -06:00
|
|
|
sm.setupListener(port)
|
2016-02-29 00:06:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:00:09 -05:00
|
|
|
done := make(chan int)
|
|
|
|
return <-done
|
2016-02-14 23:38:28 -06:00
|
|
|
}
|
2016-02-29 00:39:33 -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)
|
|
|
|
}
|
|
|
|
}
|