Browse Source

Add docker build

master
Matthew Faltys 5 years ago
parent
commit
e23963d550
  1. 34
      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

34
Makefile

@ -1,6 +1,7 @@
GOC=go build GOC=go build
GOFLAGS=-a -ldflags '-s' GOFLAGS=-a -ldflags '-s'
CGOR=CGO_ENABLED=0 CGOR=CGO_ENABLED=0
OS_PERMS=sudo
NETHACK_DIR=$(shell pwd)/savedata NETHACK_DIR=$(shell pwd)/savedata
IMAGE_NAME=nethack_launch IMAGE_NAME=nethack_launch
@ -22,19 +23,26 @@ reclist:
dependencies: dependencies:
go get github.com/gorilla/mux go get github.com/gorilla/mux
#build_docker: build_docker:
# mkdir -p stage.tmp/ mkdir -p stage.tmp/
# cp -R deps/Dockerfile stage.tmp/ cp deps/Dockerfile stage.tmp/
# cp deps/server_config.gcfg stage.tmp/config.gcfg
#run: savedata cp bin/reclist stage.tmp/
# $(OS_PERMS) docker run \ cp bin/nethack-launcher stage.tmp/
# -d \ cp deps/redis.conf stage.tmp/
# -p 23:23 \ cp deps/run.sh stage.tmp/
# -v $(NETHACK_DIR)/var:/opt/nethack/nethack.alt.org/nh343/var:rw \ cd stage.tmp/ && \
# -v $(NETHACK_DIR)/dgldir:/opt/nethack/nethack.alt.org/dgldir:rw \ $(OS_PERMS) docker build -t $(IMAGE_NAME) .
# --name=nethack \
# --restart always \ run_docker:
# $(IMAGE_NAME) $(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: clean:
rm -rf bin/ rm -rf bin/

4
config.gcfg

@ -4,7 +4,9 @@
nethackversion = "3.6.0" nethackversion = "3.6.0"
inprogressdir = "savedata/dgldir/inprogress-nh343" inprogressdir = "savedata/dgldir/inprogress-nh343"
userdir = "savedata/dgldir/userdata" userdir = "savedata/dgldir/userdata"
recordlocation = "record" hackdir = "savedata/hack"
nhdatlocation = "/usr/lib/games/nethack/nhdat"
recordfilelocation = "record"
reclistlocation = "reclist" reclistlocation = "reclist"
bootstrapdelay = 1 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 Loglevel string
ServerDisplay string ServerDisplay string
NethackVersion string NethackVersion string
InProgressDir string InProgressDir string // TODO: depricate this
UserDir string UserDir string // TODO: depricate this
HackDir string
NhdatLocation string
RecordFileLocation string RecordFileLocation string
ReclistLocation string ReclistLocation string
BootstrapDelay time.Duration BootstrapDelay time.Duration
@ -59,6 +61,10 @@ func main() {
glogger.Debug.Println("connection to redis succeeded.") glogger.Debug.Println("connection to redis succeeded.")
} }
// create initial files needed by nethack
createInitialFiles()
// start homescreen
screenFunction := printWelcomeScreen(redisClient) screenFunction := printWelcomeScreen(redisClient)
fmt.Printf("screen %s recieved\n", screenFunction) fmt.Printf("screen %s recieved\n", screenFunction)
} }
@ -153,6 +159,7 @@ func printWelcomeScreen(redisClient *redis.Client) string {
clearScreen() clearScreen()
printProgressScreen(redisClient) printProgressScreen(redisClient)
case "q": case "q":
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen() clearScreen()
os.Exit(0) os.Exit(0)
default: default:
@ -192,10 +199,13 @@ func printUserScreen(redisClient *redis.Client, username string) string {
clearScreen() clearScreen()
printProgressScreen(redisClient) printProgressScreen(redisClient)
case "p": case "p":
// TODO save to inprogress
// restart display // 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() exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
clearScreen() clearScreen()
nh := exec.Command("ttyrec", "yeto.ttyrec", "-e", "nethack", "-u", username) nh := exec.Command("ttyrec", ttyrecPath, "-e", nhCommand)
nh.Stdout = os.Stdout nh.Stdout = os.Stdout
nh.Stdin = os.Stdin nh.Stdin = os.Stdin
nh.Stderr = os.Stderr nh.Stderr = os.Stderr
@ -317,9 +327,23 @@ func printRegisterScreen(redisClient *redis.Client) {
fmt.Println("Lets try that again.") fmt.Println("Lets try that again.")
} }
} }
// reset display
exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run()
// set user in redis // set user in redis
secHash := sha3.Sum512([]byte(sec)) secHash := sha3.Sum512([]byte(sec))
redisClient.Set(fmt.Sprintf("user:%s", username), fmt.Sprintf("%x", secHash), 0).Err() 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) 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) { func gethighscore(w http.ResponseWriter, r *http.Request) {
// run script // run script
output, err := exec.Command(config.NethackLauncher.ReclistLocation, output, err := exec.Command(config.NethackLauncher.ReclistLocation,

Loading…
Cancel
Save