Skip to content

Commit

Permalink
fix remote ending issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Apr 11, 2024
1 parent db60568 commit 790b6fd
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 18 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ members = [
]

[workspace.dependencies]
tempfile = "3.10.1"
os_info = "3.7"
itertools = "0.12.1"
documented = "0.4.1"
ratatui = "0.26.1"
crossterm = "0.27.0"
serde_json = "1.0.115"
bincode = "1.3.3"
anyhow = "1.0.81"
uuid = { version = "1.8.0", features = ["serde", "v4"] }
Expand Down
2 changes: 2 additions & 0 deletions tiron-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version.workspace = true
edition.workspace = true

[dependencies]
tempfile = { workspace = true }
serde_json = { workspace = true }
os_info = { workspace = true }
documented = { workspace = true }
uuid = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion tiron-node/src/action/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub fn run_command(
for arg in args {
cmd.arg(arg);
}
let mut child = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
let mut child = cmd
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::null())
.spawn()?;

let stdout = child.stdout.take();
let stderr = child.stderr.take();
Expand Down
29 changes: 21 additions & 8 deletions tiron-node/src/action/copy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::path::{Path, PathBuf};
use std::{io::Write, path::Path};

use anyhow::{Context, Result};
use anyhow::{anyhow, Result};
use crossbeam_channel::Sender;
use documented::{Documented, DocumentedFields};
use rcl::{error::Error, runtime::Value};
use serde::{Deserialize, Serialize};
use tiron_common::action::{ActionId, ActionMessage};

use super::{Action, ActionDoc, ActionParamDoc, ActionParamType};
use super::{command::run_command, Action, ActionDoc, ActionParamDoc, ActionParamType};

/// Copy the file to the remote machine
#[derive(Default, Clone, Serialize, Deserialize, Documented, DocumentedFields)]
Expand Down Expand Up @@ -72,12 +72,25 @@ impl Action for CopyAction {
Ok(input)
}

fn execute(&self, _id: ActionId, bytes: &[u8], _tx: &Sender<ActionMessage>) -> Result<String> {
fn execute(&self, id: ActionId, bytes: &[u8], tx: &Sender<ActionMessage>) -> Result<String> {
let input: CopyAction = bincode::deserialize(bytes)?;
let dest = PathBuf::from(&input.dest);
std::fs::write(dest, input.content)
.with_context(|| format!("can't copy to dest {}", input.dest))?;
Ok(format!("copy to {}", input.dest))
let mut temp = tempfile::NamedTempFile::new()?;
temp.write_all(&input.content)?;
temp.flush()?;
let status = run_command(
id,
tx,
"cp",
&[
temp.path().to_string_lossy().to_string(),
input.dest.clone(),
],
)?;
if status.success() {
Ok(format!("copy to {}", input.dest))
} else {
Err(anyhow!("can't copy to {}", input.dest))
}
}

fn doc(&self) -> ActionDoc {
Expand Down
20 changes: 15 additions & 5 deletions tiron-node/src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::{BufRead, Write};
use anyhow::Result;
use crossbeam_channel::Receiver;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;

pub fn stdio_transport<W, R, RpcMessage1, RpcMessage2>(
mut writer: W,
Expand All @@ -24,8 +25,9 @@ pub fn stdio_transport<W, R, RpcMessage1, RpcMessage2>(
});
std::thread::spawn(move || -> Result<()> {
loop {
let msg = read_msg(&mut reader)?;
reader_sender.send(msg)?;
if let Some(msg) = read_msg(&mut reader)? {
reader_sender.send(msg)?;
}
}
});
}
Expand All @@ -35,16 +37,24 @@ where
W: Write,
RpcMessage: Serialize,
{
out.write_all(&bincode::serialize(&msg)?)?;
let msg = format!("{}\n", serde_json::to_string(&msg)?);
out.write_all(msg.as_bytes())?;
out.flush()?;
Ok(())
}

pub fn read_msg<R, RpcMessage>(inp: &mut R) -> Result<RpcMessage>
pub fn read_msg<R, RpcMessage>(inp: &mut R) -> Result<Option<RpcMessage>>
where
R: BufRead,
RpcMessage: DeserializeOwned,
{
let msg: RpcMessage = bincode::deserialize_from(inp)?;
let mut buf = String::new();
let _ = inp.read_line(&mut buf)?;
let value: Value = serde_json::from_str(&buf)?;

let msg = match serde_json::from_value::<RpcMessage>(value) {
Ok(msg) => Some(msg),
Err(_) => None,
};
Ok(msg)
}
4 changes: 4 additions & 0 deletions tiron/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Node {
};

{
let node_tx = tx.clone();
let tx = self.tx.clone();
let host_id = self.id;
std::thread::spawn(move || {
Expand All @@ -85,6 +86,9 @@ impl Node {
});
}
let _ = exit_tx.send(false);
// this doens't do anything but to hold the node's tx
// so that it doesn't get dropped
node_tx.is_empty();
});
}

Expand Down
11 changes: 7 additions & 4 deletions tiron/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ pub fn start_remote(
.spawn()?,
_ => {
let mut cmd = remote.command_builder();
if sudo {
cmd.arg("sudo");
}
cmd.arg(&tiron_node_file)
let arg = if sudo {
format!("sudo {tiron_node_file}")
} else {
tiron_node_file
};
cmd.arg(&arg)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()?
}
};
Expand Down

0 comments on commit 790b6fd

Please sign in to comment.