forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the nodejs example to build in a docker image (googleforgames#945
) * Update the nodejs example: - to build in a docker image. - to follow the same pattern as the c++ example - to have a tutorial for deploying in an agones cluster * Update the js-yaml dependency to fix a security vulnerability.
- Loading branch information
1 parent
fa1eb4a
commit 20a4b09
Showing
11 changed files
with
556 additions
and
63 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
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,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 | ||
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 |
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,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) |
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 |
---|---|---|
@@ -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! | ||
``` |
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,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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.