Skip to content

Commit 5d5822a

Browse files
committed
add rpc implementation
1 parent f4915d0 commit 5d5822a

9 files changed

Lines changed: 294 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coman/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ iroh = "0.95.1"
8484
rand = "0.9.2"
8585
regex = "1.12.2"
8686
sha2 = "0.10.9"
87+
tarpc = { version = "0.37.0", features = [
88+
"serde-transport",
89+
"serde-transport-bincode",
90+
"tcp",
91+
"tokio1",
92+
] }
93+
tokio-duplex = "1.0.1"
8794

8895
[build-dependencies]
8996
anyhow = "1.0.90"

coman/src/cli/app.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ pub enum CscsJobCommands {
303303
#[clap(help="id or name of the job (name uses newest job of that name)", add = ArgValueCompleter::new(job_id_or_name_completer))]
304304
job: JobIdOrName,
305305
},
306+
#[clap(
307+
alias = "ru",
308+
about = "show current resource usage of the job, needs coman to be injected in the session [aliases: ru]"
309+
)]
310+
ResourceUsage {
311+
#[clap(help="id or name of the job (name uses newest job of that name)", add = ArgValueCompleter::new(job_id_or_name_completer))]
312+
job: JobIdOrName,
313+
},
306314
}
307315
fn job_id_or_name_completer(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
308316
let mut completions = vec![];

coman/src/cli/exec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use iroh::{
77
endpoint::ConnectionError,
88
protocol::{ProtocolHandler, Router},
99
};
10+
use nom::AsBytes;
1011
use pid1::Pid1Settings;
1112
use rust_supervisor::{ChildType, Supervisor, SupervisorConfig};
1213
use tokio::{io::AsyncWriteExt, net::TcpStream};
1314

15+
use crate::cli::rpc::RpcHandler;
16+
1417
const SECRET_KEY_ENV: &str = "COMAN_IROH_SECRET";
1518
const PORT_FORWARD_ENV: &str = "COMAN_FORWARDED_PORTS";
1619
const SSH_PORT: u16 = 15263;
@@ -121,6 +124,10 @@ async fn port_forward() -> Result<()> {
121124
builder = builder.accept(alpn.clone().into_bytes(), handler);
122125
println!("set up port forwarding for port {port} ({alpn})");
123126
}
127+
128+
// add rpc server
129+
let rpc_handler = RpcHandler;
130+
builder = builder.accept(b"/coman/rpc/".as_bytes(), rpc_handler);
124131
let _router = builder.spawn();
125132
println!("port forwarding started");
126133

coman/src/cli/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod app;
22
pub mod exec;
33
pub mod proxy;
4+
pub mod rpc;

coman/src/cli/rpc.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use futures::StreamExt;
2+
use iroh::protocol::ProtocolHandler;
3+
use tarpc::{
4+
serde_transport as transport, server, server::Channel, tokio_serde::formats::Bincode,
5+
tokio_util::codec::LengthDelimitedCodec,
6+
};
7+
use tokio_duplex::Duplex;
8+
9+
use crate::cli::app::COMAN_VERSION;
10+
11+
#[tarpc::service]
12+
pub trait ComanRPC {
13+
async fn version() -> String;
14+
}
15+
#[derive(Debug, Clone)]
16+
struct RpcServer;
17+
18+
impl ComanRPC for RpcServer {
19+
async fn version(self, _: tarpc::context::Context) -> String {
20+
COMAN_VERSION.to_string()
21+
}
22+
}
23+
24+
#[derive(Debug, Default)]
25+
pub struct RpcHandler;
26+
27+
impl ProtocolHandler for RpcHandler {
28+
async fn accept(&self, connection: iroh::endpoint::Connection) -> Result<(), iroh::protocol::AcceptError> {
29+
let endpoint_id = connection.remote_id();
30+
match connection.accept_bi().await {
31+
Ok((iroh_send, iroh_recv)) => {
32+
println!("Accepted bidirectional stream from {endpoint_id}");
33+
let codec_builder = LengthDelimitedCodec::builder();
34+
let combined = Duplex::new(iroh_recv, iroh_send);
35+
let framed = codec_builder.new_framed(combined);
36+
37+
let transport = transport::new(framed, Bincode::default());
38+
let server = server::BaseChannel::with_defaults(transport);
39+
tokio::spawn(server.execute(RpcServer.serve()).for_each(spawn));
40+
}
41+
Err(e) => {
42+
println!("Failed to accept bidirectional stream to rpc: {e}");
43+
}
44+
}
45+
46+
Ok(())
47+
}
48+
}
49+
50+
async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
51+
tokio::spawn(fut);
52+
}

coman/src/cscs/cli.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
api_client::{client::JobStartOptions, types::JobStatus},
2323
handlers::{
2424
cscs_file_delete, cscs_file_download, cscs_file_list, cscs_file_upload, cscs_job_cancel, cscs_job_details,
25-
cscs_job_list, cscs_job_log, cscs_job_start, cscs_login, cscs_port_forward, cscs_system_list,
26-
cscs_system_set,
25+
cscs_job_list, cscs_job_log, cscs_job_start, cscs_login, cscs_port_forward, cscs_resource_usage,
26+
cscs_system_list, cscs_system_set,
2727
},
2828
},
2929
};
@@ -136,6 +136,16 @@ pub(crate) async fn cli_cscs_port_forward(
136136
cscs_port_forward(job_id, source_port, destination_port, system).await
137137
}
138138

139+
pub(crate) async fn cli_cscs_job_resource_usage(
140+
job: JobIdOrName,
141+
system: Option<String>,
142+
platform: Option<ComputePlatform>,
143+
) -> Result<()> {
144+
let job_id = maybe_job_id_from_name(job, system.clone(), platform.clone()).await?;
145+
println!("running port forward for job {job_id}");
146+
cscs_resource_usage(job_id, system).await
147+
}
148+
139149
#[allow(clippy::too_many_arguments)]
140150
pub(crate) async fn cli_cscs_job_start(
141151
name: Option<String>,

0 commit comments

Comments
 (0)