From cb99d5e8813616612103e83a222c28e8d2143ae4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 29 Feb 2016 06:39:33 +0000 Subject: [PATCH] Added option to use the public IP of a server, and added spindownall command. --- dospin.go | 42 +++++++++++++++++++++++++++++++----------- droplethandler.go | 17 ++++++++++------- settings.go | 1 + 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/dospin.go b/dospin.go index 22069b6..2c1b33c 100644 --- a/dospin.go +++ b/dospin.go @@ -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() +func spindownAll(opts cmdOptions) { + // load settings file + settings, err := loadSettings(opts.config) + if err != nil { + log.Fatal(err) + } - sm.Spinup() - sm.Spindown() - - sm.Stop() - sm.Done() + // spin down servers + for name, _ := range settings.Servers { + dh := NewDropletHandler(settings) + dh.Spindown(name) + } } -func main() { - opts := parseCmdOptions() +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) + } +} diff --git a/droplethandler.go b/droplethandler.go index 9a5fbca..1662a4d 100644 --- a/droplethandler.go +++ b/droplethandler.go @@ -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 { - return droplet.PrivateIPv4() + 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,11 +133,11 @@ 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() } - return droplet.PrivateIPv4() } } diff --git a/settings.go b/settings.go index 0bad8be..7a63a1b 100644 --- a/settings.go +++ b/settings.go @@ -19,6 +19,7 @@ type Settings struct { type Server struct { Ports []int + UsePublicIP bool InitialSize string Size string Region string