Skip to content

Commit

Permalink
Move connect_to_docker to Arg impl
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Jul 16, 2024
1 parent aac2440 commit 3ed3586
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 70 deletions.
6 changes: 2 additions & 4 deletions cmd/soroban-cli/src/commands/network/container/logs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use futures_util::TryStreamExt;

use crate::commands::network::container::shared::{
connect_to_docker, Error as ConnectionError, Network,
};
use crate::commands::network::container::shared::{Error as ConnectionError, Network};

use super::shared::Args;

Expand All @@ -28,7 +26,7 @@ pub struct Cmd {
impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = self.container_args.get_container_name(self.network);
let docker = connect_to_docker(&self.container_args.docker_host).await?;
let docker = self.container_args.connect_to_docker().await?;
let logs_stream = &mut docker.logs(
&container_name,
Some(bollard::container::LogsOptions {
Expand Down
116 changes: 58 additions & 58 deletions cmd/soroban-cli/src/commands/network/container/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,64 @@ impl Args {
|container_name| format!("--name {container_name}"),
)
}

pub(crate) async fn connect_to_docker(&self) -> Result<Docker, Error> {
// if no docker_host is provided, use the default docker host:
// "unix:///var/run/docker.sock" on unix machines
// "npipe:////./pipe/docker_engine" on windows machines
let host = self.docker_host.as_ref().map_or_else(
|| DEFAULT_DOCKER_HOST.to_string(),
std::string::ToString::to_string,
);

// this is based on the `connect_with_defaults` method which has not yet been released in the bollard crate
// https://github.com/fussybeaver/bollard/blob/0972b1aac0ad5c08798e100319ddd0d2ee010365/src/docker.rs#L660
let connection = match host.clone() {
// if tcp or http, use connect_with_http_defaults
// if unix and host starts with "unix://" use connect_with_unix
// if windows and host starts with "npipe://", use connect_with_named_pipe
// else default to connect_with_unix
h if h.starts_with("tcp://") || h.starts_with("http://") => {
Docker::connect_with_http_defaults()
}
#[cfg(unix)]
h if h.starts_with("unix://") => {
Docker::connect_with_unix(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
}
#[cfg(windows)]
h if h.starts_with("npipe://") => {
Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
}
_ => {
return Err(Error::UnsupportedURISchemeError {
uri: host.to_string(),
});
}
}?;

match check_docker_connection(&connection).await {
Ok(()) => Ok(connection),
// If we aren't able to connect with the defaults, or with the provided docker_host
// try to connect with the default docker desktop socket since that is a common use case for devs
#[allow(unused_variables)]
Err(e) => {
// if on unix, try to connect to the default docker desktop socket
#[cfg(unix)]
{
let docker_desktop_connection = try_docker_desktop_socket(&host)?;
match check_docker_connection(&docker_desktop_connection).await {
Ok(()) => Ok(docker_desktop_connection),
Err(err) => Err(err)?,
}
}

#[cfg(windows)]
{
Err(e)?
}
}
}
}
}

#[derive(ValueEnum, Debug, Copy, Clone, PartialEq)]
Expand All @@ -99,64 +157,6 @@ impl fmt::Display for Network {
}
}

pub async fn connect_to_docker(docker_host: &Option<String>) -> Result<Docker, Error> {
// if no docker_host is provided, use the default docker host:
// "unix:///var/run/docker.sock" on unix machines
// "npipe:////./pipe/docker_engine" on windows machines

let host = docker_host
.clone()
.unwrap_or(DEFAULT_DOCKER_HOST.to_string());

// this is based on the `connect_with_defaults` method which has not yet been released in the bollard crate
// https://github.com/fussybeaver/bollard/blob/0972b1aac0ad5c08798e100319ddd0d2ee010365/src/docker.rs#L660
let connection = match host.clone() {
// if tcp or http, use connect_with_http_defaults
// if unix and host starts with "unix://" use connect_with_unix
// if windows and host starts with "npipe://", use connect_with_named_pipe
// else default to connect_with_unix
h if h.starts_with("tcp://") || h.starts_with("http://") => {
Docker::connect_with_http_defaults()
}
#[cfg(unix)]
h if h.starts_with("unix://") => {
Docker::connect_with_unix(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
}
#[cfg(windows)]
h if h.starts_with("npipe://") => {
Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION)
}
_ => {
return Err(Error::UnsupportedURISchemeError {
uri: host.to_string(),
});
}
}?;

match check_docker_connection(&connection).await {
Ok(()) => Ok(connection),
// If we aren't able to connect with the defaults, or with the provided docker_host
// try to connect with the default docker desktop socket since that is a common use case for devs
#[allow(unused_variables)]
Err(e) => {
// if on unix, try to connect to the default docker desktop socket
#[cfg(unix)]
{
let docker_desktop_connection = try_docker_desktop_socket(&host)?;
match check_docker_connection(&docker_desktop_connection).await {
Ok(()) => Ok(docker_desktop_connection),
Err(err) => Err(err)?,
}
}

#[cfg(windows)]
{
Err(e)?
}
}
}
}

#[cfg(unix)]
fn try_docker_desktop_socket(host: &str) -> Result<Docker, bollard::errors::Error> {
let default_docker_desktop_host =
Expand Down
6 changes: 2 additions & 4 deletions cmd/soroban-cli/src/commands/network/container/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use bollard::{
};
use futures_util::TryStreamExt;

use crate::commands::network::container::shared::{
connect_to_docker, Error as ConnectionError, Network,
};
use crate::commands::network::container::shared::{Error as ConnectionError, Network};

use super::shared::Args;

Expand Down Expand Up @@ -58,7 +56,7 @@ impl Cmd {
}

async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> {
let docker = connect_to_docker(&cmd.container_args.docker_host).await?;
let docker = cmd.container_args.connect_to_docker().await?;

let image = get_image_name(cmd);
docker
Expand Down
6 changes: 2 additions & 4 deletions cmd/soroban-cli/src/commands/network/container/stop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::commands::network::container::shared::{
connect_to_docker, Error as BollardConnectionError, Network,
};
use crate::commands::network::container::shared::{Error as BollardConnectionError, Network};

use super::shared::Args;

Expand Down Expand Up @@ -33,7 +31,7 @@ pub struct Cmd {
impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let container_name = self.container_args.get_container_name(self.network);
let docker = connect_to_docker(&self.container_args.docker_host).await?;
let docker = self.container_args.connect_to_docker().await?;
println!("ℹ️ Stopping container: {container_name}");
docker
.stop_container(&container_name, None)
Expand Down

0 comments on commit 3ed3586

Please sign in to comment.