Add checking to prevent failed deletion of nonexistent droplets and
unnecessary archiving of droplets in images
This commit is contained in:
parent
2f105fa6b5
commit
d57884e2d7
@ -8,6 +8,7 @@
|
|||||||
"Size": "4gb",
|
"Size": "4gb",
|
||||||
"Region": "nyc1",
|
"Region": "nyc1",
|
||||||
"SshKeys": [513314, 1667247],
|
"SshKeys": [513314, 1667247],
|
||||||
|
"UsePersistentImage": false,
|
||||||
"ImageSlug": "ubuntu-16-04-x64",
|
"ImageSlug": "ubuntu-16-04-x64",
|
||||||
"Volumes": ["volume-nyc1-01"],
|
"Volumes": ["volume-nyc1-01"],
|
||||||
"UserData": "#!/bin/bash\napt-get update\napt-get install -y docker.io\nmkdir -p /mnt/volume-nyc1-01\nmount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01\necho /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01 ext4 defaults,nofail,discard 0 0 | tee -a /etc/fstab\ndocker run -d --restart=always -p 25565:25565 -v /mnt/volume-nyc1-01/minecraft-server:/minecraft-server -w /minecraft-server -t java:8-alpine sh start.sh"
|
"UserData": "#!/bin/bash\napt-get update\napt-get install -y docker.io\nmkdir -p /mnt/volume-nyc1-01\nmount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01\necho /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01 ext4 defaults,nofail,discard 0 0 | tee -a /etc/fstab\ndocker run -d --restart=always -p 25565:25565 -v /mnt/volume-nyc1-01/minecraft-server:/minecraft-server -w /minecraft-server -t java:8-alpine sh start.sh"
|
||||||
|
@ -153,8 +153,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// delete the image
|
// delete the image
|
||||||
log.Println("Spinup: Deleting image " + name)
|
if image.ID > 0 {
|
||||||
if image.ID > -1 {
|
log.Println("Spinup: Deleting image " + name)
|
||||||
_, err = me.client.Images.Delete(image.ID)
|
_, err = me.client.Images.Delete(image.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Spinup: Could not delete image: ", err)
|
log.Println("Spinup: Could not delete image: ", err)
|
||||||
@ -182,7 +182,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
func (me *DropletHandler) Spindown(name string) error {
|
func (me *DropletHandler) Spindown(name string) error {
|
||||||
droplet, err := me.getDroplet(name)
|
droplet, err := me.getDroplet(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
// droplet not existing is not an error
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// power off
|
// power off
|
||||||
@ -192,12 +193,14 @@ func (me *DropletHandler) Spindown(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// snapshot existing droplet
|
// snapshot existing droplet
|
||||||
log.Println("Spindown: Creating image " + name)
|
if me.settings.Servers[name].UsePersistentImage {
|
||||||
action, _, err := me.client.DropletActions.Snapshot(droplet.ID, DROPLET_NS+name)
|
log.Println("Spindown: Creating image " + name)
|
||||||
if err != nil || !me.actionWait(action.ID) {
|
action, _, err := me.client.DropletActions.Snapshot(droplet.ID, DROPLET_NS+name)
|
||||||
return err
|
if err != nil || !me.actionWait(action.ID) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Println("Spindown: Created image " + name)
|
||||||
}
|
}
|
||||||
log.Println("Spindown: Created image " + name)
|
|
||||||
|
|
||||||
// delete droplet
|
// delete droplet
|
||||||
log.Println("Spindown: Deleting droplet " + name)
|
log.Println("Spindown: Deleting droplet " + name)
|
||||||
|
10
net.go
10
net.go
@ -29,24 +29,26 @@ func portForward(wanConn *net.TCPConn, lanIp, port string, connStatus chan ConnS
|
|||||||
log.Print("Proxy: Connecting to ", lanIp+":"+port)
|
log.Print("Proxy: Connecting to ", lanIp+":"+port)
|
||||||
lanConn, err := net.Dial("tcp", lanIp+":"+port)
|
lanConn, err := net.Dial("tcp", lanIp+":"+port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Proxy: LAN dial error:", err)
|
log.Print("Proxy: LAN dial error: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go forwardConn(wanConn, lanConn, done)
|
go forwardConn(wanConn, lanConn, done)
|
||||||
go forwardConn(lanConn, wanConn, done)
|
go forwardConn(lanConn, wanConn, done)
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Minute * 1)
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; {
|
||||||
select {
|
select {
|
||||||
case err = <-done:
|
case err = <-done:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Proxy:", err)
|
log.Print("Proxy: ", err)
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
connStatus <- ConnStatus{Status: CONN_ACTIVE}
|
connStatus <- ConnStatus{Status: CONN_ACTIVE}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Print("Proxy: ending connection: ", wanConn.LocalAddr().String())
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
|
||||||
wanConn.Close()
|
wanConn.Close()
|
||||||
|
@ -49,6 +49,7 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
|
|||||||
sm.in = make(chan serverManagerEvent)
|
sm.in = make(chan serverManagerEvent)
|
||||||
sm.done = make(chan interface{})
|
sm.done = make(chan interface{})
|
||||||
sm.server = server
|
sm.server = server
|
||||||
|
sm.lastKeepAliveTime = time.Now()
|
||||||
|
|
||||||
return sm
|
return sm
|
||||||
}
|
}
|
||||||
@ -59,8 +60,8 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
|
|||||||
func (me *ServerManager) Serve() {
|
func (me *ServerManager) Serve() {
|
||||||
// TODO: see if server is currently up, and setup port forwarding if so
|
// TODO: see if server is currently up, and setup port forwarding if so
|
||||||
|
|
||||||
fiveMin := time.Duration(5) * time.Minute
|
activityTimeout := time.Duration(5 * time.Minute)
|
||||||
ticker := time.NewTicker(fiveMin)
|
ticker := time.NewTicker(activityTimeout)
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
for running := true; running; {
|
for running := true; running; {
|
||||||
@ -72,8 +73,9 @@ func (me *ServerManager) Serve() {
|
|||||||
case action := <-me.in:
|
case action := <-me.in:
|
||||||
running = me.serveAction(action)
|
running = me.serveAction(action)
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if time.Since(me.lastKeepAliveTime) > fiveMin {
|
if time.Since(me.lastKeepAliveTime) > activityTimeout {
|
||||||
me.Spindown()
|
log.Println("ServerManager: Activity timeout for", me.name, " - killing server")
|
||||||
|
running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,6 +143,7 @@ func (me *ServerManager) serveAction(event serverManagerEvent) bool {
|
|||||||
switch event.eventType {
|
switch event.eventType {
|
||||||
case SERVERMANAGER_SPINUP:
|
case SERVERMANAGER_SPINUP:
|
||||||
targetIp, err := me.server.Spinup(me.name)
|
targetIp, err := me.server.Spinup(me.name)
|
||||||
|
me.lastKeepAliveTime = time.Now()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("ServerManager: Got IP for", me.name+":", targetIp)
|
log.Println("ServerManager: Got IP for", me.name+":", targetIp)
|
||||||
wanAddr := event.tcpConn.LocalAddr().String()
|
wanAddr := event.tcpConn.LocalAddr().String()
|
||||||
|
19
settings.go
19
settings.go
@ -18,15 +18,16 @@ type Settings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Ports []int
|
Ports []int
|
||||||
UsePublicIP bool
|
UsePublicIP bool
|
||||||
InitialSize string
|
InitialSize string
|
||||||
Size string
|
Size string
|
||||||
Region string
|
Region string
|
||||||
ImageSlug string
|
UsePersistentImage bool
|
||||||
UserData string
|
ImageSlug string
|
||||||
SshKeys []int
|
UserData string
|
||||||
Volumes []string
|
SshKeys []int
|
||||||
|
Volumes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSettings(path string) (Settings, error) {
|
func loadSettings(path string) (Settings, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user