Skip to content

Commit

Permalink
feat(cli): start and stop commands
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 14, 2022
1 parent 3316a53 commit 0cc13b1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
4 changes: 4 additions & 0 deletions iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ tonic = "0.8"
git-version = "0.3.5"
iroh-metrics = { path = "../iroh-metrics", default-features = false, features = ["rpc-grpc"] }
iroh-api = { path = "../iroh-api"}
iroh-util = { path = "../iroh-util"}
fork = "0.1"
which = "4.3.0"
sysinfo = "0.26.4"

[dev-dependencies]
trycmd = "0.13.7"
Expand Down
77 changes: 77 additions & 0 deletions iroh/src/daemon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use anyhow::{anyhow, Result};
use fork::{daemon, Fork};
use iroh_util::lock::ProgramLock;
use std::{collections::HashSet, path::PathBuf, process::Command};
use sysinfo::{ProcessExt, SystemExt};

pub struct DaemonDetails {
pub bin_paths: Vec<PathBuf>,
}

// TODO(b5) - this leaves a stray "iroh start" process running on my machine
// TODO(b5) - have start spin up iroh cloud services for now until iroh-one RPC api is figured out
pub fn start() -> Result<DaemonDetails> {
// check for any running iroh services
let locks = existing_daemon_locks()?;
if locks.contains("iroh-one") {
return Err(anyhow!("iroh-one is already running"));
}
println!("{:?}", locks);

// ensure iroh-one exists
let iroh_one_path = which::which("iroh-one")?;

// start iroh
if let Ok(Fork::Child) = daemon(false, false) {
Command::new("iroh-one")
.output()
.expect("failed to execute process");
}

// TODO - confirm communication with RPC API

Ok(DaemonDetails {
bin_paths: vec![iroh_one_path],
})
}

fn existing_daemon_locks() -> Result<HashSet<&'static str>> {
let mut locks = HashSet::new();
let locks = vec!["iroh-one", "iroh-gateway", "iroh-p2p", "iroh-store"]
.iter()
.fold(&mut locks, |accum, program| {
ProgramLock::new(program)
.unwrap()
.is_locked()
.then(|| accum.insert(program.to_owned()));

accum
});

Ok(locks.to_owned())
}

// TODO(b5) - in an ideal world the lock files would contain PIDs of daemon processes
pub fn stop() -> Result<HashSet<&'static str>> {
let mut system = sysinfo::System::new();
system.refresh_all();

let mut locks = HashSet::new();
let locks = vec!["iroh-one", "iroh-gateway", "iroh-p2p", "iroh-store"]
.iter()
.fold(&mut locks, |accum, program| {
for p in system.processes_by_name(program) {
println!("stopping process {} with pid {}", p.name(), p.pid());
let mut kill = Command::new("kill")
.args(["-s", "SIGKILL", p.pid().to_string().as_str()])
.spawn()
.unwrap();

kill.wait().unwrap();
}

accum
});

Ok(locks.to_owned())
}
1 change: 1 addition & 0 deletions iroh/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod daemon;
#[cfg(feature = "testing")]
mod fixture;
pub mod metrics;
Expand Down
30 changes: 22 additions & 8 deletions iroh/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::path::PathBuf;

use crate::daemon;
#[cfg(feature = "testing")]
use crate::fixture::get_fixture_api;
use crate::p2p::{run_command as run_p2p_command, P2p};
Expand Down Expand Up @@ -31,14 +32,6 @@ impl Cli {

#[derive(Subcommand, Debug, Clone)]
enum Commands {
// status checks the health of the different processes
#[clap(about = "Check the health of the different iroh processes.")]
Status {
#[clap(short, long)]
/// when true, updates the status table whenever a change in a process's status occurs
watch: bool,
},
P2p(P2p),
#[clap(about = "break up a file into block and provide those blocks on the ipfs network")]
Add {
path: PathBuf,
Expand All @@ -56,6 +49,19 @@ enum Commands {
/// filesystem path to write to. Defaults to CID
output: Option<PathBuf>,
},
P2p(P2p),
//
#[clap(about = "Start a long running iroh process")]
Start {},
// status checks the health of the different processes
#[clap(about = "Check the health of the different iroh processes.")]
Status {
#[clap(short, long)]
/// when true, updates the status table whenever a change in a process's status occurs
watch: bool,
},
#[clap(about = "Stop all local iroh services")]
Stop {},
}

impl Cli {
Expand Down Expand Up @@ -122,6 +128,14 @@ impl Cli {
.unwrap_or_else(|| PathBuf::from(&cid.to_string()));
println!("Saving file(s) to {}", real_output.to_str().unwrap());
}
Commands::Start {} => {
daemon::start().unwrap();
println!("started iroh");
}
Commands::Stop {} => {
daemon::stop().unwrap();
println!("stopped iroh");
}
};

Ok(())
Expand Down

0 comments on commit 0cc13b1

Please sign in to comment.