Browse Source

Add docker build

master
Matthew Faltys 5 years ago
parent
commit
e23963d550
  1. 30
      Makefile
  2. 4
      config.gcfg
  3. 24
      deps/Dockerfile
  4. 4
      deps/redis.conf
  5. 4
      deps/run.sh
  6. 15
      deps/server_config.gcfg
  7. 54
      nethack-launcher.go

30
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 \
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 \
# --name=nethack \
# --restart always \
# $(IMAGE_NAME)
clean:
rm -rf bin/

4
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

24
deps/Dockerfile vendored

@ -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"]

4
deps/redis.conf vendored

@ -0,0 +1,4 @@
daemonize yes
dbfilename dump.rdb
dir /redisbackup/
save 30 1

4
deps/run.sh vendored

@ -0,0 +1,4 @@
#!/bin/bash
# start redis server
redis-server /redis.conf | sed "s/^/[redis] /"

15
deps/server_config.gcfg vendored

@ -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 = ""

54
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/<config>
// 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,

Loading…
Cancel
Save