add remove feature

This commit is contained in:
2023-09-25 18:30:14 +02:00
parent 565cb660ee
commit 40385bbae2
9 changed files with 222 additions and 36 deletions

View File

@@ -1,9 +1,11 @@
use bollard::Docker;
use bollard::errors::Error;
use crate::deploy;
use crate::deploy::container::Container;
use crate::conf::storage::MemStorage;
use crate::conf::Instance;
use std::sync::Mutex;
use log::{error};
use log::error;
pub struct Controller {
driver: Docker,
@@ -25,13 +27,22 @@ impl Controller {
}
pub async fn create_container(&self, domain: String, ip: String, image: String) -> String {
match self.load_container(None,domain.clone(),ip.clone(),image.clone()).await {
Ok(c) => {
let ret = c.get_id();
self.storage.lock().unwrap().new_instance(c).unwrap();
ret
let is_stored = self.storage.lock().unwrap().search_instance(domain.clone());
match is_stored {
Some(c) => match c.docker_id {
Some(id) => id,
None => "Container without docker_id".to_string()
},
Err(_e) => "error creating container".to_string(),
None => {
match self.load_container(None,domain.clone(),ip.clone(),image.clone()).await {
Ok(c) => {
log::debug!("poisoned: {}",self.storage.is_poisoned());
self.storage.try_lock().unwrap().new_instance(c.clone()).unwrap();
c.get_id()
},
Err(_e) => "error creating container".to_string(),
}
}
}
}
@@ -39,7 +50,7 @@ impl Controller {
let image = match self.storage.lock().unwrap().id_to_image(instance.image.clone()) {
Ok(i) => i,
Err(e) => {
log::error!("image not found: {}", e);
error!("image not found: {}", e);
return
},
};
@@ -51,7 +62,7 @@ impl Controller {
Ok(c) => {
self.storage.lock().unwrap().loaded_instance(instance, c);
},
Err(e) => log::error!("{}",e),
Err(e) => error!("{}",e),
}
}
@@ -77,7 +88,7 @@ impl Controller {
let data = match self.storage.lock().unwrap().get_instances_db() {
Ok(d) => d,
Err(e) => {
log::error!("instances can't be loaded: {}",e);
error!("instances can't be loaded: {}",e);
return false
},
};
@@ -88,5 +99,44 @@ impl Controller {
return true;
}
//pub async fn stop_container(&self, )
async fn stop_given_container(&self, container:Container) -> Result<String, Error> {
match container.stop(&self.driver).await {
Ok(_i) => Ok(container.get_id()),
Err(e) => Err(e),
}
}
async fn prune_given_container(&self, container:Container) -> Result<String, Error> {
match container.remove(&self.driver).await {
Ok(_i) => Ok(container.get_id()),
Err(e) => Err(e),
}
}
pub async fn stop_container(&self, domain: String) -> Result<String, Error> {
match self.storage.lock().unwrap().stop_instance(domain) {
Some(c) => {
self.stop_given_container(c).await
},
None => Err(Error::DockerResponseServerError {
status_code: 404, message: "container not found".to_string()
}),
}
}
pub async fn delete_container(&self, domain: String) -> Result<String, Error> {
match self.storage.lock().unwrap().remove_instance(domain) {
Some(data) => {
match data.1 {
Some(c) => self.prune_given_container(c).await,
None => Ok(data.0.docker_id),
}
},
None => Err(Error::DockerResponseServerError {
status_code: 404, message: "container not found".to_string()
}),
}
}
}