From e23963d550334eaa496bc983e4849bb13f749f89 Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Tue, 12 Nov 2019 16:41:46 +0000 Subject: [PATCH] Add docker build --- Makefile | 34 ++++++++++++++++---------- config.gcfg | 4 ++- deps/Dockerfile | 24 ++++++++++++++++++ deps/redis.conf | 4 +++ deps/run.sh | 4 +++ deps/server_config.gcfg | 15 ++++++++++++ nethack-launcher.go | 54 ++++++++++++++++++++++++++++++++++++++--- 7 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 deps/Dockerfile create mode 100644 deps/redis.conf create mode 100755 deps/run.sh create mode 100644 deps/server_config.gcfg diff --git a/Makefile b/Makefile index 070d4d4..446b07a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ GOC=go build GOFLAGS=-a -ldflags '-s' CGOR=CGO_ENABLED=0 +OS_PERMS=sudo NETHACK_DIR=$(shell pwd)/savedata IMAGE_NAME=nethack_launch @@ -22,19 +23,26 @@ reclist: dependencies: go get github.com/gorilla/mux -#build_docker: -# mkdir -p stage.tmp/ -# cp -R deps/Dockerfile stage.tmp/ -# -#run: savedata -# $(OS_PERMS) docker run \ -# -d \ -# -p 23:23 \ -# -v $(NETHACK_DIR)/var:/opt/nethack/nethack.alt.org/nh343/var:rw \ -# -v $(NETHACK_DIR)/dgldir:/opt/nethack/nethack.alt.org/dgldir:rw \ -# --name=nethack \ -# --restart always \ -# $(IMAGE_NAME) +build_docker: + mkdir -p stage.tmp/ + cp deps/Dockerfile stage.tmp/ + cp deps/server_config.gcfg stage.tmp/config.gcfg + cp bin/reclist stage.tmp/ + cp bin/nethack-launcher stage.tmp/ + cp deps/redis.conf stage.tmp/ + cp deps/run.sh stage.tmp/ + cd stage.tmp/ && \ + $(OS_PERMS) docker build -t $(IMAGE_NAME) . + +run_docker: + $(OS_PERMS) docker run \ + -d \ + --name=nethack \ + --restart always \ + $(IMAGE_NAME) + + #-v $(NETHACK_DIR)/var:/opt/nethack/nethack.alt.org/nh343/var:rw \ + #-v $(NETHACK_DIR)/dgldir:/opt/nethack/nethack.alt.org/dgldir:rw \ clean: rm -rf bin/ diff --git a/config.gcfg b/config.gcfg index 19ebb47..b554e4c 100644 --- a/config.gcfg +++ b/config.gcfg @@ -4,7 +4,9 @@ nethackversion = "3.6.0" inprogressdir = "savedata/dgldir/inprogress-nh343" userdir = "savedata/dgldir/userdata" - recordlocation = "record" + hackdir = "savedata/hack" + nhdatlocation = "/usr/lib/games/nethack/nhdat" + recordfilelocation = "record" reclistlocation = "reclist" bootstrapdelay = 1 diff --git a/deps/Dockerfile b/deps/Dockerfile new file mode 100644 index 0000000..f0fa378 --- /dev/null +++ b/deps/Dockerfile @@ -0,0 +1,24 @@ +FROM debian + +# install needed packages +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y \ + ttyrec \ + nethack-console \ + redis-server \ + vim + +# make required dirs +RUN mkdir -p /redisbackup/ + +# update nethack bin location +RUN mv /usr/games/nethack /bin/nethack + +# copy in files +COPY config.gcfg / +COPY nethack-launcher / +COPY reclist /bin/ +COPY redis.conf / +COPY run.sh / + +CMD ["/run.sh"] diff --git a/deps/redis.conf b/deps/redis.conf new file mode 100644 index 0000000..6554e91 --- /dev/null +++ b/deps/redis.conf @@ -0,0 +1,4 @@ +daemonize yes +dbfilename dump.rdb +dir /redisbackup/ +save 30 1 diff --git a/deps/run.sh b/deps/run.sh new file mode 100755 index 0000000..1166a6d --- /dev/null +++ b/deps/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# start redis server +redis-server /redis.conf | sed "s/^/[redis] /" diff --git a/deps/server_config.gcfg b/deps/server_config.gcfg new file mode 100644 index 0000000..45e50f7 --- /dev/null +++ b/deps/server_config.gcfg @@ -0,0 +1,15 @@ +[nethacklauncher] + loglevel = "debug" + serverdisplay = "unixvoid.com underground nethack server" + nethackversion = "3.6.0" + inprogressdir = "savedata/dgldir/inprogress-nh343" + userdir = "savedata/dgldir/userdata" + hackdir = "/hack" + nhdatlocation = "/usr/lib/games/nethack/nhdat" + recordfilelocation = "/hack/record" + reclistlocation = "/bin/reclist" + bootstrapdelay = 1 + +[redis] + host = "localhost:6379" + password = "" diff --git a/nethack-launcher.go b/nethack-launcher.go index bee8037..2c5bfeb 100644 --- a/nethack-launcher.go +++ b/nethack-launcher.go @@ -23,8 +23,10 @@ type Config struct { Loglevel string ServerDisplay string NethackVersion string - InProgressDir string - UserDir string + InProgressDir string // TODO: depricate this + UserDir string // TODO: depricate this + HackDir string + NhdatLocation string RecordFileLocation string ReclistLocation string BootstrapDelay time.Duration @@ -59,6 +61,10 @@ func main() { glogger.Debug.Println("connection to redis succeeded.") } + // create initial files needed by nethack + createInitialFiles() + + // start homescreen screenFunction := printWelcomeScreen(redisClient) fmt.Printf("screen %s recieved\n", screenFunction) } @@ -153,6 +159,7 @@ func printWelcomeScreen(redisClient *redis.Client) string { clearScreen() printProgressScreen(redisClient) case "q": + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() clearScreen() os.Exit(0) default: @@ -192,10 +199,13 @@ func printUserScreen(redisClient *redis.Client, username string) string { clearScreen() printProgressScreen(redisClient) case "p": + // TODO save to inprogress // restart display + nhCommand := fmt.Sprintf("nethack -d %s -u %s\n", config.NethackLauncher.HackDir, username) + ttyrecPath := fmt.Sprintf("%s/user/%s/ttyrec/yeet.ttyrec", config.NethackLauncher.HackDir, username) exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() clearScreen() - nh := exec.Command("ttyrec", "yeto.ttyrec", "-e", "nethack", "-u", username) + nh := exec.Command("ttyrec", ttyrecPath, "-e", nhCommand) nh.Stdout = os.Stdout nh.Stdin = os.Stdin nh.Stderr = os.Stderr @@ -317,9 +327,23 @@ func printRegisterScreen(redisClient *redis.Client) { fmt.Println("Lets try that again.") } } + + // reset display + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() + // set user in redis secHash := sha3.Sum512([]byte(sec)) redisClient.Set(fmt.Sprintf("user:%s", username), fmt.Sprintf("%x", secHash), 0).Err() + + // TODO create config file + // HackDir/user/username/ + // HackDir/user/username/ttyrec/ + + // create user directories + userPath := fmt.Sprintf("%s/user/%s/ttyrec/", config.NethackLauncher.HackDir, username) + exec.Command("mkdir", "-p", userPath).Run() + + // back to main screen printUserScreen(redisClient, username) } } @@ -430,6 +454,30 @@ func printProgressScreen(redisClient *redis.Client) { } } +func createInitialFiles() { + // create necessary directories if they dont exist + if _, err := os.Stat(config.NethackLauncher.HackDir); os.IsNotExist(err) { + os.Mkdir(config.NethackLauncher.HackDir, os.ModeDir) + } + if _, err := os.Stat(fmt.Sprintf("%s/dumps/", config.NethackLauncher.HackDir)); os.IsNotExist(err) { + os.Mkdir(fmt.Sprintf("%s/dumps/", config.NethackLauncher.HackDir), os.ModeDir) + } + if _, err := os.Stat(fmt.Sprintf("%s/save/", config.NethackLauncher.HackDir)); os.IsNotExist(err) { + os.Mkdir(fmt.Sprintf("%s/save/", config.NethackLauncher.HackDir), os.ModeDir) + } + + // create necessary files if they dont exist + os.OpenFile(fmt.Sprintf("%s/logfile", config.NethackLauncher.HackDir), os.O_RDONLY|os.O_CREATE, 0666) + os.OpenFile(fmt.Sprintf("%s/perm", config.NethackLauncher.HackDir), os.O_RDONLY|os.O_CREATE, 0666) + os.OpenFile(fmt.Sprintf("%s/record", config.NethackLauncher.HackDir), os.O_RDONLY|os.O_CREATE, 0666) + os.OpenFile(fmt.Sprintf("%s/xlogfile", config.NethackLauncher.HackDir), os.O_RDONLY|os.O_CREATE, 0666) + + // move in nhdat file if it does not exist + if _, err := os.Stat(fmt.Sprintf("%s/nhdat", config.NethackLauncher.HackDir)); os.IsNotExist(err) { + exec.Command("cp", config.NethackLauncher.NhdatLocation, config.NethackLauncher.HackDir).Run() + } +} + func gethighscore(w http.ResponseWriter, r *http.Request) { // run script output, err := exec.Command(config.NethackLauncher.ReclistLocation,