From e292608cd06e93c78bbebd36744971434745afe0 Mon Sep 17 00:00:00 2001 From: Dave Pedu Date: Sat, 13 Feb 2016 00:07:28 -0800 Subject: [PATCH] Initial commit --- Dockerfile | 23 ++++++++++++++ README.md | 26 ++++++++++++++++ cli.py | 67 ++++++++++++++++++++++++++++++++++++++++ config.json | 6 ++++ start | 10 ++++++ supervisor-terraria.conf | 5 +++ supervisor.conf | 2 ++ 7 files changed, 139 insertions(+) create mode 100755 Dockerfile create mode 100644 README.md create mode 100755 cli.py create mode 100644 config.json create mode 100755 start create mode 100644 supervisor-terraria.conf create mode 100644 supervisor.conf diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..3859148 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM ubuntu:trusty + +#RUN sed -i -E 's/deb http:\/\/archive.ubuntu.com/deb http:\/\/debmirror.services.davepedu.com:8080/' /etc/apt/sources.list + +RUN apt-get update && \ + apt-get install -y screen mono-complete wget unzip supervisor python3-requests && \ + wget -O /tmp/terraria.zip https://github.com/NyxStudios/TShock/releases/download/v4.3.12/tshock_4.3.12.zip && \ + cd /tmp && \ + unzip terraria.zip && \ + rm terraria.zip && \ + mkdir -p /opt/terraria/tshock && \ + mv * /opt/terraria + +ADD supervisor.conf /etc/supervisor/conf.d/supervisor.conf +ADD supervisor-terraria.conf /etc/supervisor/conf.d/terraria.conf +ADD config.json /opt/terraria/tshock/config.json +ADD cli.py /usr/local/bin/trcli +ADD start /start + +EXPOSE 7777 +EXPOSE 7878 + +ENTRYPOINT ["/start"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fb4bee --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +docker-terraria +=============== + +Terraria (tshock) under Mono in a docker container. + +Reccomended usage: + +`docker run -d -p 7777:7777 -p 7878:7878 -v /host/terraria_data/:/opt/terraria/tshock/ terraria` + +Admin cli +--------- + +Included is an admin tool for interacting with a server. + +First-time setup: + +* `trcli -a mkuser` + +Per-run token generation: + +* `trcli -a gettoken` + +Execute arbitrary commads: + +* `trcli -a cmd -c '/protectspawn' + diff --git a/cli.py b/cli.py new file mode 100755 index 0000000..2937047 --- /dev/null +++ b/cli.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +from requests import get +import sqlite3 +from os.path import exists +import json + +def create_user(): + db = sqlite3.connect("/opt/terraria/tshock/tshock.sqlite") + cursor = db.cursor() + cursor.execute('SELECT COUNT(*) FROM Users WHERE Username="superadmin";') + if cursor.fetchall()[0][0] == 0: + cursor.execute('INSERT INTO Users (Username, Password, Usergroup) VALUES ("superadmin", "$2a$07$S7i/jFGLQQzAItq0RlQJd.D4/npGbFwUGCZwe1QfqHRCDrYk0GxQK","superadmin");') + cursor.close() + db.commit() + db.close() + + +def get_token(): + if exists("/tmp/clitoken"): + with open("/tmp/clitoken", "r") as f: + return f.read().strip() + else: + token = get("http://127.0.0.1:7878/token/create/superadmin/ballsack").json()["token"] + with open("/tmp/clitoken", "w") as f: + f.write(token) + return token + + +def destroy_token(token): + get("http://127.0.0.1:7878/token/destroy/"+token, params={"token":token}) + + +def run_command(command, token): + return get("http://127.0.0.1:7878/v2/server/rawcmd", params={"token":token, "cmd":command}).json() + + +def status(): + return get("http://127.0.0.1:7878/v2/server/status").json() + + +def main(): + import argparse + + parser = argparse.ArgumentParser("Terraria REST CLI") + parser.add_argument("-a", "--action", help="action to take", choices=["mkuser", "cmd", "gettoken", "status"], required=True) + parser.add_argument("-c", "--command", help="command to run") + parser.add_argument("-t", "--token", help="api token, if needed") + + args = parser.parse_args() + + if args.action=="cmd": + if not args.token: + args.token = get_token() + print(json.dumps(run_command(args.command, args.token), indent=4)) + elif args.action=="status": + print(json.dumps(status(), indent=4)) + elif args.action=="gettoken": + print(get_token()) + elif args.action=="deltoken": + destroy_token(args.token) + elif args.action=="mkuser": + create_user() + else: + parser.print_help() + +if __name__ == '__main__': + main() diff --git a/config.json b/config.json new file mode 100644 index 0000000..05630b2 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "RestApiEnabled": true, + "RestApiPort": 7878, + "RestUseNewPermissionModel": true, + "ApplicationRestTokens": {} +} \ No newline at end of file diff --git a/start b/start new file mode 100755 index 0000000..1ba56ed --- /dev/null +++ b/start @@ -0,0 +1,10 @@ +#!/bin/bash + +trap 'kill $(jobs -p)' EXIT + +: ${WORLDSIZE:=3} +: ${WORLDFILENAME:=terraria.wld} + +export WORLDSIZE WORLDFILENAME + +supervisord diff --git a/supervisor-terraria.conf b/supervisor-terraria.conf new file mode 100644 index 0000000..90e55ca --- /dev/null +++ b/supervisor-terraria.conf @@ -0,0 +1,5 @@ +[program:terraria] +command=mono TerrariaServer.exe -port 7777 -maxplayers 100 -world "/opt/terraria/tshock/%(ENV_WORLDFILENAME)s" -autocreate "%(ENV_WORLDSIZE)s" +directory=/opt/terraria +autorestart=true + diff --git a/supervisor.conf b/supervisor.conf new file mode 100644 index 0000000..54296b8 --- /dev/null +++ b/supervisor.conf @@ -0,0 +1,2 @@ +[supervisord] +nodaemon=true