Skip to content

Commit 8f2feae

Browse files
committed
add entrypoint/init-system functionality
1 parent 3035ba8 commit 8f2feae

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

Cargo.lock

Lines changed: 23 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ tui-realm-treeview = "3.0.0"
7474
aws-sdk-s3 = "1.115.0"
7575
toml_edit = "0.23.9"
7676
clap_complete = "4.5.61"
77+
pid1 = "0.1.5"
78+
rust_supervisor = "0.2.0"
7779

7880
[build-dependencies]
7981
anyhow = "1.0.90"
80-
8182
vergen-gix = { version = "1.0.2", features = ["build", "cargo"] }
8283

8384
[dev-dependencies]

coman/src/cli.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::{error::Error, path::PathBuf};
1+
use std::{error::Error, path::PathBuf, process::Stdio, thread, time::Duration};
22

33
use clap::{Args, Command, Parser, Subcommand, ValueHint, builder::TypedValueParser};
44
use clap_complete::{Generator, Shell, generate};
55
use color_eyre::Result;
6+
use pid1::Pid1Settings;
7+
use rust_supervisor::{ChildType, Supervisor, SupervisorConfig};
68
use strum::VariantNames;
79

810
use crate::{
@@ -63,6 +65,11 @@ pub enum CliCommands {
6365
#[clap(value_enum)]
6466
generator: Shell,
6567
},
68+
#[clap(about = "Execute a process/command through coman, with additional monitoring and side processes")]
69+
Exec {
70+
#[clap(trailing_var_arg = true, help = "The command to run", value_hint=ValueHint::Other)]
71+
command: Vec<String>,
72+
},
6673
}
6774

6875
#[derive(Subcommand, Debug)]
@@ -372,3 +379,41 @@ fn is_bare_string(value_str: &str) -> bool {
372379
pub fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
373380
generate(generator, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
374381
}
382+
pub(crate) async fn cli_exec_command(command: Vec<String>) -> Result<()> {
383+
// Pid1 takes care of proper terminating of processes and signal handling when running in a container
384+
Pid1Settings::new()
385+
.enable_log(true)
386+
.timeout(Duration::from_secs(2))
387+
.launch()
388+
.expect("Launch failed");
389+
390+
let mut supervisor = Supervisor::new(SupervisorConfig::default());
391+
supervisor.add_process("iroh-ssh", ChildType::Permanent, || {
392+
thread::spawn(|| {
393+
// iroh!
394+
})
395+
});
396+
supervisor.add_process("main-process", ChildType::Temporary, move || {
397+
let command = command.clone();
398+
thread::spawn(move || {
399+
let mut child = std::process::Command::new(command[0].clone())
400+
.args(&command[1..])
401+
.spawn()
402+
.expect("Failed to start compute job");
403+
child.wait().expect("Failed to wait on compute job");
404+
})
405+
});
406+
407+
let supervisor = supervisor.start_monitoring();
408+
loop {
409+
thread::sleep(Duration::from_secs(1));
410+
411+
if let Some(state) = supervisor.get_process_state("main-process") {
412+
match state {
413+
rust_supervisor::ProcessState::Failed | rust_supervisor::ProcessState::Stopped => break,
414+
_ => {}
415+
}
416+
}
417+
}
418+
Ok(())
419+
}

coman/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
model::Model,
1818
user_events::{CscsEvent, FileEvent, StatusEvent, UserEvent},
1919
},
20-
cli::{Cli, get_config, print_completions, set_config, version},
20+
cli::{Cli, cli_exec_command, get_config, print_completions, set_config, version},
2121
components::{
2222
file_tree::FileTree, global_listener::GlobalListener, status_bar::StatusBar, toolbar::Toolbar,
2323
workload_list::WorkloadList,
@@ -135,6 +135,7 @@ async fn main() -> Result<()> {
135135
},
136136
},
137137
cli::CliCommands::Init { destination, name } => Config::create_project_config(destination, name)?,
138+
cli::CliCommands::Exec { command } => cli_exec_command(command).await?,
138139
},
139140
None => run_tui(args.tick_rate)?,
140141
}

0 commit comments

Comments
 (0)