Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the nodejs example to build in a docker image #945

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cpp-simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is a very simple "server" that doesn't do much other than show how the SDK
It will
- Setup the Agones SDK
- Call `SDK::Ready()` to register that it is ready with Agones.
- Every 10 seconds, write a log saying "Hi! I'm a Game Server"
- Every 10 seconds, write a log showing how long it has been running for
- After 60 seconds, call `SDK::Shutdown()` to shut the server down.

To learn how to deploy this example service to GKE, please see the tutorial [Build and Run a Simple Gameserver (C++)](https://agones.dev/site/docs/tutorials/simple-gameserver-cpp/).
Expand Down
29 changes: 29 additions & 0 deletions examples/nodejs-simple/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM debian:stretch
markmandel marked this conversation as resolved.
Show resolved Hide resolved
RUN useradd -m server
RUN apt-get update && apt-get install -y curl && apt-get clean
RUN curl -sL https://deb.nodesource.com/setup_11.x | bash - && \
apt-get install -y nodejs

WORKDIR /home/server

COPY ./sdks/nodejs sdks/nodejs
COPY ./examples/nodejs-simple examples/nodejs-simple
RUN cd examples/nodejs-simple && \
npm install

USER server
ENTRYPOINT cd /home/server/examples/nodejs-simple && npm start
46 changes: 46 additions & 0 deletions examples/nodejs-simple/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Makefile for building the world's simplest node.js game server
#

# __ __ _ _ _
# \ \ / /_ _ _ __(_) __ _| |__ | | ___ ___
# \ \ / / _` | '__| |/ _` | '_ \| |/ _ \ __|
# \ V / (_| | | | | (_| | |_) | | __\__ \
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/
#

REPOSITORY = gcr.io/agones-images

# Directory that this Makefile is in.
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
server_tag = $(REPOSITORY)/nodejs-simple-server:0.1
root_path = $(realpath $(project_path)/../..)

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
# | |/ _` | '__/ _` |/ _ \ __/ __|
# | | (_| | | | (_| | __/ |_\__ \
# |_|\__,_|_| \__, |\___|\__|___/
# |___/

# build the cpp-simple binary
build:
cd $(root_path) && docker build -f $(project_path)/Dockerfile --tag=$(server_tag) .

run:
docker run --network=host $(server_tag)
113 changes: 109 additions & 4 deletions examples/nodejs-simple/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,109 @@
### Instructions
`npm install` - install dependencies

`npm start` - start the self-running example
# Simple node.js Example

This is a very simple "server" that doesn't do much other than show how the SDK works in node.js.

It will
- Setup the Agones SDK
- Call `sdk.ready()` to register that it is ready with Agones.
- Every 10 seconds, write a log showing how long it has been running for
- After 60 seconds, call `sdk.shutdown()` to shut the server down.

To learn how to deploy this example service to GKE, please see the tutorial [Build and Run a Simple Gameserver (node.js)](https://agones.dev/site/docs/tutorials/simple-gameserver-nodejs/).

## Building

If you want to modify the source code and/or build an updated container image, run `make build` from this directory.
This will run the `docker build` command with the correct context.

This example uses a Docker container to host the SDK and example it inside a container so that no special build
tools need to be installed on your system.

## Testing locally with Docker

If you want to run the example locally, you need to start an instance of the SDK-server. To run an SDK-server for
120 seconds, run
```bash
$ cd ../../build; make run-sdk-conformance-local TIMEOUT=120 TESTS=ready,watch,health,gameserver
```

In a separate terminal, while the SDK-server is still running, build and start a container with the example gameserver:
```bash
$ make build
$ make run
```

You will see the output like the following:
```
docker run --network=host gcr.io/agones-images/nodejs-simple-server:0.1

> @ start /home/server/examples/nodejs-simple
> node src/index.js

node.js Game Server has started!
Setting a label
(node:18) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Setting an annotation
GameServer Update:
name: local
state: Ready
GameServer Update:
name: local
state: Ready
Marking server as ready...
...marked Ready
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 10 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 20 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 30 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 40 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 50 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Shutting down after 60 seconds...
...marked for Shutdown
Running for 60 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 70 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 80 seconds!
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Health ping sent
Running for 90 seconds!
```
31 changes: 31 additions & 0 deletions examples/nodejs-simple/gameserver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2017 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: "agones.dev/v1"
kind: GameServer
metadata:
# generate a unique name
# will need to be created with `kubectl create`
generateName: nodejs-simple-
spec:
ports:
- name: default
portPolicy: Dynamic
containerPort: 7654
template:
spec:
containers:
- name: nodejs-simple
image: gcr.io/agones-images/nodejs-simple-server:0.1
imagePullPolicy: Always
13 changes: 13 additions & 0 deletions examples/nodejs-simple/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 61 additions & 46 deletions examples/nodejs-simple/src/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const AgonesSDK = require('agones');

const agonesSDK = new AgonesSDK();

const connect = async function() {
agonesSDK.watchGameServer((result) => {
console.log('watch', result);
});

try {
await agonesSDK.ready();
await agonesSDK.setLabel("label", "labelValue");
await agonesSDK.setAnnotation("annotation", "annotationValue");
const result = await agonesSDK.getGameServer();
console.log('gameServer', result);
setTimeout(() => {
console.log('send health ping');
agonesSDK.health();
}, 2000);
setTimeout(() => {
console.log('send shutdown request');
agonesSDK.shutdown();
}, 4000);
setTimeout(() => {
agonesSDK.close();
}, 6000);
} catch (error) {
console.error(error);
}
};

connect();
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const AgonesSDK = require('agones');

const agonesSDK = new AgonesSDK();

const connect = async function() {
agonesSDK.watchGameServer((result) => {
console.log("GameServer Update:\n\tname:", result.objectMeta.name, "\n\tstate:", result.status.state);
});
setInterval(() => {
agonesSDK.health();
console.log('Health ping sent');
}, 20000);

try {
console.log('node.js Game Server has started!');

console.log('Setting a label');
await agonesSDK.setLabel("test-label", "test-value");
console.log('Setting an annotation');
await agonesSDK.setAnnotation("test-annotation", "test value");

console.log('Marking server as ready...');
await agonesSDK.ready();
console.log('...marked Ready');

var count = 0;
setInterval(() => {
count = count + 10;
console.log('Running for', count, 'seconds!');
}, 10000);
setTimeout(() => {
console.log('Shutting down after 60 seconds...');
agonesSDK.shutdown();
console.log('...marked for Shutdown');
}, 60000);
setTimeout(() => {
agonesSDK.close();
}, 90000);
setTimeout(() => {
process.exit(0);
}, 100000);
} catch (error) {
console.error(error);
}
};

connect();
14 changes: 5 additions & 9 deletions sdks/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading