-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for running in a container.
This commit is focused on building and running the container in a local docker environment. Parameters were added to the go program to allow us to adjust the number of registration retries and the time between each retry, looking forward to the need to wait for a delay in our container's external network connectivity. An example env file, example-container.env, has been provided to show how to pass parameters to our container. Commentary in example-container.env describes how to build a local Docker container and then run it using the env file. Documentation for each important environment variable can be found in the container's startup script, container-startup.sh.
- Loading branch information
curfman
committed
Feb 10, 2016
1 parent
3566e68
commit 5c92525
Showing
5 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# | ||
# Build from the go source directory: | ||
# docker build -t goroom:1.5 . | ||
|
||
FROM golang:1.5 | ||
|
||
# We need gorilla, so go get it. | ||
RUN cd $GOPATH && go get github.com/gorilla/websocket | ||
|
||
# Copy our go source files and build/install the | ||
# code. If successful, the executable file, | ||
# $GOPATH/bin/gameon-room-go, will be built. | ||
RUN mkdir -p $GOPATH/src/gameon-room-go | ||
COPY ./*.go $GOPATH/src/gameon-room-go/ | ||
COPY ./container-startup.sh /usr/bin/container-startup.sh | ||
RUN cd $GOPATH/src/gameon-room-go && go install | ||
|
||
# Our room should always listen on port 3000 (-lp) | ||
# although the mapped host callback port (-cp) may | ||
# be different. | ||
EXPOSE 3000 | ||
WORKDIR $GOPATH/gameon-room-go/src | ||
|
||
# The real work of running the game is done in the startup script | ||
# which reads environment variables to drive its choices for startup. | ||
ENTRYPOINT ["/bin/bash", "/usr/bin/container-startup.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/bin/bash | ||
# This is the startup script for the golang GameOn! room. | ||
# | ||
# The following environment variables are required: | ||
# CONTAINER_IP - The public IP address to which your container is bound. | ||
# (This is needed for the websocket callback.) | ||
# GAMEON_ID - The ID given in Game On! | ||
# GAMEON_SECRET - The shared secret given in Game On! | ||
# ROOM_NAME - Your name for the room. | ||
# | ||
# The following environment variables are optional: | ||
# GAMEON_ADDR - The game server address, defaults to game-on.org. | ||
# GAMEON_PORT - Our external port, defaults to 3000. | ||
# (This is needed for the websocket callback.) | ||
# GAMEON_DEBUG - Any non-empty value turns on debug output | ||
# GAMEON_TIMESHIFT - Use this to adjust our timestamps to match the | ||
# GameOn! server in cases where the time is skewed | ||
# relative to our room and the server. This is | ||
# expressed in milliseconds. The default is 0. | ||
# | ||
# The following two variables allow for a delay in network connectivity. | ||
# | ||
# GAMEON_REG_RETRIES - Number of initial registration attempts. | ||
# Defaults to 5 | ||
# GAMEON_REG_SECONDS_BETWEEN - Number of seconds between registration | ||
# attempts. Defaults to 30 | ||
|
||
export ROOM_BINARY=$GOPATH/bin/gameon-room-go | ||
# Print an error message ($1) and exit. | ||
function fatal | ||
{ | ||
echo "FAIL: FATAL ERROR, $1" | ||
exit 1 | ||
} | ||
|
||
# $1 is var name | ||
# $2 is var content | ||
function assert_var_set | ||
{ | ||
if [ -z "$2" ] ; then | ||
fatal "$1 was not set." | ||
fi | ||
} | ||
|
||
# Make sure the required env vars are defined with non-empty values | ||
assert_var_set CONTAINER_IP $CONTAINER_IP | ||
assert_var_set GAMEON_ID $GAMEON_ID | ||
assert_var_set GAMEON_SECRET $GAMEON_SECRET | ||
assert_var_set ROOM_NAME $ROOM_NAME | ||
|
||
# Make sure any optional env vars are given default values if they are not defined | ||
export GAMEON_ADDR=${GAMEON_ADDR-game-on.org} | ||
export GAMEON_PORT=${GAMEON_PORT-3000} | ||
export GAMEON_REG_RETRIES=${GAMEON_REG_RETRIES-10} | ||
export GAMEON_REG_SECONDS_BETWEEN=${GAMEON_REG_SECONDS_BETWEEN-15} | ||
export GAMEON_TIMESHIFT=${GAMEON_TIMESHIFT-0} | ||
if [ -z "$GAMEON_DEBUG" ] ; then | ||
DEBUG_FLAG="" | ||
else | ||
DEBUG_FLAG="-d" | ||
fi | ||
|
||
$ROOM_BINARY \ | ||
-c $CONTAINER_IP \ | ||
-g $GAMEON_ADDR \ | ||
-cp 3000 \ | ||
-lp $GAMEON_PORT \ | ||
-r $ROOM_NAME \ | ||
-id "$GAMEON_ID" \ | ||
-secret "$GAMEON_SECRET" \ | ||
-retries $GAMEON_REG_RETRIES \ | ||
-between $GAMEON_REG_SECONDS_BETWEEN \ | ||
-ts $GAMEON_TIMESHIFT \ | ||
$DEBUG_FLAG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This is an example environment variable file for use with | ||
# the gameon-room-go container. The current values are just | ||
# for illustration and most certainly will not work for you | ||
# until you change the values to match your needs. | ||
# | ||
# Build the container locally: | ||
# docker build -t go-room:1.0 . | ||
# Run the container locally using this file: | ||
# docker run -it --env-file=example-container.env go-room:1.0 | ||
# | ||
CONTAINER_IP=127.0.0.1 | ||
GAMEON_ID=MyFakeId | ||
GAMEON_SECRET=MyFakeSecret | ||
ROOM_NAME=MyRoom | ||
GAMEON_PORT=3000 | ||
GAMEON_ADDR=127.0.0.1 | ||
GAMEON_DEBUG=true | ||
GAMEON_REG_RETRIES=2 | ||
GAMEON_REG_SECONDS_BETWEEN=5 | ||
GAMEON_TIMESHIFT=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters