Compare commits
9 Commits
d57884e2d7
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
432b47d224 | |||
6c1321e485 | |||
32408e4471 | |||
8879463cb9 | |||
2b06810bd7 | |||
5dd596d755 | |||
0a2bc38196 | |||
42e65ebf52 | |||
10a959e9f3 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
dospin.json
|
dospin.json
|
||||||
test_config.json
|
test_config.yaml
|
||||||
dospin
|
dospin
|
||||||
main
|
main
|
||||||
|
19
dospin.go
19
dospin.go
@@ -10,6 +10,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -18,14 +19,16 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type cmdOptions struct {
|
type cmdOptions struct {
|
||||||
config string
|
config string
|
||||||
cmd string
|
logFile string
|
||||||
|
cmd string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseCmdOptions() cmdOptions {
|
func parseCmdOptions() cmdOptions {
|
||||||
var o cmdOptions
|
var o cmdOptions
|
||||||
flag.StringVar(&o.cmd, "cmd", CMD_SERVE, "Mode to run command in ("+CMD_SERVE+","+CMD_SPINDOWNALL+")")
|
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()
|
flag.Parse()
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
@@ -45,6 +48,16 @@ func spindownAll(opts cmdOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runServer(opts cmdOptions) {
|
func runServer(opts cmdOptions) {
|
||||||
|
if opts.logFile != "stdout" {
|
||||||
|
logFile, err := os.OpenFile(opts.logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
||||||
|
if err == nil {
|
||||||
|
defer logFile.Close()
|
||||||
|
log.SetOutput(logFile)
|
||||||
|
} else {
|
||||||
|
log.Print("Could not open log file: ", err)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
log.Println("Loading config:", opts.config)
|
log.Println("Loading config:", opts.config)
|
||||||
settings, err := loadSettings(opts.config)
|
settings, err := loadSettings(opts.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
17
dospin.json
17
dospin.json
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"ApiToken": "<your token here>",
|
|
||||||
"Servers": {
|
|
||||||
"minecraft": {
|
|
||||||
"Ports": [25565],
|
|
||||||
"UsePublicIP": false,
|
|
||||||
"InitialSize": "4gb",
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
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
|
@@ -28,22 +28,6 @@ func (t *tokenSource) Token() (*oauth2.Token, error) {
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sshKeys(ids []int) []godo.DropletCreateSSHKey {
|
|
||||||
var out []godo.DropletCreateSSHKey
|
|
||||||
for _, id := range ids {
|
|
||||||
out = append(out, godo.DropletCreateSSHKey{ID: id})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func volumes(names []string) []godo.DropletCreateVolume {
|
|
||||||
var out []godo.DropletCreateVolume
|
|
||||||
for _, name := range names {
|
|
||||||
out = append(out, godo.DropletCreateVolume{Name: name})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
type DropletHandler struct {
|
type DropletHandler struct {
|
||||||
client *godo.Client
|
client *godo.Client
|
||||||
settings Settings
|
settings Settings
|
||||||
@@ -103,8 +87,8 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
Region: vd.Region,
|
Region: vd.Region,
|
||||||
Size: size,
|
Size: size,
|
||||||
PrivateNetworking: true,
|
PrivateNetworking: true,
|
||||||
SSHKeys: sshKeys(vd.SshKeys),
|
SSHKeys: me.sshKeys(vd.SshKeys),
|
||||||
Volumes: volumes(vd.Volumes),
|
Volumes: me.volumes(vd.Volumes),
|
||||||
UserData: vd.UserData,
|
UserData: vd.UserData,
|
||||||
Image: image,
|
Image: image,
|
||||||
}
|
}
|
||||||
@@ -321,3 +305,46 @@ func (me *DropletHandler) actionWait(actionId int) bool {
|
|||||||
time.Sleep(1000 * time.Millisecond)
|
time.Sleep(1000 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (me *DropletHandler) sshKeys(keyNames []string) []godo.DropletCreateSSHKey {
|
||||||
|
// build key map
|
||||||
|
page := 0
|
||||||
|
perPage := 200
|
||||||
|
keyMap := make(map[string]string)
|
||||||
|
for {
|
||||||
|
page++
|
||||||
|
opt := &godo.ListOptions{
|
||||||
|
Page: page,
|
||||||
|
PerPage: perPage,
|
||||||
|
}
|
||||||
|
keys, _, err := me.client.Keys.List(opt)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range keys {
|
||||||
|
keyMap[v.Name] = v.Fingerprint
|
||||||
|
}
|
||||||
|
|
||||||
|
// check next page?
|
||||||
|
if len(keys) < perPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// build output key list
|
||||||
|
var out []godo.DropletCreateSSHKey
|
||||||
|
for _, kn := range keyNames {
|
||||||
|
fp := keyMap[kn]
|
||||||
|
out = append(out, godo.DropletCreateSSHKey{Fingerprint: fp})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *DropletHandler) volumes(names []string) []godo.DropletCreateVolume {
|
||||||
|
var out []godo.DropletCreateVolume
|
||||||
|
for _, name := range names {
|
||||||
|
out = append(out, godo.DropletCreateVolume{Name: name})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
@@ -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 .
|
|
@@ -35,10 +35,11 @@ type ServerManager struct {
|
|||||||
name string
|
name string
|
||||||
ports []int
|
ports []int
|
||||||
in chan serverManagerEvent
|
in chan serverManagerEvent
|
||||||
done chan interface{}
|
done chan int
|
||||||
connStatus chan ConnStatus
|
connStatus chan ConnStatus
|
||||||
lastKeepAliveTime time.Time
|
lastKeepAliveTime time.Time
|
||||||
server ServerHandler
|
server ServerHandler
|
||||||
|
activityTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerManager(name string, server ServerHandler, settings Settings) *ServerManager {
|
func NewServerManager(name string, server ServerHandler, settings Settings) *ServerManager {
|
||||||
@@ -47,10 +48,18 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser
|
|||||||
sm.name = name
|
sm.name = name
|
||||||
sm.ports = settings.Servers[name].Ports
|
sm.ports = settings.Servers[name].Ports
|
||||||
sm.in = make(chan serverManagerEvent)
|
sm.in = make(chan serverManagerEvent)
|
||||||
sm.done = make(chan interface{})
|
sm.done = make(chan int)
|
||||||
|
sm.connStatus = make(chan ConnStatus)
|
||||||
sm.server = server
|
sm.server = server
|
||||||
sm.lastKeepAliveTime = time.Now()
|
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
|
return sm
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +69,7 @@ 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
|
||||||
|
|
||||||
activityTimeout := time.Duration(5 * time.Minute)
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
ticker := time.NewTicker(activityTimeout)
|
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
for running := true; running; {
|
for running := true; running; {
|
||||||
@@ -73,8 +81,7 @@ 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) > activityTimeout {
|
if time.Since(me.lastKeepAliveTime) > me.activityTimeout {
|
||||||
log.Println("ServerManager: Activity timeout for", me.name, " - killing server")
|
|
||||||
running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN})
|
running = me.serveAction(serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,7 +90,7 @@ func (me *ServerManager) Serve() {
|
|||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
|
||||||
// notify done
|
// notify done
|
||||||
me.done <- 42
|
me.done <- 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
29
settings.go
29
settings.go
@@ -8,26 +8,27 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
ApiToken string
|
ApiToken string `yaml:"api_token"`
|
||||||
Servers map[string]Server
|
Servers map[string]Server `yaml:"servers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Ports []int
|
Ports []int `yaml:"ports"`
|
||||||
UsePublicIP bool
|
ActivityTimeout string `yaml:"activity_timeout"`
|
||||||
InitialSize string
|
UsePublicIP bool `yaml:"use_public_ip"`
|
||||||
Size string
|
InitialSize string `yaml:"initial_size"`
|
||||||
Region string
|
Size string `yaml:"size"`
|
||||||
UsePersistentImage bool
|
Region string `yaml:"region"`
|
||||||
ImageSlug string
|
UsePersistentImage bool `yaml:"use_persistent_image"`
|
||||||
UserData string
|
ImageSlug string `yaml:"image_slug"`
|
||||||
SshKeys []int
|
UserData string `yaml:"user_data"`
|
||||||
Volumes []string
|
SshKeys []string `yaml:"ssh_keys"`
|
||||||
|
Volumes []string `yaml:"volumes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSettings(path string) (Settings, error) {
|
func loadSettings(path string) (Settings, error) {
|
||||||
@@ -37,7 +38,7 @@ func loadSettings(path string) (Settings, error) {
|
|||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(data, &s)
|
err = yaml.Unmarshal(data, &s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user