diff --git a/control/cli/src/main.rs b/control/cli/src/main.rs index aa6b6a9..917e941 100644 --- a/control/cli/src/main.rs +++ b/control/cli/src/main.rs @@ -141,7 +141,7 @@ fn main() { .arg("-TERM") .arg("-g") .arg(ide_id) - .output().unwrap(); + .output().unwrap(); }, (SYSTEMD_SUBCOMMAND_NAME, Some(system_cmd)) => { let ide_service_name = system_cmd.value_of(IDE_SERVICE_NAME_ARGUMENT_NAME) @@ -149,13 +149,13 @@ fn main() { true => Some(v.to_owned()), _ => Some(format!("{}.service",v)) }).unwrap(); - let unit_folder = system_cmd.value_of(SYSTEMD_UNIT_FOLDER_ARGUMENT_NAME).unwrap(); + let unit_folder = system_cmd.value_of(SYSTEMD_UNIT_FOLDER_ARGUMENT_NAME).unwrap(); let service_file= shellexpand::full(&format!("{}/{}", unit_folder, ide_service_name)) .and_then(|v| Ok(String::from(v))).unwrap(); let clean_service_file= shellexpand::full(&format!("{}/clean-{}", unit_folder, ide_service_name)) .and_then(|v| Ok(String::from(v))).unwrap(); match system_cmd.subcommand() { - (UNINSTALL_SUBCOMMAND_NAME, _ ) => { + (UNINSTALL_SUBCOMMAND_NAME, _ ) => { fs::remove_file(&service_file).expect(&format!("could remove service file {}", service_file)); fs::remove_file(&clean_service_file).expect(&format!("could remove service file {}", clean_service_file)); }, @@ -165,7 +165,7 @@ fn main() { let args = install_cmd.value_of(START_ARGS_ARGUMENT_NAME).unwrap(); let create_service_script = format!("create_service_file.sh {}", ide_service_name); - let nix_expression = format!("with import {{}}; callPackage {} {}", file, args); + let nix_expression = format!("with import {{}}; callPackage {} {}", file, args); Command::new("nix-shell") .stderr(Stdio::null()) .stdout(Stdio::null()) @@ -181,7 +181,7 @@ fn main() { if force { mv_cmd.arg("--force"); } - + mv_cmd.arg(&format!("{}.service", ide_service_name)) .arg(service_file) .output() @@ -198,8 +198,8 @@ fn main() { println!("{}", ide_service_name); }, _ => {} - } - } + } + } _ => println!("-1") }; } diff --git a/server/podman/src/lib.rs b/server/podman/src/lib.rs index f686009..a9a3843 100644 --- a/server/podman/src/lib.rs +++ b/server/podman/src/lib.rs @@ -8,12 +8,23 @@ extern crate serde_json; use nixideserver_lib::*; use rocket::http::Status; -use std::fs; -use std::path::{Path, PathBuf}; -use std::process::Command; -use std::thread; - -// podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh +use std::{ + fs, + io::{ + BufRead, + BufReader, + ErrorKind + }, + path::{ + Path, + PathBuf + }, + process::{ + Command, + Stdio + }, + thread +}; #[derive(Serialize, Deserialize, Clone)] pub enum PodmanState { @@ -256,7 +267,20 @@ fn clean_thread(thread_param: &ThreadRunningParam) { }) .unwrap_or_default(); eng.save().unwrap_or_default(); - // todo delete container + + Command::new("podman") + .arg("stop") + .arg(&thread_param.container.ide_id) + .status() + .map(|_| ()) + .unwrap_or_default(); + + Command::new("podman") + .arg("rm") + .arg(&thread_param.container.ide_id) + .status() + .map(|_| ()) + .unwrap_or_default(); fs::remove_dir_all(&thread_param.ide_folder).unwrap_or_default(); } @@ -276,7 +300,7 @@ fn working_thread(thread_param: &ThreadRunningParam) -> Result<(), Error> { let port = thread_param.container.ide_param.listen_port; let nix_expression = format!("with import {{}}; callPackage /nixide/repo/.nixide/start-ide.nix {{listenPort = {}; projectFolder = \"/nixide/repo\"; }}", port); - let out = Command::new("podman") + let create_container_result = Command::new("podman") .current_dir(ide_folder) .arg("create") .arg("--name") @@ -294,32 +318,39 @@ fn working_thread(thread_param: &ThreadRunningParam) -> Result<(), Error> { .arg("--pure") .arg("--run") .arg("run_ide.sh") - .status().map_err(|_| Error{})?; + .status(); - // let nix_expression = format!("with import {{}}; callPackage {} {}", file, args); - // let cmd = Command::new("nix-shell") - // .stderr(Stdio::null()) - // //.stdout(Stdio::null()) - // .stdout(Stdio::piped()) - // .arg("--expr") - // .arg(nix_expression) - // .arg("--pure") - // .arg("--run") - // .arg(run_script) - // .spawn() - // .expect("start ide command failed"); - // podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh - // .arg("create") - // .arg("--name") - // .arg(format!("{:x}", hash)) - // .arg("-p") - // .arg("3000:3000") - // .arg("docker.io/nixos/nix") - // // .arg(format!("git clone {}", param.clone_url)) - // .output() - // - // String::from_utf8(out.stderr).unwrap() - Ok(()) + match create_container_result { + Ok(s) if s.success() => match Command::new("podman").arg("start").arg(ide_id).status() { + Ok(s) if s.success() => { + let cmd = Command::new("podman") + .stderr(Stdio::piped()) + .stdout(Stdio::piped()) + .arg("logs") + .arg("--follow") + .arg(ide_id) + .spawn() + .expect("start ide command failed"); + + let out = cmd.stdout.ok_or_else(|| std::io::Error::new(ErrorKind::Other,"Could not capture standard output.")).unwrap(); + let reader = BufReader::new(out); + let start_up_log = reader.lines() + .filter_map(|line| line.ok()) + .take_while(|line| !line.find("theia start").is_some()); + start_up_log.for_each(|line| println!("{}", line)); + match eng.find_mut_first_container(&ide_id) { + Some(i) => { + i.state = PodmanState::RunningContainer; + i.ide_state = IdeState::OPENED; + eng.save().map_err(|_| Error {})?; + } + _ => {} + }; + Ok(())}, + _ => Err(Error {}), + }, + _ => Err(Error {}), + } } #[test]