2018-06-08 10:22:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-07-06 16:21:50 +00:00
|
|
|
"os/exec"
|
2018-06-08 10:22:28 +00:00
|
|
|
"os/signal"
|
2018-07-06 16:21:50 +00:00
|
|
|
"strings"
|
2018-06-08 10:22:28 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
|
|
|
FiFoFile string `flag:"fifo" default:"/home/gameserver/terraria_cmd" description:"Path to create the fifo at"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
log.Fatalf("Unable to parse commandline options: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("terraria-docker %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse log level")
|
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := syscall.Mkfifo(cfg.FiFoFile, 0644); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to create fifo")
|
|
|
|
}
|
|
|
|
defer os.Remove(cfg.FiFoFile)
|
|
|
|
|
2018-07-06 16:21:50 +00:00
|
|
|
cmd := exec.Command(rconfig.Args()[1], rconfig.Args()[1:]...)
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to create stdin pipe")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdIN processing to send commands to Terraria
|
2018-06-08 10:22:28 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2018-07-06 16:21:50 +00:00
|
|
|
go handleSignal(sigs, stdin)
|
2018-06-08 10:22:28 +00:00
|
|
|
|
2018-07-06 16:21:50 +00:00
|
|
|
go inputLoop(stdin)
|
2018-06-08 10:22:28 +00:00
|
|
|
|
2018-07-06 16:21:50 +00:00
|
|
|
// StdOUT processing to react on log output
|
|
|
|
stdoutRead, stdoutWrite := io.Pipe()
|
|
|
|
cmd.Stdout = stdoutWrite
|
|
|
|
cmd.Stderr = stdoutWrite
|
|
|
|
|
|
|
|
go outputLoop(stdoutRead, stdin)
|
|
|
|
|
|
|
|
cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func outputLoop(in io.Reader, out io.Writer) {
|
|
|
|
scanner := bufio.NewScanner(in)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
log.Info(line)
|
|
|
|
|
|
|
|
if strings.Contains(line, "has left.") {
|
|
|
|
// A player left the server and after the last player
|
|
|
|
// left the server will not save anymore so we'll do
|
|
|
|
// that whenever a player leaves...
|
|
|
|
fmt.Fprintln(out, "save")
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|
2018-07-06 16:32:40 +00:00
|
|
|
|
|
|
|
if strings.Contains(line, "Backing up world file") {
|
|
|
|
// Announce a successful server save through console
|
|
|
|
fmt.Fprintln(out, "say World saved...")
|
|
|
|
}
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:21:50 +00:00
|
|
|
func inputLoop(comm io.Writer) {
|
2018-06-08 10:22:28 +00:00
|
|
|
for {
|
|
|
|
f, err := os.Open(cfg.FiFoFile)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to (re)open fifo, quitting now")
|
2018-07-06 16:21:50 +00:00
|
|
|
fmt.Fprintln(comm, "exit")
|
|
|
|
break
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
2018-07-06 16:21:50 +00:00
|
|
|
fmt.Fprintln(comm, scanner.Text())
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch scanner.Err() {
|
|
|
|
case nil:
|
|
|
|
// This is fine
|
|
|
|
case io.EOF:
|
|
|
|
// This is fine
|
|
|
|
default:
|
|
|
|
log.WithError(err).Error("Unable to read from fifo")
|
2018-07-06 16:21:50 +00:00
|
|
|
fmt.Fprintln(comm, "exit")
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:21:50 +00:00
|
|
|
func handleSignal(sigs chan os.Signal, comm io.Writer) {
|
2018-06-08 10:22:28 +00:00
|
|
|
sig := <-sigs
|
|
|
|
log.WithField("signal", sig).Info("Received terminating singal")
|
2018-07-06 16:21:50 +00:00
|
|
|
fmt.Fprintln(comm, "exit")
|
2018-06-08 10:22:28 +00:00
|
|
|
}
|