-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent/grpc): add gRPC server (#27)
* feat(agent): add and compile workload proto service Signed-off-by: Martin Moreira de Jesus <[email protected]> * refactor(agent/workload): add mod.rs Signed-off-by: Martin Moreira de Jesus <[email protected]> * feat(agent/grpc): add service skeleton Signed-off-by: Martin Moreira de Jesus <[email protected]> * feat(agent/grpc): run agent on request and send result Signed-off-by: Martin Moreira de Jesus <[email protected]> * fix(agent/grpc): add server config in file Signed-off-by: Martin Moreira de Jesus <[email protected]> * fix(agent/grpc): typo Signed-off-by: Martin Moreira de Jesus <[email protected]> * feat(agent/grpc): add ungraceful shutdown Signed-off-by: Martin Moreira de Jesus <[email protected]> * nitpick: move agent.proto at root proto folder Signed-off-by: Matéo Fernandez <[email protected]> * refactor(grpc): use map_err instead of match Signed-off-by: Matéo Fernandez <[email protected]> --------- Signed-off-by: Martin Moreira de Jesus <[email protected]> Signed-off-by: Matéo Fernandez <[email protected]> Co-authored-by: Matéo Fernandez <[email protected]>
- Loading branch information
1 parent
2338841
commit decb5bd
Showing
10 changed files
with
166 additions
and
15 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,31 @@ | ||
syntax = "proto3"; | ||
package cloudlet.agent; | ||
import "google/protobuf/empty.proto"; | ||
|
||
message ExecuteRequest {} | ||
|
||
message ExecuteResponse { | ||
enum Stage { | ||
PENDING = 0; | ||
BUILDING = 1; | ||
RUNNING = 2; | ||
DONE = 3; | ||
FAILED = 4; | ||
DEBUG = 5; | ||
} | ||
|
||
string stdout = 1; | ||
string stderr = 2; | ||
int32 exit_code = 3; | ||
} | ||
|
||
message SignalRequest { | ||
enum Signal { | ||
KILL = 0; | ||
} | ||
} | ||
|
||
service WorkloadRunner { | ||
rpc Execute(ExecuteRequest) returns (stream ExecuteResponse) {} | ||
rpc Signal(SignalRequest) returns (google.protobuf.Empty) {} | ||
} |
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 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tonic_build::compile_protos("../../proto/agent.proto")?; | ||
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
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 |
---|---|---|
@@ -1,18 +1,38 @@ | ||
use agent::workload::{config::Config, runner::Runner}; | ||
use agent::{ | ||
agent::workload_runner_server::WorkloadRunnerServer, | ||
workload::{config::Config, runner::Runner, service::WorkloadRunnerService}, | ||
}; | ||
use clap::Parser; | ||
use std::path::PathBuf; | ||
use std::{net::ToSocketAddrs, path::PathBuf}; | ||
use tonic::transport::Server; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Args { | ||
#[clap(short, long, default_value = "/etc/cloudlet/agent/config.toml")] | ||
config: PathBuf, | ||
} | ||
|
||
fn main() { | ||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let args = Args::parse(); | ||
|
||
let config = Config::from_file(&args.config).unwrap(); | ||
|
||
let bind_address = format!("{}:{}", config.server.address, config.server.port) | ||
.to_socket_addrs() | ||
.unwrap() | ||
.next() | ||
.unwrap(); | ||
|
||
let runner = Runner::new(config); | ||
|
||
runner.run().unwrap(); | ||
let server = WorkloadRunnerService::new(runner); | ||
|
||
Server::builder() | ||
.add_service(WorkloadRunnerServer::new(server)) | ||
.serve(bind_address) | ||
.await | ||
.unwrap(); | ||
|
||
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
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,3 @@ | ||
pub mod config; | ||
pub mod runner; | ||
pub mod service; |
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,58 @@ | ||
use super::runner::Runner; | ||
use crate::agent::{self, ExecuteRequest, ExecuteResponse, SignalRequest}; | ||
use agent::workload_runner_server::WorkloadRunner; | ||
use std::{process, sync::Arc}; | ||
use tokio::sync::Mutex; | ||
use tokio_stream::wrappers::ReceiverStream; | ||
use tonic::{Request, Response}; | ||
|
||
type Result<T> = std::result::Result<Response<T>, tonic::Status>; | ||
|
||
pub struct WorkloadRunnerService { | ||
runner: Arc<Mutex<Runner>>, | ||
} | ||
|
||
impl WorkloadRunnerService { | ||
pub fn new(runner: Runner) -> Self { | ||
WorkloadRunnerService { | ||
runner: Arc::new(Mutex::new(runner)), | ||
} | ||
} | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl WorkloadRunner for WorkloadRunnerService { | ||
type ExecuteStream = ReceiverStream<std::result::Result<ExecuteResponse, tonic::Status>>; | ||
|
||
async fn execute(&self, _: Request<ExecuteRequest>) -> Result<Self::ExecuteStream> { | ||
let (tx, rx) = tokio::sync::mpsc::channel(4); | ||
|
||
// We assume there's only one request at a time | ||
let runner = self | ||
.runner | ||
.try_lock() | ||
.map_err(|e| tonic::Status::unavailable(format!("Runner is busy: {:?}", e)))?; | ||
|
||
let res = runner | ||
.run() | ||
.map_err(|e| tonic::Status::internal(e.to_string()))?; | ||
|
||
let _ = tx | ||
.send(Ok(ExecuteResponse { | ||
stdout: res.stdout, | ||
stderr: res.stderr, | ||
exit_code: res.exit_code, | ||
})) | ||
.await | ||
.map_err(|e| { | ||
println!("Failed to send response: {:?}", e); | ||
tonic::Status::internal("Failed to send response") | ||
})?; | ||
|
||
Ok(Response::new(ReceiverStream::new(rx))) | ||
} | ||
|
||
async fn signal(&self, _: Request<SignalRequest>) -> Result<()> { | ||
process::exit(0); | ||
} | ||
} |