Skip to content

Commit

Permalink
Merge pull request #36 from illa-family/develop
Browse files Browse the repository at this point in the history
feat!: add mount local dir functionality and add `--data` arg for subcommand `remove`
  • Loading branch information
naj1n authored Sep 23, 2022
2 parents 60265a8 + 8699d7c commit 250707e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "illa"
version = "1.1.0"
version = "1.2.0"
authors = ["ILLA <[email protected]>"]
edition = "2021"

Expand Down
20 changes: 13 additions & 7 deletions src/command/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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";
Expand Down Expand Up @@ -185,18 +185,24 @@ 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),
env: Some(builder_env),
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()
Expand Down
1 change: 1 addition & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod restart;
pub mod stop;
pub mod ui;
pub mod update;
pub mod utils;
12 changes: 10 additions & 2 deletions src/command/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,31 @@ 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!(),
};
Ok(())
}
}

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!(
Expand Down
16 changes: 10 additions & 6 deletions src/command/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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", "[email protected]");
builder_labels.insert("license", "Apache-2.0");
Expand Down Expand Up @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 250707e

Please sign in to comment.