start container

This commit is contained in:
stubbfelnewpc
2020-07-08 23:54:30 +02:00
parent df4befae64
commit 558ec03db6
2 changed files with 71 additions and 40 deletions

View File

@@ -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 <nixpkgs> {{}}; 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 <nixpkgs> {{}}; 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]