diff --git a/Cargo.lock b/Cargo.lock index 3554748..f13cd78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1011,6 +1011,7 @@ dependencies = [ "log", "rusqlite", "serde", + "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 2380de2..f694bdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ env_logger = "*" derive_more = "*" log = "*" serde = "*" +serde_json = "*" fallible-iterator = "*" [target.x86_64-unknown-linux-gnu] diff --git a/src/database/direct_access.rs b/src/database/direct_access.rs index 985bfac..47bb3c0 100644 --- a/src/database/direct_access.rs +++ b/src/database/direct_access.rs @@ -1,6 +1,7 @@ use rusqlite::{Connection, Result}; use fallible_iterator::FallibleIterator; use crate::database::instance::Instance; +use crate::deploy::container_options::Options; const PATH: &str = "mdeploy.db"; @@ -35,6 +36,7 @@ impl InstanceStorage { docker_id TEXT, ip TEXT, domain TEXT, + options TEXT, image INTEGER, FOREIGN KEY(image) REFERENCES images(id)); COMMIT;" @@ -48,14 +50,15 @@ impl InstanceStorage { } pub fn load_instances(&mut self) -> Result> { - let mut stmt = self.con.prepare("select id, docker_id, ip, domain, image from l_instances")?; + let mut stmt = self.con.prepare("select id, docker_id, ip, domain, options, image from l_instances")?; let mut rows = stmt.query([])?; rows.map(|r| Ok(Instance{ id: r.get::<_, i64>(0)?, docker_id: r.get::<_, String>(1)?, ip: r.get::<_, String>(2)?, domain: r.get::<_, String>(3)?, - image: r.get::<_, i64>(4)?, + ops: Options::new_from_json(r.get::<_, String>(4)?), + image: r.get::<_, i64>(5)?, })).collect() } @@ -89,18 +92,20 @@ impl InstanceStorage { }) } - pub fn new_instance(&mut self, docker_id: String, ip: String, domain: String, image: i64) -> Result{ + pub fn new_instance(&mut self, docker_id: String, ip: String, domain: String, ops: Options, image: i64) -> Result{ //let image_id = self.create_or_get_image_id(image.clone(), 0)?; - let mut stmt = self.con.prepare("INSERT INTO l_instances(docker_id, ip, domain, image) values(?1, ?2, ?3, ?4)")?; + let mut stmt = self.con.prepare("INSERT INTO l_instances(docker_id, ip, domain, options, image) values(?1, ?2, ?3, ?4, ?5)")?; stmt.execute([docker_id.clone(), ip.clone(), domain.clone(), + ops.to_json(), image.to_string()])?; Ok(Instance { id: image, docker_id: docker_id, ip: ip, domain: domain, + ops: ops, image: image, }) } diff --git a/src/database/exposer.rs b/src/database/exposer.rs index b991aca..3548d77 100644 --- a/src/database/exposer.rs +++ b/src/database/exposer.rs @@ -44,6 +44,7 @@ impl MemStorage { container.get_id(), container.ip.clone(), container.name.clone(), + container.ops.clone(), image_id)?; self.containers.push((ins, Some(container))); Ok(()) diff --git a/src/database/instance.rs b/src/database/instance.rs index 4010f86..0ef666d 100644 --- a/src/database/instance.rs +++ b/src/database/instance.rs @@ -1,3 +1,5 @@ +use crate::deploy::container_options::Options; + #[derive(Clone)] pub struct Instance { pub id: i64, @@ -5,4 +7,5 @@ pub struct Instance { pub ip: String, pub domain: String, pub image: i64, + pub ops: Options, } diff --git a/src/deploy/container.rs b/src/deploy/container.rs index e391ff7..3a976fa 100644 --- a/src/deploy/container.rs +++ b/src/deploy/container.rs @@ -21,7 +21,7 @@ pub struct Container { pub net: String, pub docker_id: Option, pub database_id: Option, - ops: Options, + pub ops: Options, } impl Container { diff --git a/src/deploy/container_options.rs b/src/deploy/container_options.rs index 9e26929..813a7c6 100644 --- a/src/deploy/container_options.rs +++ b/src/deploy/container_options.rs @@ -1,4 +1,7 @@ -#[derive(Clone)] +use serde::{Deserialize, Serialize}; +use serde_json::Result; + +#[derive(Clone, Serialize, Deserialize)] pub struct Options { memory: Option, rolling_logs: bool, @@ -14,6 +17,21 @@ impl Options { } } + pub fn new_from_json(json: String) -> Self { + match serde_json::from_str(&json) { + Ok(r) => r, + Err(e) => { + log::warn!("Error deserialicing:{}",e); + log::warn!("insert default values"); + Self { + memory: None, + msg: None, + rolling_logs: true, + } + } + } + } + pub fn generate_config(&self) -> Vec { let mut ret = Vec::new(); if self.rolling_logs { @@ -33,5 +51,15 @@ impl Options { None => (), } } + + pub fn to_json(&self) -> String { + match serde_json::to_string(self) { + Ok(j) => j, + Err(e) => { + log::warn!("Error serialicing:{}",e); + String::from("") + } + } + } }