Added option to use the public IP of a server, and added spindownall command.
This commit is contained in:
parent
9a5eb7ce4b
commit
cb99d5e881
42
dospin.go
42
dospin.go
@ -12,31 +12,39 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMD_SERVE = "serve"
|
||||||
|
CMD_SPINDOWNALL = "spindownall"
|
||||||
|
)
|
||||||
|
|
||||||
type cmdOptions struct {
|
type cmdOptions struct {
|
||||||
config string
|
config string
|
||||||
|
cmd string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCmdOptions() cmdOptions {
|
func parseCmdOptions() cmdOptions {
|
||||||
var o cmdOptions
|
var o cmdOptions
|
||||||
flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
|
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()
|
flag.Parse()
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
func testServerManager(settings Settings) {
|
func spindownAll(opts cmdOptions) {
|
||||||
dh := NewDropletHandler(settings)
|
// load settings file
|
||||||
sm := NewServerManager("minecraft", dh, settings)
|
settings, err := loadSettings(opts.config)
|
||||||
go sm.Serve()
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
sm.Spinup()
|
// spin down servers
|
||||||
sm.Spindown()
|
for name, _ := range settings.Servers {
|
||||||
|
dh := NewDropletHandler(settings)
|
||||||
sm.Stop()
|
dh.Spindown(name)
|
||||||
sm.Done()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func runServer(opts cmdOptions) {
|
||||||
opts := parseCmdOptions()
|
|
||||||
log.Println("Loading config:", opts.config)
|
log.Println("Loading config:", opts.config)
|
||||||
settings, err := loadSettings(opts.config)
|
settings, err := loadSettings(opts.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -60,3 +68,15 @@ func main() {
|
|||||||
done := make(chan interface{})
|
done := make(chan interface{})
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
opts := parseCmdOptions()
|
||||||
|
switch opts.cmd {
|
||||||
|
case CMD_SPINDOWNALL:
|
||||||
|
spindownAll(opts)
|
||||||
|
case CMD_SERVE:
|
||||||
|
runServer(opts)
|
||||||
|
default:
|
||||||
|
println("Invalid cmd: " + opts.cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -49,8 +49,13 @@ func NewDropletHandler(settings Settings) *DropletHandler {
|
|||||||
Gets the Droplet if it already exists, instantiates it if it does not.
|
Gets the Droplet if it already exists, instantiates it if it does not.
|
||||||
*/
|
*/
|
||||||
func (me *DropletHandler) Spinup(name string) (string, error) {
|
func (me *DropletHandler) Spinup(name string) (string, error) {
|
||||||
|
vd := me.settings.Servers[name]
|
||||||
if droplet, err := me.getDroplet(name); err == nil {
|
if droplet, err := me.getDroplet(name); err == nil {
|
||||||
return droplet.PrivateIPv4()
|
if vd.UsePublicIP {
|
||||||
|
return droplet.PublicIPv4()
|
||||||
|
} else {
|
||||||
|
return droplet.PrivateIPv4()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// create the droplet
|
// create the droplet
|
||||||
image, err := me.getSnapshot(name)
|
image, err := me.getSnapshot(name)
|
||||||
@ -58,8 +63,6 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
vd := me.settings.Servers[name]
|
|
||||||
|
|
||||||
// determine droplet size
|
// determine droplet size
|
||||||
var size string
|
var size string
|
||||||
if vd.InitialSize != "" {
|
if vd.InitialSize != "" {
|
||||||
@ -130,11 +133,11 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
log.Println("Spinup: Deleted image " + name)
|
log.Println("Spinup: Deleted image " + name)
|
||||||
|
|
||||||
// get the private IP and return it
|
// get the private IP and return it
|
||||||
droplet, _, err = me.client.Droplets.Get(droplet.ID)
|
if vd.UsePublicIP {
|
||||||
if err != nil {
|
return droplet.PublicIPv4()
|
||||||
return "", err
|
} else {
|
||||||
|
return droplet.PrivateIPv4()
|
||||||
}
|
}
|
||||||
return droplet.PrivateIPv4()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ type Settings struct {
|
|||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Ports []int
|
Ports []int
|
||||||
|
UsePublicIP bool
|
||||||
InitialSize string
|
InitialSize string
|
||||||
Size string
|
Size string
|
||||||
Region string
|
Region string
|
||||||
|
Loading…
Reference in New Issue
Block a user