Added option to use the public IP of a server, and added spindownall command.

This commit is contained in:
Gary Talent 2016-02-29 06:39:33 +00:00
parent 9a5eb7ce4b
commit cb99d5e881
3 changed files with 42 additions and 18 deletions

View File

@ -12,31 +12,39 @@ import (
"log"
)
const (
CMD_SERVE = "serve"
CMD_SPINDOWNALL = "spindownall"
)
type cmdOptions struct {
config string
cmd string
}
func parseCmdOptions() cmdOptions {
var o cmdOptions
flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
flag.Parse()
return o
}
func testServerManager(settings Settings) {
dh := NewDropletHandler(settings)
sm := NewServerManager("minecraft", dh, settings)
go sm.Serve()
sm.Spinup()
sm.Spindown()
sm.Stop()
sm.Done()
func spindownAll(opts cmdOptions) {
// load settings file
settings, err := loadSettings(opts.config)
if err != nil {
log.Fatal(err)
}
func main() {
opts := parseCmdOptions()
// 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)
if err != nil {
@ -60,3 +68,15 @@ func main() {
done := make(chan interface{})
<-done
}
func main() {
opts := parseCmdOptions()
switch opts.cmd {
case CMD_SPINDOWNALL:
spindownAll(opts)
case CMD_SERVE:
runServer(opts)
default:
println("Invalid cmd: " + opts.cmd)
}
}

View File

@ -49,8 +49,13 @@ func NewDropletHandler(settings Settings) *DropletHandler {
Gets the Droplet if it already exists, instantiates it if it does not.
*/
func (me *DropletHandler) Spinup(name string) (string, error) {
vd := me.settings.Servers[name]
if droplet, err := me.getDroplet(name); err == nil {
if vd.UsePublicIP {
return droplet.PublicIPv4()
} else {
return droplet.PrivateIPv4()
}
} else {
// create the droplet
image, err := me.getSnapshot(name)
@ -58,8 +63,6 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
return "", err
}
vd := me.settings.Servers[name]
// determine droplet size
var size string
if vd.InitialSize != "" {
@ -130,13 +133,13 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
log.Println("Spinup: Deleted image " + name)
// get the private IP and return it
droplet, _, err = me.client.Droplets.Get(droplet.ID)
if err != nil {
return "", err
}
if vd.UsePublicIP {
return droplet.PublicIPv4()
} else {
return droplet.PrivateIPv4()
}
}
}
func (me *DropletHandler) Spindown(name string) error {
droplet, err := me.getDroplet(name)

View File

@ -19,6 +19,7 @@ type Settings struct {
type Server struct {
Ports []int
UsePublicIP bool
InitialSize string
Size string
Region string