From 9874226646ba4a7920b3906c12aa2316f0466aed Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 6 Jul 2017 23:40:39 -0400 Subject: [PATCH] Add Rust container --- Dockerfile | 22 +++++++++++++ README.md | 3 +- entrypoint.sh | 14 +++++++++ wrapper.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 entrypoint.sh create mode 100644 wrapper.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b0273c5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM ubuntu:16.04 + +MAINTAINER Isaac A., + +RUN apt update && \ + apt upgrade -y && \ + apt install -y lib32gcc1 lib32stdc++6 curl && \ + curl -sL https://deb.nodesource.com/setup_6.x | bash - && \ + apt install -y nodejs && \ + useradd -d /home/container -m container + +USER container +ENV USER container +ENV HOME /home/container + +WORKDIR /home/container + +COPY ./entrypoint.sh /entrypoint.sh +COPY ./wrapper.js /wrapper.js +COPY ./node_modules/ /node_modules/ + +CMD ["/bin/bash", "/entrypoint.sh"] diff --git a/README.md b/README.md index 6362937..2f1d5ef 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# Containers -Generic docker containers designed to work with Pterodactyl Panel and its daemon. +# Rust diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..c89f06d --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/bash +sleep 2 + +cd /home/container + +# Update Rust Server +./steam/steamcmd.sh +login anonymous +force_install_dir /home/container +app_update 258550 +quit + +# Replace Startup Variables +MODIFIED_STARTUP=`eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')` +echo ":/home/container$ ${MODIFIED_STARTUP}" + +# Run the Server +node /wrapper.js "${MODIFIED_STARTUP}" diff --git a/wrapper.js b/wrapper.js new file mode 100644 index 0000000..3f61a94 --- /dev/null +++ b/wrapper.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node + +var startupCmd = ""; +const fs = require("fs"); +fs.writeFile("latest.log", ""); + +var args = process.argv.splice(process.execArgv.length + 2); +for (var i = 0; i < args.length; i++) { + if (i === args.length - 1) { + startupCmd += args[i]; + } else { + startupCmd += args[i] + " "; + } +} + +if (startupCmd.length < 1) { + console.log("Error: Please specify a startup command."); + process.exit(); +} + +var exec = require("child_process").exec; +console.log("Starting Rust..."); +exec(startupCmd); + +var waiting = true; + +var poll = function( ) { + function createPacket(command) { + var packet = { + Identifier: -1, + Message: command, + Name: "WebRcon" + }; + return JSON.stringify(packet); + } + + var serverHostname = "localhost"; + var serverPort = process.env.RCON_PORT; + var serverPassword = process.env.RCON_PASS; + var WebSocket = require("ws"); + var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword); + + ws.on("open", function open() { + waiting = false; + process.stdin.resume(); + process.stdin.setEncoding("utf8"); + var util = require("util"); + process.stdin.on('data', function (text) { + ws.send(createPacket(text)); + }); + }); + + ws.on("message", function(data, flags) { + try { + var json = JSON.parse(data); + if (json !== undefined) { + if (json.Message !== undefined && json.Message.length > 0) { + console.log(json.Message); + const fs = require("fs"); + fs.appendFile("latest.log", "\n" + json.Message); + } + } else { + console.log("Error: Invalid JSON received"); + } + } catch (e) { + if (e) { + console.log(e); + } + } + }); + + ws.on("error", function(err) { + waiting = true; + console.log("Waiting for RCON to come up..."); + setTimeout(poll, 5000); + }); + + ws.on("close", function() { + if (!waiting) { + console.log("Connection to server closed."); + process.exit(); + } + }); +} +poll();