diff --git a/src/api/routes.rs b/src/api/routes.rs index ddd6db4..fa55213 100644 --- a/src/api/routes.rs +++ b/src/api/routes.rs @@ -2,6 +2,7 @@ use actix_web::{patch, put, delete, web, App, HttpServer, Responder}; use crate::api::error::ReturnedError; use crate::controller::Controller; use crate::deploy::starter; +use crate::deploy::container_options::Options; use serde::Deserialize; #[derive(Debug, Deserialize)] @@ -9,6 +10,8 @@ struct ConainerParams { name: String, ip: String, image: String, + msg: Option, + memory: Option } #[derive(Debug, Deserialize)] @@ -19,7 +22,8 @@ struct ConainerStopParams { #[put("/container")] async fn create_container(controller: web::Data, params: web::Query) -> impl Responder { - controller.create_container(params.name.clone(), params.ip.clone(), params.image.clone()).await + let ops = Options::new(params.memory.clone(), params.msg.clone()); + controller.create_container(params.name.clone(), params.ip.clone(), params.image.clone(), ops).await } #[patch("/container/stop")] @@ -55,3 +59,4 @@ pub async fn start() -> std::io::Result<()> { .run() .await } + diff --git a/src/controller/mod.rs b/src/controller/mod.rs index d07a69a..f26d9f7 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -4,6 +4,7 @@ use crate::deploy; use crate::deploy::container::Container; use crate::database::exposer::MemStorage; use crate::database::instance::Instance; +use crate::deploy::container_options::Options; use std::sync::Mutex; use log::error; @@ -26,7 +27,7 @@ impl Controller { Ok(cont) } - pub async fn create_container(&self, domain: String, ip: String, image: String) -> String { + pub async fn create_container(&self, domain: String, ip: String, image: String, ops: Options) -> String { let is_stored = self.storage.lock().unwrap().search_instance(domain.clone()); match is_stored { Some(c) => match c.docker_id { @@ -34,7 +35,7 @@ impl Controller { None => "Container without docker_id".to_string() }, None => { - match self.load_container(None,domain.clone(),ip.clone(),image.clone()).await { + match self.load_container(None,domain.clone(),ip.clone(),image.clone(),ops).await { Ok(c) => { log::debug!("poisoned: {}",self.storage.is_poisoned()); self.storage.try_lock().unwrap().new_instance(c.clone()).unwrap(); @@ -58,7 +59,8 @@ impl Controller { Some(instance.docker_id.clone()), instance.domain.clone(), instance.ip.clone(), - image).await { + image, + Options::new(None, None)).await { Ok(c) => { self.storage.lock().unwrap().loaded_instance(instance, c); }, @@ -69,7 +71,8 @@ impl Controller { async fn load_container(&self, docker_id: Option, domain: String, ip: String, - image: String) -> Result Result { deploy::container::Container::new(self.driver.clone(), @@ -77,7 +80,8 @@ impl Controller { domain, ip, image, - self.network.clone()).await + self.network.clone(), + ops).await } pub async fn is_started(&self) -> bool { @@ -141,3 +145,4 @@ impl Controller { + diff --git a/src/deploy/container.rs b/src/deploy/container.rs index d5a24e2..e391ff7 100644 --- a/src/deploy/container.rs +++ b/src/deploy/container.rs @@ -10,6 +10,7 @@ use bollard::errors::{Error, Error::IOError}; use std::io::ErrorKind::AlreadyExists; use std::collections::hash_map::HashMap; use crate::deploy::bind; +use crate::deploy::container_options::Options; #[derive(Clone)] pub struct Container { @@ -19,7 +20,8 @@ pub struct Container { pub image: String, pub net: String, pub docker_id: Option, - pub database_id: Option + pub database_id: Option, + ops: Options, } impl Container { @@ -28,7 +30,8 @@ impl Container { name: String, ip: String, image: String, - net: String) -> Result { + net: String, + ops: Options) -> Result { let mut container = Container { name: name, @@ -38,6 +41,7 @@ impl Container { start: false, docker_id: docker_id, database_id: None, + ops: ops, }; match container.start(docker).await { Ok(c) => { @@ -66,7 +70,8 @@ impl Container { create_config(self.name.clone(), self.ip.clone(), self.net.clone(), - self.image.clone())) + self.image.clone(), + self.ops.clone())) .await?.id); false }, @@ -125,7 +130,7 @@ fn create_search_container(name: String) -> ListContainersOptions { } } -fn create_config(name: String, ip: String, net: String, image: String) -> Config { +fn create_config(name: String, ip: String, net: String, image: String, ops: Options) -> Config { let endpoint = EndpointSettings { ipam_config: Some(EndpointIpamConfig{ ipv4_address: Some(ip), @@ -138,7 +143,8 @@ fn create_config(name: String, ip: String, net: String, image: String) -> Config let net = NetworkingConfig { endpoints_config: hash }; - let env = vec!["EULA=TRUE".to_string()]; + let mut env = vec!["EULA=TRUE".to_string()]; + env.append(&mut ops.generate_config()); Config { image: Some(image), env: Some(env), diff --git a/src/deploy/container_options.rs b/src/deploy/container_options.rs new file mode 100644 index 0000000..9e26929 --- /dev/null +++ b/src/deploy/container_options.rs @@ -0,0 +1,37 @@ +#[derive(Clone)] +pub struct Options { + memory: Option, + rolling_logs: bool, + msg: Option +} + +impl Options { + pub fn new(memory: Option, msg: Option) -> Self { + Self { + memory: memory, + rolling_logs: true, + msg: msg + } + } + + pub fn generate_config(&self) -> Vec { + let mut ret = Vec::new(); + if self.rolling_logs { + ret.push("ENABLE_ROLLING_LOGS=true".to_string()); + } + Self::insert_to_vec("MEMORY=", self.memory.clone(), &mut ret); + Self::insert_to_vec("MOTD=", self.msg.clone(), &mut ret); + ret + } + + fn insert_to_vec(variable: &str, value: Option, data: &mut Vec) { + match value { + Some(mut val) => { + val.insert_str(0, variable); + data.push(val); + }, + None => (), + } + } +} + diff --git a/src/deploy/mod.rs b/src/deploy/mod.rs index 3009e85..c3621bf 100644 --- a/src/deploy/mod.rs +++ b/src/deploy/mod.rs @@ -1,4 +1,6 @@ pub mod container; pub mod network; pub mod starter; +pub mod container_options; mod bind; +