diff --git a/dospin.json b/dospin.json index eb75bfb..a77117f 100644 --- a/dospin.json +++ b/dospin.json @@ -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" diff --git a/droplethandler.go b/droplethandler.go index a0bb4a4..7536660 100644 --- a/droplethandler.go +++ b/droplethandler.go @@ -153,8 +153,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) { } // delete the image - log.Println("Spinup: Deleting image " + name) - if image.ID > -1 { + if image.ID > 0 { + log.Println("Spinup: Deleting image " + name) _, 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 - 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 + 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) } - log.Println("Spindown: Created image " + name) // delete droplet log.Println("Spindown: Deleting droplet " + name) diff --git a/net.go b/net.go index d8e619e..37be33d 100644 --- a/net.go +++ b/net.go @@ -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() diff --git a/servermanager.go b/servermanager.go index 79c55c0..4a1119d 100644 --- a/servermanager.go +++ b/servermanager.go @@ -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() diff --git a/settings.go b/settings.go index 47b92b0..cb59acb 100644 --- a/settings.go +++ b/settings.go @@ -18,15 +18,16 @@ type Settings struct { } type Server struct { - Ports []int - UsePublicIP bool - InitialSize string - Size string - Region string - ImageSlug string - UserData string - SshKeys []int - Volumes []string + Ports []int + UsePublicIP bool + InitialSize string + Size string + Region string + UsePersistentImage bool + ImageSlug string + UserData string + SshKeys []int + Volumes []string } func loadSettings(path string) (Settings, error) {