Add checking to prevent failed deletion of nonexistent droplets and

unnecessary archiving of droplets in images
This commit is contained in:
Gary Talent 2017-01-27 18:06:27 -06:00
parent 2f105fa6b5
commit d57884e2d7
5 changed files with 35 additions and 25 deletions

View File

@ -8,6 +8,7 @@
"Size": "4gb",
"Region": "nyc1",
"SshKeys": [513314, 1667247],
"UsePersistentImage": false,
"ImageSlug": "ubuntu-16-04-x64",
"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"

View File

@ -153,8 +153,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
}
// delete the image
if image.ID > 0 {
log.Println("Spinup: Deleting image " + name)
if image.ID > -1 {
_, err = me.client.Images.Delete(image.ID)
if err != nil {
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 {
droplet, err := me.getDroplet(name)
if err != nil {
return err
// droplet not existing is not an error
return nil
}
// power off
@ -192,12 +193,14 @@ func (me *DropletHandler) Spindown(name string) error {
}
// snapshot existing droplet
if me.settings.Servers[name].UsePersistentImage {
log.Println("Spindown: Creating image " + name)
action, _, err := me.client.DropletActions.Snapshot(droplet.ID, DROPLET_NS+name)
if err != nil || !me.actionWait(action.ID) {
return err
}
log.Println("Spindown: Created image " + name)
}
// delete droplet
log.Println("Spindown: Deleting droplet " + name)

10
net.go
View File

@ -29,24 +29,26 @@ func portForward(wanConn *net.TCPConn, lanIp, port string, connStatus chan ConnS
log.Print("Proxy: Connecting to ", lanIp+":"+port)
lanConn, err := net.Dial("tcp", lanIp+":"+port)
if err != nil {
log.Print("Proxy: LAN dial error:", err)
log.Print("Proxy: LAN dial error: ", err)
return
}
go forwardConn(wanConn, lanConn, done)
go forwardConn(lanConn, wanConn, done)
ticker := time.NewTicker(time.Minute * 1)
for i := 0; i < 2; i++ {
ticker := time.NewTicker(1 * time.Minute)
for i := 0; i < 2; {
select {
case err = <-done:
if err != nil {
log.Print("Proxy:", err)
log.Print("Proxy: ", err)
}
i++
case <-ticker.C:
connStatus <- ConnStatus{Status: CONN_ACTIVE}
}
}
log.Print("Proxy: ending connection: ", wanConn.LocalAddr().String())
ticker.Stop()
wanConn.Close()

View File

@ -49,6 +49,7 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
sm.in = make(chan serverManagerEvent)
sm.done = make(chan interface{})
sm.server = server
sm.lastKeepAliveTime = time.Now()
return sm
}
@ -59,8 +60,8 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
func (me *ServerManager) Serve() {
// TODO: see if server is currently up, and setup port forwarding if so
fiveMin := time.Duration(5) * time.Minute
ticker := time.NewTicker(fiveMin)
activityTimeout := time.Duration(5 * time.Minute)
ticker := time.NewTicker(activityTimeout)
// event loop
for running := true; running; {
@ -72,8 +73,9 @@ func (me *ServerManager) Serve() {
case action := <-me.in:
running = me.serveAction(action)
case <-ticker.C:
if time.Since(me.lastKeepAliveTime) > fiveMin {
me.Spindown()
if time.Since(me.lastKeepAliveTime) > activityTimeout {
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 {
case SERVERMANAGER_SPINUP:
targetIp, err := me.server.Spinup(me.name)
me.lastKeepAliveTime = time.Now()
if err == nil {
log.Println("ServerManager: Got IP for", me.name+":", targetIp)
wanAddr := event.tcpConn.LocalAddr().String()

View File

@ -23,6 +23,7 @@ type Server struct {
InitialSize string
Size string
Region string
UsePersistentImage bool
ImageSlug string
UserData string
SshKeys []int