-
Notifications
You must be signed in to change notification settings - Fork 828
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
Support Rust SDK by gRPC-rs #230
Merged
cyriltovena
merged 1 commit into
googleforgames:master
from
thara:support-rust-sdk-by-grpcrs
Jun 8, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
cd /go/src/agones.dev/agones | ||
protoc --rust_out sdks/rust/src/grpc --grpc_out=sdks/rust/src/grpc --plugin=protoc-gen-grpc=`which grpc_rust_plugin` sdk.proto |
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,19 @@ | ||
|
||
# Created by https://www.gitignore.io/api/rust | ||
|
||
### Rust ### | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
||
# End of https://www.gitignore.io/api/rust | ||
|
||
sdk |
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,6 @@ | ||
[package] | ||
name = "rust-simple" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
agones = { path = "../../sdks/rust" } |
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,50 @@ | ||
FROM rust:1.25.0 as builder | ||
RUN useradd -m build | ||
|
||
# Rust SDK depends on https://github.com/pingcap/grpc-rs and it requires CMake and Go | ||
|
||
## Cmake | ||
ENV CMAKE_MINOR_VERSION=v3.10 \ | ||
CMAKE_FULL_VERSION=3.10.3 | ||
RUN mkdir -p /usr/src/cmake \ | ||
&& curl -fSLO https://cmake.org/files/${CMAKE_MINOR_VERSION}/cmake-${CMAKE_FULL_VERSION}.tar.gz \ | ||
&& curl -fSLO https://cmake.org/files/${CMAKE_MINOR_VERSION}/cmake-${CMAKE_FULL_VERSION}-SHA-256.txt \ | ||
&& sha256sum -c cmake-${CMAKE_FULL_VERSION}-SHA-256.txt 2>&1 | grep OK \ | ||
&& tar xf cmake-${CMAKE_FULL_VERSION}.tar.gz -C /usr/src/cmake --strip-components=1 \ | ||
&& rm -f cmake-${CMAKE_FULL_VERSION}.* \ | ||
&& cd /usr/src/cmake \ | ||
&& ./configure && make -j$(nproc) && make install | ||
|
||
## Go | ||
ENV GO_VERSION=1.10.2 \ | ||
GO_CHECKSUM=4b677d698c65370afa33757b6954ade60347aaca310ea92a63ed717d7cb0c2ff | ||
RUN mkdir -p /usr/local/go \ | ||
&& curl -fSO https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz \ | ||
&& shasum -a 256 go${GO_VERSION}.linux-amd64.tar.gz | grep ${GO_CHECKSUM} \ | ||
&& tar xf go${GO_VERSION}.linux-amd64.tar.gz -C /usr/local/go --strip-components=1 \ | ||
&& rm -f go${GO_VERSION}.linux-amd64.tar.gz | ||
ENV PATH $PATH:/usr/local/go/bin | ||
|
||
# SDK | ||
COPY sdk/src /home/builder/agones/sdks/rust/src | ||
COPY sdk/Cargo.toml /home/builder/agones/sdks/rust/ | ||
COPY sdk/Cargo.lock /home/builder/agones/sdks/rust/ | ||
|
||
# Example | ||
COPY src /home/builder/agones/examples/rust-simple/src | ||
COPY Cargo.toml /home/builder/agones/examples/rust-simple/ | ||
COPY Cargo.lock /home/builder/agones/examples/rust-simple/ | ||
COPY Makefile /home/builder/agones/examples/rust-simple/ | ||
|
||
WORKDIR /home/builder/agones/examples/rust-simple | ||
RUN make build | ||
|
||
FROM debian:stretch | ||
RUN useradd -m server | ||
|
||
COPY --from=builder /home/builder/agones/examples/rust-simple/target/release/rust-simple /home/server/rust-simple | ||
RUN chown -R server /home/server && \ | ||
chmod o+x /home/server/rust-simple | ||
|
||
USER server | ||
ENTRYPOINT /home/server/rust-simple |
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,40 @@ | ||
# | ||
# Makefile for building the world's simplest Rust game server | ||
# | ||
|
||
# __ __ _ _ _ | ||
# \ \ / /_ _ _ __(_) __ _| |__ | | ___ ___ | ||
# \ \ / / _` | '__| |/ _` | '_ \| |/ _ \ __| | ||
# \ V / (_| | | | | (_| | |_) | | __\__ \ | ||
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/ | ||
# | ||
|
||
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) | ||
project_path := $(dir $(mkfile_path)) | ||
server_tag = rust-simple-server:0.1 | ||
|
||
# _____ _ | ||
# |_ _|_ _ _ __ __ _ ___| |_ ___ | ||
# | |/ _` | '__/ _` |/ _ \ __/ __| | ||
# | | (_| | | | (_| | __/ |_\__ \ | ||
# |_|\__,_|_| \__, |\___|\__|___/ | ||
# |___/ | ||
|
||
# build the library | ||
build: build-server | ||
|
||
# Build the server | ||
build-server: | ||
cargo build --release | ||
|
||
# Build a docker image for the server, and tag it | ||
build-image: | ||
# Docker does not allow to copy outside files, | ||
mkdir -p $(project_path)sdk | ||
cp -rf $(project_path)../../sdks/rust/src $(project_path)sdk/src | ||
cp -rf $(project_path)../../sdks/rust/Cargo.* $(project_path)sdk | ||
docker build $(project_path) --tag=$(server_tag) | ||
|
||
clean: | ||
cargo clean | ||
rm -rf $(project_path)sdk |
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,34 @@ | ||
# Simple Rust Example | ||
|
||
This is a very simple "server" that doesn't do much other than show how the SDK works in Rust. | ||
|
||
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" | ||
- After 60 seconds, call `SDK::Shutdown()` to shut the server down. | ||
|
||
## Running by minikube | ||
|
||
First of all, you have to configure Agones on minikude. Check out [these instructions](https://github.com/GoogleCloudPlatform/agones/blob/3b856a4b90862a3ea183643869f81801d4468220/install/README.md). | ||
|
||
``` | ||
$ eval $(minikube docker-env) | ||
$ make build-image | ||
$ kubectl create -f gameserver.yaml | ||
``` | ||
|
||
You can see output of the example by the following. | ||
|
||
``` | ||
$ POD_NAME=`kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'` | ||
$ kubectl logs $POD_NAME -c rust-simple | ||
Rust Game Server has started! | ||
Creating SDK instance | ||
Marking server as ready... | ||
Running for 0 seconds | ||
Health ping sent | ||
Health ping sent | ||
Health ping sent | ||
Health ping sent | ||
``` |
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,14 @@ | ||
apiVersion: "stable.agones.dev/v1alpha1" | ||
kind: GameServer | ||
metadata: | ||
# generate a unique name | ||
# will need to be created with `kubectl create` | ||
generateName: rust-simple- | ||
spec: | ||
containerPort: 7654 | ||
template: | ||
spec: | ||
containers: | ||
- name: rust-simple | ||
image: gcr.io/agones-images/rust-simple-server:0.1 | ||
imagePullPolicy: IfNotPresent |
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,68 @@ | ||
extern crate agones; | ||
|
||
use std::result::Result; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
macro_rules! enclose { | ||
( ($( $x:ident ),*) $y:expr ) => { | ||
{ | ||
$(let mut $x = $x.clone();)* | ||
$y | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
println!("Rust Game Server has started!"); | ||
|
||
::std::process::exit(match run() { | ||
Ok(_) => { | ||
println!("Rust Game Server finished."); | ||
0 | ||
}, | ||
Err(msg) => { | ||
println!("{}", msg); | ||
1 | ||
} | ||
}); | ||
} | ||
|
||
fn run() -> Result<(), String>{ | ||
|
||
println!("Creating SDK instance"); | ||
let sdk = agones::Sdk::new().map_err(|_| "Could not connect to the sidecar. Exiting!")?; | ||
|
||
let _t = thread::spawn(enclose!{(sdk) move || { | ||
loop { | ||
match sdk.health() { | ||
(s, Ok(_)) => { | ||
println!("Health ping sent"); | ||
sdk = s; | ||
}, | ||
(s, Err(e)) => { | ||
println!("Health ping failed : {:?}", e); | ||
sdk = s; | ||
} | ||
} | ||
thread::sleep(Duration::from_secs(2)); | ||
} | ||
}}); | ||
|
||
println!("Marking server as ready..."); | ||
|
||
for i in 0..10 { | ||
let time = i * 10; | ||
println!("Running for {} seconds", time); | ||
|
||
thread::sleep(Duration::from_secs(10)); | ||
|
||
if i == 5 { | ||
println!("Shutting down after 60 seconds..."); | ||
sdk.shutdown().map_err(|e| format!("Could not run Shutdown: {}. Exiting!", e))?; | ||
println!("...marked for Shutdown"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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,17 @@ | ||
|
||
# Created by https://www.gitignore.io/api/rust | ||
|
||
### Rust ### | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
||
# End of https://www.gitignore.io/api/rust |
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,10 @@ | ||
[package] | ||
name = "agones" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
grpcio = "0.3.0" | ||
grpcio-proto = "0.3.0" | ||
protobuf = "2.0.2" | ||
futures = "^0.1.15" | ||
error-chain = "0.11.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Rust Game Server Client SDK | ||
|
||
This is the Rust version of the Agones Game Server Client SDK. | ||
Check the [Client SDK Documentation](../) for more details on each of the SDK functions and how to run the SDK locally. | ||
|
||
## Prerequisites | ||
|
||
- CMake >= 3.8.0 | ||
- Rust >= 1.19.0 | ||
- Go (>=1.7) | ||
|
||
The SDK needs the above for building to [gRPC-rs](https://github.com/pingcap/grpc-rs). | ||
|
||
## Usage | ||
|
||
Add this crate to `dependencies` section in your Cargo.toml. | ||
Specify a directory where this README.md is located to the `path`. | ||
|
||
```toml | ||
[dependencies] | ||
agones = { path = "../agones/sdks/rust" } | ||
``` | ||
|
||
Add `extern crate agones` to your crate root. | ||
|
||
To begin working with the SDK, create an instance of it. This function blocks until connection and handshake are made. | ||
|
||
```rust | ||
let sdk = agones::Sdk::new()?; | ||
``` | ||
|
||
To send a [health check](../README.md#health) ping call `sdk.health()`. | ||
|
||
```rust | ||
if sdk.health().is_ok() { | ||
println!("Health ping sent"); | ||
} | ||
``` | ||
|
||
To mark that the [game session is completed](../README.md#shutdown) and the game server should be shut down call `sdk.shutdown()`. | ||
|
||
```rust | ||
if sdk.shutdown().is_err() { | ||
println!("Could not run Shutdown"); | ||
} | ||
``` |
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,13 @@ | ||
// Wrap in a new error | ||
error_chain!{ | ||
foreign_links { | ||
Grpc(::grpcio::Error); | ||
} | ||
|
||
errors { | ||
HealthPingConnectionFailure(t: String) { | ||
description("health ping connection failure"), | ||
display("health ping connection failure: '{}'", t), | ||
} | ||
} | ||
} |
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,2 @@ | ||
pub mod sdk; | ||
pub mod sdk_grpc; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the multi-stage builder image - we should do that more.
Out of curiosity, why do we need Go here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Rust SDK depends gRPC-RS.
It needs Go for TLS encryption and some authentication mechanism.
See also https://github.com/pingcap/grpc-rs#prerequisites.