Added support for resizing Droplets after creation.
This commit is contained in:
parent
92a31f2244
commit
c4dc4c2de1
@ -3,6 +3,7 @@
|
|||||||
"Servers": {
|
"Servers": {
|
||||||
"minecraft": {
|
"minecraft": {
|
||||||
"Ports": [25565],
|
"Ports": [25565],
|
||||||
|
"InitialSize": "512mb",
|
||||||
"Size": "4gb",
|
"Size": "4gb",
|
||||||
"Region": "nyc3"
|
"Region": "nyc3"
|
||||||
}
|
}
|
||||||
|
@ -57,11 +57,21 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
vd := me.settings.Servers[name]
|
vd := me.settings.Servers[name]
|
||||||
|
|
||||||
|
// determine droplet size
|
||||||
|
var size string
|
||||||
|
if vd.InitialSize != "" {
|
||||||
|
size = vd.InitialSize
|
||||||
|
} else {
|
||||||
|
size = vd.Size
|
||||||
|
}
|
||||||
|
|
||||||
createRequest := &godo.DropletCreateRequest{
|
createRequest := &godo.DropletCreateRequest{
|
||||||
Name: DROPLET_NS + name,
|
Name: DROPLET_NS + name,
|
||||||
Region: vd.Region,
|
Region: vd.Region,
|
||||||
Size: vd.Size,
|
Size: size,
|
||||||
PrivateNetworking: true,
|
PrivateNetworking: true,
|
||||||
Image: godo.DropletCreateImage{
|
Image: godo.DropletCreateImage{
|
||||||
ID: image.ID,
|
ID: image.ID,
|
||||||
@ -89,6 +99,28 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
}
|
}
|
||||||
log.Println("Spinup: Created " + name)
|
log.Println("Spinup: Created " + name)
|
||||||
|
|
||||||
|
// resize if necessary
|
||||||
|
if vd.InitialSize != "" && vd.InitialSize != vd.Size {
|
||||||
|
// power off
|
||||||
|
me.poweroff(name)
|
||||||
|
|
||||||
|
// resize
|
||||||
|
log.Println("Spinup: Resizing " + name)
|
||||||
|
action, _, err := me.client.DropletActions.Resize(droplet.ID, vd.Size, false)
|
||||||
|
if err != nil || !me.actionWait(action.ID) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
log.Println("Spinup: Resized " + name)
|
||||||
|
|
||||||
|
// power back on
|
||||||
|
log.Println("Spinup: Powering on " + name)
|
||||||
|
action, _, err = me.client.DropletActions.PowerOn(droplet.ID)
|
||||||
|
if err != nil || !me.actionWait(action.ID) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
log.Println("Spinup: Powered on " + name)
|
||||||
|
}
|
||||||
|
|
||||||
// delete the image
|
// delete the image
|
||||||
log.Println("Spinup: Deleting image " + name)
|
log.Println("Spinup: Deleting image " + name)
|
||||||
_, err = me.client.Images.Delete(image.ID)
|
_, err = me.client.Images.Delete(image.ID)
|
||||||
@ -113,23 +145,9 @@ func (me *DropletHandler) Spindown(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// power off
|
// power off
|
||||||
if droplet.Status != "off" {
|
err = me.poweroff(name)
|
||||||
log.Println("Spindown: Powering down " + name)
|
if err != nil {
|
||||||
// wait until machine is off
|
return err
|
||||||
for {
|
|
||||||
droplet, err = me.getDroplet(name)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
} else if droplet.Status == "off" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
_, _, err = me.client.DropletActions.Shutdown(droplet.ID)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Spindown: Power down of ", name, " failed: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Println("Spindown: Powered down " + name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// snapshot existing droplet
|
// snapshot existing droplet
|
||||||
@ -151,6 +169,35 @@ func (me *DropletHandler) Spindown(name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (me *DropletHandler) poweroff(name string) error {
|
||||||
|
droplet, err := me.getDroplet(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if droplet.Status != "off" {
|
||||||
|
log.Println("Powering down " + name)
|
||||||
|
// wait until machine is off
|
||||||
|
for {
|
||||||
|
droplet, err = me.getDroplet(name)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Power down of", name, "failed:", err)
|
||||||
|
if droplet.ID < 1 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if droplet.Status == "off" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
_, _, err = me.client.DropletActions.Shutdown(droplet.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Power down of", name, "failed:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println("Powered down", name)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (me *DropletHandler) getDroplet(name string) (godo.Droplet, error) {
|
func (me *DropletHandler) getDroplet(name string) (godo.Droplet, error) {
|
||||||
name = DROPLET_NS + name
|
name = DROPLET_NS + name
|
||||||
page := 0
|
page := 0
|
||||||
@ -220,8 +267,7 @@ func (me *DropletHandler) actionWait(actionId int) bool {
|
|||||||
for {
|
for {
|
||||||
a, _, err := me.client.Actions.Get(actionId)
|
a, _, err := me.client.Actions.Get(actionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Action failed: ", err)
|
log.Println("Action retrieval failed: ", err)
|
||||||
return false
|
|
||||||
} else if a.Status == "completed" {
|
} else if a.Status == "completed" {
|
||||||
return true
|
return true
|
||||||
} else if a.Status == "errored" {
|
} else if a.Status == "errored" {
|
||||||
|
@ -92,7 +92,7 @@ func (me *ServerManager) serveAction(action int) bool {
|
|||||||
case SERVERMANAGER_SPINUP:
|
case SERVERMANAGER_SPINUP:
|
||||||
ip, err := me.server.Spinup(me.name)
|
ip, err := me.server.Spinup(me.name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("ServerManager: Got IP for", me.name, ":", ip)
|
log.Println("ServerManager: Got IP for", me.name+":", ip)
|
||||||
me.addPortForwards(ip)
|
me.addPortForwards(ip)
|
||||||
} else {
|
} else {
|
||||||
log.Println("ServerManager: Could not spin up "+me.name+":", err)
|
log.Println("ServerManager: Could not spin up "+me.name+":", err)
|
||||||
|
@ -18,9 +18,10 @@ type Settings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Ports []int
|
Ports []int
|
||||||
Size string
|
InitialSize string
|
||||||
Region string
|
Size string
|
||||||
|
Region string
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSettings(path string) (Settings, error) {
|
func loadSettings(path string) (Settings, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user