diff --git a/Cargo.lock b/Cargo.lock index 33181f4..a33e8e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,7 +457,7 @@ dependencies = [ [[package]] name = "illa" -version = "1.1.0" +version = "1.2.0" dependencies = [ "anyhow", "bollard", diff --git a/Cargo.toml b/Cargo.toml index 0201810..0880244 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "illa" -version = "1.1.0" +version = "1.2.0" authors = ["ILLA "] edition = "2021" diff --git a/src/command/deploy.rs b/src/command/deploy.rs index a5b1a6b..a308d17 100644 --- a/src/command/deploy.rs +++ b/src/command/deploy.rs @@ -2,7 +2,7 @@ use crate::{command::*, result::Result}; use anyhow::Ok; use bollard::container::{Config, CreateContainerOptions, LogsOptions, StartContainerOptions}; use bollard::image::CreateImageOptions; -use bollard::models::HostConfig; +use bollard::models::{HostConfig, Mount, MountTypeEnum}; use bollard::service::PortBinding; use bollard::{service::CreateImageInfo, Docker}; use clap::{ArgAction::SetTrue, ArgGroup, Args}; @@ -14,7 +14,7 @@ use std::fmt::format; use std::hash::Hash; use std::thread; use std::time::{Duration, Instant}; -use std::{process, string}; +use std::{env, process, string}; use uuid::Uuid; const ILLA_BUILDER_IMAGE: &str = "illasoft/illa-builder"; @@ -185,6 +185,16 @@ async fn deploy_self_host( host_ip: Some("0.0.0.0".to_string()), }]), ); + if env::consts::OS == "macos" { + utils::local_bind_init(); + } + let mounts = vec![Mount { + target: Some("/var/lib/postgresql/data".to_string()), + source: Some("/tmp/illa-data".to_string()), + typ: Some(MountTypeEnum::BIND), + read_only: Some(false), + ..Default::default() + }]; let builder_config = Config { image: Some(builder_image), @@ -192,11 +202,7 @@ async fn deploy_self_host( labels: Some(builder_labels), host_config: Some(HostConfig { port_bindings: Some(builder_port_bindings), - // TODO: based on different operating system to bind host path and volume - // binds: Some(vec![format!( - // "{}:{}", - // "/tmp/illa-data", "/var/lib/postgresql/data" - // )]), + mounts: Some(mounts), ..Default::default() }), ..Default::default() diff --git a/src/command/mod.rs b/src/command/mod.rs index fff3390..2af6eb3 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -7,3 +7,4 @@ pub mod restart; pub mod stop; pub mod ui; pub mod update; +pub mod utils; diff --git a/src/command/remove.rs b/src/command/remove.rs index 58ecf4e..cf9140d 100644 --- a/src/command/remove.rs +++ b/src/command/remove.rs @@ -26,13 +26,17 @@ pub struct Cmd { /// If the ILLA Builder is running, kill it before removing it #[clap(short = 'f', long = "force", action = SetTrue)] force: bool, + + /// Remove the persistent data of ILLA Builder + #[clap(short = 'd', long = "data", action = SetTrue)] + data: bool, } impl Cmd { pub async fn run(&self) -> Result { let (self_host, cloud) = (self.self_host, self.cloud); match (self_host, cloud) { - (true, _) => remove_local(self.force).await?, + (true, _) => remove_local(self.force, self.data).await?, (_, true) => println!("{} Looking forward to onboarding you!", ui::emoji::DIAMOND), _ => unreachable!(), }; @@ -40,9 +44,13 @@ impl Cmd { } } -async fn remove_local(is_force: bool) -> Result { +async fn remove_local(is_force: bool, data: bool) -> Result { println!("{} Trying to remove the ILLA Builder...", ui::emoji::BUILD); + if data { + utils::local_bind_delete(); + } + let _docker = Docker::connect_with_local_defaults().unwrap(); if (_docker.ping().await).is_err() { println!( diff --git a/src/command/update.rs b/src/command/update.rs index 78b2d8d..de01841 100644 --- a/src/command/update.rs +++ b/src/command/update.rs @@ -6,7 +6,7 @@ use bollard::{ RemoveContainerOptions, StartContainerOptions, StatsOptions, }, image::CreateImageOptions, - models::HostConfig, + models::{HostConfig, Mount, MountTypeEnum}, Docker, }; use clap::{builder, ArgAction::SetTrue, ArgGroup, Args}; @@ -101,6 +101,14 @@ async fn update_local(progress_style: ProgressStyle) -> Result { builder_env_cp[1].as_str(), builder_env_cp[2].as_str(), ]; + utils::local_bind_init(); + let mounts = vec![Mount { + target: Some("/var/lib/postgresql/data".to_string()), + source: Some("/tmp/illa-data".to_string()), + typ: Some(MountTypeEnum::BIND), + read_only: Some(false), + ..Default::default() + }]; let mut builder_labels = HashMap::new(); builder_labels.insert("maintainer", "opensource@illasoft.com"); builder_labels.insert("license", "Apache-2.0"); @@ -189,11 +197,7 @@ async fn update_local(progress_style: ProgressStyle) -> Result { labels: Some(builder_labels), host_config: Some(HostConfig { port_bindings: builder_port_bindings.clone(), - // TODO: based on different operating system to bind host path and volume - // binds: Some(vec![format!( - // "{}:{}", - // "/tmp/illa-data", "/var/lib/postgresql/data" - // )]), + mounts: Some(mounts), ..Default::default() }), ..Default::default() diff --git a/src/command/utils.rs b/src/command/utils.rs new file mode 100644 index 0000000..2af875d --- /dev/null +++ b/src/command/utils.rs @@ -0,0 +1,29 @@ +use crate::{command::*, result::Result}; +use anyhow::Ok; +use std::{env, fs}; + +#[cfg(target_os = "macos")] +pub fn local_bind_init() -> Result { + use std::os::unix::fs::PermissionsExt; + fs::create_dir_all("/tmp/illa-data"); + let mut perms = fs::metadata("/tmp/illa-data")?.permissions(); + perms.set_mode(0o777); + fs::set_permissions("/tmp/illa-data", perms); + + Ok(()) +} + +#[cfg(target_os = "windows")] +pub fn local_bind_init() -> Result { + Ok(()) +} + +#[cfg(target_os = "linux")] +pub fn local_bind_init() -> Result { + Ok(()) +} + +pub fn local_bind_delete() -> Result { + fs::remove_dir_all("/tmp/illa-data"); + Ok(()) +}