Compare commits
4 Commits
release-0.
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
432b47d224 | |||
6c1321e485 | |||
32408e4471 | |||
8879463cb9 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
dospin.json
|
||||
test_config.json
|
||||
test_config.yaml
|
||||
dospin
|
||||
main
|
||||
|
@@ -27,7 +27,7 @@ type cmdOptions struct {
|
||||
func parseCmdOptions() cmdOptions {
|
||||
var o cmdOptions
|
||||
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
|
||||
flag.StringVar(&o.config, "config", "dospin.json", "Path to the dospin config file")
|
||||
flag.StringVar(&o.config, "config", "dospin.yaml", "Path to the dospin config file")
|
||||
flag.StringVar(&o.logFile, "logFile", "stdout", "Path to the dospin log file")
|
||||
flag.Parse()
|
||||
return o
|
||||
|
20
dospin.json
20
dospin.json
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"ApiToken": "<your token here>",
|
||||
"Servers": {
|
||||
"minecraft": {
|
||||
"Ports": [25565],
|
||||
"UsePublicIP": false,
|
||||
"InitialSize": "4gb",
|
||||
"Size": "4gb",
|
||||
"Region": "nyc1",
|
||||
"SshKeys": [
|
||||
"Key1",
|
||||
"gtalent2@gmail.com"
|
||||
],
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
26
dospin.yaml
Normal file
26
dospin.yaml
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
api_token: <your token here>
|
||||
servers:
|
||||
minecraft:
|
||||
ports:
|
||||
- 25565
|
||||
activity_timeout_min: 20m
|
||||
use_public_ip: false
|
||||
initial_size: 4gb
|
||||
size: 4gb
|
||||
region: nyc1
|
||||
ssh_keys:
|
||||
- Key1
|
||||
- gtalent2@gmail.com
|
||||
use_persistent_image: false
|
||||
image_slug: ubuntu-16-04-x64
|
||||
volumes:
|
||||
- volume-nyc1-01
|
||||
user_data: |-
|
||||
#!/bin/bash
|
||||
apt-get update
|
||||
apt-get install -y docker.io
|
||||
mkdir -p /mnt/volume-nyc1-01
|
||||
mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01
|
||||
echo /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01 ext4 defaults,nofail,discard 0 0 | tee -a /etc/fstab
|
||||
docker 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
|
@@ -1,2 +0,0 @@
|
||||
#! /usr/bin/env sh
|
||||
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/droplets?page=1&per_page=100&private=true" | jq .
|
@@ -1,2 +0,0 @@
|
||||
#! /usr/bin/env sh
|
||||
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/images?page=1&per_page=100&private=true" | jq .
|
@@ -1,4 +0,0 @@
|
||||
#! /usr/bin/env sh
|
||||
|
||||
echo Keys
|
||||
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/account/keys" | jq .
|
@@ -1,4 +0,0 @@
|
||||
#! /usr/bin/env sh
|
||||
|
||||
echo Volumes
|
||||
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer `cat dospin.json | jq -r .ApiToken`" "https://api.digitalocean.com/v2/volumes" | jq .
|
@@ -39,6 +39,7 @@ type ServerManager struct {
|
||||
connStatus chan ConnStatus
|
||||
lastKeepAliveTime time.Time
|
||||
server ServerHandler
|
||||
activityTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewServerManager(name string, server ServerHandler, settings Settings) *ServerManager {
|
||||
@@ -52,6 +53,13 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
|
||||
sm.server = server
|
||||
sm.lastKeepAliveTime = time.Now()
|
||||
|
||||
activityTimeout, err := time.ParseDuration(settings.Servers[sm.name].ActivityTimeout)
|
||||
if err != nil { // invalid timeout, default to 5 minutes
|
||||
activityTimeout = time.Duration(5 * time.Minute)
|
||||
}
|
||||
sm.activityTimeout = activityTimeout
|
||||
log.Println("ServerManager: ", name, " has activity timeout of ", sm.activityTimeout.String())
|
||||
|
||||
return sm
|
||||
}
|
||||
|
||||
@@ -61,8 +69,7 @@ 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
|
||||
|
||||
activityTimeout := time.Duration(5 * time.Minute)
|
||||
ticker := time.NewTicker(activityTimeout)
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
|
||||
// event loop
|
||||
for running := true; running; {
|
||||
@@ -74,7 +81,7 @@ func (me *ServerManager) Serve() {
|
||||
case action := <-me.in:
|
||||
running = me.serveAction(action)
|
||||
case <-ticker.C:
|
||||
if time.Since(me.lastKeepAliveTime) > activityTimeout {
|
||||
if time.Since(me.lastKeepAliveTime) > me.activityTimeout {
|
||||
running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN})
|
||||
}
|
||||
}
|
||||
|
29
settings.go
29
settings.go
@@ -8,26 +8,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
ApiToken string
|
||||
Servers map[string]Server
|
||||
ApiToken string `yaml:"api_token"`
|
||||
Servers map[string]Server `yaml:"servers"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Ports []int
|
||||
UsePublicIP bool
|
||||
InitialSize string
|
||||
Size string
|
||||
Region string
|
||||
UsePersistentImage bool
|
||||
ImageSlug string
|
||||
UserData string
|
||||
SshKeys []string
|
||||
Volumes []string
|
||||
Ports []int `yaml:"ports"`
|
||||
ActivityTimeout string `yaml:"activity_timeout"`
|
||||
UsePublicIP bool `yaml:"use_public_ip"`
|
||||
InitialSize string `yaml:"initial_size"`
|
||||
Size string `yaml:"size"`
|
||||
Region string `yaml:"region"`
|
||||
UsePersistentImage bool `yaml:"use_persistent_image"`
|
||||
ImageSlug string `yaml:"image_slug"`
|
||||
UserData string `yaml:"user_data"`
|
||||
SshKeys []string `yaml:"ssh_keys"`
|
||||
Volumes []string `yaml:"volumes"`
|
||||
}
|
||||
|
||||
func loadSettings(path string) (Settings, error) {
|
||||
@@ -37,7 +38,7 @@ func loadSettings(path string) (Settings, error) {
|
||||
return s, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &s)
|
||||
err = yaml.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user