Skip to content
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

fix(iroh): do not remove the rpc lockfile if an iroh node is already running #2013

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions iroh/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{bail, ensure, Context, Result};
use clap::Parser;
use tokio_util::task::LocalPoolHandle;

use crate::config::{iroh_data_root, ConsoleEnv, NodeConfig};
use crate::config::{ConsoleEnv, NodeConfig};

use self::blob::{BlobAddOptions, BlobSource};
use self::rpc::{RpcCommands, RpcStatus};
Expand Down Expand Up @@ -84,37 +84,39 @@ pub enum Commands {
}

impl Cli {
pub async fn run(self, rt: LocalPoolHandle) -> Result<()> {
pub async fn run(self, rt: LocalPoolHandle, data_dir: &Path) -> Result<()> {
match self.command {
Commands::Console => {
let env = ConsoleEnv::for_console()?;
let env = ConsoleEnv::for_console(data_dir)?;
if self.start {
let config = NodeConfig::from_env(self.config.as_deref())?;
start::run_with_command(
&rt,
&config,
data_dir,
RunType::SingleCommandNoAbort,
|iroh| async move { console::run(&iroh, &env).await },
)
.await
} else {
let iroh = iroh_quic_connect().await.context("rpc connect")?;
let iroh = iroh_quic_connect(data_dir).await.context("rpc connect")?;
console::run(&iroh, &env).await
}
}
Commands::Rpc(command) => {
let env = ConsoleEnv::for_cli()?;
let env = ConsoleEnv::for_cli(data_dir)?;
if self.start {
let config = NodeConfig::from_env(self.config.as_deref())?;
start::run_with_command(
&rt,
&config,
data_dir,
RunType::SingleCommandAbortable,
|iroh| async move { command.run(&iroh, &env).await },
)
.await
} else {
let iroh = iroh_quic_connect().await.context("rpc connect")?;
let iroh = iroh_quic_connect(data_dir).await.context("rpc connect")?;
command.run(&iroh, &env).await
}
}
Expand All @@ -134,12 +136,18 @@ impl Cli {
options: add_options,
});

start::run_with_command(&rt, &config, RunType::UntilStopped, |client| async move {
match add_command {
None => Ok(()),
Some(command) => command.run(&client).await,
}
})
start::run_with_command(
&rt,
&config,
data_dir,
RunType::UntilStopped,
|client| async move {
match add_command {
None => Ok(()),
Some(command) => command.run(&client).await,
}
},
)
.await
}
Commands::Doctor { command } => {
Expand All @@ -150,8 +158,7 @@ impl Cli {
}
}

async fn iroh_quic_connect() -> Result<iroh::client::quic::Iroh> {
let root = iroh_data_root()?;
async fn iroh_quic_connect(root: &Path) -> Result<iroh::client::quic::Iroh> {
let rpc_status = RpcStatus::load(root).await?;
match rpc_status {
RpcStatus::Stopped => {
Expand Down
2 changes: 1 addition & 1 deletion iroh/src/commands/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Repl {
pub fn run(self) -> anyhow::Result<()> {
let mut rl =
DefaultEditor::with_config(Config::builder().check_cursor_position(true).build())?;
let history_path = ConsolePaths::History.with_env()?;
let history_path = ConsolePaths::History.with_root(self.env.iroh_data_dir());
rl.load_history(&history_path).ok();
loop {
// prepare a channel to receive a signal from the main thread when a command completed
Expand Down
8 changes: 3 additions & 5 deletions iroh/src/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,17 +947,15 @@ mod tests {

let data_dir = tempfile::tempdir()?;

// run iroh node in the background, as if running `iroh start`
std::env::set_var("IROH_DATA_DIR", data_dir.path().as_os_str());
let lp = tokio_util::task::LocalPoolHandle::new(1);
let node = crate::commands::start::start_node(&lp, None).await?;
let node = crate::commands::start::start_node(&lp, data_dir.path(), None).await?;
let client = node.client();
let doc = client.docs.create().await.context("doc create")?;
let author = client.authors.create().await.context("author create")?;

// set up command, getting iroh node
let cli = ConsoleEnv::for_console().context("ConsoleEnv")?;
let iroh = crate::commands::iroh_quic_connect()
let cli = ConsoleEnv::for_console(data_dir.path()).context("ConsoleEnv")?;
let iroh = crate::commands::iroh_quic_connect(data_dir.path())
.await
.context("rpc connect")?;

Expand Down
4 changes: 2 additions & 2 deletions iroh/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
time::{Duration, Instant},
};

use crate::config::{path_with_env, NodeConfig};
use crate::config::{iroh_data_root, NodeConfig};

use anyhow::Context;
use clap::Subcommand;
Expand Down Expand Up @@ -859,7 +859,7 @@ fn create_secret_key(secret_key: SecretKeyOption) -> anyhow::Result<SecretKey> {
SecretKey::try_from(&bytes[..])?
}
SecretKeyOption::Local => {
let path = path_with_env(IrohPaths::SecretKey)?;
let path = IrohPaths::SecretKey.with_root(iroh_data_root()?);
if path.exists() {
let bytes = std::fs::read(&path)?;
SecretKey::try_from_openssh(bytes)?
Expand Down
Loading
Loading