Skip to content

Commit

Permalink
Merge branch 'nightly' into release/nightly/bin/ncdns/0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s authored Nov 2, 2023
2 parents d33d87d + fa4632a commit a48a525
Show file tree
Hide file tree
Showing 43 changed files with 1,143 additions and 667 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

38 changes: 21 additions & 17 deletions bin/nanocl/src/commands/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ async fn exec_cargo_create(
) -> IoResult<()> {
let client = &cli_conf.client;
let cargo = opts.clone().into();
let item = client.create_cargo(&cargo, args.namespace.clone()).await?;
let item = client
.create_cargo(&cargo, args.namespace.as_deref())
.await?;
println!("{}", &item.key);
Ok(())
}
Expand Down Expand Up @@ -82,7 +84,7 @@ async fn exec_cargo_rm(
force: Some(opts.force),
};
for name in &opts.names {
client.delete_cargo(name, &query).await?;
client.delete_cargo(name, Some(&query)).await?;
}
Ok(())
}
Expand All @@ -109,7 +111,7 @@ async fn exec_cargo_ls(
opts: &CargoListOpts,
) -> IoResult<()> {
let client = &cli_conf.client;
let items = client.list_cargo(args.namespace.clone()).await?;
let items = client.list_cargo(args.namespace.as_deref()).await?;
let rows = items
.into_iter()
.map(CargoRow::from)
Expand Down Expand Up @@ -150,7 +152,7 @@ async fn exec_cargo_start(
) -> IoResult<()> {
let client = &cli_conf.client;
client
.start_cargo(&opts.name, args.namespace.clone())
.start_cargo(&opts.name, args.namespace.as_deref())
.await?;
Ok(())
}
Expand Down Expand Up @@ -178,7 +180,7 @@ async fn exec_cargo_stop(
) -> IoResult<()> {
let client = &cli_conf.client;
for name in &opts.names {
client.stop_cargo(name, args.namespace.clone()).await?;
client.stop_cargo(name, args.namespace.as_deref()).await?;
}
Ok(())
}
Expand Down Expand Up @@ -206,7 +208,9 @@ async fn exec_cargo_restart(
) -> IoResult<()> {
let client = &cli_conf.client;
for name in &opts.names {
client.restart_cargo(name, args.namespace.clone()).await?;
client
.restart_cargo(name, args.namespace.as_deref())
.await?;

Check warning on line 213 in bin/nanocl/src/commands/cargo.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/cargo.rs#L211-L213

Added lines #L211 - L213 were not covered by tests
}
Ok(())
}
Expand All @@ -233,9 +237,8 @@ async fn exec_cargo_patch(
opts: &CargoPatchOpts,
) -> IoResult<()> {
let client = &cli_conf.client;
let cargo = opts.clone().into();
client
.patch_cargo(&opts.name, cargo, args.namespace.clone())
.patch_cargo(&opts.name, &opts.clone().into(), args.namespace.as_deref())
.await?;
Ok(())
}
Expand Down Expand Up @@ -263,7 +266,7 @@ async fn exec_cargo_inspect(
) -> IoResult<()> {
let client = &cli_conf.client;
let cargo = client
.inspect_cargo(&opts.name, args.namespace.clone())
.inspect_cargo(&opts.name, args.namespace.as_deref())
.await?;
let display = opts
.display
Expand Down Expand Up @@ -297,12 +300,12 @@ async fn exec_cargo_exec(
let client = &cli_conf.client;
let exec: CreateExecOptions = opts.clone().into();
let result = client
.create_exec(&opts.name, exec, args.namespace.clone())
.create_exec(&opts.name, &exec, args.namespace.as_deref())
.await?;
let mut stream = client
.start_exec(
&result.id,
StartExecOptions {
&StartExecOptions {
tty: opts.tty,
..Default::default()
},
Expand Down Expand Up @@ -356,7 +359,7 @@ async fn exec_cargo_history(
) -> IoResult<()> {
let client = &cli_conf.client;
let histories = client
.list_history_cargo(&opts.name, args.namespace.clone())
.list_history_cargo(&opts.name, args.namespace.as_deref())
.await?;
utils::print::print_yml(histories)?;
Ok(())
Expand Down Expand Up @@ -394,7 +397,7 @@ async fn exec_cargo_logs(
stderr: None,
stdout: None,
};
let mut stream = client.logs_cargo(&opts.name, &query).await?;
let mut stream = client.logs_cargo(&opts.name, Some(&query)).await?;

Check warning on line 400 in bin/nanocl/src/commands/cargo.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/cargo.rs#L400

Added line #L400 was not covered by tests
while let Some(log) = stream.next().await {
let log = match log {
Ok(log) => log,
Expand Down Expand Up @@ -455,7 +458,8 @@ async fn exec_cargo_stats(
let mut tx = tx.clone();
let client = client.clone();
async move {
let Ok(mut stream) = client.stats_cargo(&name, &query).await else {
let Ok(mut stream) = client.stats_cargo(&name, Some(&query)).await

Check warning on line 461 in bin/nanocl/src/commands/cargo.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/cargo.rs#L461

Added line #L461 was not covered by tests
else {
return;
};
while let Some(stats) = stream.next().await {
Expand Down Expand Up @@ -514,7 +518,7 @@ async fn exec_cargo_revert(
) -> IoResult<()> {
let client = &cli_conf.client;
let cargo = client
.revert_cargo(&opts.name, &opts.history_id, args.namespace.clone())
.revert_cargo(&opts.name, &opts.history_id, args.namespace.as_deref())
.await?;
utils::print::print_yml(cargo)?;
Ok(())
Expand Down Expand Up @@ -547,10 +551,10 @@ async fn exec_cargo_run(
exec_cargo_image_pull(client, &opts.image).await?;
}
let cargo = client
.create_cargo(&opts.clone().into(), args.namespace.clone())
.create_cargo(&opts.clone().into(), args.namespace.as_deref())
.await?;
client
.start_cargo(&cargo.name, Some(cargo.namespace_name))
.start_cargo(&cargo.name, Some(&cargo.namespace_name))
.await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion bin/nanocl/src/commands/cargo_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn exec_cargo_image_ls(
client: &NanocldClient,
opts: &CargoImageListOpts,
) -> IoResult<()> {
let items = client.list_cargo_image(Some(opts.clone().into())).await?;
let items = client.list_cargo_image(Some(&opts.clone().into())).await?;
let rows = items
.into_iter()
.map(CargoImageRow::from)
Expand Down
5 changes: 1 addition & 4 deletions bin/nanocl/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::models::{
/// ## Return
///
/// * [Result](Result) The result of the operation
/// * [Ok](()) The operation was successful
/// * [Ok](Ok<()>) The operation was successful
/// * [Err](nanocl_error::io::IoError) An error occured
///
pub async fn exec_install(args: &InstallOpts) -> IoResult<()> {
Expand Down Expand Up @@ -106,9 +106,6 @@ pub async fn exec_install(args: &InstallOpts) -> IoResult<()> {
let installer = utils::installer::get_template(args.template.clone()).await?;
let data: liquid::Object = nanocld_args.clone().into();
let installer = utils::state::compile(&installer, &data)?;

println!("{installer}");

let deployment = serde_yaml::from_str::<StateDeployment>(&installer)
.map_err(|err| {
err.map_err_context(|| "Unable to extract deployment from installer")
Expand Down
13 changes: 5 additions & 8 deletions bin/nanocl/src/commands/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub async fn log_cargo(
let cargo = match client
.inspect_cargo(
&cargo.name,
Some(opts.namespace.to_owned().unwrap_or("global".to_string())),
Some(opts.namespace.as_deref().unwrap_or("global")),
)
.await
{
Expand Down Expand Up @@ -260,7 +260,7 @@ pub async fn log_cargo(
timestamps,
..Default::default()
};
match client.logs_cargo(&name, &query).await {
match client.logs_cargo(&name, Some(&query)).await {
Err(err) => {
eprintln!("Cannot attach to cargo {name}: {err}");
}
Expand Down Expand Up @@ -378,12 +378,10 @@ fn gen_client(host: &str, meta: &StateMeta) -> IoResult<NanocldClient> {
.ok_or(IoError::not_found("Version", "is not specified"))?;
paths.remove(paths.len() - 1);
let url = paths.join("/");
let url = Box::leak(url.into_boxed_str());
NanocldClient::connect_to(url, Some(version.into()))
NanocldClient::connect_to(&url, Some(version.into()))

Check warning on line 381 in bin/nanocl/src/commands/state.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/state.rs#L381

Added line #L381 was not covered by tests
}
api_version if meta.api_version.starts_with('v') => {
let url = Box::leak(host.to_owned().into_boxed_str());
NanocldClient::connect_to(url, Some(api_version))
NanocldClient::connect_to(host, Some(api_version))
}
_ => {
let mut paths = meta
Expand All @@ -399,8 +397,7 @@ fn gen_client(host: &str, meta: &StateMeta) -> IoResult<NanocldClient> {
paths.remove(paths.len() - 1);
let url = paths.join("/");
let url = format!("https://{url}");
let url = Box::leak(url.into_boxed_str());
NanocldClient::connect_to(url, Some(version.into()))
NanocldClient::connect_to(&url, Some(version.into()))

Check warning on line 400 in bin/nanocl/src/commands/state.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/state.rs#L400

Added line #L400 was not covered by tests
}
};
Ok(client)
Expand Down
4 changes: 2 additions & 2 deletions bin/nanocl/src/commands/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn exec_process(
) -> IoResult<()> {
let client = &cli_conf.client;
let opts = args.clone().into();
let items = client.process(Some(opts)).await?;
let items = client.process(Some(&opts)).await?;

Check warning on line 34 in bin/nanocl/src/commands/system.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/system.rs#L34

Added line #L34 was not covered by tests
let rows = items
.into_iter()
.map(ProcessRow::from)
Expand Down Expand Up @@ -62,7 +62,7 @@ pub async fn exec_http(
) -> IoResult<()> {
match &opts.command {
SystemHttpCommand::Logs(opts) => {
let logs = client.list_http_metric(Some(opts.clone().into())).await?;
let logs = client.list_http_metric(Some(&opts.clone().into())).await?;

Check warning on line 65 in bin/nanocl/src/commands/system.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/system.rs#L65

Added line #L65 was not covered by tests
utils::print::print_yml(logs)?;
}
}
Expand Down
20 changes: 17 additions & 3 deletions bin/nanocl/src/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nanocld_client::stubs::state::StateDeployment;

use bollard_next::container::{InspectContainerOptions, RemoveContainerOptions};

use crate::utils;
use crate::{utils, version};
use crate::models::UninstallOpts;

/// ## Exec uninstall
Expand All @@ -19,17 +19,31 @@ use crate::models::UninstallOpts;
/// ## Return
///
/// * [Result](Result) The result of the operation
/// * [Ok](()) The operation was successful
/// * [Ok](Ok<()>) The operation was successful
/// * [Err](nanocl_error::io::IoError) An error occured
///
pub async fn exec_uninstall(args: &UninstallOpts) -> IoResult<()> {
let detected_host = utils::docker::detect_docker_host()?;
let (docker_host, _) = match &args.docker_host {
let (docker_host, is_docker_desktop) = match &args.docker_host {
Some(docker_host) => (docker_host.to_owned(), args.is_docker_desktop),
None => detected_host,
};
let docker = utils::docker::connect(&docker_host)?;
let installer = utils::installer::get_template(args.template.clone()).await?;
let data = liquid::object!({
"docker_host": docker_host,
"state_dir": "/tmp/random",
"conf_dir": "/tmp/random",
"gateway": "127.0.0.1",
"hosts": "tcp://127.0.0.1:8585",
"hostname": "localhost",
"advertise_addr": "127.0.0.1:8585",
"is_docker_desktop": is_docker_desktop,
"gid": "0",
"home_dir": "/tmp/random",
"channel": version::CHANNEL.to_owned(),
});
let installer = utils::state::compile(&installer, &data)?;
let installer = serde_yaml::from_str::<StateDeployment>(&installer)
.map_err(|err| err.map_err_context(|| "Unable to parse installer"))?;
let cargoes = installer.cargoes.unwrap_or_default();
Expand Down
41 changes: 21 additions & 20 deletions bin/nanocl/src/commands/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use std::collections::HashMap;

use futures::StreamExt;
use indicatif::{ProgressBar, MultiProgress};

use nanocl_error::io::{IoError, FromIo, IoResult};
use nanocld_client::stubs::cargo_config::CargoConfigPartial;

use crate::utils;
use crate::{utils, version};
use crate::config::CliConfig;
use crate::models::UpgradeOpts;
use super::cargo_image::exec_cargo_image_pull;
Expand All @@ -23,23 +18,35 @@ use super::cargo_image::exec_cargo_image_pull;
/// ## Return
///
/// * [Result](Result) The result of the operation
/// * [Ok](()) The operation was successful
/// * [Ok](Ok<()>) The operation was successful
/// * [Err](IoError) An error occured
///
pub async fn exec_upgrade(
cli_conf: &CliConfig,
args: &UpgradeOpts,
) -> IoResult<()> {
let detected_host = utils::docker::detect_docker_host()?;
let (docker_host, is_docker_desktop) = match &args.docker_host {
Some(docker_host) => (docker_host.to_owned(), args.is_docker_desktop),
None => detected_host,
};
let home_dir = std::env::var("HOME").map_err(|err| {
IoError::interupted("Unable to get $HOME env variable", &err.to_string())
})?;
let client = &cli_conf.client;
let config = client.info().await?.config;
let data = liquid::object!({
"advertise_addr": config.advertise_addr,
"state_dir": config.state_dir,
"docker_host": config.docker_host,
"docker_host": docker_host,
"is_docker_desktop": is_docker_desktop,
"gateway": config.gateway,
"conf_dir": config.conf_dir,
"hostname": config.hostname,
"hosts": config.hosts.join(" "),
"gid": config.gid,
"home_dir": home_dir,
"channel": version::CHANNEL,
});
let installer = utils::installer::get_template(args.template.clone()).await?;
let installer = utils::state::compile(&installer, &data)?;
Expand All @@ -60,18 +67,12 @@ pub async fn exec_upgrade(
"is not specified".into(),
))?;
exec_cargo_image_pull(client, &image).await?;
}
let data =
serde_json::from_value::<serde_json::Value>(data).map_err(|err| {
err.map_err_context(|| "Unable to convert upgrade to json")
})?;
let mut stream = client.apply_state(&data).await?;
let multiprogress = MultiProgress::new();
multiprogress.set_move_cursor(false);
let mut layers: HashMap<String, ProgressBar> = HashMap::new();
while let Some(res) = stream.next().await {
let res = res?;
utils::state::update_progress(&multiprogress, &mut layers, &res.key, &res);
print!("Upgrading {}", cargo.name);
let _ = client
.put_cargo(&cargo.name.clone(), &cargo, Some("system"))

Check warning on line 72 in bin/nanocl/src/commands/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/upgrade.rs#L72

Added line #L72 was not covered by tests
.await;
ntex::time::sleep(std::time::Duration::from_secs(2)).await;
println!(" {} has been upgraded successfully!", cargo.name);
}
Ok(())
}
Loading

0 comments on commit a48a525

Please sign in to comment.