add container adding
This commit is contained in:
@@ -23,6 +23,7 @@ use rocket_okapi::{gen::OpenApiGenerator, response::OpenApiResponder};
|
||||
use schemars::JsonSchema;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::cell::RefCell;
|
||||
|
||||
#[derive(Serialize, Deserialize, Hash, JsonSchema, Clone)]
|
||||
pub enum IdeState {
|
||||
@@ -51,15 +52,15 @@ pub struct IdeStateResponse {
|
||||
|
||||
pub trait NixIdeManageServiceEngine {
|
||||
fn fetch_current_ide_state(&self, ide_id: &str) -> Result<IdeState, Status>;
|
||||
fn start_open(&self, ide_id: &str, param: &OpenGitParam) -> Result<IdeState, Status>;
|
||||
fn start_open(&mut self, ide_id: &str, param: &OpenGitParam) -> Result<IdeState, Status>;
|
||||
}
|
||||
|
||||
pub struct NixIdeManageService {
|
||||
eng: Box<dyn NixIdeManageServiceEngine>,
|
||||
eng: RefCell<Box<dyn NixIdeManageServiceEngine>>,
|
||||
}
|
||||
impl NixIdeManageService {
|
||||
pub fn new(engine: Box<dyn NixIdeManageServiceEngine>) -> Self {
|
||||
Self { eng: engine }
|
||||
Self { eng: RefCell::new(engine) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +74,7 @@ impl NixIdeManageServiceEngine for DummyEngine {
|
||||
Ok(IdeState::OPENING)
|
||||
}
|
||||
|
||||
fn start_open(&self, _ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||
fn start_open(&mut self, _ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||
Ok(IdeState::OPENING)
|
||||
}
|
||||
}
|
||||
@@ -144,8 +145,7 @@ pub fn v1_open_inquirer_git(
|
||||
};
|
||||
|
||||
let ide_id = create_ide_id(¶m);
|
||||
|
||||
srv.eng
|
||||
srv.eng.borrow_mut()
|
||||
.start_open(&ide_id, ¶m)
|
||||
.map_err(|_| Error {})
|
||||
.and_then(|starting_result| {
|
||||
@@ -163,7 +163,7 @@ pub fn v1_ide_state(
|
||||
ide_id: String,
|
||||
srv: State<NixIdeManageService>,
|
||||
) -> Result<Json<IdeStateResponse>, status::NotFound<String>> {
|
||||
srv.eng
|
||||
srv.eng.borrow()
|
||||
.fetch_current_ide_state(&ide_id)
|
||||
.and_then(|ide_state| {
|
||||
Ok(Json(IdeStateResponse {
|
||||
|
||||
@@ -10,10 +10,7 @@ use nixideserver_lib::*;
|
||||
use rocket::http::Status;
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{
|
||||
Path,
|
||||
PathBuf
|
||||
};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
// 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
|
||||
|
||||
@@ -29,34 +26,41 @@ pub enum PodmanState {
|
||||
DeletingSources,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct PodmanContainer{
|
||||
state : PodmanState,
|
||||
ide_param : NixIdeServerParam
|
||||
pub struct PodmanContainer {
|
||||
state: PodmanState,
|
||||
ide_param: NixIdeServerParam,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct PodmanContainerList{
|
||||
container : Vec<PodmanContainer>
|
||||
pub struct PodmanContainerList {
|
||||
container: Vec<PodmanContainer>,
|
||||
}
|
||||
|
||||
pub struct PodmanEngine {
|
||||
working_folder: PathBuf,
|
||||
list : PodmanContainerList
|
||||
list: PodmanContainerList,
|
||||
}
|
||||
|
||||
impl PodmanEngine {
|
||||
pub fn new(working_folder: PathBuf) -> Self {
|
||||
let list_path = format!("{}/.podman_containers", &working_folder.display());
|
||||
let list = match read_from_list_file(&list_path) {
|
||||
Ok(p) => p,
|
||||
_ => PodmanContainerList{container: Vec::new()}
|
||||
};
|
||||
let list = match read_from_list_file(&list_path) {
|
||||
Ok(p) => p,
|
||||
_ => PodmanContainerList {
|
||||
container: Vec::new(),
|
||||
},
|
||||
};
|
||||
|
||||
Self {
|
||||
working_folder,
|
||||
list
|
||||
list,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<(), std::io::Error> {
|
||||
let json_string = serde_json::to_string_pretty(&self.list).unwrap();// .map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData))?;
|
||||
fs::write(&format!("{}/.podman_containers", &self.working_folder.display()), json_string)
|
||||
}
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PodmanIdeStatus {
|
||||
@@ -65,27 +69,59 @@ struct PodmanIdeStatus {
|
||||
|
||||
impl NixIdeManageServiceEngine for PodmanEngine {
|
||||
fn fetch_current_ide_state(&self, ide_id: &str) -> Result<IdeState, Status> {
|
||||
let state_file = create_state_file_path( &self.working_folder, ide_id);
|
||||
let mut file = fs::File::open(state_file).map_err(|_|Status::new(500, "could not open state file"))?;
|
||||
let state_file = create_state_file_path(&self.working_folder, ide_id);
|
||||
let mut file = fs::File::open(state_file)
|
||||
.map_err(|_| Status::new(500, "could not open state file"))?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).map_err(|_| Status::NotFound)?;
|
||||
let ide_state = read_ide_state(&contents).map_err(|_|Status::new(500, "unexpected json string"))?;
|
||||
file.read_to_string(&mut contents)
|
||||
.map_err(|_| Status::NotFound)?;
|
||||
let ide_state =
|
||||
read_ide_state(&contents).map_err(|_| Status::new(500, "unexpected json string"))?;
|
||||
Ok(ide_state)
|
||||
}
|
||||
|
||||
fn start_open(&self, ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||
let ide_folder = create_ide_folder_path( &self.working_folder, ide_id);
|
||||
fn start_open(&mut self, ide_id: &str, _param: &OpenGitParam) -> Result<IdeState, Status> {
|
||||
let ide_folder = create_ide_folder_path(&self.working_folder, ide_id);
|
||||
let state_file = create_state_file_path_from_ide_folder(&ide_folder);
|
||||
fs::create_dir_all(&ide_folder)
|
||||
.and_then(|_| fs::OpenOptions::new().create(true).write(true).read(true).open(state_file))
|
||||
.and_then(|_| {
|
||||
fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.read(true)
|
||||
.open(state_file)
|
||||
})
|
||||
.and_then(|mut file| {
|
||||
let mut buffer = String::new();
|
||||
match file.read_to_string(&mut buffer) {
|
||||
Ok(s) if s > 0 => {
|
||||
read_ide_state(&buffer).map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData))
|
||||
},
|
||||
Ok(s) if s > 0 => read_ide_state(&buffer)
|
||||
.map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData)),
|
||||
_ => {
|
||||
let status =PodmanIdeStatus{ide_state: IdeState::OPENING};
|
||||
let status = PodmanIdeStatus {
|
||||
ide_state: IdeState::OPENING,
|
||||
};
|
||||
let listen_port = self
|
||||
.list
|
||||
.container
|
||||
.iter()
|
||||
.max_by_key(|i| i.ide_param.listen_port)
|
||||
.and_then(|i| Some(i.ide_param.listen_port + 1))
|
||||
.or(Some(3000))
|
||||
.unwrap();
|
||||
let ide_param = NixIdeServerParam {
|
||||
listen_address: default_listen_address(),
|
||||
listen_port: listen_port,
|
||||
app_folder: default_app_folder(),
|
||||
project_folder: default_project_folder(),
|
||||
working_folder: default_working_folder(),
|
||||
};
|
||||
|
||||
let container = PodmanContainer {
|
||||
state: PodmanState::FetchingSource,
|
||||
ide_param,
|
||||
};
|
||||
self.list.container.push(container);
|
||||
self.save()?;
|
||||
file.write(serde_json::to_string_pretty(&status)?.as_bytes())?;
|
||||
Ok(status.ide_state)
|
||||
}
|
||||
@@ -121,6 +157,17 @@ pub struct NixIdeServerParam {
|
||||
working_folder: String,
|
||||
}
|
||||
|
||||
impl Default for NixIdeServerParam {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen_address: default_listen_address(),
|
||||
listen_port: default_listen_port(),
|
||||
app_folder: default_app_folder(),
|
||||
project_folder: default_project_folder(),
|
||||
working_folder: default_working_folder(),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn default_listen_address() -> String {
|
||||
"0.0.0.0".to_owned()
|
||||
}
|
||||
@@ -141,30 +188,28 @@ fn default_working_folder() -> String {
|
||||
"$PWD".to_owned()
|
||||
}
|
||||
|
||||
|
||||
fn create_state_file_path(working_folder_path : &Path, ide_id: &str) -> String {
|
||||
fn create_state_file_path(working_folder_path: &Path, ide_id: &str) -> String {
|
||||
let ide_folder = format!("{}/{}", working_folder_path.display(), ide_id);
|
||||
create_state_file_path_from_ide_folder(&ide_folder)
|
||||
}
|
||||
|
||||
fn create_state_file_path_from_ide_folder(ide_folder : &str) -> String {
|
||||
fn create_state_file_path_from_ide_folder(ide_folder: &str) -> String {
|
||||
format!("{}/.ide_state", ide_folder)
|
||||
}
|
||||
|
||||
fn create_ide_folder_path(working_folder_path : &Path, ide_id: &str) -> String {
|
||||
fn create_ide_folder_path(working_folder_path: &Path, ide_id: &str) -> String {
|
||||
format!("{}/{}", working_folder_path.display(), ide_id)
|
||||
}
|
||||
|
||||
fn read_ide_state(json_string :&str) -> Result<IdeState,serde_json::Error>
|
||||
{
|
||||
fn read_ide_state(json_string: &str) -> Result<IdeState, serde_json::Error> {
|
||||
let status: PodmanIdeStatus = serde_json::from_str(json_string)?;
|
||||
Ok(status.ide_state)
|
||||
}
|
||||
|
||||
fn read_from_list_file(path : &str) -> Result<PodmanContainerList, std::io::Error> {
|
||||
match fs::read_to_string(path)
|
||||
{
|
||||
Ok(json_string) => serde_json::from_str::<PodmanContainerList>(&json_string).map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData)),
|
||||
Err(e) => Err(e)
|
||||
fn read_from_list_file(path: &str) -> Result<PodmanContainerList, std::io::Error> {
|
||||
match fs::read_to_string(path) {
|
||||
Ok(json_string) => serde_json::from_str::<PodmanContainerList>(&json_string)
|
||||
.map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData)),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user