From 98a4199a5f15e9580c65282544d07f91ec36875b Mon Sep 17 00:00:00 2001 From: Charley Date: Sat, 20 Apr 2024 22:22:51 +0200 Subject: [PATCH] feat: add grpc client api and add proto folder Signed-off-by: Charley --- .gitignore | 4 ++++ proto/vmm.proto | 30 ++++++++++++++++++++++++++++++ src/api/Cargo.toml | 5 +++++ src/api/build.rs | 4 ++++ src/api/src/client.rs | 32 ++++++++++++++++++++++++++++++++ src/api/src/lib.rs | 1 + src/api/src/main.rs | 17 +++++------------ src/api/src/services.rs | 32 +++++++++++--------------------- 8 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 proto/vmm.proto create mode 100644 src/api/build.rs create mode 100644 src/api/src/client.rs diff --git a/.gitignore b/.gitignore index f73ca97..49e45fc 100644 --- a/.gitignore +++ b/.gitignore @@ -224,3 +224,7 @@ $RECYCLE.BIN/ *.lnk # End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,vim,intellij+all,rust,rust-analyzer,visualstudiocode + +# RooFs and Kernel +kernel/* +rootfs/* \ No newline at end of file diff --git a/proto/vmm.proto b/proto/vmm.proto new file mode 100644 index 0000000..a6f48fe --- /dev/null +++ b/proto/vmm.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package vmmorchestrator; + +enum Language { + PYTHON = 0; + JAVASCRIPT = 1; + RUST = 2; +} + +enum LogLevel { + DEBUG = 0; + INFO = 1; + WARN = 2; + ERROR = 3; +} + +service VmmService { + rpc Run (RunVmmRequest) returns (RunVmmResponse); +} + +message RunVmmRequest { + Language language = 1; + LogLevel log_level = 4; + string code = 2; + string env = 3; +} + +message RunVmmResponse { +} diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml index 088cf4d..017133e 100644 --- a/src/api/Cargo.toml +++ b/src/api/Cargo.toml @@ -8,3 +8,8 @@ edition = "2021" [dependencies] actix-web = "4.5.1" serde = "1.0.197" +tonic = "0.9" +prost = "0.11" + +[build-dependencies] +tonic-build = "0.9" \ No newline at end of file diff --git a/src/api/build.rs b/src/api/build.rs new file mode 100644 index 0000000..c53d705 --- /dev/null +++ b/src/api/build.rs @@ -0,0 +1,4 @@ +fn main() -> Result<(), Box> { + tonic_build::compile_protos("../../proto/vmm.proto")?; + Ok(()) +} diff --git a/src/api/src/client.rs b/src/api/src/client.rs new file mode 100644 index 0000000..73030a6 --- /dev/null +++ b/src/api/src/client.rs @@ -0,0 +1,32 @@ +use tonic::transport::Channel; +use vmmorchestrator::vmm_service_client::VmmServiceClient; + +pub mod vmmorchestrator { + tonic::include_proto!("vmmorchestrator"); +} + +pub struct VmmClient { + client: VmmServiceClient, +} + +impl VmmClient { + pub async fn new() -> Result { + let client = VmmServiceClient::connect("http://[::1]:50051") + .await + .expect("Failed to connect to VMM service"); + + Ok(VmmClient { client }) + } + + pub async fn run_vmm(&mut self) { + let request = tonic::Request::new(vmmorchestrator::RunVmmRequest { + language: vmmorchestrator::Language::Rust as i32, + env: "fn main() { println!(\"Hello, World!\"); }".to_string(), + code: "fn main() { println!(\"Hello, World!\"); }".to_string(), + log_level: vmmorchestrator::LogLevel::Info as i32, + }); + + let response = self.client.run(request).await.unwrap(); + println!("RESPONSE={:?}", response); + } +} diff --git a/src/api/src/lib.rs b/src/api/src/lib.rs index 4e379ae..24f4aba 100644 --- a/src/api/src/lib.rs +++ b/src/api/src/lib.rs @@ -1 +1,2 @@ +pub mod client; pub mod services; diff --git a/src/api/src/main.rs b/src/api/src/main.rs index 162db20..2de501b 100644 --- a/src/api/src/main.rs +++ b/src/api/src/main.rs @@ -1,20 +1,13 @@ use actix_web::{App, HttpServer}; -use api::services::{configuration, logs, metrics, run, shutdown}; +use api::services::{run, shutdown}; #[actix_web::main] async fn main() -> std::io::Result<()> { let port = 3000; println!("Starting server on port: {}", port); - HttpServer::new(|| { - App::new() - .service(configuration) - .service(run) - .service(logs) - .service(metrics) - .service(shutdown) - }) - .bind(("127.0.0.1", port))? - .run() - .await + HttpServer::new(|| App::new().service(run).service(shutdown)) + .bind(("127.0.0.1", port))? + .run() + .await } diff --git a/src/api/src/services.rs b/src/api/src/services.rs index 97d724c..897cd7a 100644 --- a/src/api/src/services.rs +++ b/src/api/src/services.rs @@ -1,28 +1,18 @@ -use actix_web::{get, post, web, HttpResponse, Responder}; - -#[post("/configuration")] -pub async fn configuration(req_body: String) -> impl Responder { - // TODO: Use the body to create the vm configuration - HttpResponse::Ok().body(req_body) -} +use crate::client::VmmClient; +use actix_web::{post, HttpResponse, Responder}; #[post("/run")] pub async fn run(req_body: String) -> impl Responder { - // TODO: Use the body id to start the fm - HttpResponse::Ok().body(req_body) -} - -#[get("/logs/{id}")] -pub async fn logs(id: web::Path) -> HttpResponse { - // TODO: maybe not close the stream and keep sending the logs - HttpResponse::Ok().body(format!("Logs here: {}", &id)) -} - -#[get("/metrics/{id}")] -pub async fn metrics(id: web::Path) -> HttpResponse { - // TODO: Get the metrics for a VM with the given ID + let grpc_client = VmmClient::new().await; - HttpResponse::Ok().body(format!("Metrics here: {}", &id)) + match grpc_client { + Ok(mut client) => { + client.run_vmm().await; + HttpResponse::Ok().body(req_body) + } + Err(e) => HttpResponse::InternalServerError() + .body("Failed to connect to VMM service with error: ".to_string() + &e.to_string()), + } } #[post("/shutdown")]