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"
|
||||
)
|
||||
|
||||
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) {
|
||||
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)
|
||||
sm := NewServerManager("minecraft", dh, settings)
|
||||
go sm.Serve()
|
||||
|
||||
sm.Spinup()
|
||||
sm.Spindown()
|
||||
|
||||
sm.Stop()
|
||||
sm.Done()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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,12 +133,12 @@ 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 {
|
||||
|
@ -19,6 +19,7 @@ type Settings struct {
|
||||
|
||||
type Server struct {
|
||||
Ports []int
|
||||
UsePublicIP bool
|
||||
InitialSize string
|
||||
Size string
|
||||
Region string
|
||||
|
Loading…
Reference in New Issue
Block a user