Merge branch 'master' of github.com:gtalent/dospin
This commit is contained in:
commit
6f3f8d5474
@ -20,12 +20,14 @@ const (
|
|||||||
type cmdOptions struct {
|
type cmdOptions struct {
|
||||||
config string
|
config string
|
||||||
cmd string
|
cmd string
|
||||||
|
varStateDir 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", "/etc/dospin.json", "Path to the dospin config file")
|
||||||
|
flag.StringVar(&o.varStateDir, "varstate", "/var/lib/dospin", "Path to the var state directory")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
{
|
{
|
||||||
"ApiToken": "<your token here>",
|
"ApiToken": "<your token here>",
|
||||||
|
"GatewayInterface": "eth0:2",
|
||||||
"Servers": {
|
"Servers": {
|
||||||
"minecraft": {
|
"minecraft": {
|
||||||
"Ports": [25565],
|
"Ports": [25565],
|
||||||
"UsePublicIP": false,
|
"UsePublicIP": false,
|
||||||
"InitialSize": "512mb",
|
"InitialSize": "4gb",
|
||||||
"Size": "4gb",
|
"Size": "4gb",
|
||||||
"Region": "nyc3"
|
"Region": "nyc1",
|
||||||
|
"SshKeys": [513314, 1667247],
|
||||||
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,22 @@ 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
|
||||||
@ -58,10 +74,21 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// create the droplet
|
// create the droplet
|
||||||
image, err := me.getSnapshot(name)
|
var image godo.DropletCreateImage
|
||||||
|
|
||||||
|
if vd.ImageSlug == "" {
|
||||||
|
snapshot, err := me.getSnapshot(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
image = godo.DropletCreateImage{
|
||||||
|
ID: snapshot.ID,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
image = godo.DropletCreateImage{
|
||||||
|
Slug: vd.ImageSlug,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// determine droplet size
|
// determine droplet size
|
||||||
var size string
|
var size string
|
||||||
@ -76,9 +103,10 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
Region: vd.Region,
|
Region: vd.Region,
|
||||||
Size: size,
|
Size: size,
|
||||||
PrivateNetworking: true,
|
PrivateNetworking: true,
|
||||||
Image: godo.DropletCreateImage{
|
SSHKeys: sshKeys(vd.SshKeys),
|
||||||
ID: image.ID,
|
Volumes: volumes(vd.Volumes),
|
||||||
},
|
UserData: vd.UserData,
|
||||||
|
Image: image,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Spinup: Creating " + name)
|
log.Println("Spinup: Creating " + name)
|
||||||
@ -126,18 +154,28 @@ func (me *DropletHandler) Spinup(name string) (string, error) {
|
|||||||
|
|
||||||
// delete the image
|
// delete the image
|
||||||
log.Println("Spinup: Deleting image " + name)
|
log.Println("Spinup: Deleting image " + name)
|
||||||
|
if image.ID > -1 {
|
||||||
_, 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)
|
||||||
}
|
} else {
|
||||||
log.Println("Spinup: Deleted image " + name)
|
log.Println("Spinup: Deleted image " + name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// get the private IP and return it
|
// get the private IP and return it
|
||||||
|
|
||||||
|
// get new copy of droplet that has IP
|
||||||
|
droplet, _, err = me.client.Droplets.Get(droplet.ID)
|
||||||
|
if err == nil {
|
||||||
if vd.UsePublicIP {
|
if vd.UsePublicIP {
|
||||||
return droplet.PublicIPv4()
|
return droplet.PublicIPv4()
|
||||||
} else {
|
} else {
|
||||||
return droplet.PrivateIPv4()
|
return droplet.PrivateIPv4()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
list_keys.sh
Executable file
4
list_keys.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#! /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 .
|
4
list_volumes.sh
Executable file
4
list_volumes.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#! /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 .
|
30
net_linux.go
30
net_linux.go
@ -8,29 +8,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// just have this stub to allow building on Linux
|
func addPortForward(ruleName, gatewayInt, localIp, targetIp, port string) {
|
||||||
func addPortForward(ruleName, ip, port string) {
|
log.Println("Setting up port", port, "to", targetIp)
|
||||||
log.Print("Port forwarding not currently implemented for Linux/iptables")
|
cmdOut, err := exec.Command("iptables", "-A", "PREROUTING", "-t", "nat", "-i", "\""+gatewayInt+"\"", "-p", "tcp", "--dport", port, "-j", "DNAT", "--to", targetIp+":"+port).Output()
|
||||||
|
log.Println("iptables", "-A", "PREROUTING,", "-t", "nat", "-i", "\""+gatewayInt+"\"", "-p", "tcp", "--dport", port, "-j", "DNAT", "--to", targetIp+":"+port)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("iptables error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdOut, err = exec.Command("iptables", "-A", "FORWARD", "-p", "tcp", "-d", targetIp, "--dport", port, "-j", "ACCEPT").Output()
|
||||||
|
log.Println("iptables", "-A", "FORWARD", "-p", "tcp", "-d", targetIp, "--dport", port, "-j", "ACCEPT")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("iptables error:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func rmPortForward(ruleName string) {
|
func rmPortForward(ruleName string) {
|
||||||
log.Print("Port forwarding not currently implemented for Linux/iptables")
|
log.Print("Port forwarding not currently implemented for Linux/iptables")
|
||||||
}
|
}
|
||||||
|
|
||||||
func portUsageCount(ports ...int) int {
|
|
||||||
cmd := "sockstat -4c"
|
|
||||||
for _, v := range ports {
|
|
||||||
cmd += " -p " + strconv.Itoa(v)
|
|
||||||
}
|
|
||||||
out, err := exec.Command(cmd).Output()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Port Usage Check: Could not run ", cmd)
|
|
||||||
}
|
|
||||||
return bytes.Count(out, []byte{'\n'}) - 1
|
|
||||||
}
|
|
||||||
|
@ -19,6 +19,11 @@ const (
|
|||||||
SERVERMANAGER_STOP
|
SERVERMANAGER_STOP
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type serverManagerEvent struct {
|
||||||
|
eventType int
|
||||||
|
tcpConn *net.TCPConn
|
||||||
|
}
|
||||||
|
|
||||||
type ServerHandler interface {
|
type ServerHandler interface {
|
||||||
// Takes snapshot name, and returns the IP to connect to.
|
// Takes snapshot name, and returns the IP to connect to.
|
||||||
Spinup(name string) (string, error)
|
Spinup(name string) (string, error)
|
||||||
@ -28,7 +33,8 @@ type ServerHandler interface {
|
|||||||
type ServerManager struct {
|
type ServerManager struct {
|
||||||
name string
|
name string
|
||||||
ports []int
|
ports []int
|
||||||
in chan int
|
gatewayInt string
|
||||||
|
in chan serverManagerEvent
|
||||||
done chan interface{}
|
done chan interface{}
|
||||||
usageScore int // spin down server when this reaches 0
|
usageScore int // spin down server when this reaches 0
|
||||||
server ServerHandler
|
server ServerHandler
|
||||||
@ -39,7 +45,8 @@ 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 int)
|
sm.gatewayInt = settings.GatewayInterface
|
||||||
|
sm.in = make(chan serverManagerEvent)
|
||||||
sm.done = make(chan interface{})
|
sm.done = make(chan interface{})
|
||||||
sm.usageScore = 5
|
sm.usageScore = 5
|
||||||
sm.server = server
|
sm.server = server
|
||||||
@ -68,32 +75,38 @@ func (me *ServerManager) Serve() {
|
|||||||
/*
|
/*
|
||||||
Sends the serve loop a spinup message.
|
Sends the serve loop a spinup message.
|
||||||
*/
|
*/
|
||||||
func (me *ServerManager) Spinup() {
|
func (me *ServerManager) Spinup(c *net.TCPConn) {
|
||||||
me.in <- SERVERMANAGER_SPINUP
|
me.in <- serverManagerEvent{eventType: SERVERMANAGER_SPINUP, tcpConn: c}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Sends the serve loop a spindown message.
|
Sends the serve loop a spindown message.
|
||||||
*/
|
*/
|
||||||
func (me *ServerManager) Spindown() {
|
func (me *ServerManager) Spindown(c *net.TCPConn) {
|
||||||
me.in <- SERVERMANAGER_SPINDOWN
|
me.in <- serverManagerEvent{eventType: SERVERMANAGER_SPINDOWN}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Sends the serve loop a quit message.
|
Sends the serve loop a quit message.
|
||||||
*/
|
*/
|
||||||
func (me *ServerManager) Stop() {
|
func (me *ServerManager) Stop() {
|
||||||
me.in <- SERVERMANAGER_STOP
|
me.in <- serverManagerEvent{eventType: SERVERMANAGER_STOP}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *ServerManager) Done() {
|
func (me *ServerManager) Done() {
|
||||||
<-me.done
|
<-me.done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *ServerManager) addPortForwards(ip string) {
|
func (me *ServerManager) addPortForwards(localIp, remoteIp string) {
|
||||||
|
log.Println("Ports:", me.ports)
|
||||||
|
for _, p := range me.ports {
|
||||||
|
port := strconv.Itoa(p)
|
||||||
|
addPortForward(me.name, me.gatewayInt, localIp, remoteIp, port)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *ServerManager) rmPortForwards() {
|
func (me *ServerManager) rmPortForwards() {
|
||||||
|
rmPortForward(me.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *ServerManager) setupListener(port int) {
|
func (me *ServerManager) setupListener(port int) {
|
||||||
@ -118,7 +131,7 @@ func (me *ServerManager) setupListener(port int) {
|
|||||||
// connection accepted
|
// connection accepted
|
||||||
|
|
||||||
// spinup machine
|
// spinup machine
|
||||||
me.Spinup()
|
me.Spinup(conn)
|
||||||
|
|
||||||
// close existing connection, not doing anything with it
|
// close existing connection, not doing anything with it
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -128,14 +141,15 @@ func (me *ServerManager) setupListener(port int) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *ServerManager) serveAction(action int) bool {
|
func (me *ServerManager) serveAction(event serverManagerEvent) bool {
|
||||||
running := true
|
running := true
|
||||||
switch action {
|
switch event.eventType {
|
||||||
case SERVERMANAGER_SPINUP:
|
case SERVERMANAGER_SPINUP:
|
||||||
ip, err := me.server.Spinup(me.name)
|
targetIp, err := me.server.Spinup(me.name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("ServerManager: Got IP for", me.name+":", ip)
|
log.Println("ServerManager: Got IP for", me.name+":", targetIp)
|
||||||
me.addPortForwards(ip)
|
localIp := event.tcpConn.LocalAddr().String()
|
||||||
|
me.addPortForwards(localIp, targetIp)
|
||||||
} else {
|
} else {
|
||||||
log.Println("ServerManager: Could not spin up "+me.name+":", err)
|
log.Println("ServerManager: Could not spin up "+me.name+":", err)
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
ApiToken string
|
ApiToken string
|
||||||
|
GatewayInterface string
|
||||||
Servers map[string]Server
|
Servers map[string]Server
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,6 +24,10 @@ type Server struct {
|
|||||||
InitialSize string
|
InitialSize string
|
||||||
Size string
|
Size string
|
||||||
Region string
|
Region string
|
||||||
|
ImageSlug string
|
||||||
|
UserData string
|
||||||
|
SshKeys []int
|
||||||
|
Volumes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSettings(path string) (Settings, error) {
|
func loadSettings(path string) (Settings, error) {
|
||||||
|
19
varstate.go
Normal file
19
varstate.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 gtalent2@gmail.com
|
||||||
|
|
||||||
|
This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
// Firewall forwarding rule managed by dospin.
|
||||||
|
type ForwardingRule struct {
|
||||||
|
destHost string // Destination IP
|
||||||
|
destPort int
|
||||||
|
srcPort int
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadForwardingRules() []ForwardingRule {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user